diff --git a/source/Sprite.cpp b/source/Sprite.cpp new file mode 100644 index 0000000..ce05d69 --- /dev/null +++ b/source/Sprite.cpp @@ -0,0 +1,56 @@ +#include "Sprite.h" + +Sprite::Sprite() { + // C2D_SpriteSetCenter(&this->spr, 0.5f, 0.5f); + // C2D_SpriteSetPos(&this->spr, 400/2, 240/2); + this->x = 200; this->y = 120; this->spr = new C2D_Sprite; +} + +Sprite::Sprite(C2D_Sprite* spr, float x, float y) { + this->x = x; this->y = y; this->spr = spr; + + C2D_SpriteSetCenter(this->spr, 0.5f, 0.5f); + C2D_SpriteSetPos(this->spr, x, y); +} + +void Sprite::setPosition(float x, float y) { + this->x = x; + this->y = y; + + C2D_SpriteSetPos(this->spr, x, y); +} + +void Sprite::move(float dx, float dy) { + if (this->x >= 400 || this->x < 1 || this->y >= 240 || this->x < 1) { + return; + } + + this->x += dx; + this->y += dy; + + C2D_SpriteSetPos(this->spr, this->x, this->y); +} + +void Sprite::setCenter(float x, float y) { + C2D_SpriteSetCenter(this->spr, x, y); +} + +void Sprite::draw() { + C2D_DrawSprite(this->spr); +} + +float Sprite::getPosX() { + return this->x; +} + +float Sprite::getPosY() { + return this->y; +} + +C2D_Sprite* Sprite::getSpr() { + return this->spr; +} + +void Sprite::setSpr(C2D_Sprite* newSpr) { + this->spr = newSpr; +} \ No newline at end of file diff --git a/source/Sprite.h b/source/Sprite.h new file mode 100644 index 0000000..b6d87c3 --- /dev/null +++ b/source/Sprite.h @@ -0,0 +1,36 @@ +#pragma once + +#ifndef SPRITE_H +#define SPRITE_H + +#include + +class Sprite { +public: + Sprite(); + + Sprite(C2D_Sprite* spr, float x, float y); + + void setPosition(float x, float y); + + void move(float dx, float dy); + + void setCenter(float x, float y); + + void draw(); + + float getPosX(); + + float getPosY(); + + C2D_Sprite* getSpr(); + + void setSpr(C2D_Sprite* newSpr); + + C2D_Sprite* spr; + +private: + float x, y; +}; + +#endif \ No newline at end of file diff --git a/source/sprites.h b/source/SpriteList.h similarity index 100% rename from source/sprites.h rename to source/SpriteList.h diff --git a/source/main.c b/source/main.cpp similarity index 68% rename from source/main.c rename to source/main.cpp index 126ef1a..8c4f1b0 100644 --- a/source/main.c +++ b/source/main.cpp @@ -6,28 +6,20 @@ */ #include -#include -#include -#include #include -#include #include +#include "Sprite.h" -#include "sprites.h" +#include "SpriteList.h" #define MAX_SPRITES 768 #define SCREEN_WIDTH 400 #define SCREEN_HEIGHT 240 -typedef struct { - C2D_Sprite spr; - float x, y; // pos -} Sprite; - // init spritesheet static C2D_SpriteSheet spriteSheet; -static Sprite sprites[MAX_SPRITES]; +Sprite sprites[MAX_SPRITES]; static void initSprites() { size_t numImages = C2D_SpriteSheetCount(spriteSheet); @@ -37,36 +29,24 @@ static void initSprites() { float x = SCREEN_WIDTH / 2; float y = SCREEN_HEIGHT / 2; - C2D_SpriteFromSheet(&thisSprite->spr, spriteSheet, i); - C2D_SpriteSetCenter(&thisSprite->spr, 0.5f, 0.5f); - C2D_SpriteSetPos(&thisSprite->spr, x, y); - thisSprite->x = x; - thisSprite->y = y; + C2D_SpriteFromSheet(thisSprite->spr, spriteSheet, i); + C2D_SpriteSetCenter(thisSprite->spr, 0.5f, 0.5f); + thisSprite->setPosition(x, y); } // set bird consts - C2D_SpriteSetPos(&sprites[SPR_BIRD].spr, SCREEN_WIDTH / 4, SCREEN_HEIGHT / 3); + sprites[SPR_BIRD].setPosition(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 3); + // set bottom screen scorecard - C2D_SpriteSetCenter(&sprites[SPR_SCORECARD].spr, 0.0, 0.0); - C2D_SpriteSetPos(&sprites[SPR_SCORECARD].spr, 2, 40); + sprites[SPR_SCORECARD].setCenter(0.0, 0.0); + + sprites[SPR_SCORECARD].setPosition(2, 40); + + sprites[SPR_PIPETOP].setPosition(280, 14); + sprites[SPR_PIPEBOTTOM].setPosition(280, 200); } -void moveSprite(Sprite *spr, s16 dx, s16 dy) { - // stop sprite going OOB - if (spr->x >= SCREEN_WIDTH || spr->x < 1 || spr->y >= SCREEN_HEIGHT || spr->x < 1) { - return; - } - - spr->x += dx; - spr->y += dy; - - C2D_SpriteSetPos(&spr->spr, spr->x, spr->y); -} - -void drawSprite(int id) { - C2D_DrawSprite(&sprites[id].spr); -} int main(int argc, char* argv[]) { @@ -106,8 +86,8 @@ int main(int argc, char* argv[]) { float v = 0; // velocity // set bg properties - C2D_SpriteSetPos(&sprites[SPR_BG].spr, 200, 120); // center bg - C2D_SpriteSetScale(&sprites[SPR_BG].spr, 2.7778, 2.7907); // scale image to 400x240 (3ds screen res) + sprites[SPR_BG].setPosition(200, 120); + C2D_SpriteSetScale(sprites[SPR_BG].spr, 2.7778, 2.7907); // scale image to 400x240 (3ds screen res) size_t score = 0; @@ -123,12 +103,16 @@ int main(int argc, char* argv[]) { // gravity calcs v += a; - moveSprite(&sprites[SPR_BIRD], 0, v); - C2D_SpriteSetRotationDegrees(&sprites[SPR_BIRD].spr, v*9.8); + sprites[SPR_BIRD].move(0, v); + C2D_SpriteSetRotationDegrees(sprites[SPR_BIRD].spr, v*9.8); + + sprites[SPR_PIPETOP].move(-0.2, 0); + sprites[SPR_PIPEBOTTOM].move(-0.2, 0); if (kDown & KEY_A) { v = -5.5; - moveSprite(&sprites[SPR_BIRD], 0, v); + // moveSprite(&sprites[SPR_BIRD], 0, v); + sprites[SPR_BIRD].move(0, v); // temp score = num of flaps score++; } @@ -144,12 +128,18 @@ int main(int argc, char* argv[]) { C3D_FrameBegin(C3D_FRAME_SYNCDRAW); C2D_TargetClear(top, C3D_CLEAR_COLOR); C2D_SceneBegin(top); - drawSprite(SPR_BG); - drawSprite(SPR_BIRD); + // drawSprite(SPR_BG); + // drawSprite(SPR_BIRD); + sprites[SPR_BG].draw(); + sprites[SPR_BIRD].draw(); + + // drawSprite(SPR_PIPEBOTTOM); + // drawSprite(SPR_PIPETOP); C2D_TargetClear(bottom, C2D_Color32f(0.3294f, 0.7529f, 0.7882f, 1.0f)); C2D_SceneBegin(bottom); - drawSprite(SPR_SCORECARD); + // drawSprite(SPR_SCORECARD); + sprites[SPR_SCORECARD].draw(); // C2D_DrawSprite(&scoreSprite->spr);