diff --git a/.vscode/launch.json b/.vscode/launch.json index 88266ac..dc9ed8f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -187,6 +187,35 @@ ], "avoidWindowsConsoleRedirection": false, "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": "(gdb, lxdream-nitro) Launch flappyBird.elf", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/src/flappyBird.elf", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "/opt/toolchains/dc/sh-elf/bin/sh-elf-gdb", + "miDebuggerServerAddress": "localhost:3263", + "debugServerPath": "/home/user/Downloads/lxdream-nitro/builddir/lxdream-nitro", + "debugServerArgs": "-d -g 3263 -e ${workspaceFolder}/build/src/flappyBird.elf", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set SuperH architechture", + "text": "set architecture sh4" + } + ], + "avoidWindowsConsoleRedirection": false, + "internalConsoleOptions": "openOnSessionStart" } ] } diff --git a/src/engine/entity.hh b/src/engine/entity.hh index ea76627..ff69166 100644 --- a/src/engine/entity.hh +++ b/src/engine/entity.hh @@ -16,6 +16,7 @@ public: std::vector children = {}; virtual void Draw() {}; virtual void Update() {}; + virtual void Start() {}; bool b_destroyed = false; Vector3 GetPosition() const; Vector3 GetGlobalPosition() const; diff --git a/src/flappyBird/Assets.hh b/src/flappyBird/Assets.hh index 903e2c7..884474a 100644 --- a/src/flappyBird/Assets.hh +++ b/src/flappyBird/Assets.hh @@ -2,6 +2,11 @@ #define ASSETS_HH #include namespace assets { - std::vector bird(); + struct Image { + int width; + int height; + std::vector data; + }; + extern Image bird; } #endif diff --git a/src/flappyBird/Bird.cc b/src/flappyBird/Bird.cc index f282bb8..f5089ee 100644 --- a/src/flappyBird/Bird.cc +++ b/src/flappyBird/Bird.cc @@ -1,22 +1,29 @@ #include "Bird.hh" - +#include "Assets.hh" +#include #include +#include void Bird::Draw() { engine::Vector3 one = {-1.0, -1.0, 0.0}; engine::Vector3 two = {0.0, 1.0, 0.0}; engine::Vector3 three = {1.0, -1.0, 0.0f}; + glEnable(GL_TEXTURE_2D); glBegin(GL_TRIANGLES); - glColor3f(1.0f, 0.0f, 0.0f); + // glColor3f(1.0f, 0.0f, 0.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3fv((float*)&(one)); - glColor3f(0.0f, 1.0f, 0.0f); + // glColor3f(0.0f, 1.0f, 0.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3fv((float*)&two); - glColor3f(0.0f, 0.0f, 1.0f); + // glColor3f(0.0f, 0.0f, 1.0f); + glTexCoord2f(0.5f, 1.0f); glVertex3fv((float*)&three); glEnd(); + glDisable(GL_TEXTURE_2D); } void Bird::Update() { @@ -63,3 +70,17 @@ Bird::Bird(engine::Engine* engie, engine::Entity* parent, PipesContainer* pipes) : Entity(engie, parent) { this->pipes = pipes; } + +void Bird::Start() { + auto bird = assets::bird; + + glGenTextures(1, &ui_birdTexture); + glBindTexture(GL_TEXTURE_2D, ui_birdTexture); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bird.width, bird.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bird.data.data()); +} diff --git a/src/flappyBird/Bird.hh b/src/flappyBird/Bird.hh index a776310..a6a0157 100644 --- a/src/flappyBird/Bird.hh +++ b/src/flappyBird/Bird.hh @@ -7,6 +7,7 @@ class Bird : public engine::Entity { public: void Draw() override; void Update() override; + void Start() override; Bird(engine::Engine* engie, engine::Entity* parent, PipesContainer* pipes); bool b_isHit = false; @@ -15,5 +16,6 @@ class Bird : public engine::Entity { static constexpr float JUMP_CONST = 0.5; float f_verticalSpeed = JUMP_CONST; PipesContainer *pipes = nullptr; + unsigned int ui_birdTexture = 0; }; #endif diff --git a/src/flappyBird/FlappyBird.cc b/src/flappyBird/FlappyBird.cc index cafc2af..491ac6c 100644 --- a/src/flappyBird/FlappyBird.cc +++ b/src/flappyBird/FlappyBird.cc @@ -105,3 +105,7 @@ FlappyBird::~FlappyBird() p_thread->detach(); delete p_thread; } + +void FlappyBird::Start() { + p_bird->Start(); +} diff --git a/src/flappyBird/FlappyBird.hh b/src/flappyBird/FlappyBird.hh index f04fc79..0185b58 100644 --- a/src/flappyBird/FlappyBird.hh +++ b/src/flappyBird/FlappyBird.hh @@ -11,6 +11,7 @@ class FlappyBird : public engine::Engine { void GameLoop() override; + void Start() override; void SpawnPipes(); void MainGameLoop(); void MainMenuLoop(); diff --git a/src/flappyBird/assets/yellowbird-downflap.png b/src/flappyBird/assets/yellowbird-downflap.png index e9e1c77..943fe36 100644 Binary files a/src/flappyBird/assets/yellowbird-downflap.png and b/src/flappyBird/assets/yellowbird-downflap.png differ diff --git a/src/flappyBird/generateAssets.py b/src/flappyBird/generateAssets.py index a67cb8a..60c22bf 100644 --- a/src/flappyBird/generateAssets.py +++ b/src/flappyBird/generateAssets.py @@ -3,13 +3,13 @@ from pathlib import Path from PIL import Image parser = argparse.ArgumentParser() -parser.add_argument("function") +parser.add_argument("variable") parser.add_argument("input") parser.add_argument("output") args = parser.parse_args() -function = args.function +variable = args.variable input = Path(args.input) output = Path(args.output) @@ -18,10 +18,14 @@ if not input.exists(): raise SystemExit(1) hexList = list() +imageWidth = 0 +imageHeight = 0 with Image.open(input) as image: - imageBytes = bytes(image.getdata()) - byteList = list(imageBytes) + imageWidth = image.width + imageHeight = image.height + # flatten a tuple of RGBA bytes into a list of bytes + byteList = [item for tup in [*image.getdata()] for item in tup] for byte in byteList: hexList.append(f'0x{byte:02x}') @@ -31,9 +35,11 @@ code = f"""#include #include namespace assets {{ -std::vector {function}() {{ - return {{ {','.join(hexList)} }}; -}} +Image {variable} = {{ + .width = {imageWidth}, + .height = {imageHeight}, + .data = {{ {','.join(hexList)} }} +}}; }} """