From aaa6c15a9acfe39be1fe636d90bcccf062160127 Mon Sep 17 00:00:00 2001 From: Fries Date: Mon, 1 Apr 2024 17:28:55 -0700 Subject: [PATCH] Move engine stuff to its own namespace. --- .clang-format | 5 +- .clang-tidy | 6 + src/cube.cc | 4 +- src/cube/Cube.cc | 12 +- src/cube/Cube.hh | 10 +- src/cube/CubeEntity.cc | 28 ++--- src/cube/CubeEntity.hh | 10 +- src/engine/controller/controller.hh | 8 +- src/engine/controller/desktopController.cc | 52 ++++---- src/engine/controller/desktopController.hh | 5 +- src/engine/controller/dreamcastController.cc | 16 +-- src/engine/controller/dreamcastController.hh | 8 +- src/engine/desktopEngine.cc | 35 +++--- src/engine/desktopEngine.hh | 8 +- src/engine/dreamcastEngine.cc | 21 ++-- src/engine/dreamcastEngine.hh | 7 +- src/engine/engine.cc | 84 +++++++------ src/engine/engine.hh | 69 +++++------ src/engine/entity.cc | 73 +++++++----- src/engine/entity.hh | 49 ++++---- src/engine/nativeEngine.hh | 14 ++- src/flappyBird.cc | 6 +- src/flappyBird/Bird.cc | 49 ++++---- src/flappyBird/Bird.hh | 12 +- src/flappyBird/FlappyBird.cc | 118 ++++++++++--------- src/flappyBird/FlappyBird.hh | 24 ++-- src/flappyBird/MainMenu.cc | 12 +- src/flappyBird/MainMenu.hh | 10 +- src/flappyBird/Pipes.cc | 52 ++++---- src/flappyBird/Pipes.hh | 24 ++-- src/gl.cc | 6 +- src/gl/Gl.cc | 10 +- src/gl/Gl.hh | 6 +- src/gl/GlEntity.cc | 30 ++--- src/gl/GlEntity.hh | 14 +-- src/hello.cc | 4 +- src/hello/Hello.cc | 6 +- src/hello/Hello.hh | 10 +- src/hello/HelloEntity.cc | 4 +- src/hello/HelloEntity.hh | 6 +- 40 files changed, 501 insertions(+), 426 deletions(-) create mode 100644 .clang-tidy diff --git a/.clang-format b/.clang-format index d479040..9be652c 100644 --- a/.clang-format +++ b/.clang-format @@ -1,4 +1,5 @@ -BasedOnStyle: Google -UseTab: Always +--- +BasedOnStyle: WebKit IndentWidth: 4 TabWidth: 4 +UseTab: Always diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..a395f2b --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,6 @@ +--- +Checks: 'clang-diagnostic-*,clang-analyzer-*,readability-identifier-naming' +CheckOptions: + readability-identifier-naming.ClassCase: CamelCase + readability-identifier-naming.FunctionCase: CamelCase + readability-identifier-naming.MemberHungarianPrefix: LowerCase diff --git a/src/cube.cc b/src/cube.cc index 497f0ce..5672967 100644 --- a/src/cube.cc +++ b/src/cube.cc @@ -2,6 +2,6 @@ int main() { Cube engine; - engine.initializeEngine(); - engine.initializeGameLoop(); + engine.InitializeEngine(); + engine.InitializeGameLoop(); } diff --git a/src/cube/Cube.cc b/src/cube/Cube.cc index 7c67bfa..3dd5dbf 100644 --- a/src/cube/Cube.cc +++ b/src/cube/Cube.cc @@ -15,10 +15,10 @@ #endif Cube::Cube() { - entity = new CubeEntity(this, nullptr); + p_entity = new CubeEntity(this, nullptr); } -void Cube::initScreen() { +void Cube::InitScreen() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, 640.0f / 480.0f, 0.1f, 100.0f); @@ -29,8 +29,8 @@ void Cube::initScreen() { glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); + glGenTextures(1, &ui_texture); + glBindTexture(GL_TEXTURE_2D, ui_texture); int width, height, nr_channels; @@ -50,11 +50,11 @@ void Cube::initScreen() { stbi_image_free(data); } -void Cube::gameLoop() { +void Cube::GameLoop() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -5.0f); glRotatef(90, 0.0f, 1.0f, 0.5f); - Entity::drawEntity(entity); + engine::DrawEntity(p_entity); SwapBuffers(); } diff --git a/src/cube/Cube.hh b/src/cube/Cube.hh index 1aceede..f58e018 100644 --- a/src/cube/Cube.hh +++ b/src/cube/Cube.hh @@ -2,12 +2,12 @@ #define CUBE_HH #include #include "CubeEntity.hh" -class Cube : public Engine { +class Cube : public engine::Engine { public: - CubeEntity* entity; - void gameLoop() override; - void initScreen() override; + CubeEntity* p_entity; + void GameLoop() override; + void InitScreen() override; Cube(); - unsigned int texture; + unsigned int ui_texture; }; #endif diff --git a/src/cube/CubeEntity.cc b/src/cube/CubeEntity.cc index 651c8b1..d4a118e 100644 --- a/src/cube/CubeEntity.cc +++ b/src/cube/CubeEntity.cc @@ -12,26 +12,26 @@ #endif -CubeEntity::CubeEntity(Engine* engine, Entity* parent) : Entity(engine, parent) { +CubeEntity::CubeEntity(engine::Engine* engine, Entity* parent) : Entity(engine, parent) { char* message = new char; - if (!parseObj(CUBE_LOCATION, reader, message)) + if (!ParseObj(CUBE_LOCATION, reader, message)) throw std::runtime_error(message); } -void CubeEntity::draw() { +void CubeEntity::Draw() { const tinyobj::attrib_t& attrib = reader.GetAttrib(); auto& shapes = reader.GetShapes(); - angle += 1.0f; - glRotatef(angle, 1.0f, 0.0f, 1.0f); + f_angle += 1.0f; + glRotatef(f_angle, 1.0f, 0.0f, 1.0f); int drawCalls = 0; for (size_t s = 0; s < shapes.size(); s++) { // Loop over faces(polygon) size_t index_offset = 0; - std::vector vertexes{}; - std::vector normals{}; - std::vector textureCoords{}; + std::vector vertexes{}; + std::vector normals{}; + std::vector textureCoords{}; for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) { size_t fv = size_t(shapes[s].mesh.num_face_vertices[f]); @@ -47,7 +47,7 @@ void CubeEntity::draw() { attrib.vertices[3 * size_t(idx.vertex_index) + 1]; tinyobj::real_t vz = attrib.vertices[3 * size_t(idx.vertex_index) + 2]; - Engine::Vector3 vertex = {vx, vy, vz}; + engine::Vector3 vertex = {vx, vy, vz}; vertexes.push_back(vertex); // glVertex3f(vx, vy, vz); // drawCalls += 1; @@ -61,12 +61,12 @@ void CubeEntity::draw() { attrib.normals[3 * size_t(idx.normal_index) + 1]; tinyobj::real_t nz = attrib.normals[3 * size_t(idx.normal_index) + 2]; - Engine::Vector3 normal = {nx, ny, nz}; + engine::Vector3 normal = {nx, ny, nz}; normals.push_back(normal); // glNormal3f(nx, ny, nz); // drawCalls += 1; } else { - normals.push_back(Engine::Vector3::zero); + normals.push_back(engine::Vector3::zero); } // Check if `texcoord_index` is zero or positive. negative = no @@ -76,12 +76,12 @@ void CubeEntity::draw() { attrib.texcoords[2 * size_t(idx.texcoord_index) + 0]; tinyobj::real_t ty = attrib.texcoords[2 * size_t(idx.texcoord_index) + 1]; - Engine::Vector3 textureCoord = {tx, ty, 0}; + engine::Vector3 textureCoord = {tx, ty, 0}; textureCoords.push_back(textureCoord); // glTexCoord2f(tx, ty); // drawCalls += 1; } else { - textureCoords.push_back(Engine::Vector3::zero); + textureCoords.push_back(engine::Vector3::zero); } } index_offset += fv; @@ -105,7 +105,7 @@ void CubeEntity::draw() { // glDrawArrays(GL_QUADS, 0, susSize); } -bool CubeEntity::parseObj(std::string objFile, tinyobj::ObjReader& reader, +bool CubeEntity::ParseObj(std::string objFile, tinyobj::ObjReader& reader, char* message) { tinyobj::ObjReaderConfig readerConfig; readerConfig.mtl_search_path = "./"; diff --git a/src/cube/CubeEntity.hh b/src/cube/CubeEntity.hh index 13f1c0b..036f8c1 100644 --- a/src/cube/CubeEntity.hh +++ b/src/cube/CubeEntity.hh @@ -4,13 +4,13 @@ #include -class CubeEntity : public Entity { - void draw() override; +class CubeEntity : public engine::Entity { + void Draw() override; tinyobj::ObjReader reader; - bool parseObj(std::string objFile, tinyobj::ObjReader& reader, + bool ParseObj(std::string objFile, tinyobj::ObjReader& reader, char* message); - float angle; + float f_angle; public: - CubeEntity(Engine* engine, Entity* parent); + CubeEntity(engine::Engine* engine, Entity* parent); }; #endif diff --git a/src/engine/controller/controller.hh b/src/engine/controller/controller.hh index f6a1f3e..1145921 100644 --- a/src/engine/controller/controller.hh +++ b/src/engine/controller/controller.hh @@ -1,7 +1,8 @@ #ifndef CONTROLLER_HH #define CONTROLLER_HH +namespace engine { class Controller { - public: +public: enum Button { A = (1 << 0), B = (1 << 1), @@ -13,13 +14,14 @@ class Controller { DPAD_RIGHT = (1 << 7) }; virtual bool InitializeController() { return false; }; - virtual void PollController(){}; + virtual void PollController() {}; 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: +protected: virtual int GetButtonMask(Button button) const { return 0; } }; +} #endif diff --git a/src/engine/controller/desktopController.cc b/src/engine/controller/desktopController.cc index 2f5fdce..e06f350 100644 --- a/src/engine/controller/desktopController.cc +++ b/src/engine/controller/desktopController.cc @@ -4,7 +4,9 @@ #include #include -bool DesktopController::InitializeController() { +namespace engine { +bool DesktopController::InitializeController() +{ std::ifstream gameControllerDB; gameControllerDB.open("./gamecontrollerdb.txt"); if (gameControllerDB.is_open()) { @@ -20,39 +22,45 @@ bool DesktopController::InitializeController() { return false; } -void DesktopController::PollController() { +void DesktopController::PollController() +{ glfwGetGamepadState(GLFW_JOYSTICK_1, &controllerState); } -bool DesktopController::IsButtonPressed(Button button) const { +bool DesktopController::IsButtonPressed(Button button) const +{ return controllerState.buttons[GetButtonMask(button)]; } -float DesktopController::GetLeftJoystickXAxis() const { +float DesktopController::GetLeftJoystickXAxis() const +{ return controllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_X]; } -float DesktopController::GetLeftJoystickYAxis() const { +float DesktopController::GetLeftJoystickYAxis() const +{ return controllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_Y]; } -int DesktopController::GetButtonMask(Button button) const { +int DesktopController::GetButtonMask(Button button) const +{ switch (button) { - case Button::A: - return GLFW_GAMEPAD_BUTTON_A; - case Button::B: - return GLFW_GAMEPAD_BUTTON_B; - case Button::X: - return GLFW_GAMEPAD_BUTTON_X; - case Button::Y: - return GLFW_GAMEPAD_BUTTON_Y; - case Button::DPAD_UP: - return GLFW_GAMEPAD_BUTTON_DPAD_UP; - case Button::DPAD_DOWN: - return GLFW_GAMEPAD_BUTTON_DPAD_DOWN; - case Button::DPAD_LEFT: - return GLFW_GAMEPAD_BUTTON_DPAD_LEFT; - case Button::DPAD_RIGHT: - return GLFW_GAMEPAD_BUTTON_DPAD_RIGHT; + case Button::A: + return GLFW_GAMEPAD_BUTTON_A; + case Button::B: + return GLFW_GAMEPAD_BUTTON_B; + case Button::X: + return GLFW_GAMEPAD_BUTTON_X; + case Button::Y: + return GLFW_GAMEPAD_BUTTON_Y; + case Button::DPAD_UP: + return GLFW_GAMEPAD_BUTTON_DPAD_UP; + case Button::DPAD_DOWN: + return GLFW_GAMEPAD_BUTTON_DPAD_DOWN; + case Button::DPAD_LEFT: + return GLFW_GAMEPAD_BUTTON_DPAD_LEFT; + case Button::DPAD_RIGHT: + return GLFW_GAMEPAD_BUTTON_DPAD_RIGHT; } } +} diff --git a/src/engine/controller/desktopController.hh b/src/engine/controller/desktopController.hh index 8ecc6ea..ae48faf 100644 --- a/src/engine/controller/desktopController.hh +++ b/src/engine/controller/desktopController.hh @@ -3,8 +3,9 @@ #include "controller.hh" #include +namespace engine { class DesktopController : public Controller { - public: +public: bool InitializeController() override; void PollController() override; bool IsButtonPressed(Button button) const override; @@ -13,5 +14,5 @@ class DesktopController : public Controller { int GetButtonMask(Button button) const override; GLFWgamepadstate controllerState; }; - +} #endif diff --git a/src/engine/controller/dreamcastController.cc b/src/engine/controller/dreamcastController.cc index b41da2d..9bdf011 100644 --- a/src/engine/controller/dreamcastController.cc +++ b/src/engine/controller/dreamcastController.cc @@ -1,35 +1,36 @@ #include "dreamcastController.hh" #include "controller.hh" +namespace engine { DreamcastController::DreamcastController() { } bool DreamcastController::InitializeController() { maple_device_t* cont = maple_enum_type(0, MAPLE_FUNC_CONTROLLER); if (cont->valid) { - this->controller = cont; - this->controller_state = (cont_state_t*)maple_dev_status(controller); + this->p_controller = cont; + this->p_controller_state = (cont_state_t*)maple_dev_status(p_controller); return true; } else { - controller = nullptr; + p_controller = nullptr; return false; } } void DreamcastController::PollController() { - controller_state = (cont_state_t*)maple_dev_status(controller); + p_controller_state = (cont_state_t*)maple_dev_status(p_controller); } bool DreamcastController::IsButtonPressed(Button button) const { - return controller_state->buttons & GetButtonMask(button); + return p_controller_state->buttons & GetButtonMask(button); } float DreamcastController::GetLeftJoystickXAxis() const { - return (controller_state->joyx / 127.0f); + return (p_controller_state->joyx / 127.0f); } float DreamcastController::GetLeftJoystickYAxis() const { - return (controller_state->joyy / 127.0f); + return (p_controller_state->joyy / 127.0f); } int DreamcastController::GetButtonMask(Button button) const { @@ -53,3 +54,4 @@ int DreamcastController::GetButtonMask(Button button) const { } return 0; } +} diff --git a/src/engine/controller/dreamcastController.hh b/src/engine/controller/dreamcastController.hh index 1e02498..052c1ee 100644 --- a/src/engine/controller/dreamcastController.hh +++ b/src/engine/controller/dreamcastController.hh @@ -3,8 +3,9 @@ #include #include "controller.hh" +namespace engine { class DreamcastController : public Controller { - public: +public: DreamcastController(); bool InitializeController() override; void PollController() override; @@ -12,7 +13,8 @@ class DreamcastController : public Controller { 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; + maple_device_t* p_controller = nullptr; + cont_state_t* p_controller_state = nullptr; }; +} #endif diff --git a/src/engine/desktopEngine.cc b/src/engine/desktopEngine.cc index dfdb130..7e1b2ee 100644 --- a/src/engine/desktopEngine.cc +++ b/src/engine/desktopEngine.cc @@ -7,49 +7,56 @@ #include "controller/desktopController.hh" -void DesktopEngine::initializeEngine() { +namespace engine { +void DesktopEngine::InitializeEngine() +{ glfwInit(); // Set OpenGL version to 1.2. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // Create a GLFW window. - window = glfwCreateWindow(640, 480, "DC Engine", nullptr, nullptr); + p_window = glfwCreateWindow(640, 480, "DC Engine", nullptr, nullptr); // Fail if the window has failed to create. - if (window == nullptr) { + if (p_window == nullptr) { const char* description; glfwGetError(&description); std::cout << "Failed to create GLFW window.\n" << description << std::endl; } - glfwMakeContextCurrent(window); + glfwMakeContextCurrent(p_window); // printSystemInformation(); } -void DesktopEngine::initializeController() { +void DesktopEngine::InitializeController() +{ DesktopController* linuxCont = new DesktopController; if (linuxCont->InitializeController()) { - controller = linuxCont; + p_controller = linuxCont; } else { throw std::runtime_error("No controller detected"); } } -void DesktopEngine::SwapBuffers() { - glfwSwapBuffers(window); +void DesktopEngine::SwapBuffers() +{ + glfwSwapBuffers(p_window); glfwPollEvents(); } -bool DesktopEngine::ShouldWindowClose() { - return glfwWindowShouldClose(this->window); +bool DesktopEngine::ShouldWindowClose() +{ + return glfwWindowShouldClose(this->p_window); } -DesktopEngine::~DesktopEngine() { - delete controller; - glfwDestroyWindow(window); +DesktopEngine::~DesktopEngine() +{ + delete p_controller; + glfwDestroyWindow(p_window); glfwTerminate(); - window = nullptr; + p_window = nullptr; +} } #endif diff --git a/src/engine/desktopEngine.hh b/src/engine/desktopEngine.hh index 53ebf20..4fb326f 100644 --- a/src/engine/desktopEngine.hh +++ b/src/engine/desktopEngine.hh @@ -3,12 +3,14 @@ #include "nativeEngine.hh" #include +namespace engine { class DesktopEngine : public NativeEngine { - void initializeController() override; - void initializeEngine() override; + void InitializeController() override; + void InitializeEngine() override; void SwapBuffers() override; bool ShouldWindowClose() override; ~DesktopEngine() override; - GLFWwindow* window = nullptr; + GLFWwindow* p_window = nullptr; }; +} #endif diff --git a/src/engine/dreamcastEngine.cc b/src/engine/dreamcastEngine.cc index ca0907f..f89b17e 100644 --- a/src/engine/dreamcastEngine.cc +++ b/src/engine/dreamcastEngine.cc @@ -1,28 +1,33 @@ #ifdef _arch_dreamcast #include "dreamcastEngine.hh" -#include -#include #include "controller/dreamcastController.hh" - -void DreamcastEngine::initializeEngine() { +#include +#include +namespace engine { +void DreamcastEngine::InitializeEngine() +{ glKosInit(); // printSystemInformation(); } -void DreamcastEngine::initializeController() { +void DreamcastEngine::InitializeController() +{ DreamcastController* dreamCont = new DreamcastController; if (dreamCont->InitializeController()) { - controller = dreamCont; + p_controller = dreamCont; } else { throw std::runtime_error("Can't initialize the controller."); } } -void DreamcastEngine::SwapBuffers() { +void DreamcastEngine::SwapBuffers() +{ glKosSwapBuffers(); } -bool DreamcastEngine::ShouldWindowClose() { +bool DreamcastEngine::ShouldWindowClose() +{ return false; } +} #endif diff --git a/src/engine/dreamcastEngine.hh b/src/engine/dreamcastEngine.hh index cec8151..dbfb817 100644 --- a/src/engine/dreamcastEngine.hh +++ b/src/engine/dreamcastEngine.hh @@ -1,11 +1,12 @@ #ifndef DREAMCAST_ENGINE_HH #define DREAMCAST_ENGINE_HH #include "nativeEngine.hh" - +namespace engine { class DreamcastEngine : public NativeEngine { - void initializeController() override; - void initializeEngine() override; + void InitializeController() override; + void InitializeEngine() override; void SwapBuffers() override; bool ShouldWindowClose() override; }; +} #endif diff --git a/src/engine/engine.cc b/src/engine/engine.cc index fdbf94f..f94eb84 100644 --- a/src/engine/engine.cc +++ b/src/engine/engine.cc @@ -14,27 +14,31 @@ #include #include -void Engine::initializeEngine() { +namespace engine { +void Engine::InitializeEngine() +{ #ifdef _arch_dreamcast - engine = new DreamcastEngine; + p_engine = new DreamcastEngine; #else - engine = new DesktopEngine; + p_engine = new DesktopEngine; #endif - engine->initializeEngine(); - initScreen(); + p_engine->InitializeEngine(); + InitScreen(); } -void Engine::initializeController() { - engine->initializeController(); - controller = engine->controller; +void Engine::InitializeController() +{ + p_engine->InitializeController(); + p_controller = p_engine->p_controller; } -void Engine::SwapBuffers() const { engine->SwapBuffers(); } +void Engine::SwapBuffers() const { p_engine->SwapBuffers(); } -bool Engine::ShouldWindowClose() const { return engine->ShouldWindowClose(); } +bool Engine::ShouldWindowClose() const { return p_engine->ShouldWindowClose(); } -Engine::~Engine() { delete engine; } +Engine::~Engine() { delete p_engine; } -void Engine::initScreen() { +void Engine::InitScreen() +{ glMatrixMode(GL_PROJECTION); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glViewport(0, 0, 640, 480); @@ -45,7 +49,8 @@ void Engine::initScreen() { // glLoadIdentity(); } -void Engine::printSystemInformation() { +void Engine::PrintSystemInformation() +{ printf("GL_VENDOR: %s\n", glGetString(GL_VENDOR)); printf("GL_RENDERER: %s\n", glGetString(GL_RENDERER)); printf("GL_VERSION: %s\n", glGetString(GL_VERSION)); @@ -53,54 +58,59 @@ void Engine::printSystemInformation() { } template -void Engine::deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame, - struct timeval& endOfFrame) { +void Engine::DeltaTimeLoop(T&& callback, struct timeval& beginningOfFrame, + struct timeval& endOfFrame) +{ gettimeofday(&beginningOfFrame, 0); (this->*callback)(); gettimeofday(&endOfFrame, 0); - float time = ((float)(endOfFrame.tv_usec) * microSecond) - - ((float)(beginningOfFrame.tv_usec) * microSecond); + float time = ((float)(endOfFrame.tv_usec) * f_microSecond) - ((float)(beginningOfFrame.tv_usec) * f_microSecond); if (time > 0.0f) { - deltaTime = time; + f_deltaTime = time; } } -void Engine::initializeGameLoop() { +void Engine::InitializeGameLoop() +{ struct timeval beginningOfFrame, endOfFrame; - start(); + Start(); while (!ShouldWindowClose()) { - deltaTimeLoop(&Engine::gameLoop, beginningOfFrame, endOfFrame); + DeltaTimeLoop(&Engine::GameLoop, beginningOfFrame, endOfFrame); } } -void Engine::pressButton(Controller::Button button, - std::function callback) { - if (controller->IsButtonPressed(button)) { - if (!(buttonsPressed & button)) { - buttonsPressed |= button; +void Engine::PressButton(Controller::Button button, + std::function callback) +{ + if (p_controller->IsButtonPressed(button)) { + if (!(ui_buttonsPressed & button)) { + ui_buttonsPressed |= button; callback(); } - } else if ((buttonsPressed & button) && - !controller->IsButtonPressed(button)) { - buttonsPressed &= ~button; + } else if ((ui_buttonsPressed & button) && !p_controller->IsButtonPressed(button)) { + ui_buttonsPressed &= ~button; } } -constexpr Engine::Vector3 Engine::Vector3::zero = {0, 0, 0}; +float Engine::GetDeltaTime() const { return f_deltaTime; } -Engine::Vector3 Engine::Vector3::operator/(float amount) const { - return {this->x / amount, this->y / amount, this->z / amount}; +constexpr Vector3 Vector3::zero = { 0, 0, 0 }; + +Vector3 Vector3::operator/(float amount) const +{ + return { this->x / amount, this->y / amount, this->z / amount }; } -void Engine::Vector3::operator/=(float amount) { +void Vector3::operator/=(float amount) +{ Vector3 vec = *this / amount; std::memcpy(this, &vec, sizeof(vec)); } -float Engine::getDeltaTime() const { return deltaTime; } - -Engine::Vector3 Engine::Vector3::operator+(Engine::Vector3 vector) const { - return {this->x + vector.x, this->y + vector.y, this->z + vector.z}; +Vector3 Vector3::operator+(Vector3 vector) const +{ + return { this->x + vector.x, this->y + vector.y, this->z + vector.z }; } +}; diff --git a/src/engine/engine.hh b/src/engine/engine.hh index 4ccc4d8..5697df3 100644 --- a/src/engine/engine.hh +++ b/src/engine/engine.hh @@ -14,49 +14,52 @@ #include #endif +namespace engine { class Engine { - protected: - static constexpr float microSecond = 0.000001f; +protected: + static constexpr float f_microSecond = 0.000001f; - float angle = 0; - float deltaTime = 1.0 / 60.0; - float speed = 5.0f; + float f_angle = 0; + float f_deltaTime = 1.0 / 60.0; + float f_speed = 5.0f; - bool thirtyfps; - unsigned int buttonsPressed; + bool b_thirtyfps; + unsigned int ui_buttonsPressed; - - virtual void initScreen(); - virtual void gameLoop(){}; - virtual void start() {}; - void printSystemInformation(); + virtual void InitScreen(); + virtual void GameLoop() {}; + virtual void Start() {}; + void PrintSystemInformation(); void SwapBuffers() const; bool ShouldWindowClose() const; - private: - NativeEngine* engine = nullptr; +private: + NativeEngine* p_engine = nullptr; template - void deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame, - struct timeval& endOfFrame); + void DeltaTimeLoop(T&& callback, struct timeval& beginningOfFrame, + struct timeval& endOfFrame); - public: - void initializeController(); - void initializeEngine(); - void initializeGameLoop(); - void cleanupEngine(); - float getDeltaTime() const; - void pressButton(Controller::Button button, std::function callback); +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 { - Vector3 operator/(float amount) const; - void operator/=(float amount); - Vector3 operator+(Vector3 vector) const; - float x; - float y; - float z; - static const Vector3 zero; - }; + Controller* p_controller = nullptr; +}; + +struct Vector3 { +public: + Vector3 operator/(float amount) const; + void operator/=(float amount); + Vector3 operator+(Vector3 vector) const; + float x; + float y; + float z; + static const Vector3 zero; +}; }; #endif diff --git a/src/engine/entity.cc b/src/engine/entity.cc index df7020c..540b982 100644 --- a/src/engine/entity.cc +++ b/src/engine/entity.cc @@ -1,81 +1,94 @@ #include "entity.hh" #include -void Entity::DestroyChildren() { +namespace engine { +void Entity::DestroyChildren() +{ for (Entity* childEntity : children) { delete childEntity; } } -Entity::~Entity() { +Entity::~Entity() +{ DestroyChildren(); } -void Entity::drawEntity(Entity* entity) { +void 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(); + Vector3 position = entity->GetPosition(); + Vector3 scale = entity->GetScale(); + glTranslatef(position.x, position.y, position.z); + glScalef(scale.x, scale.y, scale.z); + entity->Draw(); glPopMatrix(); if (entity->children.size() > 0) { - drawChildrenEntity(entity); + DrawChildrenEntity(entity); } } -void Entity::updateEntity(Entity* entity) { - entity->update(); +void UpdateEntity(Entity* entity) +{ + entity->Update(); if (entity->children.size() > 0) { - updateChildrenEntity(entity); + UpdateChildrenEntity(entity); } } -void Entity::drawChildrenEntity(Entity* entity) { +void DrawChildrenEntity(Entity* entity) +{ for (Entity* childEntity : entity->children) { glPushMatrix(); - if (childEntity->parent == nullptr) { + if (childEntity->p_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); + Vector3 addedPosition = childEntity->p_parent->GetPosition() + childEntity->GetPosition(); + glTranslatef(addedPosition.x, addedPosition.y, addedPosition.z); } - glScalef(childEntity->scale.x, childEntity->scale.y, - childEntity->scale.z); - childEntity->draw(); + Vector3 scale = childEntity->GetScale(); + glScalef(scale.x, scale.y, scale.z); + childEntity->Draw(); glPopMatrix(); - drawChildrenEntity(childEntity); + DrawChildrenEntity(childEntity); } } -void Entity::updateChildrenEntity(Entity* entity) { +void UpdateChildrenEntity(Entity* entity) +{ for (Entity* childEntity : entity->children) { - childEntity->update(); - updateChildrenEntity(childEntity); + childEntity->Update(); + UpdateChildrenEntity(childEntity); } } -Engine::Vector3 Entity::getPosition() const { +Vector3 Entity::GetPosition() const +{ return position; } -Engine::Vector3 Entity::getGlobalPosition() const { +Vector3 Entity::GetGlobalPosition() const +{ return globalPosition; } -void Entity::updatePosition(Engine::Vector3 position) { +void Entity::UpdatePosition(Vector3 position) +{ this->position = position; - if (this->parent != nullptr) { - globalPosition = this->position + this->parent->globalPosition; + if (this->p_parent != nullptr) { + globalPosition = this->position + this->p_parent->globalPosition; } else { globalPosition = position; } } -Engine::Vector3 Entity::getScale() const { +Vector3 Entity::GetScale() const +{ return scale; } -void Entity::updateScale(Engine::Vector3 scale) { +void Entity::UpdateScale(Vector3 scale) +{ this->scale = scale; } +} diff --git a/src/engine/entity.hh b/src/engine/entity.hh index 800fd57..ea76627 100644 --- a/src/engine/entity.hh +++ b/src/engine/entity.hh @@ -1,34 +1,37 @@ #ifndef ENTITY_HH #define ENTITY_HH #include "engine.hh" +namespace engine { class Entity { - public: - Entity(Engine* engie, Entity* parent) { - engine = engie; - this->parent = parent; - updatePosition(Engine::Vector3::zero); +public: + Entity(Engine* engie, Entity* parent) + { + p_engine = engie; + this->p_parent = parent; + UpdatePosition(Vector3::zero); }; virtual ~Entity(); - Engine* engine; - Entity* parent = nullptr; + Engine* p_engine; + Entity* p_parent = nullptr; std::vector children = {}; - virtual void draw(){}; - virtual void update(){}; - bool destroyed = false; - static void drawEntity(Entity* entity); - static void updateEntity(Entity* entity); - Engine::Vector3 getPosition() const; - Engine::Vector3 getGlobalPosition() const; - void updatePosition(Engine::Vector3 position); - Engine::Vector3 getScale() const; - void updateScale(Engine::Vector3 scale); + virtual void Draw() {}; + virtual void Update() {}; + bool b_destroyed = false; + Vector3 GetPosition() const; + Vector3 GetGlobalPosition() const; + void UpdatePosition(Vector3 position); + Vector3 GetScale() const; + void UpdateScale(Vector3 scale); void DestroyChildren(); - private: - Engine::Vector3 position = Engine::Vector3::zero; - Engine::Vector3 globalPosition = Engine::Vector3::zero; - Engine::Vector3 scale = {1, 1, 1}; - static void drawChildrenEntity(Entity* entity); - static void updateChildrenEntity(Entity* entity); +private: + Vector3 position = Vector3::zero; + Vector3 globalPosition = Vector3::zero; + Vector3 scale = { 1, 1, 1 }; +}; +void DrawEntity(Entity* entity); +void UpdateEntity(Entity* entity); +void DrawChildrenEntity(Entity* entity); +void UpdateChildrenEntity(Entity* entity); }; #endif diff --git a/src/engine/nativeEngine.hh b/src/engine/nativeEngine.hh index 7ceb789..46688a2 100644 --- a/src/engine/nativeEngine.hh +++ b/src/engine/nativeEngine.hh @@ -1,13 +1,15 @@ #ifndef NATIVE_ENGINE_HH #define NATIVE_ENGINE_HH #include "controller/controller.hh" +namespace engine { class NativeEngine { - public: - virtual void initializeController(){}; - virtual void initializeEngine(){}; - virtual void SwapBuffers(){}; - virtual bool ShouldWindowClose(){return false;}; +public: + virtual void InitializeController() {}; + virtual void InitializeEngine() {}; + virtual void SwapBuffers() {}; + virtual bool ShouldWindowClose() { return false; }; virtual ~NativeEngine() {}; - Controller* controller = nullptr; + Controller* p_controller = nullptr; }; +} #endif diff --git a/src/flappyBird.cc b/src/flappyBird.cc index 644cd76..e62882f 100644 --- a/src/flappyBird.cc +++ b/src/flappyBird.cc @@ -2,7 +2,7 @@ int main() { FlappyBird engine; - engine.initializeEngine(); - engine.initializeController(); - engine.initializeGameLoop(); + engine.InitializeEngine(); + engine.InitializeController(); + engine.InitializeGameLoop(); } diff --git a/src/flappyBird/Bird.cc b/src/flappyBird/Bird.cc index 1197e80..f282bb8 100644 --- a/src/flappyBird/Bird.cc +++ b/src/flappyBird/Bird.cc @@ -2,10 +2,10 @@ #include -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}; +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); @@ -19,48 +19,47 @@ void Bird::draw() { glEnd(); } -void Bird::update() { +void Bird::Update() { if (pipes != nullptr) { for (size_t i = 0; i < pipes->children.size(); i++) { - if (getGlobalPosition().x >= - pipes->children[i]->getGlobalPosition().x - 0.1f && - getGlobalPosition().x <= - pipes->children[i]->getGlobalPosition().x + 0.15f) { + if (GetGlobalPosition().x >= + pipes->children[i]->GetGlobalPosition().x - 0.1f && + GetGlobalPosition().x <= + pipes->children[i]->GetGlobalPosition().x + 0.15f) { Pipes* pipe = (Pipes*)pipes->children[i]; - float topPipeBottomPosition = 1 - ((Pipe*)(pipe->children[0]))->pipeLength; - float bottomPipeTopPosition = -1 + ((Pipe*)(pipe->children[1]))->pipeLength; - bool betweenPipes = getGlobalPosition().y < topPipeBottomPosition && getGlobalPosition().y > bottomPipeTopPosition; + float topPipeBottomPosition = 1 - ((Pipe*)(pipe->children[0]))->f_pipeLength; + float bottomPipeTopPosition = -1 + ((Pipe*)(pipe->children[1]))->f_pipeLength; + bool betweenPipes = GetGlobalPosition().y < topPipeBottomPosition && GetGlobalPosition().y > bottomPipeTopPosition; if(betweenPipes) { printf("between pipes\n"); } else { printf("colliding: %f\n", - pipes->children[i]->getGlobalPosition().x); - isHit = true; + pipes->children[i]->GetGlobalPosition().x); + b_isHit = true; } - } } } - float deltaTime = engine->getDeltaTime(); - engine->pressButton(Controller::A, [&]() { - if (verticalSpeed < 0) { - verticalSpeed = JUMP_CONST; + float deltaTime = p_engine->GetDeltaTime(); + p_engine->PressButton(engine::Controller::A, [&]() { + if (f_verticalSpeed < 0) { + f_verticalSpeed = JUMP_CONST; return; } - verticalSpeed = std::clamp(verticalSpeed + JUMP_CONST, 0.0f, 1.0f); + f_verticalSpeed = std::clamp(f_verticalSpeed + JUMP_CONST, 0.0f, 1.0f); }); - Engine::Vector3 pos = getPosition(); + engine::Vector3 pos = GetPosition(); - pos.y = std::clamp(pos.y + (verticalSpeed * deltaTime), -1.0f, 1.0f); - updatePosition(pos); - verticalSpeed -= FALLING_CONST * deltaTime; + pos.y = std::clamp(pos.y + (f_verticalSpeed * deltaTime), -1.0f, 1.0f); + UpdatePosition(pos); + f_verticalSpeed -= FALLING_CONST * deltaTime; } -Bird::Bird(Engine* engie, Entity* parent, PipesContainer* pipes) +Bird::Bird(engine::Engine* engie, engine::Entity* parent, PipesContainer* pipes) : Entity(engie, parent) { this->pipes = pipes; } diff --git a/src/flappyBird/Bird.hh b/src/flappyBird/Bird.hh index 9c83e06..a776310 100644 --- a/src/flappyBird/Bird.hh +++ b/src/flappyBird/Bird.hh @@ -3,17 +3,17 @@ #include #include "Pipes.hh" -class Bird : public Entity { +class Bird : public engine::Entity { public: - void draw() override; - void update() override; - Bird(Engine* engie, Entity* parent, PipesContainer* pipes); - bool isHit = false; + void Draw() override; + void Update() override; + Bird(engine::Engine* engie, engine::Entity* parent, PipesContainer* pipes); + bool b_isHit = false; private: static constexpr float FALLING_CONST = 0.65; static constexpr float JUMP_CONST = 0.5; - float verticalSpeed = JUMP_CONST; + float f_verticalSpeed = JUMP_CONST; PipesContainer *pipes = nullptr; }; #endif diff --git a/src/flappyBird/FlappyBird.cc b/src/flappyBird/FlappyBird.cc index 2b3a303..cafc2af 100644 --- a/src/flappyBird/FlappyBird.cc +++ b/src/flappyBird/FlappyBird.cc @@ -6,96 +6,102 @@ #include "MainMenu.hh" -void FlappyBird::gameLoop() { - if (mainMenu->active) { - mainMenuLoop(); +void FlappyBird::GameLoop() +{ + if (p_mainMenu->b_active) { + MainMenuLoop(); return; } - if (!thread) { - thread = new std::thread(&FlappyBird::spawnPipes, this); + if (!p_thread) { + p_thread = new std::thread(&FlappyBird::SpawnPipes, this); } - mainGameLoop(); + MainGameLoop(); } -void FlappyBird::spawnPipes() { - threadRunning = true; - while (threadRunning) { - doneSpawningPipes = false; - pipesContainer->children.push_back( - Pipes::CreatePipes(this, pipesContainer)); - doneSpawningPipes = true; +void FlappyBird::SpawnPipes() +{ + b_threadRunning = true; + while (b_threadRunning) { + b_doneSpawningPipes = false; + p_pipesContainer->children.push_back( + Pipes::CreatePipes(this, p_pipesContainer)); + b_doneSpawningPipes = true; cv.notify_one(); std::this_thread::sleep_for(std::chrono::seconds(2)); } } -void FlappyBird::mainGameLoop() { - if (bird->isHit) { - mainMenu->active = true; - bird->isHit = false; +void FlappyBird::MainGameLoop() +{ + if (p_bird->b_isHit) { + p_mainMenu->b_active = true; + p_bird->b_isHit = false; SwapBuffers(); return; } - 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); + for (size_t i = 0; i < p_pipesContainer->children.size(); i++) { + if (p_pipesContainer->children[i]->b_destroyed) { + delete p_pipesContainer->children[i]; + p_pipesContainer->children.erase(p_pipesContainer->children.begin() + i); } } std::unique_lock lock(mutex); - cv.wait(lock, [&]{return doneSpawningPipes;}); - rendering = true; + cv.wait(lock, [&] { return b_doneSpawningPipes; }); + b_rendering = true; glClear(GL_COLOR_BUFFER_BIT); - controller->PollController(); - Entity::updateEntity(this->bird); - Entity::updateEntity(this->pipesContainer); - Entity::drawEntity(this->bird); - Entity::drawEntity(this->pipesContainer); + p_controller->PollController(); + engine::UpdateEntity(p_bird); + engine::UpdateEntity(p_pipesContainer); + engine::DrawEntity(p_bird); + engine::DrawEntity(p_pipesContainer); SwapBuffers(); } -void FlappyBird::mainMenuLoop() { - if (thread) { - threadRunning = false; - thread->detach(); - delete thread; - thread = nullptr; - pipesContainer->DestroyChildren(); - pipesContainer->children.clear(); +void FlappyBird::MainMenuLoop() +{ + if (p_thread) { + b_threadRunning = false; + p_thread->detach(); + delete p_thread; + p_thread = nullptr; + p_pipesContainer->DestroyChildren(); + p_pipesContainer->children.clear(); SwapBuffers(); return; } - controller->PollController(); - Entity::updateEntity(this->mainMenu); - Entity::drawEntity(this->mainMenu); + p_controller->PollController(); + engine::UpdateEntity(p_mainMenu); + engine::DrawEntity(p_mainMenu); SwapBuffers(); } -FlappyBird::FlappyBird() { - pipesContainer = new PipesContainer(this, nullptr); - bird = new Bird(this, nullptr, pipesContainer); - Vector3 scale = bird->getScale(); - Vector3 position = bird->getPosition(); +FlappyBird::FlappyBird() +{ + p_pipesContainer = new PipesContainer(this, nullptr); + p_bird = new Bird(this, nullptr, p_pipesContainer); + engine::Vector3 scale = p_bird->GetScale(); + engine::Vector3 position = p_bird->GetPosition(); scale.x = 0.1; scale.y = 0.1; position.x = -0.75; - bird->updateScale(scale); - bird->updatePosition(position); + p_bird->UpdateScale(scale); + p_bird->UpdatePosition(position); - mainMenu = new MainMenu(this, nullptr); - mainMenu->active = true; + p_mainMenu = new MainMenu(this, nullptr); + p_mainMenu->b_active = true; } -FlappyBird::~FlappyBird() { - delete bird; - delete pipesContainer; - delete mainMenu; - threadRunning = false; - if (thread != nullptr) thread->detach(); - delete thread; +FlappyBird::~FlappyBird() +{ + delete p_bird; + delete p_pipesContainer; + delete p_mainMenu; + b_threadRunning = false; + if (p_thread != nullptr) + p_thread->detach(); + delete p_thread; } diff --git a/src/flappyBird/FlappyBird.hh b/src/flappyBird/FlappyBird.hh index 9cd213e..f04fc79 100644 --- a/src/flappyBird/FlappyBird.hh +++ b/src/flappyBird/FlappyBird.hh @@ -9,20 +9,20 @@ #include "Pipes.hh" #include "MainMenu.hh" -class FlappyBird : public Engine { - void gameLoop() override; - void spawnPipes(); - void mainGameLoop(); - void mainMenuLoop(); +class FlappyBird : public engine::Engine { + void GameLoop() override; + void SpawnPipes(); + void MainGameLoop(); + void MainMenuLoop(); public: - Bird* bird; - PipesContainer* pipesContainer; - MainMenu* mainMenu; - std::thread* thread = nullptr; - bool threadRunning; - bool doneSpawningPipes = true; - bool rendering = false; + Bird* p_bird; + PipesContainer* p_pipesContainer; + MainMenu* p_mainMenu; + std::thread* p_thread = nullptr; + bool b_threadRunning; + bool b_doneSpawningPipes = true; + bool b_rendering = false; std::mutex mutex; std::condition_variable cv; FlappyBird(); diff --git a/src/flappyBird/MainMenu.cc b/src/flappyBird/MainMenu.cc index 2278482..396133d 100644 --- a/src/flappyBird/MainMenu.cc +++ b/src/flappyBird/MainMenu.cc @@ -1,12 +1,14 @@ #include "MainMenu.hh" -#include #include +#include -void MainMenu::draw() { +void MainMenu::Draw() +{ glClear(GL_COLOR_BUFFER_BIT); } -void MainMenu::update() { - engine->pressButton(Controller::A, [&]() { - active = false; +void MainMenu::Update() +{ + p_engine->PressButton(engine::Controller::A, [&]() { + b_active = false; }); } diff --git a/src/flappyBird/MainMenu.hh b/src/flappyBird/MainMenu.hh index 67205f2..33c043a 100644 --- a/src/flappyBird/MainMenu.hh +++ b/src/flappyBird/MainMenu.hh @@ -2,12 +2,12 @@ #define MAIN_MENU_HH #include -class MainMenu : public Entity { - void draw() override; - void update() override; +class MainMenu : public engine::Entity { + void Draw() override; + void Update() override; public: - MainMenu(Engine* engie, Entity* parent) : Entity(engie, parent) {}; - bool active; + MainMenu(engine::Engine* engie, engine::Entity* parent) : engine::Entity(engie, parent) {}; + bool b_active; }; #endif diff --git a/src/flappyBird/Pipes.cc b/src/flappyBird/Pipes.cc index 56d5796..62bd353 100644 --- a/src/flappyBird/Pipes.cc +++ b/src/flappyBird/Pipes.cc @@ -4,9 +4,9 @@ #include "engine/entity.hh" -void Pipe::draw() { glRectf(-1.0f, pipeLength, 1.0f, -pipeLength); } +void Pipe::Draw() { glRectf(-1.0f, f_pipeLength, 1.0f, -f_pipeLength); } -Pipes::Pipes(Engine* engie, Entity* parent) : Entity(engie, parent) { +Pipes::Pipes(engine::Engine* engie, Entity* parent) : Entity(engie, parent) { std::random_device r; std::default_random_engine generator(r()); @@ -16,31 +16,31 @@ Pipes::Pipes(Engine* engie, Entity* parent) : Entity(engie, parent) { std::uniform_real_distribution offsetGenerator(0.35f, 0.8f); if (upOrDown) { - pipeOffset = -offsetGenerator(generator); + f_pipeOffset = -offsetGenerator(generator); } else { - pipeOffset = offsetGenerator(generator); + f_pipeOffset = offsetGenerator(generator); } - Pipe* pipe1 = new Pipe(engine, this); - Pipe* pipe2 = new Pipe(engine, this); + Pipe* pipe1 = new Pipe(p_engine, this); + Pipe* pipe2 = new Pipe(p_engine, this); - Engine::Vector3 pipe1Scale = pipe1->getScale(); - Engine::Vector3 pipe1Position = pipe1->getPosition(); + engine::Vector3 pipe1Scale = pipe1->GetScale(); + engine::Vector3 pipe1Position = pipe1->GetPosition(); - Engine::Vector3 pipe2Scale = pipe2->getScale(); - Engine::Vector3 pipe2Position = pipe2->getPosition(); + engine::Vector3 pipe2Scale = pipe2->GetScale(); + engine::Vector3 pipe2Position = pipe2->GetPosition(); pipe1Scale.x = 0.1; pipe1Position.y = 1.0f; - pipe1->pipeLength = std::abs(pipeLength + pipeOffset); - pipe1->updateScale(pipe1Scale); - pipe1->updatePosition(pipe1Position); + pipe1->f_pipeLength = std::abs(f_pipeLength + f_pipeOffset); + pipe1->UpdateScale(pipe1Scale); + pipe1->UpdatePosition(pipe1Position); pipe2Scale.x = 0.1; pipe2Position.y = -1.0f; - pipe2->pipeLength = std::abs(pipeLength - pipeOffset); - pipe2->updateScale(pipe2Scale); - pipe2->updatePosition(pipe2Position); + pipe2->f_pipeLength = std::abs(f_pipeLength - f_pipeOffset); + pipe2->UpdateScale(pipe2Scale); + pipe2->UpdatePosition(pipe2Position); children.push_back(pipe1); children.push_back(pipe2); @@ -48,23 +48,23 @@ Pipes::Pipes(Engine* engie, Entity* parent) : Entity(engie, parent) { // position.x -= 0.1f; } -void Pipes::update() { - Engine::Vector3 position = this->getPosition(); - position.x -= 0.5f * engine->getDeltaTime(); +void Pipes::Update() { + engine::Vector3 position = this->GetPosition(); + position.x -= 0.5f * p_engine->GetDeltaTime(); if (position.x < -2) { - destroyed = true; + b_destroyed = true; } - this->updatePosition(position); + this->UpdatePosition(position); } -Pipes* Pipes::CreatePipes(Engine* engie, Entity* parent) { +Pipes* Pipes::CreatePipes(engine::Engine* engie, Entity* parent) { Pipes* pipes = new Pipes(engie, parent); - Engine::Vector3 pipesScale = pipes->getScale(); - Engine::Vector3 pipesPosition = pipes->getPosition(); + engine::Vector3 pipesScale = pipes->GetScale(); + engine::Vector3 pipesPosition = pipes->GetPosition(); pipesScale.x = 0.1f; pipesScale.y = 0.1f; pipesPosition.x = 1.25f; - pipes->updateScale(pipesScale); - pipes->updatePosition(pipesPosition); + pipes->UpdateScale(pipesScale); + pipes->UpdatePosition(pipesPosition); return pipes; } diff --git a/src/flappyBird/Pipes.hh b/src/flappyBird/Pipes.hh index 2523eef..307d33b 100644 --- a/src/flappyBird/Pipes.hh +++ b/src/flappyBird/Pipes.hh @@ -2,25 +2,25 @@ #define PIPES_HH #include -class Pipes : public Entity { +class Pipes : public engine::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); + Pipes(engine::Engine* engie, engine::Entity* parent); + void Update() override; + float f_pipeLength = 0.7f; + float f_pipeOffset = 0.35f; + static Pipes* CreatePipes(engine::Engine* engie, engine::Entity* parent); }; -class Pipe : public Entity { - void draw() override; +class Pipe : public engine::Entity { + void Draw() override; public: - Pipe(Engine* engie, Entity* parent) : Entity(engie, parent) {} - float pipeLength = 1.0f; + Pipe(engine::Engine* engie, engine::Entity* parent) : engine::Entity(engie, parent) {} + float f_pipeLength = 1.0f; }; -class PipesContainer : public Entity { +class PipesContainer : public engine::Entity { public: - PipesContainer(Engine* engie, Entity* parent) : Entity(engie, parent) {} + PipesContainer(engine::Engine* engie, engine::Entity* parent) : engine::Entity(engie, parent) {} }; #endif diff --git a/src/gl.cc b/src/gl.cc index 8662c4a..4742614 100644 --- a/src/gl.cc +++ b/src/gl.cc @@ -2,7 +2,7 @@ int main() { Glcc engine; - engine.initializeEngine(); - engine.initializeController(); - engine.initializeGameLoop(); + engine.InitializeEngine(); + engine.InitializeController(); + engine.InitializeGameLoop(); } diff --git a/src/gl/Gl.cc b/src/gl/Gl.cc index eb41c24..4f56e0d 100644 --- a/src/gl/Gl.cc +++ b/src/gl/Gl.cc @@ -3,13 +3,13 @@ #include "GlEntity.hh" Glcc::Glcc() { - entity = new GlEntity(this, nullptr); + p_entity = new GlEntity(this, nullptr); } -void Glcc::gameLoop() { +void Glcc::GameLoop() { glClear(GL_COLOR_BUFFER_BIT); - controller->PollController(); - Entity::updateEntity(entity); - Entity::drawEntity(entity); + p_controller->PollController(); + engine::UpdateEntity(p_entity); + engine::DrawEntity(p_entity); SwapBuffers(); } diff --git a/src/gl/Gl.hh b/src/gl/Gl.hh index 8f22dbc..5f45dfa 100644 --- a/src/gl/Gl.hh +++ b/src/gl/Gl.hh @@ -3,12 +3,12 @@ #include #include "GlEntity.hh" -class Glcc : public Engine { +class Glcc : public engine::Engine { public: Glcc(); private: - void gameLoop() override; - GlEntity* entity; + void GameLoop() override; + GlEntity* p_entity; }; #endif diff --git a/src/gl/GlEntity.cc b/src/gl/GlEntity.cc index 91628fb..7d38854 100644 --- a/src/gl/GlEntity.cc +++ b/src/gl/GlEntity.cc @@ -5,27 +5,27 @@ #include #endif -GlEntity::GlEntity(Engine* engie, Entity* parent) : Entity(engie, parent) {} +GlEntity::GlEntity(engine::Engine* engie, engine::Entity* parent) : engine::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; }); +void GlEntity::Update() { + p_engine->PressButton(engine::Controller::Button::DPAD_LEFT, + [&]() { f_speed -= 1.0f; }); + p_engine->PressButton(engine::Controller::Button::DPAD_RIGHT, + [&]() { f_speed += 1.0f; }); + p_engine->PressButton(engine::Controller::Button::A, + [&]() { b_thirtyFps = !b_thirtyFps; }); - speed = std::clamp(speed, 1.0f, 10.0f); + f_speed = std::clamp(f_speed, 1.0f, 10.0f); - angle += engine->controller->GetLeftJoystickXAxis() * - engine->getDeltaTime() * speed; - printf("%f\n", speed); + f_angle += p_engine->p_controller->GetLeftJoystickXAxis() * + p_engine->GetDeltaTime() * f_speed; + printf("%f\n", f_speed); } -void GlEntity::draw() { +void GlEntity::Draw() { // clang-format off glPushMatrix(); - glRotatef(angle, 0, 0, 1); + glRotatef(f_angle, 0, 0, 1); glColor3f(0.5f, 0.0f, 1.0f); glBegin(GL_POLYGON); glColor3f(1.0f, 0.0f, 0.0f); @@ -44,7 +44,7 @@ void GlEntity::draw() { // clang-format on #ifdef _arch_dreamcast - if (thirtyFps) { + if (b_thirtyFps) { vid_waitvbl(); vid_waitvbl(); } diff --git a/src/gl/GlEntity.hh b/src/gl/GlEntity.hh index e95fa51..71a4f2b 100644 --- a/src/gl/GlEntity.hh +++ b/src/gl/GlEntity.hh @@ -1,15 +1,15 @@ #ifndef GL_ENTITY_HH #define GL_ENTITY_HH #include -class GlEntity : public Entity { +class GlEntity : public engine::Entity { public: - GlEntity(Engine* engie, Entity* parent); + GlEntity(engine::Engine* engie, engine::Entity* parent); private: - void update() override; - void draw() override; - bool thirtyFps; - float angle = 0; - float speed = 5.0f; + void Update() override; + void Draw() override; + bool b_thirtyFps; + float f_angle = 0; + float f_speed = 5.0f; }; #endif diff --git a/src/hello.cc b/src/hello.cc index 760efb7..e02f951 100644 --- a/src/hello.cc +++ b/src/hello.cc @@ -2,6 +2,6 @@ int main(int argc, char *argv[]) { Hello engine; - engine.initializeEngine(); - engine.initializeGameLoop(); + engine.InitializeEngine(); + engine.InitializeGameLoop(); } diff --git a/src/hello/Hello.cc b/src/hello/Hello.cc index dfe18da..00fea2b 100644 --- a/src/hello/Hello.cc +++ b/src/hello/Hello.cc @@ -1,11 +1,11 @@ #include "Hello.hh" Hello::Hello() { - entity = new HelloEntity(this, nullptr); + p_entity = new HelloEntity(this, nullptr); } -void Hello::gameLoop() { +void Hello::GameLoop() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - Entity::drawEntity(entity); + engine::DrawEntity(p_entity); SwapBuffers(); } diff --git a/src/hello/Hello.hh b/src/hello/Hello.hh index 3aa9b2e..abb6beb 100644 --- a/src/hello/Hello.hh +++ b/src/hello/Hello.hh @@ -1,12 +1,12 @@ #ifndef HELLO_HH #define HELLO_HH -#include #include "HelloEntity.hh" +#include -class Hello : public Engine { - public: - HelloEntity* entity; - void gameLoop() override; +class Hello : public engine::Engine { +public: + HelloEntity* p_entity; + void GameLoop() override; Hello(); }; #endif diff --git a/src/hello/HelloEntity.cc b/src/hello/HelloEntity.cc index cb20dcc..95d1058 100644 --- a/src/hello/HelloEntity.cc +++ b/src/hello/HelloEntity.cc @@ -1,6 +1,6 @@ #include "HelloEntity.hh" -void HelloEntity::draw() { +void HelloEntity::Draw() { glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); @@ -13,4 +13,4 @@ void HelloEntity::draw() { glEnd(); } -HelloEntity::HelloEntity(Engine* engie, Entity* parent) : Entity(engie, parent) {} +HelloEntity::HelloEntity(engine::Engine* engie, engine::Entity* parent) : engine::Entity(engie, parent) {} diff --git a/src/hello/HelloEntity.hh b/src/hello/HelloEntity.hh index f2b3235..e8636ef 100644 --- a/src/hello/HelloEntity.hh +++ b/src/hello/HelloEntity.hh @@ -1,9 +1,9 @@ #ifndef HELLO_ENTITY_HH #define HELLO_ENTITY_HH #include -class HelloEntity : public Entity { - void draw() override; +class HelloEntity : public engine::Entity { + void Draw() override; public: - HelloEntity(Engine* engine, Entity* parent); + HelloEntity(engine::Engine* engine, engine::Entity* parent); }; #endif