Add a semi-working bird sprite to the flappyBird game.

This commit is contained in:
Fries 2024-04-23 16:52:55 -07:00
parent dd801873de
commit 2fc701723a
9 changed files with 81 additions and 12 deletions

29
.vscode/launch.json vendored
View file

@ -187,6 +187,35 @@
], ],
"avoidWindowsConsoleRedirection": false, "avoidWindowsConsoleRedirection": false,
"internalConsoleOptions": "openOnSessionStart" "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"
} }
] ]
} }

View file

@ -16,6 +16,7 @@ public:
std::vector<Entity*> children = {}; std::vector<Entity*> children = {};
virtual void Draw() {}; virtual void Draw() {};
virtual void Update() {}; virtual void Update() {};
virtual void Start() {};
bool b_destroyed = false; bool b_destroyed = false;
Vector3 GetPosition() const; Vector3 GetPosition() const;
Vector3 GetGlobalPosition() const; Vector3 GetGlobalPosition() const;

View file

@ -2,6 +2,11 @@
#define ASSETS_HH #define ASSETS_HH
#include <vector> #include <vector>
namespace assets { namespace assets {
std::vector<unsigned char> bird(); struct Image {
int width;
int height;
std::vector<unsigned char> data;
};
extern Image bird;
} }
#endif #endif

View file

@ -1,22 +1,29 @@
#include "Bird.hh" #include "Bird.hh"
#include "Assets.hh"
#include <GL/gl.h>
#include <cstdio> #include <cstdio>
#include <vector>
void Bird::Draw() { void Bird::Draw() {
engine::Vector3 one = {-1.0, -1.0, 0.0}; engine::Vector3 one = {-1.0, -1.0, 0.0};
engine::Vector3 two = {0.0, 1.0, 0.0}; engine::Vector3 two = {0.0, 1.0, 0.0};
engine::Vector3 three = {1.0, -1.0, 0.0f}; engine::Vector3 three = {1.0, -1.0, 0.0f};
glEnable(GL_TEXTURE_2D);
glBegin(GL_TRIANGLES); 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)); 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); 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); glVertex3fv((float*)&three);
glEnd(); glEnd();
glDisable(GL_TEXTURE_2D);
} }
void Bird::Update() { void Bird::Update() {
@ -63,3 +70,17 @@ Bird::Bird(engine::Engine* engie, engine::Entity* parent, PipesContainer* pipes)
: Entity(engie, parent) { : Entity(engie, parent) {
this->pipes = pipes; 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());
}

View file

@ -7,6 +7,7 @@ class Bird : public engine::Entity {
public: public:
void Draw() override; void Draw() override;
void Update() override; void Update() override;
void Start() override;
Bird(engine::Engine* engie, engine::Entity* parent, PipesContainer* pipes); Bird(engine::Engine* engie, engine::Entity* parent, PipesContainer* pipes);
bool b_isHit = false; bool b_isHit = false;
@ -15,5 +16,6 @@ class Bird : public engine::Entity {
static constexpr float JUMP_CONST = 0.5; static constexpr float JUMP_CONST = 0.5;
float f_verticalSpeed = JUMP_CONST; float f_verticalSpeed = JUMP_CONST;
PipesContainer *pipes = nullptr; PipesContainer *pipes = nullptr;
unsigned int ui_birdTexture = 0;
}; };
#endif #endif

View file

@ -105,3 +105,7 @@ FlappyBird::~FlappyBird()
p_thread->detach(); p_thread->detach();
delete p_thread; delete p_thread;
} }
void FlappyBird::Start() {
p_bird->Start();
}

View file

@ -11,6 +11,7 @@
class FlappyBird : public engine::Engine { class FlappyBird : public engine::Engine {
void GameLoop() override; void GameLoop() override;
void Start() override;
void SpawnPipes(); void SpawnPipes();
void MainGameLoop(); void MainGameLoop();
void MainMenuLoop(); void MainMenuLoop();

Binary file not shown.

Before

Width:  |  Height:  |  Size: 426 B

After

Width:  |  Height:  |  Size: 4.4 KiB

View file

@ -3,13 +3,13 @@ from pathlib import Path
from PIL import Image from PIL import Image
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("function") parser.add_argument("variable")
parser.add_argument("input") parser.add_argument("input")
parser.add_argument("output") parser.add_argument("output")
args = parser.parse_args() args = parser.parse_args()
function = args.function variable = args.variable
input = Path(args.input) input = Path(args.input)
output = Path(args.output) output = Path(args.output)
@ -18,10 +18,14 @@ if not input.exists():
raise SystemExit(1) raise SystemExit(1)
hexList = list() hexList = list()
imageWidth = 0
imageHeight = 0
with Image.open(input) as image: with Image.open(input) as image:
imageBytes = bytes(image.getdata()) imageWidth = image.width
byteList = list(imageBytes) 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: for byte in byteList:
hexList.append(f'0x{byte:02x}') hexList.append(f'0x{byte:02x}')
@ -31,9 +35,11 @@ code = f"""#include <Assets.hh>
#include <vector> #include <vector>
namespace assets {{ namespace assets {{
std::vector<unsigned char> {function}() {{ Image {variable} = {{
return {{ {','.join(hexList)} }}; .width = {imageWidth},
}} .height = {imageHeight},
.data = {{ {','.join(hexList)} }}
}};
}} }}
""" """