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

View file

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

View file

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

View file

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

View file

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

View file

@ -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);
}

View file

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

View file

@ -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);
};

View file

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

View file

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

View file

@ -1,5 +1,7 @@
#include "FlappyBird.hh"
#include <chrono>
#include <mutex>
#include <thread>
#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);

View file

@ -1,5 +1,7 @@
#ifndef FLAPPY_BIRD_HH
#define FLAPPY_BIRD_HH
#include <condition_variable>
#include <mutex>
#include <thread>
#include <engine/engine.hh>
#include <engine/entity.hh>
@ -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();
};