From bf849afea6ac78aa52179adb0c2a76b90a15cdaa Mon Sep 17 00:00:00 2001 From: pradyun Date: Fri, 15 Sep 2023 09:36:35 +1200 Subject: [PATCH] everythings laid out on the floor but oh its working --- Flappy3DS.smdh | Bin 0 -> 14016 bytes source/Bakery2D/Sprite/Sprite.cpp | 106 ------------------- source/Bakery2D/Sprite/Sprite.hpp | 46 --------- source/{Bakery2D/Level => }/Level.cpp | 3 +- source/{Bakery2D/Level => }/Level.hpp | 10 +- source/MainLevel.cpp | 109 ++++++++++++++++++++ source/MainLevel.hpp | 50 +++++++++ source/Sprite.cpp | 7 +- source/SpriteList.h | 17 ---- source/main.cpp | 4 +- source/scenes/MainLevel.cpp | 140 -------------------------- upload.sh | 2 +- 12 files changed, 173 insertions(+), 321 deletions(-) create mode 100644 Flappy3DS.smdh delete mode 100644 source/Bakery2D/Sprite/Sprite.cpp delete mode 100644 source/Bakery2D/Sprite/Sprite.hpp rename source/{Bakery2D/Level => }/Level.cpp (84%) rename source/{Bakery2D/Level => }/Level.hpp (88%) create mode 100644 source/MainLevel.cpp create mode 100644 source/MainLevel.hpp delete mode 100644 source/SpriteList.h delete mode 100644 source/scenes/MainLevel.cpp diff --git a/Flappy3DS.smdh b/Flappy3DS.smdh new file mode 100644 index 0000000000000000000000000000000000000000..cf896c7f58126df535a7e9606956e8469048a97b GIT binary patch literal 14016 zcmeHMKWJM=7{4w?gNF_t1TOd$M9H8brWrg0j7(gbfS07f)DVN}s#L^on>KDS#)}CW zLdoDnnL;rJdoZaD8M3*Hu=HF*BH_VA0WTUl$ikDYL6Fz)d*`3;e0|bW{ja6HJ3sxt z`@Zjf@7;Iz?%kbl4PES?_k)ZZ*EWV|d zo@cWCf1c3|=eGTC-+%l5+xg$l|MvTDzyEgqXV-sr{cqR*cK>Jhe|Geu;)MF zrwAwau;)J~kKI|+wC6we{KuaE*z+HI{$tO7?D>y9|FP#k_Wb7}&wu9Uh5t8CznnZu zzHs=Hcuy}Wxoa_fBYi{Af2w5y9u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0000000 --- a/source/Bakery2D/Sprite/Sprite.cpp +++ /dev/null @@ -1,106 +0,0 @@ -#include "Sprite.hpp" - -Sprite::Sprite() { - // C2D_SpriteSetCenter(&this->spr, 0.5f, 0.5f); - // C2D_SpriteSetPos(&this->spr, 400/2, 240/2); - this->x = 200; this->y = 120; -} - -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, bool overrideOOB) { - if (!overrideOOB) { - // make sure translate doesnt move to OOB - const float newX = this->x + dx; - const float newY = this->y + dy; - if (!((unsigned)(newX-1) <= 398 && (unsigned)(newY-1) <= 238)) { - return; - } - } - - this->x += dx; - this->y += dy; - - C2D_SpriteSetPos(&this->spr, this->x, this->y); -} - -void Sprite::setCenter(float x, float y) { - this->center[0] = x; this->center[1] = y; - - C2D_SpriteSetCenter(&this->spr, x, y); -} - -void Sprite::setHitbox(float w, float h) { - this->width = w; this->height = h; -} - -bool Sprite::isCollidingWith(Sprite &other) { - // hitbox not set - if (this->width == 0 && this->height == 0) return false; - - const float thisHalfWidth = this->width/2; - const float otherHalfWidth = other.width/2; - const float thisHalfHeight = this->height/2; - const float otherHalfHeight = other.height/2; - - // check x collision - if (this->x + thisHalfWidth >= other.x - otherHalfWidth || - this->x - thisHalfWidth <= other.x + otherHalfWidth - ) { - return true; - } - - // check y collision - if (this->y + thisHalfHeight >= other.y - otherHalfHeight || - this->y - thisHalfHeight <= other.y + otherHalfHeight - ) { - return true; - } - - return false; -} - -float* Sprite::getCenter() { - return this->center; -} - -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; -} - -void Sprite::copy(Sprite* other) { - memcpy(&other->x, &this->x, sizeof(this->x)); - memcpy(&other->y, &this->y, sizeof(this->y)); - memcpy(&other->width, &this->width, sizeof(this->width)); - memcpy(&other->height, &this->height, sizeof(this->height)); - memcpy(other->center, this->center, sizeof(this->center)); - memcpy(&other->spr, &this->spr, sizeof(this->spr)); -} \ No newline at end of file diff --git a/source/Bakery2D/Sprite/Sprite.hpp b/source/Bakery2D/Sprite/Sprite.hpp deleted file mode 100644 index 6222d1b..0000000 --- a/source/Bakery2D/Sprite/Sprite.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#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, bool overrideOOB=false); - - void setCenter(float x, float y); - - void setHitbox(float width, float height); - - bool isCollidingWith(Sprite &other); - - void draw(); - - float getPosX(); - - float getPosY(); - - float* getCenter(); - - C2D_Sprite* getSpr(); - - void setSpr(C2D_Sprite* newSpr); - - void copy(Sprite* other); - - C2D_Sprite spr; - -private: - float x, y; - float width, height; // for hitbox - float center[2]; -}; - -#endif \ No newline at end of file diff --git a/source/Bakery2D/Level/Level.cpp b/source/Level.cpp similarity index 84% rename from source/Bakery2D/Level/Level.cpp rename to source/Level.cpp index dd1401c..6114eca 100644 --- a/source/Bakery2D/Level/Level.cpp +++ b/source/Level.cpp @@ -1,7 +1,6 @@ #include "Level.hpp" -template -Level::Level(T* levelName, char* spritesheetPath, C3D_RenderTarget* top, C3D_RenderTarget* bottom) : level(levelName), top(top), bottom(bottom) { +Level::Level(Level* levelName, char* spritesheetPath, C3D_RenderTarget* top, C3D_RenderTarget* bottom) : level(levelName), top(top), bottom(bottom) { this->spritesheet = C2D_SpriteSheetLoad(spritesheetPath); size_t numImages = C2D_SpriteSheetCount(spritesheet); diff --git a/source/Bakery2D/Level/Level.hpp b/source/Level.hpp similarity index 88% rename from source/Bakery2D/Level/Level.hpp rename to source/Level.hpp index 6d0f84f..b14574c 100644 --- a/source/Bakery2D/Level/Level.hpp +++ b/source/Level.hpp @@ -8,8 +8,8 @@ #ifndef LEVEL_HPP #define LEVEL_HPP -#include -#include "../Sprite/Sprite.hpp" +// #include +#include "Sprite.hpp" #define SCREEN_WIDTH 400 #define SCREEN_HEIGHT 240 @@ -64,12 +64,12 @@ public: virtual void drawTop() = 0; virtual void drawBottom() = 0; - template - Level(T* levelName, char* spritesheetPath, C3D_RenderTarget* top, C3D_RenderTarget* bottom); + + Level(Level* levelName, char* spritesheetPath, C3D_RenderTarget* top, C3D_RenderTarget* bottom); // sets up the level if it hasnt been, then updates the logic, and draws the frame void run(); - virtual void cleanup(); + virtual void cleanup() = 0; }; #endif diff --git a/source/MainLevel.cpp b/source/MainLevel.cpp new file mode 100644 index 0000000..178c9cc --- /dev/null +++ b/source/MainLevel.cpp @@ -0,0 +1,109 @@ +#include "MainLevel.hpp" + +MainLevel::MainLevel(C3D_RenderTarget* top, C3D_RenderTarget* bottom): Level(this, "romfs:/gfx/sprites.t3x", top, bottom) {} + +void MainLevel::setup() { + //* SPRITE INIT *// + // set bird consts + sprites[SPR_BIRD].setPosition(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 3); + sprites[SPR_BIRD].setHitbox(17, 12); + + // set bottom screen scorecard + sprites[SPR_SCORECARD].setCenter(0.0, 0.0); + + sprites[SPR_SCORECARD].setPosition(2, 40); + + sprites[SPR_PIPETOP].setPosition(280, 3); + sprites[SPR_PIPEBOTTOM].setPosition(280, 280); + + // set bg properties + sprites[SPR_BG].setPosition(200, 120); + C2D_SpriteSetScale(&sprites[SPR_BG].spr, 2.7778, 2.7907); // scale image to 400x240 (3ds screen res) + + // if theres a better way to do this,,, i dont know it + for (int i = 0; i < NUM_PIPES; i++) { + memcpy(&pipes[i], &sprites[SPR_BOTHPIPES], sizeof(sprites[SPR_BOTHPIPES])); + pipes[i].setCenter(0.5f, 0.5f); + pipes[i].setPosition(SCREEN_WIDTH + 40 + i * (100), (rand() % 150) + 50); + pipes[i].setHitbox(26, 403); + } + + //* FONT INIT *// + // Load fonts and text + g_staticBuf = C2D_TextBufNew(4096); + font = C2D_FontLoad("romfs:/gfx/fbfont.bcfnt"); + C2D_TextFontParse(&scoreText, font, g_staticBuf, "0"); + C2D_TextOptimize(&scoreText); +} + +void MainLevel::update() { + hidScanInput(); + + // Respond to user input + u32 kDown = hidKeysDown(); + if (kDown & KEY_START) + svcBreak(USERBREAK_USER); // break in order to return to hbmenu + + + // gravity calcs + v += a; + sprites[SPR_BIRD].move(0, v); + C2D_SpriteSetRotationDegrees(&sprites[SPR_BIRD].spr, v*9.8); + + if (!gameOver && (kDown & KEY_A)) { + v = -5.5; + sprites[SPR_BIRD].move(0, v, true); + } + + // bird hitting ground is game over + if (sprites[SPR_BIRD].getPosY() == SCREEN_HEIGHT) { + gameOver = true; + } + + if (!gameOver) { + for (int i = 0; i < NUM_PIPES; i++) { + pipes[i].move(pipeSpeed, 0, true); + + // collision detection + if (pipes[i].isCollidingWith(sprites[SPR_BIRD])) { + gameOver = true; + break; + } + + // inc score + if (pipes[i].getPosX() == sprites[SPR_BIRD].getPosX()) + score++; + + // send pipes back to front of screen + if (pipes[i].getPosX() < -(SCREEN_WIDTH/NUM_PIPES)) { + pipes[i].setPosition(SCREEN_WIDTH, (rand() % 150) + 50); + } + } + } + + // make score text + // char scoreString[(((sizeof score) * CHAR_BIT) + 2)/3 + 2]; + // sprintf(scoreString, "%d", score); + // C2D_TextFontParse(&scoreText, font, g_staticBuf, scoreString); + // C2D_TextOptimize(&scoreText); +} + +void MainLevel::drawTop() { + sprites[SPR_BG].draw(); + sprites[SPR_BIRD].draw(); + + for (int i = 0; i < NUM_PIPES; i++) { + pipes[i].draw(); + } +} + +void MainLevel::drawBottom() { + sprites[SPR_SCORECARD].draw(); + // C2D_DrawText(&scoreText, 0, 240, 88, 0.0f, 0.9f, 0.9f); +} + +void MainLevel::cleanup() { + C2D_SpriteSheetFree(spritesheet); + C2D_TextBufDelete(g_staticBuf); + C2D_FontFree(font); +} \ No newline at end of file diff --git a/source/MainLevel.hpp b/source/MainLevel.hpp new file mode 100644 index 0000000..17a52f4 --- /dev/null +++ b/source/MainLevel.hpp @@ -0,0 +1,50 @@ +#pragma once +#ifndef MAINLEVEL_HPP +#define MAINLEVEL_HPP + +#include "Level.hpp" + +#define SPR_BIRD 0 + +#define SPR_BG 1 +#define SPR_FLOOR 2 +#define SPR_LOGO 3 +#define SPR_PIPEBOTTOM 4 +#define SPR_PIPETOP 5 + +#define SPR_SCORECARD 6 +#define SPR_BOTHPIPES 7 + +#define NUM_PIPES 5 + +class MainLevel: public Level { +public: + float a = 0.4; // acceleration + float v = 0; // velocity + float pipeSpeed = -1; + + Sprite pipes[NUM_PIPES]; + + int score = 0; + bool gameOver = false; + + C2D_TextBuf g_staticBuf; + C2D_Text scoreText; + C2D_Font font; + + MainLevel(C3D_RenderTarget* top, C3D_RenderTarget* bottom); + + void setup(); + + void update(); + + void drawTop(); + + void drawBottom(); + + void cleanup(); + +}; + + +#endif \ No newline at end of file diff --git a/source/Sprite.cpp b/source/Sprite.cpp index e349509..a1ea7bd 100644 --- a/source/Sprite.cpp +++ b/source/Sprite.cpp @@ -97,5 +97,10 @@ void Sprite::setSpr(C2D_Sprite* newSpr) { } void Sprite::copy(Sprite* other) { - memcpy(&other, this, sizeof(this)); + memcpy(&other->x, &this->x, sizeof(this->x)); + memcpy(&other->y, &this->y, sizeof(this->y)); + memcpy(&other->width, &this->width, sizeof(this->width)); + memcpy(&other->height, &this->height, sizeof(this->height)); + memcpy(other->center, this->center, sizeof(this->center)); + memcpy(&other->spr, &this->spr, sizeof(this->spr)); } \ No newline at end of file diff --git a/source/SpriteList.h b/source/SpriteList.h deleted file mode 100644 index b681bee..0000000 --- a/source/SpriteList.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#ifndef SPRITES_H -#define SPRITES_H - -#define SPR_BIRD 0 - -#define SPR_BG 1 -#define SPR_FLOOR 2 -#define SPR_LOGO 3 -#define SPR_PIPEBOTTOM 4 -#define SPR_PIPETOP 5 - -#define SPR_SCORECARD 6 -#define SPR_BOTHPIPES 7 - -#endif \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index ad84111..3d63537 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -9,10 +9,8 @@ #include #include #include -#include "Sprite.hpp" -#include "SpriteList.h" -#include "scenes/MainLevel.cpp" +#include "MainLevel.hpp" int main(int argc, char* argv[]) { diff --git a/source/scenes/MainLevel.cpp b/source/scenes/MainLevel.cpp deleted file mode 100644 index 6133626..0000000 --- a/source/scenes/MainLevel.cpp +++ /dev/null @@ -1,140 +0,0 @@ -#include "../Bakery2D/Level/Level.hpp" - -#define SPRITES_H - -#define SPR_BIRD 0 - -#define SPR_BG 1 -#define SPR_FLOOR 2 -#define SPR_LOGO 3 -#define SPR_PIPEBOTTOM 4 -#define SPR_PIPETOP 5 - -#define SPR_SCORECARD 6 -#define SPR_BOTHPIPES 7 - -#define NUM_PIPES 5 - -struct MainLevel: Level { - float a = 0.4; // acceleration - float v = 0; // velocity - float pipeSpeed = -1; - - Sprite pipes[NUM_PIPES]; - - int score = 0; - bool gameOver = false; - - C2D_TextBuf g_staticBuf; - C2D_Text scoreText; - C2D_Font font; - - MainLevel(C3D_RenderTarget* top, C3D_RenderTarget* bottom): Level(this, "romfs:/gfx/sprites.t3x", top, bottom) {} - - void setup() { - //* SPRITE INIT *// - // set bird consts - sprites[SPR_BIRD].setPosition(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 3); - sprites[SPR_BIRD].setHitbox(17, 12); - - // set bottom screen scorecard - sprites[SPR_SCORECARD].setCenter(0.0, 0.0); - - sprites[SPR_SCORECARD].setPosition(2, 40); - - sprites[SPR_PIPETOP].setPosition(280, 3); - sprites[SPR_PIPEBOTTOM].setPosition(280, 280); - - // set bg properties - sprites[SPR_BG].setPosition(200, 120); - C2D_SpriteSetScale(&sprites[SPR_BG].spr, 2.7778, 2.7907); // scale image to 400x240 (3ds screen res) - - // if theres a better way to do this,,, i dont know it - for (int i = 0; i < NUM_PIPES; i++) { - memcpy(&pipes[i], &sprites[SPR_BOTHPIPES], sizeof(sprites[SPR_BOTHPIPES])); - pipes[i].setCenter(0.5f, 0.5f); - pipes[i].setPosition(SCREEN_WIDTH + 40 + i * (100), (rand() % 150) + 50); - pipes[i].setHitbox(26, 403); - } - - //* FONT INIT *// - // Load fonts and text - g_staticBuf = C2D_TextBufNew(4096); - font = C2D_FontLoad("romfs:/gfx/fbfont.bcfnt"); - C2D_TextFontParse(&scoreText, font, g_staticBuf, "0"); - C2D_TextOptimize(&scoreText); - } - - void update() { - hidScanInput(); - - // Respond to user input - u32 kDown = hidKeysDown(); - if (kDown & KEY_START) - svcBreak(USERBREAK_USER); // break in order to return to hbmenu - - - // gravity calcs - v += a; - sprites[SPR_BIRD].move(0, v); - C2D_SpriteSetRotationDegrees(&sprites[SPR_BIRD].spr, v*9.8); - - if (!gameOver && (kDown & KEY_A)) { - v = -5.5; - sprites[SPR_BIRD].move(0, v, true); - } - - // bird hitting ground is game over - if (sprites[SPR_BIRD].getPosY() == SCREEN_HEIGHT) { - gameOver = true; - } - - if (!gameOver) { - for (int i = 0; i < NUM_PIPES; i++) { - pipes[i].move(pipeSpeed, 0, true); - - // collision detection - if (pipes[i].isCollidingWith(sprites[SPR_BIRD])) { - gameOver = true; - break; - } - - // inc score - if (pipes[i].getPosX() == sprites[SPR_BIRD].getPosX()) - score++; - - // send pipes back to front of screen - if (pipes[i].getPosX() < -(SCREEN_WIDTH/NUM_PIPES)) { - pipes[i].setPosition(SCREEN_WIDTH, (rand() % 150) + 50); - } - } - } - - // make score text - char scoreString[(((sizeof score) * CHAR_BIT) + 2)/3 + 2]; - sprintf(scoreString, "%d", score); - C2D_TextFontParse(&scoreText, font, g_staticBuf, scoreString); - C2D_TextOptimize(&scoreText); - } - - void drawTop() { - sprites[SPR_BG].draw(); - sprites[SPR_BIRD].draw(); - - for (int i = 0; i < NUM_PIPES; i++) { - pipes[i].draw(); - } - } - - void drawBottom() { - sprites[SPR_SCORECARD].draw(); - C2D_DrawText(&scoreText, 0, 240, 88, 0.0f, 0.9f, 0.9f); - } - - void cleanup() override { - C2D_SpriteSheetFree(spritesheet); - C2D_TextBufDelete(g_staticBuf); - C2D_FontFree(font); - } - -}; \ No newline at end of file diff --git a/upload.sh b/upload.sh index b2561ae..32d1c0b 100755 --- a/upload.sh +++ b/upload.sh @@ -1,2 +1,2 @@ # need to have ftpd open for this -curl -T Flappy3DS.3dsx ftp://192.168.88.131:5000/3ds/ \ No newline at end of file +curl -T B2DFlappy3DS.3dsx ftp://192.168.88.131:5000/3ds/ \ No newline at end of file