diff --git a/src/engine/controller/controller.hh b/src/engine/controller/controller.hh index 85979ac..f6a1f3e 100644 --- a/src/engine/controller/controller.hh +++ b/src/engine/controller/controller.hh @@ -14,12 +14,12 @@ class Controller { }; virtual bool InitializeController() { return false; }; virtual void PollController(){}; - virtual bool IsButtonPressed(Button button) { return 0; }; - virtual float GetLeftJoystickXAxis() { return 0.0; }; - virtual float GetLeftJoystickYAxis() { return 0.0; }; + virtual bool IsButtonPressed(Button button) const { return 0; }; + virtual float GetLeftJoystickXAxis() const { return 0.0; }; + virtual float GetLeftJoystickYAxis() const { return 0.0; }; virtual ~Controller() {}; protected: - virtual int GetButtonMask(Button button) { return 0; } + virtual int GetButtonMask(Button button) const { return 0; } }; #endif diff --git a/src/engine/controller/desktopController.cc b/src/engine/controller/desktopController.cc index 9c34061..7db0689 100644 --- a/src/engine/controller/desktopController.cc +++ b/src/engine/controller/desktopController.cc @@ -24,19 +24,19 @@ void DesktopController::PollController() { glfwGetGamepadState(GLFW_JOYSTICK_1, &controllerState); } -bool DesktopController::IsButtonPressed(Button button) { +bool DesktopController::IsButtonPressed(Button button) const { return controllerState.buttons[GetButtonMask(button)]; } -float DesktopController::GetLeftJoystickXAxis() { +float DesktopController::GetLeftJoystickXAxis() const { return controllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_X]; } -float DesktopController::GetLeftJoystickYAxis() { +float DesktopController::GetLeftJoystickYAxis() const { return controllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_Y]; } -int DesktopController::GetButtonMask(Button button) { +int DesktopController::GetButtonMask(Button button) const { switch (button) { case Button::A: return GLFW_GAMEPAD_BUTTON_A; diff --git a/src/engine/controller/desktopController.hh b/src/engine/controller/desktopController.hh index 2678cef..8ecc6ea 100644 --- a/src/engine/controller/desktopController.hh +++ b/src/engine/controller/desktopController.hh @@ -7,10 +7,10 @@ class DesktopController : public Controller { public: bool InitializeController() override; void PollController() override; - bool IsButtonPressed(Button button) override; - float GetLeftJoystickXAxis() override; - float GetLeftJoystickYAxis() override; - int GetButtonMask(Button button) override; + bool IsButtonPressed(Button button) const override; + float GetLeftJoystickXAxis() const override; + float GetLeftJoystickYAxis() const override; + int GetButtonMask(Button button) const override; GLFWgamepadstate controllerState; }; diff --git a/src/engine/controller/dreamcastController.cc b/src/engine/controller/dreamcastController.cc index a710200..734467f 100644 --- a/src/engine/controller/dreamcastController.cc +++ b/src/engine/controller/dreamcastController.cc @@ -21,19 +21,19 @@ void DreamcastController::PollController() { controller_state = (cont_state_t*)maple_dev_status(controller); } -bool DreamcastController::IsButtonPressed(Button button) { +bool DreamcastController::IsButtonPressed(Button button) const { return controller_state->buttons & GetButtonMask(button); } -float DreamcastController::GetLeftJoystickXAxis() { +float DreamcastController::GetLeftJoystickXAxis() const { return (controller_state->joyx / 127.0f); } -float DreamcastController::GetLeftJoystickYAxis() { +float DreamcastController::GetLeftJoystickYAxis() const { return (controller_state->joyy / 127.0f); } -int DreamcastController::GetButtonMask(Button button) { +int DreamcastController::GetButtonMask(Button button) const { switch (button) { case Button::A: return CONT_A; diff --git a/src/engine/controller/dreamcastController.hh b/src/engine/controller/dreamcastController.hh index e8c47a0..1e02498 100644 --- a/src/engine/controller/dreamcastController.hh +++ b/src/engine/controller/dreamcastController.hh @@ -8,10 +8,10 @@ class DreamcastController : public Controller { DreamcastController(); bool InitializeController() override; void PollController() override; - bool IsButtonPressed(Button button) override; - float GetLeftJoystickXAxis() override; - float GetLeftJoystickYAxis() override; - int GetButtonMask(Button button) override; + bool IsButtonPressed(Button button) const override; + float GetLeftJoystickXAxis() const override; + float GetLeftJoystickYAxis() const override; + int GetButtonMask(Button button) const override; maple_device_t* controller = nullptr; cont_state_t* controller_state = nullptr; }; diff --git a/src/engine/engine.cc b/src/engine/engine.cc index 6e89bbd..ac5021f 100644 --- a/src/engine/engine.cc +++ b/src/engine/engine.cc @@ -68,6 +68,7 @@ void Engine::deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame, void Engine::initializeGameLoop() { struct timeval beginningOfFrame, endOfFrame; + start(); while (!ShouldWindowClose()) { deltaTimeLoop(&Engine::gameLoop, beginningOfFrame, endOfFrame); } diff --git a/src/engine/engine.hh b/src/engine/engine.hh index 2caa9a5..db90971 100644 --- a/src/engine/engine.hh +++ b/src/engine/engine.hh @@ -28,6 +28,7 @@ class Engine { virtual void initScreen(); virtual void gameLoop(){}; + virtual void start() {}; void printSystemInformation(); void SwapBuffers() const; bool ShouldWindowClose() const; diff --git a/src/engine/entity.hh b/src/engine/entity.hh index 8b9faa8..800fd57 100644 --- a/src/engine/entity.hh +++ b/src/engine/entity.hh @@ -22,12 +22,12 @@ class Entity { void updatePosition(Engine::Vector3 position); Engine::Vector3 getScale() const; void updateScale(Engine::Vector3 scale); + void DestroyChildren(); private: Engine::Vector3 position = Engine::Vector3::zero; Engine::Vector3 globalPosition = Engine::Vector3::zero; Engine::Vector3 scale = {1, 1, 1}; - void DestroyChildren(); static void drawChildrenEntity(Entity* entity); static void updateChildrenEntity(Entity* entity); }; diff --git a/src/engine/meson.build b/src/engine/meson.build index 7d62a40..e66f4f3 100644 --- a/src/engine/meson.build +++ b/src/engine/meson.build @@ -10,5 +10,17 @@ endif subdir('controller') -engine = static_library('engine', ['dreamcastEngine.cc', 'desktopEngine.cc', 'entity.cc', 'engine.cc'], dependencies: engine_deps, include_directories: incdirs) +engine_src = [] + +if host_machine.system() == 'dreamcast' + engine_src += 'dreamcastEngine.cc' +endif + +if host_machine.system() != 'dreamcast' + engine_src += 'desktopEngine.cc' +endif + +engine_src += ['entity.cc', 'engine.cc'] + +engine = static_library('engine', engine_src, dependencies: engine_deps, include_directories: incdirs) engine_dep = declare_dependency(link_with: [engine, controller], include_directories: incdirs) diff --git a/src/flappyBird/Bird.cc b/src/flappyBird/Bird.cc index 49aa1fc..1197e80 100644 --- a/src/flappyBird/Bird.cc +++ b/src/flappyBird/Bird.cc @@ -21,7 +21,7 @@ void Bird::draw() { void Bird::update() { if (pipes != nullptr) { - for (int i = 0; i < pipes->children.size(); i++) { + for (size_t i = 0; i < pipes->children.size(); i++) { if (getGlobalPosition().x >= pipes->children[i]->getGlobalPosition().x - 0.1f && getGlobalPosition().x <= diff --git a/src/flappyBird/FlappyBird.cc b/src/flappyBird/FlappyBird.cc index 5e35ca9..2b3a303 100644 --- a/src/flappyBird/FlappyBird.cc +++ b/src/flappyBird/FlappyBird.cc @@ -1,5 +1,7 @@ #include "FlappyBird.hh" +#include +#include #include #include "MainMenu.hh" @@ -10,19 +12,21 @@ void FlappyBird::gameLoop() { return; } + if (!thread) { + thread = new std::thread(&FlappyBird::spawnPipes, this); + } + mainGameLoop(); } void FlappyBird::spawnPipes() { threadRunning = true; while (threadRunning) { - addNewPipe = true; - while (addNewPipe) { - if (!threadRunning) { - return; - } - std::this_thread::yield(); - } + doneSpawningPipes = false; + pipesContainer->children.push_back( + Pipes::CreatePipes(this, pipesContainer)); + doneSpawningPipes = true; + cv.notify_one(); std::this_thread::sleep_for(std::chrono::seconds(2)); } } @@ -34,21 +38,18 @@ void FlappyBird::mainGameLoop() { SwapBuffers(); return; } - 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]; + delete pipesContainer->children[i]; pipesContainer->children.erase(pipesContainer->children.begin() + i); } } - if (addNewPipe) { - pipesContainer->children.push_back( - Pipes::CreatePipes(this, pipesContainer)); - addNewPipe = false; - } + + std::unique_lock lock(mutex); + cv.wait(lock, [&]{return doneSpawningPipes;}); + rendering = true; glClear(GL_COLOR_BUFFER_BIT); controller->PollController(); Entity::updateEntity(this->bird); @@ -64,7 +65,10 @@ void FlappyBird::mainMenuLoop() { thread->detach(); delete thread; thread = nullptr; + pipesContainer->DestroyChildren(); pipesContainer->children.clear(); + SwapBuffers(); + return; } controller->PollController(); Entity::updateEntity(this->mainMenu); diff --git a/src/flappyBird/FlappyBird.hh b/src/flappyBird/FlappyBird.hh index 9a569b0..9cd213e 100644 --- a/src/flappyBird/FlappyBird.hh +++ b/src/flappyBird/FlappyBird.hh @@ -1,5 +1,7 @@ #ifndef FLAPPY_BIRD_HH #define FLAPPY_BIRD_HH +#include +#include #include #include #include @@ -19,7 +21,10 @@ class FlappyBird : public Engine { MainMenu* mainMenu; std::thread* thread = nullptr; bool threadRunning; - bool addNewPipe; + bool doneSpawningPipes = true; + bool rendering = false; + std::mutex mutex; + std::condition_variable cv; FlappyBird(); ~FlappyBird(); };