Try some threading again.

This commit is contained in:
Fries 2024-03-30 11:28:40 -07:00
parent 576179218c
commit c94fd602e8
12 changed files with 63 additions and 40 deletions

View file

@ -14,12 +14,12 @@ class Controller {
}; };
virtual bool InitializeController() { return false; }; virtual bool InitializeController() { return false; };
virtual void PollController(){}; virtual void PollController(){};
virtual bool IsButtonPressed(Button button) { return 0; }; virtual bool IsButtonPressed(Button button) const { return 0; };
virtual float GetLeftJoystickXAxis() { return 0.0; }; virtual float GetLeftJoystickXAxis() const { return 0.0; };
virtual float GetLeftJoystickYAxis() { return 0.0; }; virtual float GetLeftJoystickYAxis() const { return 0.0; };
virtual ~Controller() {}; virtual ~Controller() {};
protected: protected:
virtual int GetButtonMask(Button button) { return 0; } virtual int GetButtonMask(Button button) const { return 0; }
}; };
#endif #endif

View file

@ -24,19 +24,19 @@ void DesktopController::PollController() {
glfwGetGamepadState(GLFW_JOYSTICK_1, &controllerState); glfwGetGamepadState(GLFW_JOYSTICK_1, &controllerState);
} }
bool DesktopController::IsButtonPressed(Button button) { bool DesktopController::IsButtonPressed(Button button) const {
return controllerState.buttons[GetButtonMask(button)]; return controllerState.buttons[GetButtonMask(button)];
} }
float DesktopController::GetLeftJoystickXAxis() { float DesktopController::GetLeftJoystickXAxis() const {
return controllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_X]; return controllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_X];
} }
float DesktopController::GetLeftJoystickYAxis() { float DesktopController::GetLeftJoystickYAxis() const {
return controllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_Y]; return controllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_Y];
} }
int DesktopController::GetButtonMask(Button button) { int DesktopController::GetButtonMask(Button button) const {
switch (button) { switch (button) {
case Button::A: case Button::A:
return GLFW_GAMEPAD_BUTTON_A; return GLFW_GAMEPAD_BUTTON_A;

View file

@ -7,10 +7,10 @@ class DesktopController : public Controller {
public: public:
bool InitializeController() override; bool InitializeController() override;
void PollController() override; void PollController() override;
bool IsButtonPressed(Button button) override; bool IsButtonPressed(Button button) const override;
float GetLeftJoystickXAxis() override; float GetLeftJoystickXAxis() const override;
float GetLeftJoystickYAxis() override; float GetLeftJoystickYAxis() const override;
int GetButtonMask(Button button) override; int GetButtonMask(Button button) const override;
GLFWgamepadstate controllerState; GLFWgamepadstate controllerState;
}; };

View file

@ -21,19 +21,19 @@ void DreamcastController::PollController() {
controller_state = (cont_state_t*)maple_dev_status(controller); 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); return controller_state->buttons & GetButtonMask(button);
} }
float DreamcastController::GetLeftJoystickXAxis() { float DreamcastController::GetLeftJoystickXAxis() const {
return (controller_state->joyx / 127.0f); return (controller_state->joyx / 127.0f);
} }
float DreamcastController::GetLeftJoystickYAxis() { float DreamcastController::GetLeftJoystickYAxis() const {
return (controller_state->joyy / 127.0f); return (controller_state->joyy / 127.0f);
} }
int DreamcastController::GetButtonMask(Button button) { int DreamcastController::GetButtonMask(Button button) const {
switch (button) { switch (button) {
case Button::A: case Button::A:
return CONT_A; return CONT_A;

View file

@ -8,10 +8,10 @@ class DreamcastController : public Controller {
DreamcastController(); DreamcastController();
bool InitializeController() override; bool InitializeController() override;
void PollController() override; void PollController() override;
bool IsButtonPressed(Button button) override; bool IsButtonPressed(Button button) const override;
float GetLeftJoystickXAxis() override; float GetLeftJoystickXAxis() const override;
float GetLeftJoystickYAxis() override; float GetLeftJoystickYAxis() const override;
int GetButtonMask(Button button) override; int GetButtonMask(Button button) const override;
maple_device_t* controller = nullptr; maple_device_t* controller = nullptr;
cont_state_t* controller_state = nullptr; cont_state_t* controller_state = nullptr;
}; };

View file

@ -68,6 +68,7 @@ void Engine::deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame,
void Engine::initializeGameLoop() { void Engine::initializeGameLoop() {
struct timeval beginningOfFrame, endOfFrame; struct timeval beginningOfFrame, endOfFrame;
start();
while (!ShouldWindowClose()) { while (!ShouldWindowClose()) {
deltaTimeLoop(&Engine::gameLoop, beginningOfFrame, endOfFrame); deltaTimeLoop(&Engine::gameLoop, beginningOfFrame, endOfFrame);
} }

View file

@ -28,6 +28,7 @@ class Engine {
virtual void initScreen(); virtual void initScreen();
virtual void gameLoop(){}; virtual void gameLoop(){};
virtual void start() {};
void printSystemInformation(); void printSystemInformation();
void SwapBuffers() const; void SwapBuffers() const;
bool ShouldWindowClose() const; bool ShouldWindowClose() const;

View file

@ -22,12 +22,12 @@ class Entity {
void updatePosition(Engine::Vector3 position); void updatePosition(Engine::Vector3 position);
Engine::Vector3 getScale() const; Engine::Vector3 getScale() const;
void updateScale(Engine::Vector3 scale); void updateScale(Engine::Vector3 scale);
void DestroyChildren();
private: private:
Engine::Vector3 position = Engine::Vector3::zero; Engine::Vector3 position = Engine::Vector3::zero;
Engine::Vector3 globalPosition = Engine::Vector3::zero; Engine::Vector3 globalPosition = Engine::Vector3::zero;
Engine::Vector3 scale = {1, 1, 1}; Engine::Vector3 scale = {1, 1, 1};
void DestroyChildren();
static void drawChildrenEntity(Entity* entity); static void drawChildrenEntity(Entity* entity);
static void updateChildrenEntity(Entity* entity); static void updateChildrenEntity(Entity* entity);
}; };

View file

@ -10,5 +10,17 @@ endif
subdir('controller') 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) engine_dep = declare_dependency(link_with: [engine, controller], include_directories: incdirs)

View file

@ -21,7 +21,7 @@ void Bird::draw() {
void Bird::update() { void Bird::update() {
if (pipes != nullptr) { 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 >= if (getGlobalPosition().x >=
pipes->children[i]->getGlobalPosition().x - 0.1f && pipes->children[i]->getGlobalPosition().x - 0.1f &&
getGlobalPosition().x <= getGlobalPosition().x <=

View file

@ -1,5 +1,7 @@
#include "FlappyBird.hh" #include "FlappyBird.hh"
#include <chrono>
#include <mutex>
#include <thread> #include <thread>
#include "MainMenu.hh" #include "MainMenu.hh"
@ -10,19 +12,21 @@ void FlappyBird::gameLoop() {
return; return;
} }
if (!thread) {
thread = new std::thread(&FlappyBird::spawnPipes, this);
}
mainGameLoop(); mainGameLoop();
} }
void FlappyBird::spawnPipes() { void FlappyBird::spawnPipes() {
threadRunning = true; threadRunning = true;
while (threadRunning) { while (threadRunning) {
addNewPipe = true; doneSpawningPipes = false;
while (addNewPipe) { pipesContainer->children.push_back(
if (!threadRunning) { Pipes::CreatePipes(this, pipesContainer));
return; doneSpawningPipes = true;
} cv.notify_one();
std::this_thread::yield();
}
std::this_thread::sleep_for(std::chrono::seconds(2)); std::this_thread::sleep_for(std::chrono::seconds(2));
} }
} }
@ -34,21 +38,18 @@ void FlappyBird::mainGameLoop() {
SwapBuffers(); SwapBuffers();
return; return;
} }
if (!thread) {
thread = new std::thread(&FlappyBird::spawnPipes, this);
}
for (size_t i = 0; i < pipesContainer->children.size(); i++) { for (size_t i = 0; i < pipesContainer->children.size(); i++) {
if (pipesContainer->children[i]->destroyed) { if (pipesContainer->children[i]->destroyed) {
// delete pipesContainer->children[i]; delete pipesContainer->children[i];
pipesContainer->children.erase(pipesContainer->children.begin() + pipesContainer->children.erase(pipesContainer->children.begin() +
i); i);
} }
} }
if (addNewPipe) {
pipesContainer->children.push_back( std::unique_lock lock(mutex);
Pipes::CreatePipes(this, pipesContainer)); cv.wait(lock, [&]{return doneSpawningPipes;});
addNewPipe = false; rendering = true;
}
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
controller->PollController(); controller->PollController();
Entity::updateEntity(this->bird); Entity::updateEntity(this->bird);
@ -64,7 +65,10 @@ void FlappyBird::mainMenuLoop() {
thread->detach(); thread->detach();
delete thread; delete thread;
thread = nullptr; thread = nullptr;
pipesContainer->DestroyChildren();
pipesContainer->children.clear(); pipesContainer->children.clear();
SwapBuffers();
return;
} }
controller->PollController(); controller->PollController();
Entity::updateEntity(this->mainMenu); Entity::updateEntity(this->mainMenu);

View file

@ -1,5 +1,7 @@
#ifndef FLAPPY_BIRD_HH #ifndef FLAPPY_BIRD_HH
#define FLAPPY_BIRD_HH #define FLAPPY_BIRD_HH
#include <condition_variable>
#include <mutex>
#include <thread> #include <thread>
#include <engine/engine.hh> #include <engine/engine.hh>
#include <engine/entity.hh> #include <engine/entity.hh>
@ -19,7 +21,10 @@ class FlappyBird : public Engine {
MainMenu* mainMenu; MainMenu* mainMenu;
std::thread* thread = nullptr; std::thread* thread = nullptr;
bool threadRunning; bool threadRunning;
bool addNewPipe; bool doneSpawningPipes = true;
bool rendering = false;
std::mutex mutex;
std::condition_variable cv;
FlappyBird(); FlappyBird();
~FlappyBird(); ~FlappyBird();
}; };