From c651dcd03831a985a538531e69efe994a468d4bb Mon Sep 17 00:00:00 2001 From: Fries Date: Sat, 16 Mar 2024 22:18:15 -0700 Subject: [PATCH] Create a basic flappy bird clone. Right now, theres no score but I did make a entity abstraction which is like a GameObject from Unity where you have a position and scale (but no rotation) and the entity has a draw function and an update function. --- .vscode/launch.json | 24 +++++++++++ src/cube.cc | 4 +- src/engine/controller/meson.build | 1 - src/engine/engine.cc | 49 ++++++++++++---------- src/engine/engine.hh | 23 ++++++---- src/engine/entity.cc | 56 +++++++++++++++++++++++++ src/engine/entity.hh | 27 ++++++++++++ src/engine/linuxEngine.cc | 3 +- src/engine/meson.build | 2 +- src/flappyBird.cc | 9 ++++ src/flappyBird/Bird.cc | 33 +++++++++++++++ src/flappyBird/Bird.hh | 16 +++++++ src/flappyBird/FlappyBird.cc | 56 +++++++++++++++++++++++++ src/flappyBird/FlappyBird.hh | 22 ++++++++++ src/flappyBird/Pipes.cc | 54 ++++++++++++++++++++++++ src/flappyBird/Pipes.hh | 26 ++++++++++++ src/flappyBird/meson.build | 8 ++++ src/gl.cc | 70 +------------------------------ src/gl/Gl.cc | 15 +++++++ src/gl/Gl.hh | 14 +++++++ src/gl/GlEntity.cc | 52 +++++++++++++++++++++++ src/gl/GlEntity.hh | 15 +++++++ src/gl/meson.build | 9 ++++ src/hello.cc | 26 +----------- src/hello/Hello.cc | 11 +++++ src/hello/Hello.hh | 12 ++++++ src/hello/HelloEntity.cc | 16 +++++++ src/hello/HelloEntity.hh | 9 ++++ src/hello/meson.build | 8 ++++ src/meson.build | 12 +++++- 30 files changed, 551 insertions(+), 131 deletions(-) create mode 100644 src/engine/entity.cc create mode 100644 src/engine/entity.hh create mode 100644 src/flappyBird.cc create mode 100644 src/flappyBird/Bird.cc create mode 100644 src/flappyBird/Bird.hh create mode 100644 src/flappyBird/FlappyBird.cc create mode 100644 src/flappyBird/FlappyBird.hh create mode 100644 src/flappyBird/Pipes.cc create mode 100644 src/flappyBird/Pipes.hh create mode 100644 src/flappyBird/meson.build create mode 100644 src/gl/Gl.cc create mode 100644 src/gl/Gl.hh create mode 100644 src/gl/GlEntity.cc create mode 100644 src/gl/GlEntity.hh create mode 100644 src/gl/meson.build create mode 100644 src/hello/Hello.cc create mode 100644 src/hello/Hello.hh create mode 100644 src/hello/HelloEntity.cc create mode 100644 src/hello/HelloEntity.hh create mode 100644 src/hello/meson.build diff --git a/.vscode/launch.json b/.vscode/launch.json index 50cbc4a..88266ac 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -48,6 +48,30 @@ } ] }, + { + "name": "(gdb, Linux) Launch flappyBird.elf", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/src/flappyBird.elf", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}/build", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set Disassembly Flavor to Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ] + }, { "name": "(gdb) Launch gl.elf", "type": "cppdbg", diff --git a/src/cube.cc b/src/cube.cc index 6a823e8..e35d684 100644 --- a/src/cube.cc +++ b/src/cube.cc @@ -84,7 +84,7 @@ void Cube::model() { // glNormal3f(nx, ny, nz); // drawCalls += 1; } else { - normals.push_back(Engine::Vector3::zero()); + normals.push_back(Vector3::zero); } // Check if `texcoord_index` is zero or positive. negative = no @@ -99,7 +99,7 @@ void Cube::model() { // glTexCoord2f(tx, ty); // drawCalls += 1; } else { - textureCoords.push_back(Vector3::zero()); + textureCoords.push_back(Vector3::zero); } } index_offset += fv; diff --git a/src/engine/controller/meson.build b/src/engine/controller/meson.build index ba0762d..45dc896 100644 --- a/src/engine/controller/meson.build +++ b/src/engine/controller/meson.build @@ -1,5 +1,4 @@ sources = [] -incdirs = include_directories('.') if host_machine.system() == 'dreamcast' sources += 'dreamcastController.cc' diff --git a/src/engine/engine.cc b/src/engine/engine.cc index 8fea3d3..66f8764 100644 --- a/src/engine/engine.cc +++ b/src/engine/engine.cc @@ -4,20 +4,22 @@ #ifdef _arch_dreamcast #include + #include "dreamcastEngine.hh" #else #include "linuxEngine.hh" #endif #include +#include #include void Engine::initializeEngine() { - #ifdef _arch_dreamcast +#ifdef _arch_dreamcast engine = new DreamcastEngine; - #else +#else engine = new LinuxEngine; - #endif +#endif engine->initializeEngine(); initScreen(); } @@ -26,26 +28,21 @@ void Engine::initializeController() { engine->initializeController(); controller = engine->controller; } -void Engine::SwapBuffers() { - engine->SwapBuffers(); -} +void Engine::SwapBuffers() const { engine->SwapBuffers(); } -bool Engine::ShouldWindowClose() { - return engine->ShouldWindowClose(); -} +bool Engine::ShouldWindowClose() const { return engine->ShouldWindowClose(); } -Engine::~Engine() { - delete engine; -} +Engine::~Engine() { delete engine; } void Engine::initScreen() { - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - glViewport(0, 0, 640, 480); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glOrtho(0, 640, 0, 480, -100, 100); glMatrixMode(GL_PROJECTION); - glLoadIdentity(); + // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glViewport(0, 0, 640, 480); + // glMatrixMode(GL_MODELVIEW); + // glLoadIdentity(); + // glOrtho(0, 640, 0, 480, -100, 100); + // glMatrixMode(GL_PROJECTION); + // glLoadIdentity(); } void Engine::printSystemInformation() { @@ -76,7 +73,8 @@ void Engine::initializeGameLoop() { } } -void Engine::pressButton(Controller::Button button, std::function callback) { +void Engine::pressButton(Controller::Button button, + std::function callback) { if (controller->IsButtonPressed(button)) { if (!(buttonsPressed & button)) { buttonsPressed |= button; @@ -89,10 +87,15 @@ void Engine::pressButton(Controller::Button button, std::function callba } } -void Engine::glVertex3fNormalized(float x, float y, float z) { - constexpr int xSize = 640, ySize = 480; +const Engine::Vector3 Engine::Vector3::zero = {0, 0, 0}; - glVertex3f(x * xSize, y * ySize, z); +Engine::Vector3 Engine::Vector3::operator/(float amount) { + return {this->x / amount, this->y / amount, this->z / amount}; } -Engine::Vector3 Engine::Vector3::zero() { return {0, 0, 0}; } +void Engine::Vector3::operator/=(float amount) { + Vector3 vec = *this / amount; + std::memcpy(this, &vec, sizeof(vec)); +} + +float Engine::getDeltaTime() const { return deltaTime; } diff --git a/src/engine/engine.hh b/src/engine/engine.hh index 61f99ae..80bc45b 100644 --- a/src/engine/engine.hh +++ b/src/engine/engine.hh @@ -8,6 +8,12 @@ #include "controller/controller.hh" #include "nativeEngine.hh" +#ifdef _arch_dreamcast +#include +#else +#include +#endif + class Engine { protected: static constexpr float microSecond = 0.000001f; @@ -19,33 +25,36 @@ class Engine { bool thirtyfps; unsigned int buttonsPressed; - Controller* controller = nullptr; virtual void initScreen(); virtual void gameLoop(){}; void printSystemInformation(); - void pressButton(Controller::Button button, std::function callback); - void glVertex3fNormalized(float x, float y, float z); - void SwapBuffers(); - bool ShouldWindowClose(); + void SwapBuffers() const; + bool ShouldWindowClose() const; private: + NativeEngine* engine = nullptr; + template void deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame, struct timeval& endOfFrame); - NativeEngine* engine = nullptr; public: void initializeController(); void initializeEngine(); void initializeGameLoop(); void cleanupEngine(); + float getDeltaTime() const; + void pressButton(Controller::Button button, std::function callback); virtual ~Engine(); + Controller* controller = nullptr; struct Vector3 { - static Vector3 zero(); + Vector3 operator/(float amount); + void operator/=(float amount); float x; float y; float z; + static const Vector3 zero; }; }; #endif diff --git a/src/engine/entity.cc b/src/engine/entity.cc new file mode 100644 index 0000000..f709960 --- /dev/null +++ b/src/engine/entity.cc @@ -0,0 +1,56 @@ +#include "entity.hh" +#include + +void Entity::DestroyChildren() { + for (Entity* childEntity : children) { + delete childEntity; + } +} + +Entity::~Entity() { + DestroyChildren(); +} + +void Entity::drawEntity(Entity* entity) { + glPushMatrix(); + glTranslatef(entity->position.x, entity->position.y, entity->position.z); + glScalef(entity->scale.x, entity->scale.y, entity->scale.z); + entity->draw(); + glPopMatrix(); + if (entity->children.size() > 0) { + drawChildrenEntity(entity); + } +} + +void Entity::updateEntity(Entity* entity) { + entity->update(); + if (entity->children.size() > 0) { + updateChildrenEntity(entity); + } +} + +void Entity::drawChildrenEntity(Entity* entity) { + for (Entity* childEntity : entity->children) { + glPushMatrix(); + if (childEntity->parent == nullptr) { + printf("Error: Entity parent is nullptr.\n"); + } else { + glTranslatef( + childEntity->parent->position.x + childEntity->position.x, + childEntity->parent->position.y + childEntity->position.y, + +childEntity->parent->position.z + childEntity->position.z); + } + glScalef(childEntity->scale.x, childEntity->scale.y, + childEntity->scale.z); + childEntity->draw(); + glPopMatrix(); + drawChildrenEntity(childEntity); + } +} + +void Entity::updateChildrenEntity(Entity* entity) { + for (Entity* childEntity : entity->children) { + childEntity->update(); + updateChildrenEntity(childEntity); + } +} diff --git a/src/engine/entity.hh b/src/engine/entity.hh new file mode 100644 index 0000000..3e39d3e --- /dev/null +++ b/src/engine/entity.hh @@ -0,0 +1,27 @@ +#ifndef ENTITY_HH +#define ENTITY_HH +#include "engine.hh" +class Entity { + public: + Entity(Engine* engie, Entity* parent) { + engine = engie; + this->parent = parent; + }; + virtual ~Entity(); + Engine* engine; + Engine::Vector3 position = {0, 0, 0}; + Engine::Vector3 scale = {1, 1, 1}; + Entity* parent = nullptr; + std::vector children = {}; + virtual void draw(){}; + virtual void update(){}; + bool destroyed = false; + static void drawEntity(Entity* entity); + static void updateEntity(Entity* entity); + + private: + void DestroyChildren(); + static void drawChildrenEntity(Entity* entity); + static void updateChildrenEntity(Entity* entity); +}; +#endif diff --git a/src/engine/linuxEngine.cc b/src/engine/linuxEngine.cc index d59d98a..c8e22fe 100644 --- a/src/engine/linuxEngine.cc +++ b/src/engine/linuxEngine.cc @@ -1,4 +1,3 @@ -#include #ifdef __linux__ #include "linuxEngine.hh" @@ -6,7 +5,7 @@ #include #include -#include "linuxController.hh" +#include "controller/linuxController.hh" void LinuxEngine::initializeEngine() { glfwInit(); diff --git a/src/engine/meson.build b/src/engine/meson.build index 41d29a6..303f439 100644 --- a/src/engine/meson.build +++ b/src/engine/meson.build @@ -10,5 +10,5 @@ endif subdir('controller') -engine = static_library('engine', ['dreamcastEngine.cc', 'linuxEngine.cc', 'engine.cc'], dependencies: engine_deps, include_directories: incdirs) +engine = static_library('engine', ['dreamcastEngine.cc', 'linuxEngine.cc', 'entity.cc', 'engine.cc'], dependencies: engine_deps, include_directories: incdirs) engine_dep = declare_dependency(link_with: [engine, controller], include_directories: incdirs) diff --git a/src/flappyBird.cc b/src/flappyBird.cc new file mode 100644 index 0000000..5058a90 --- /dev/null +++ b/src/flappyBird.cc @@ -0,0 +1,9 @@ +#include "flappyBird/FlappyBird.hh" + +int main() { + FlappyBird* engine = new FlappyBird; + engine->initializeEngine(); + engine->initializeController(); + engine->initializeGameLoop(); + delete engine; +} diff --git a/src/flappyBird/Bird.cc b/src/flappyBird/Bird.cc new file mode 100644 index 0000000..2d551b7 --- /dev/null +++ b/src/flappyBird/Bird.cc @@ -0,0 +1,33 @@ +#include "Bird.hh" + +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}; + + glBegin(GL_TRIANGLES); + glColor3f(1.0f, 0.0f, 0.0f); + glVertex3fv((float*)&(one)); + + glColor3f(0.0f, 1.0f, 0.0f); + glVertex3fv((float*)&two); + + glColor3f(0.0f, 0.0f, 1.0f); + glVertex3fv((float*)&three); + glEnd(); +} + +void Bird::update() { + float deltaTime = engine->getDeltaTime(); + engine->pressButton(Controller::A, [&]() { + if (verticalSpeed < 0) { + verticalSpeed = JUMP_CONST; + return; + } + verticalSpeed = std::clamp(verticalSpeed + JUMP_CONST, 0.0f, 1.0f); + }); + + position.y = + std::clamp(position.y + (verticalSpeed * deltaTime), -1.0f, 1.0f); + verticalSpeed -= FALLING_CONST * deltaTime; +} diff --git a/src/flappyBird/Bird.hh b/src/flappyBird/Bird.hh new file mode 100644 index 0000000..17e4f1e --- /dev/null +++ b/src/flappyBird/Bird.hh @@ -0,0 +1,16 @@ +#ifndef BIRD_HH +#define BIRD_HH +#include + +class Bird : public Entity { + public: + void draw() override; + void update() override; + Bird(Engine* engie, Entity* parent) : Entity(engie, parent) {} + + private: + static constexpr float FALLING_CONST = 0.65; + static constexpr float JUMP_CONST = 0.5; + float verticalSpeed = JUMP_CONST; +}; +#endif diff --git a/src/flappyBird/FlappyBird.cc b/src/flappyBird/FlappyBird.cc new file mode 100644 index 0000000..00016d7 --- /dev/null +++ b/src/flappyBird/FlappyBird.cc @@ -0,0 +1,56 @@ +#include "FlappyBird.hh" +#include + +void FlappyBird::gameLoop() { + if (!thread) { + thread = new std::thread(&FlappyBird::spawnPipes, this); + } + for (size_t i = 0; i < pipesContainer->children.size(); i++) { + if (pipesContainer->children[i]->destroyed) { + delete pipesContainer->children[i]; + pipesContainer->children.erase(pipesContainer->children.begin() + i); + } + } + if (addNewPipe) { + pipesContainer->children.push_back( + Pipes::CreatePipes(this, pipesContainer)); + addNewPipe = false; + } + glClear(GL_COLOR_BUFFER_BIT); + controller->PollController(); + Entity::updateEntity(this->bird); + Entity::updateEntity(this->pipesContainer); + Entity::drawEntity(this->bird); + Entity::drawEntity(this->pipesContainer); + SwapBuffers(); +} + +void FlappyBird::spawnPipes() { + threadRunning = true; + while (threadRunning) { + addNewPipe = true; + while (addNewPipe) { + if (!threadRunning) { + return; + } + std::this_thread::yield(); + } + std::this_thread::sleep_for(std::chrono::seconds(2)); + } +} + +FlappyBird::FlappyBird() { + bird = new Bird(this, nullptr); + bird->scale.x = 0.1; + bird->scale.y = 0.1; + bird->position.x = -0.75; + pipesContainer = new PipesContainer(this, nullptr); +} + +FlappyBird::~FlappyBird() { + threadRunning = false; + delete bird; + delete pipesContainer; + thread->detach(); + delete thread; +} diff --git a/src/flappyBird/FlappyBird.hh b/src/flappyBird/FlappyBird.hh new file mode 100644 index 0000000..58fa057 --- /dev/null +++ b/src/flappyBird/FlappyBird.hh @@ -0,0 +1,22 @@ +#ifndef FLAPPY_BIRD_HH +#define FLAPPY_BIRD_HH +#include +#include +#include +#include "Bird.hh" +#include "Pipes.hh" + +class FlappyBird : public Engine { + void gameLoop() override; + void spawnPipes(); + + public: + Bird* bird; + PipesContainer* pipesContainer; + std::thread* thread = nullptr; + bool threadRunning; + bool addNewPipe; + FlappyBird(); + ~FlappyBird(); +}; +#endif diff --git a/src/flappyBird/Pipes.cc b/src/flappyBird/Pipes.cc new file mode 100644 index 0000000..3b9b809 --- /dev/null +++ b/src/flappyBird/Pipes.cc @@ -0,0 +1,54 @@ +#include "Pipes.hh" + +#include + +#include "engine/entity.hh" + +void Pipe::draw() { glRectf(-1.0f, pipeLength, 1.0f, -pipeLength); } + +Pipes::Pipes(Engine* engie, Entity* parent) : Entity(engie, parent) { + std::random_device r; + std::default_random_engine generator(r()); + + std::uniform_int_distribution upOrDownGenerator(0, 2); + int upOrDown = upOrDownGenerator(generator); + + std::uniform_real_distribution offsetGenerator(0.35f, 0.8f); + + if (upOrDown) { + pipeOffset = -offsetGenerator(generator); + } else { + pipeOffset = offsetGenerator(generator); + } + + Pipe* pipe1 = new Pipe(engine, this); + Pipe* pipe2 = new Pipe(engine, this); + + pipe1->scale.x = 0.1; + pipe1->position.y = 1.0f; + pipe1->pipeLength = pipeLength + pipeOffset; + + pipe2->scale.x = 0.1; + pipe2->position.y = -1.0f; + pipe2->pipeLength = pipeLength - pipeOffset; + + children.push_back(pipe1); + children.push_back(pipe2); + + // position.x -= 0.1f; +} + +void Pipes::update() { + position.x -= 0.5f * engine->getDeltaTime(); + if (position.x < -2) { + destroyed = true; + } +} + +Pipes* Pipes::CreatePipes(Engine* engie, Entity* parent) { + Pipes* pipes = new Pipes(engie, parent); + pipes->scale.x = 0.1f; + pipes->scale.y = 0.1f; + pipes->position.x = 1.25f; + return pipes; +} diff --git a/src/flappyBird/Pipes.hh b/src/flappyBird/Pipes.hh new file mode 100644 index 0000000..2523eef --- /dev/null +++ b/src/flappyBird/Pipes.hh @@ -0,0 +1,26 @@ +#ifndef PIPES_HH +#define PIPES_HH +#include + +class Pipes : public Entity { + public: + Pipes(Engine* engie, Entity* parent); + void update() override; + float pipeLength = 0.7f; + float pipeOffset = 0.35f; + static Pipes* CreatePipes(Engine* engie, Entity* parent); +}; + +class Pipe : public Entity { + void draw() override; + + public: + Pipe(Engine* engie, Entity* parent) : Entity(engie, parent) {} + float pipeLength = 1.0f; +}; + +class PipesContainer : public Entity { + public: + PipesContainer(Engine* engie, Entity* parent) : Entity(engie, parent) {} +}; +#endif diff --git a/src/flappyBird/meson.build b/src/flappyBird/meson.build new file mode 100644 index 0000000..37ae5dd --- /dev/null +++ b/src/flappyBird/meson.build @@ -0,0 +1,8 @@ +flappy_sources = [ + 'Bird.cc', + 'FlappyBird.cc', + 'Pipes.cc' +] + +flappy_bird = static_library('flappyBird', flappy_sources, dependencies: deps, include_directories: incdirs) +flappy_dep = declare_dependency(link_with: [flappy_bird], include_directories: incdirs) diff --git a/src/gl.cc b/src/gl.cc index 2e53b22..d6c01b4 100644 --- a/src/gl.cc +++ b/src/gl.cc @@ -1,75 +1,9 @@ -#ifdef _arch_dreamcast -#include -#include -#include -#else -#include -#endif -#include -#include - -#include "engine/engine.hh" - -class Glcc : public Engine { - void process_input(); - void displayStuff(); - - void gameLoop() override { - process_input(); - displayStuff(); - } -}; - -void Glcc::displayStuff() { - glClear(GL_COLOR_BUFFER_BIT); - - // clang-format off - glPushMatrix(); - glRotatef(angle, 0, 0, 1); - glColor3f(0.5f, 0.0f, 1.0f); - glBegin(GL_POLYGON); - glColor3f(1.0f, 0.0f, 0.0f); - glVertex3fNormalized(0.25f, 0.25f, 0.0f); - - glColor3f(0.0f, 1.0f, 0.0f); - glVertex3fNormalized(0.75f, 0.25f, 0.0f); - - glColor3f(0.0f, 0.0f, 1.0f); - glVertex3fNormalized(0.75f, 0.75f, 0.0f); - - glColor3f(1.0f, 1.0f, 1.0f); - glVertex3fNormalized(0.25f, 0.75f, 0.0f); - glEnd(); - glPopMatrix(); - // clang-format on - - - #ifdef _arch_dreamcast - if (thirtyfps) { - vid_waitvbl(); - vid_waitvbl(); - } - #endif - - SwapBuffers(); -} - -void Glcc::process_input() { - controller->PollController(); - pressButton(Controller::Button::DPAD_LEFT, [&]() { speed -= 1.0f; }); - pressButton(Controller::Button::DPAD_RIGHT, [&]() { speed += 1.0f; }); - pressButton(Controller::Button::A, [&]() { thirtyfps = !thirtyfps; }); - - speed = std::clamp(speed, 1.0f, 10.0f); - - angle += controller->GetLeftJoystickXAxis() * deltaTime * speed; - printf("%f\n", speed); - // printf("%f\n", controller->GetLeftJoystickXAxis()); -} +#include "gl/Gl.hh" int main() { Glcc* engine = new Glcc; engine->initializeEngine(); engine->initializeController(); engine->initializeGameLoop(); + delete engine; } diff --git a/src/gl/Gl.cc b/src/gl/Gl.cc new file mode 100644 index 0000000..eb41c24 --- /dev/null +++ b/src/gl/Gl.cc @@ -0,0 +1,15 @@ +#include "Gl.hh" +#include +#include "GlEntity.hh" + +Glcc::Glcc() { + entity = new GlEntity(this, nullptr); +} + +void Glcc::gameLoop() { + glClear(GL_COLOR_BUFFER_BIT); + controller->PollController(); + Entity::updateEntity(entity); + Entity::drawEntity(entity); + SwapBuffers(); +} diff --git a/src/gl/Gl.hh b/src/gl/Gl.hh new file mode 100644 index 0000000..8f22dbc --- /dev/null +++ b/src/gl/Gl.hh @@ -0,0 +1,14 @@ +#ifndef GL_HH +#define GL_HH +#include + +#include "GlEntity.hh" +class Glcc : public Engine { + public: + Glcc(); + + private: + void gameLoop() override; + GlEntity* entity; +}; +#endif diff --git a/src/gl/GlEntity.cc b/src/gl/GlEntity.cc new file mode 100644 index 0000000..91628fb --- /dev/null +++ b/src/gl/GlEntity.cc @@ -0,0 +1,52 @@ +#include "GlEntity.hh" + +#include +#ifdef _arch_dreamcast +#include +#endif + +GlEntity::GlEntity(Engine* engie, Entity* parent) : Entity(engie, parent) {} + +void GlEntity::update() { + engine->pressButton(Controller::Button::DPAD_LEFT, + [&]() { speed -= 1.0f; }); + engine->pressButton(Controller::Button::DPAD_RIGHT, + [&]() { speed += 1.0f; }); + engine->pressButton(Controller::Button::A, + [&]() { thirtyFps = !thirtyFps; }); + + speed = std::clamp(speed, 1.0f, 10.0f); + + angle += engine->controller->GetLeftJoystickXAxis() * + engine->getDeltaTime() * speed; + printf("%f\n", speed); +} + +void GlEntity::draw() { + // clang-format off + glPushMatrix(); + glRotatef(angle, 0, 0, 1); + glColor3f(0.5f, 0.0f, 1.0f); + glBegin(GL_POLYGON); + glColor3f(1.0f, 0.0f, 0.0f); + glVertex3f(0.25f, 0.25f, 0.0f); + + glColor3f(0.0f, 1.0f, 0.0f); + glVertex3f(0.75f, 0.25f, 0.0f); + + glColor3f(0.0f, 0.0f, 1.0f); + glVertex3f(0.75f, 0.75f, 0.0f); + + glColor3f(1.0f, 1.0f, 1.0f); + glVertex3f(0.25f, 0.75f, 0.0f); + glEnd(); + glPopMatrix(); + // clang-format on + +#ifdef _arch_dreamcast + if (thirtyFps) { + vid_waitvbl(); + vid_waitvbl(); + } +#endif +} diff --git a/src/gl/GlEntity.hh b/src/gl/GlEntity.hh new file mode 100644 index 0000000..e95fa51 --- /dev/null +++ b/src/gl/GlEntity.hh @@ -0,0 +1,15 @@ +#ifndef GL_ENTITY_HH +#define GL_ENTITY_HH +#include +class GlEntity : public Entity { + public: + GlEntity(Engine* engie, Entity* parent); + + private: + void update() override; + void draw() override; + bool thirtyFps; + float angle = 0; + float speed = 5.0f; +}; +#endif diff --git a/src/gl/meson.build b/src/gl/meson.build new file mode 100644 index 0000000..ce53c90 --- /dev/null +++ b/src/gl/meson.build @@ -0,0 +1,9 @@ +glcc_sources = [ + 'Gl.cc', + 'GlEntity.cc' +] + +glcc = static_library('glcc', glcc_sources, dependencies: deps, include_directories: incdirs) +glcc_dep = declare_dependency(link_with: [glcc], include_directories: incdirs) + + diff --git a/src/hello.cc b/src/hello.cc index 5d9ce81..5dd53d0 100644 --- a/src/hello.cc +++ b/src/hello.cc @@ -3,32 +3,8 @@ #else #include #endif -#include "engine/engine.hh" - -class Hello : public Engine { - void triangle(); - - void gameLoop() override { - triangle(); - SwapBuffers(); - } -}; - -void Hello::triangle() { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glBegin(GL_TRIANGLES); - glColor3f(1.0f, 0.0f, 0.0f); - glVertex3fNormalized(-1.0f, -1.0f, 0.0f); - - glColor3f(0.0f, 1.0f, 0.0f); - glVertex3fNormalized(0.0f, 1.0f, 0.0f); - - glColor3f(0.0f, 0.0f, 1.0f); - glVertex3fNormalized(1.0f, -1.0f, 0.0f); - glEnd(); -} +#include "hello/Hello.hh" int main(int argc, char *argv[]) { Hello* engine = new Hello; diff --git a/src/hello/Hello.cc b/src/hello/Hello.cc new file mode 100644 index 0000000..dfe18da --- /dev/null +++ b/src/hello/Hello.cc @@ -0,0 +1,11 @@ +#include "Hello.hh" + +Hello::Hello() { + entity = new HelloEntity(this, nullptr); +} + +void Hello::gameLoop() { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + Entity::drawEntity(entity); + SwapBuffers(); +} diff --git a/src/hello/Hello.hh b/src/hello/Hello.hh new file mode 100644 index 0000000..3aa9b2e --- /dev/null +++ b/src/hello/Hello.hh @@ -0,0 +1,12 @@ +#ifndef HELLO_HH +#define HELLO_HH +#include +#include "HelloEntity.hh" + +class Hello : public Engine { + public: + HelloEntity* entity; + void gameLoop() override; + Hello(); +}; +#endif diff --git a/src/hello/HelloEntity.cc b/src/hello/HelloEntity.cc new file mode 100644 index 0000000..cb20dcc --- /dev/null +++ b/src/hello/HelloEntity.cc @@ -0,0 +1,16 @@ +#include "HelloEntity.hh" + +void HelloEntity::draw() { + glBegin(GL_TRIANGLES); + glColor3f(1.0f, 0.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, 0.0f); + + glColor3f(0.0f, 1.0f, 0.0f); + glVertex3f(0.0f, 1.0f, 0.0f); + + glColor3f(0.0f, 0.0f, 1.0f); + glVertex3f(1.0f, -1.0f, 0.0f); + glEnd(); +} + +HelloEntity::HelloEntity(Engine* engie, Entity* parent) : Entity(engie, parent) {} diff --git a/src/hello/HelloEntity.hh b/src/hello/HelloEntity.hh new file mode 100644 index 0000000..f2b3235 --- /dev/null +++ b/src/hello/HelloEntity.hh @@ -0,0 +1,9 @@ +#ifndef HELLO_ENTITY_HH +#define HELLO_ENTITY_HH +#include +class HelloEntity : public Entity { + void draw() override; + public: + HelloEntity(Engine* engine, Entity* parent); +}; +#endif diff --git a/src/hello/meson.build b/src/hello/meson.build new file mode 100644 index 0000000..ebfce3c --- /dev/null +++ b/src/hello/meson.build @@ -0,0 +1,8 @@ +hello_sources = [ + 'Hello.cc', + 'HelloEntity.cc' +] + +hello = static_library('hello', hello_sources, dependencies: deps, include_directories: incdirs) +hello_dep = declare_dependency(link_with: [hello], include_directories: incdirs) + diff --git a/src/meson.build b/src/meson.build index 176598a..e521619 100644 --- a/src/meson.build +++ b/src/meson.build @@ -40,6 +40,7 @@ endif subdir('engine') deps += [engine_dep] +srcinc = include_directories('./') cube_sources = [ 'cube.cc' @@ -49,6 +50,13 @@ if host_machine.system() == 'dreamcast' cube_sources += romdsk_o endif -executable('hello.elf', ['hello.cc'], dependencies: deps, include_directories: incdirs) -executable('gl.elf', ['gl.cc'], dependencies: deps, include_directories: incdirs) +incdirs += [srcinc] + +subdir('hello') +subdir('gl') +subdir('flappyBird') + +executable('hello.elf', ['hello.cc'], dependencies: [deps] + [hello_dep], include_directories: incdirs) +executable('gl.elf', ['gl.cc'], dependencies: [deps] + [glcc_dep], include_directories: incdirs) executable('cube.elf', cube_sources, dependencies: deps, include_directories: incdirs) +executable('flappyBird.elf', ['flappyBird.cc'], dependencies: [deps] + [flappy_dep], include_directories: incdirs)