Compare commits

...

2 commits

10 changed files with 86 additions and 15 deletions

29
.vscode/launch.json vendored
View file

@ -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"
}
]
}

View file

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

View file

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

View file

@ -1,22 +1,29 @@
#include "Bird.hh"
#include "Assets.hh"
#include <GL/gl.h>
#include <cstdio>
#include <vector>
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());
}

View file

@ -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

View file

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

View file

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 426 B

After

Width:  |  Height:  |  Size: 4.4 KiB

23
src/flappyBird/generateAssets.py Normal file → Executable file
View file

@ -1,27 +1,32 @@
#!/usr/bin/env python3
import argparse
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)
if not input.exists():
print("Input " + str(path.absolute()) + " not found.")
print("Input " + str(input.absolute()) + " not found.")
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 +36,11 @@ code = f"""#include <Assets.hh>
#include <vector>
namespace assets {{
std::vector<unsigned char> {function}() {{
return {{ {','.join(hexList)} }};
}}
Image {variable} = {{
.width = {imageWidth},
.height = {imageHeight},
.data = {{ {','.join(hexList)} }}
}};
}}
"""

View file

@ -5,8 +5,9 @@ flappy_sources = [
'MainMenu.cc'
]
python3 = find_program('python3')
bird = custom_target('bird', output: 'birdSprite.cc', input: ['generateAssets.py', 'assets/yellowbird-downflap.png'], command: [python3, '@INPUT0@', 'bird', '@INPUT1@', '@OUTPUT@'])
assets = find_program('generateAssets.py')
asset_gen = generator(assets, output: '@BASENAME@.cc', arguments: ['@EXTRA_ARGS@', '@INPUT@', '@OUTPUT@'])
bird = asset_gen.process('assets/yellowbird-downflap.png', extra_args: ['bird'])
flappy_bird = static_library('flappyBird', [flappy_sources, bird], dependencies: deps, include_directories: incdirs)
flappy_dep = declare_dependency(link_with: [flappy_bird], include_directories: incdirs)