Move engine stuff to its own namespace.

This commit is contained in:
Fries 2024-04-01 17:28:55 -07:00
parent ecdeaf0b94
commit aaa6c15a9a
40 changed files with 501 additions and 426 deletions

View file

@ -1,4 +1,5 @@
BasedOnStyle: Google ---
UseTab: Always BasedOnStyle: WebKit
IndentWidth: 4 IndentWidth: 4
TabWidth: 4 TabWidth: 4
UseTab: Always

6
.clang-tidy Normal file
View file

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

View file

@ -2,6 +2,6 @@
int main() { int main() {
Cube engine; Cube engine;
engine.initializeEngine(); engine.InitializeEngine();
engine.initializeGameLoop(); engine.InitializeGameLoop();
} }

View file

@ -15,10 +15,10 @@
#endif #endif
Cube::Cube() { Cube::Cube() {
entity = new CubeEntity(this, nullptr); p_entity = new CubeEntity(this, nullptr);
} }
void Cube::initScreen() { void Cube::InitScreen() {
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
gluPerspective(45.0f, 640.0f / 480.0f, 0.1f, 100.0f); 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); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glGenTextures(1, &texture); glGenTextures(1, &ui_texture);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, ui_texture);
int width, height, nr_channels; int width, height, nr_channels;
@ -50,11 +50,11 @@ void Cube::initScreen() {
stbi_image_free(data); stbi_image_free(data);
} }
void Cube::gameLoop() { void Cube::GameLoop() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f); glTranslatef(0.0f, 0.0f, -5.0f);
glRotatef(90, 0.0f, 1.0f, 0.5f); glRotatef(90, 0.0f, 1.0f, 0.5f);
Entity::drawEntity(entity); engine::DrawEntity(p_entity);
SwapBuffers(); SwapBuffers();
} }

View file

@ -2,12 +2,12 @@
#define CUBE_HH #define CUBE_HH
#include <engine/engine.hh> #include <engine/engine.hh>
#include "CubeEntity.hh" #include "CubeEntity.hh"
class Cube : public Engine { class Cube : public engine::Engine {
public: public:
CubeEntity* entity; CubeEntity* p_entity;
void gameLoop() override; void GameLoop() override;
void initScreen() override; void InitScreen() override;
Cube(); Cube();
unsigned int texture; unsigned int ui_texture;
}; };
#endif #endif

View file

@ -12,26 +12,26 @@
#endif #endif
CubeEntity::CubeEntity(Engine* engine, Entity* parent) : Entity(engine, parent) { CubeEntity::CubeEntity(engine::Engine* engine, Entity* parent) : Entity(engine, parent) {
char* message = new char; char* message = new char;
if (!parseObj(CUBE_LOCATION, reader, message)) if (!ParseObj(CUBE_LOCATION, reader, message))
throw std::runtime_error(message); throw std::runtime_error(message);
} }
void CubeEntity::draw() { void CubeEntity::Draw() {
const tinyobj::attrib_t& attrib = reader.GetAttrib(); const tinyobj::attrib_t& attrib = reader.GetAttrib();
auto& shapes = reader.GetShapes(); auto& shapes = reader.GetShapes();
angle += 1.0f; f_angle += 1.0f;
glRotatef(angle, 1.0f, 0.0f, 1.0f); glRotatef(f_angle, 1.0f, 0.0f, 1.0f);
int drawCalls = 0; int drawCalls = 0;
for (size_t s = 0; s < shapes.size(); s++) { for (size_t s = 0; s < shapes.size(); s++) {
// Loop over faces(polygon) // Loop over faces(polygon)
size_t index_offset = 0; size_t index_offset = 0;
std::vector<Engine::Vector3> vertexes{}; std::vector<engine::Vector3> vertexes{};
std::vector<Engine::Vector3> normals{}; std::vector<engine::Vector3> normals{};
std::vector<Engine::Vector3> textureCoords{}; std::vector<engine::Vector3> textureCoords{};
for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) { 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]); 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]; attrib.vertices[3 * size_t(idx.vertex_index) + 1];
tinyobj::real_t vz = tinyobj::real_t vz =
attrib.vertices[3 * size_t(idx.vertex_index) + 2]; 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); vertexes.push_back(vertex);
// glVertex3f(vx, vy, vz); // glVertex3f(vx, vy, vz);
// drawCalls += 1; // drawCalls += 1;
@ -61,12 +61,12 @@ void CubeEntity::draw() {
attrib.normals[3 * size_t(idx.normal_index) + 1]; attrib.normals[3 * size_t(idx.normal_index) + 1];
tinyobj::real_t nz = tinyobj::real_t nz =
attrib.normals[3 * size_t(idx.normal_index) + 2]; 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); normals.push_back(normal);
// glNormal3f(nx, ny, nz); // glNormal3f(nx, ny, nz);
// drawCalls += 1; // drawCalls += 1;
} else { } else {
normals.push_back(Engine::Vector3::zero); normals.push_back(engine::Vector3::zero);
} }
// Check if `texcoord_index` is zero or positive. negative = no // 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]; attrib.texcoords[2 * size_t(idx.texcoord_index) + 0];
tinyobj::real_t ty = tinyobj::real_t ty =
attrib.texcoords[2 * size_t(idx.texcoord_index) + 1]; 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); textureCoords.push_back(textureCoord);
// glTexCoord2f(tx, ty); // glTexCoord2f(tx, ty);
// drawCalls += 1; // drawCalls += 1;
} else { } else {
textureCoords.push_back(Engine::Vector3::zero); textureCoords.push_back(engine::Vector3::zero);
} }
} }
index_offset += fv; index_offset += fv;
@ -105,7 +105,7 @@ void CubeEntity::draw() {
// glDrawArrays(GL_QUADS, 0, susSize); // 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) { char* message) {
tinyobj::ObjReaderConfig readerConfig; tinyobj::ObjReaderConfig readerConfig;
readerConfig.mtl_search_path = "./"; readerConfig.mtl_search_path = "./";

View file

@ -4,13 +4,13 @@
#include <tiny_obj_loader.h> #include <tiny_obj_loader.h>
class CubeEntity : public Entity { class CubeEntity : public engine::Entity {
void draw() override; void Draw() override;
tinyobj::ObjReader reader; tinyobj::ObjReader reader;
bool parseObj(std::string objFile, tinyobj::ObjReader& reader, bool ParseObj(std::string objFile, tinyobj::ObjReader& reader,
char* message); char* message);
float angle; float f_angle;
public: public:
CubeEntity(Engine* engine, Entity* parent); CubeEntity(engine::Engine* engine, Entity* parent);
}; };
#endif #endif

View file

@ -1,7 +1,8 @@
#ifndef CONTROLLER_HH #ifndef CONTROLLER_HH
#define CONTROLLER_HH #define CONTROLLER_HH
namespace engine {
class Controller { class Controller {
public: public:
enum Button { enum Button {
A = (1 << 0), A = (1 << 0),
B = (1 << 1), B = (1 << 1),
@ -13,13 +14,14 @@ class Controller {
DPAD_RIGHT = (1 << 7) DPAD_RIGHT = (1 << 7)
}; };
virtual bool InitializeController() { return false; }; virtual bool InitializeController() { return false; };
virtual void PollController(){}; virtual void PollController() {};
virtual bool IsButtonPressed(Button button) const { return 0; }; virtual bool IsButtonPressed(Button button) const { return 0; };
virtual float GetLeftJoystickXAxis() const { return 0.0; }; virtual float GetLeftJoystickXAxis() const { return 0.0; };
virtual float GetLeftJoystickYAxis() const { return 0.0; }; virtual float GetLeftJoystickYAxis() const { return 0.0; };
virtual ~Controller() {}; virtual ~Controller() {};
protected: protected:
virtual int GetButtonMask(Button button) const { return 0; } virtual int GetButtonMask(Button button) const { return 0; }
}; };
}
#endif #endif

View file

@ -4,7 +4,9 @@
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
bool DesktopController::InitializeController() { namespace engine {
bool DesktopController::InitializeController()
{
std::ifstream gameControllerDB; std::ifstream gameControllerDB;
gameControllerDB.open("./gamecontrollerdb.txt"); gameControllerDB.open("./gamecontrollerdb.txt");
if (gameControllerDB.is_open()) { if (gameControllerDB.is_open()) {
@ -20,39 +22,45 @@ bool DesktopController::InitializeController() {
return false; return false;
} }
void DesktopController::PollController() { void DesktopController::PollController()
{
glfwGetGamepadState(GLFW_JOYSTICK_1, &controllerState); glfwGetGamepadState(GLFW_JOYSTICK_1, &controllerState);
} }
bool DesktopController::IsButtonPressed(Button button) const { bool DesktopController::IsButtonPressed(Button button) const
{
return controllerState.buttons[GetButtonMask(button)]; return controllerState.buttons[GetButtonMask(button)];
} }
float DesktopController::GetLeftJoystickXAxis() const { float DesktopController::GetLeftJoystickXAxis() const
{
return controllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_X]; return controllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_X];
} }
float DesktopController::GetLeftJoystickYAxis() const { 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) const { 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;
case Button::B: case Button::B:
return GLFW_GAMEPAD_BUTTON_B; return GLFW_GAMEPAD_BUTTON_B;
case Button::X: case Button::X:
return GLFW_GAMEPAD_BUTTON_X; return GLFW_GAMEPAD_BUTTON_X;
case Button::Y: case Button::Y:
return GLFW_GAMEPAD_BUTTON_Y; return GLFW_GAMEPAD_BUTTON_Y;
case Button::DPAD_UP: case Button::DPAD_UP:
return GLFW_GAMEPAD_BUTTON_DPAD_UP; return GLFW_GAMEPAD_BUTTON_DPAD_UP;
case Button::DPAD_DOWN: case Button::DPAD_DOWN:
return GLFW_GAMEPAD_BUTTON_DPAD_DOWN; return GLFW_GAMEPAD_BUTTON_DPAD_DOWN;
case Button::DPAD_LEFT: case Button::DPAD_LEFT:
return GLFW_GAMEPAD_BUTTON_DPAD_LEFT; return GLFW_GAMEPAD_BUTTON_DPAD_LEFT;
case Button::DPAD_RIGHT: case Button::DPAD_RIGHT:
return GLFW_GAMEPAD_BUTTON_DPAD_RIGHT; return GLFW_GAMEPAD_BUTTON_DPAD_RIGHT;
} }
} }
}

View file

@ -3,8 +3,9 @@
#include "controller.hh" #include "controller.hh"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
namespace engine {
class DesktopController : public Controller { class DesktopController : public Controller {
public: public:
bool InitializeController() override; bool InitializeController() override;
void PollController() override; void PollController() override;
bool IsButtonPressed(Button button) const override; bool IsButtonPressed(Button button) const override;
@ -13,5 +14,5 @@ class DesktopController : public Controller {
int GetButtonMask(Button button) const override; int GetButtonMask(Button button) const override;
GLFWgamepadstate controllerState; GLFWgamepadstate controllerState;
}; };
}
#endif #endif

View file

@ -1,35 +1,36 @@
#include "dreamcastController.hh" #include "dreamcastController.hh"
#include "controller.hh" #include "controller.hh"
namespace engine {
DreamcastController::DreamcastController() { DreamcastController::DreamcastController() {
} }
bool DreamcastController::InitializeController() { bool DreamcastController::InitializeController() {
maple_device_t* cont = maple_enum_type(0, MAPLE_FUNC_CONTROLLER); maple_device_t* cont = maple_enum_type(0, MAPLE_FUNC_CONTROLLER);
if (cont->valid) { if (cont->valid) {
this->controller = cont; this->p_controller = cont;
this->controller_state = (cont_state_t*)maple_dev_status(controller); this->p_controller_state = (cont_state_t*)maple_dev_status(p_controller);
return true; return true;
} else { } else {
controller = nullptr; p_controller = nullptr;
return false; return false;
} }
} }
void DreamcastController::PollController() { 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 { bool DreamcastController::IsButtonPressed(Button button) const {
return controller_state->buttons & GetButtonMask(button); return p_controller_state->buttons & GetButtonMask(button);
} }
float DreamcastController::GetLeftJoystickXAxis() const { float DreamcastController::GetLeftJoystickXAxis() const {
return (controller_state->joyx / 127.0f); return (p_controller_state->joyx / 127.0f);
} }
float DreamcastController::GetLeftJoystickYAxis() const { float DreamcastController::GetLeftJoystickYAxis() const {
return (controller_state->joyy / 127.0f); return (p_controller_state->joyy / 127.0f);
} }
int DreamcastController::GetButtonMask(Button button) const { int DreamcastController::GetButtonMask(Button button) const {
@ -53,3 +54,4 @@ int DreamcastController::GetButtonMask(Button button) const {
} }
return 0; return 0;
} }
}

View file

@ -3,8 +3,9 @@
#include <kos.h> #include <kos.h>
#include "controller.hh" #include "controller.hh"
namespace engine {
class DreamcastController : public Controller { class DreamcastController : public Controller {
public: public:
DreamcastController(); DreamcastController();
bool InitializeController() override; bool InitializeController() override;
void PollController() override; void PollController() override;
@ -12,7 +13,8 @@ class DreamcastController : public Controller {
float GetLeftJoystickXAxis() const override; float GetLeftJoystickXAxis() const override;
float GetLeftJoystickYAxis() const override; float GetLeftJoystickYAxis() const override;
int GetButtonMask(Button button) const override; int GetButtonMask(Button button) const override;
maple_device_t* controller = nullptr; maple_device_t* p_controller = nullptr;
cont_state_t* controller_state = nullptr; cont_state_t* p_controller_state = nullptr;
}; };
}
#endif #endif

View file

@ -7,49 +7,56 @@
#include "controller/desktopController.hh" #include "controller/desktopController.hh"
void DesktopEngine::initializeEngine() { namespace engine {
void DesktopEngine::InitializeEngine()
{
glfwInit(); glfwInit();
// Set OpenGL version to 1.2. // Set OpenGL version to 1.2.
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
// Create a GLFW window. // 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. // Fail if the window has failed to create.
if (window == nullptr) { if (p_window == nullptr) {
const char* description; const char* description;
glfwGetError(&description); glfwGetError(&description);
std::cout << "Failed to create GLFW window.\n" std::cout << "Failed to create GLFW window.\n"
<< description << std::endl; << description << std::endl;
} }
glfwMakeContextCurrent(window); glfwMakeContextCurrent(p_window);
// printSystemInformation(); // printSystemInformation();
} }
void DesktopEngine::initializeController() { void DesktopEngine::InitializeController()
{
DesktopController* linuxCont = new DesktopController; DesktopController* linuxCont = new DesktopController;
if (linuxCont->InitializeController()) { if (linuxCont->InitializeController()) {
controller = linuxCont; p_controller = linuxCont;
} else { } else {
throw std::runtime_error("No controller detected"); throw std::runtime_error("No controller detected");
} }
} }
void DesktopEngine::SwapBuffers() { void DesktopEngine::SwapBuffers()
glfwSwapBuffers(window); {
glfwSwapBuffers(p_window);
glfwPollEvents(); glfwPollEvents();
} }
bool DesktopEngine::ShouldWindowClose() { bool DesktopEngine::ShouldWindowClose()
return glfwWindowShouldClose(this->window); {
return glfwWindowShouldClose(this->p_window);
} }
DesktopEngine::~DesktopEngine() { DesktopEngine::~DesktopEngine()
delete controller; {
glfwDestroyWindow(window); delete p_controller;
glfwDestroyWindow(p_window);
glfwTerminate(); glfwTerminate();
window = nullptr; p_window = nullptr;
}
} }
#endif #endif

View file

@ -3,12 +3,14 @@
#include "nativeEngine.hh" #include "nativeEngine.hh"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
namespace engine {
class DesktopEngine : public NativeEngine { class DesktopEngine : public NativeEngine {
void initializeController() override; void InitializeController() override;
void initializeEngine() override; void InitializeEngine() override;
void SwapBuffers() override; void SwapBuffers() override;
bool ShouldWindowClose() override; bool ShouldWindowClose() override;
~DesktopEngine() override; ~DesktopEngine() override;
GLFWwindow* window = nullptr; GLFWwindow* p_window = nullptr;
}; };
}
#endif #endif

View file

@ -1,28 +1,33 @@
#ifdef _arch_dreamcast #ifdef _arch_dreamcast
#include "dreamcastEngine.hh" #include "dreamcastEngine.hh"
#include <stdexcept>
#include <GL/glkos.h>
#include "controller/dreamcastController.hh" #include "controller/dreamcastController.hh"
#include <GL/glkos.h>
void DreamcastEngine::initializeEngine() { #include <stdexcept>
namespace engine {
void DreamcastEngine::InitializeEngine()
{
glKosInit(); glKosInit();
// printSystemInformation(); // printSystemInformation();
} }
void DreamcastEngine::initializeController() { void DreamcastEngine::InitializeController()
{
DreamcastController* dreamCont = new DreamcastController; DreamcastController* dreamCont = new DreamcastController;
if (dreamCont->InitializeController()) { if (dreamCont->InitializeController()) {
controller = dreamCont; p_controller = dreamCont;
} else { } else {
throw std::runtime_error("Can't initialize the controller."); throw std::runtime_error("Can't initialize the controller.");
} }
} }
void DreamcastEngine::SwapBuffers() { void DreamcastEngine::SwapBuffers()
{
glKosSwapBuffers(); glKosSwapBuffers();
} }
bool DreamcastEngine::ShouldWindowClose() { bool DreamcastEngine::ShouldWindowClose()
{
return false; return false;
} }
}
#endif #endif

View file

@ -1,11 +1,12 @@
#ifndef DREAMCAST_ENGINE_HH #ifndef DREAMCAST_ENGINE_HH
#define DREAMCAST_ENGINE_HH #define DREAMCAST_ENGINE_HH
#include "nativeEngine.hh" #include "nativeEngine.hh"
namespace engine {
class DreamcastEngine : public NativeEngine { class DreamcastEngine : public NativeEngine {
void initializeController() override; void InitializeController() override;
void initializeEngine() override; void InitializeEngine() override;
void SwapBuffers() override; void SwapBuffers() override;
bool ShouldWindowClose() override; bool ShouldWindowClose() override;
}; };
}
#endif #endif

View file

@ -14,27 +14,31 @@
#include <cstring> #include <cstring>
#include <functional> #include <functional>
void Engine::initializeEngine() { namespace engine {
void Engine::InitializeEngine()
{
#ifdef _arch_dreamcast #ifdef _arch_dreamcast
engine = new DreamcastEngine; p_engine = new DreamcastEngine;
#else #else
engine = new DesktopEngine; p_engine = new DesktopEngine;
#endif #endif
engine->initializeEngine(); p_engine->InitializeEngine();
initScreen(); InitScreen();
} }
void Engine::initializeController() { void Engine::InitializeController()
engine->initializeController(); {
controller = engine->controller; 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); glMatrixMode(GL_PROJECTION);
// glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glViewport(0, 0, 640, 480); glViewport(0, 0, 640, 480);
@ -45,7 +49,8 @@ void Engine::initScreen() {
// glLoadIdentity(); // glLoadIdentity();
} }
void Engine::printSystemInformation() { void Engine::PrintSystemInformation()
{
printf("GL_VENDOR: %s\n", glGetString(GL_VENDOR)); printf("GL_VENDOR: %s\n", glGetString(GL_VENDOR));
printf("GL_RENDERER: %s\n", glGetString(GL_RENDERER)); printf("GL_RENDERER: %s\n", glGetString(GL_RENDERER));
printf("GL_VERSION: %s\n", glGetString(GL_VERSION)); printf("GL_VERSION: %s\n", glGetString(GL_VERSION));
@ -53,54 +58,59 @@ void Engine::printSystemInformation() {
} }
template <typename T> template <typename T>
void Engine::deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame, void Engine::DeltaTimeLoop(T&& callback, struct timeval& beginningOfFrame,
struct timeval& endOfFrame) { struct timeval& endOfFrame)
{
gettimeofday(&beginningOfFrame, 0); gettimeofday(&beginningOfFrame, 0);
(this->*callback)(); (this->*callback)();
gettimeofday(&endOfFrame, 0); gettimeofday(&endOfFrame, 0);
float time = ((float)(endOfFrame.tv_usec) * microSecond) - float time = ((float)(endOfFrame.tv_usec) * f_microSecond) - ((float)(beginningOfFrame.tv_usec) * f_microSecond);
((float)(beginningOfFrame.tv_usec) * microSecond);
if (time > 0.0f) { if (time > 0.0f) {
deltaTime = time; f_deltaTime = time;
} }
} }
void Engine::initializeGameLoop() { void Engine::InitializeGameLoop()
{
struct timeval beginningOfFrame, endOfFrame; struct timeval beginningOfFrame, endOfFrame;
start(); Start();
while (!ShouldWindowClose()) { while (!ShouldWindowClose()) {
deltaTimeLoop(&Engine::gameLoop, beginningOfFrame, endOfFrame); DeltaTimeLoop(&Engine::GameLoop, beginningOfFrame, endOfFrame);
} }
} }
void Engine::pressButton(Controller::Button button, void Engine::PressButton(Controller::Button button,
std::function<void()> callback) { std::function<void()> callback)
if (controller->IsButtonPressed(button)) { {
if (!(buttonsPressed & button)) { if (p_controller->IsButtonPressed(button)) {
buttonsPressed |= button; if (!(ui_buttonsPressed & button)) {
ui_buttonsPressed |= button;
callback(); callback();
} }
} else if ((buttonsPressed & button) && } else if ((ui_buttonsPressed & button) && !p_controller->IsButtonPressed(button)) {
!controller->IsButtonPressed(button)) { ui_buttonsPressed &= ~button;
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 { constexpr Vector3 Vector3::zero = { 0, 0, 0 };
return {this->x / amount, this->y / amount, this->z / amount};
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; Vector3 vec = *this / amount;
std::memcpy(this, &vec, sizeof(vec)); std::memcpy(this, &vec, sizeof(vec));
} }
float Engine::getDeltaTime() const { return deltaTime; } Vector3 Vector3::operator+(Vector3 vector) const
{
Engine::Vector3 Engine::Vector3::operator+(Engine::Vector3 vector) const { return { this->x + vector.x, this->y + vector.y, this->z + vector.z };
return {this->x + vector.x, this->y + vector.y, this->z + vector.z};
} }
};

View file

@ -14,49 +14,52 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#endif #endif
namespace engine {
class Engine { class Engine {
protected: protected:
static constexpr float microSecond = 0.000001f; static constexpr float f_microSecond = 0.000001f;
float angle = 0; float f_angle = 0;
float deltaTime = 1.0 / 60.0; float f_deltaTime = 1.0 / 60.0;
float speed = 5.0f; float f_speed = 5.0f;
bool thirtyfps; bool b_thirtyfps;
unsigned int buttonsPressed; unsigned int ui_buttonsPressed;
virtual void InitScreen();
virtual void initScreen(); virtual void GameLoop() {};
virtual void gameLoop(){}; virtual void Start() {};
virtual void start() {}; void PrintSystemInformation();
void printSystemInformation();
void SwapBuffers() const; void SwapBuffers() const;
bool ShouldWindowClose() const; bool ShouldWindowClose() const;
private: private:
NativeEngine* engine = nullptr; NativeEngine* p_engine = nullptr;
template <typename T> template <typename T>
void deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame, void DeltaTimeLoop(T&& callback, struct timeval& beginningOfFrame,
struct timeval& endOfFrame); struct timeval& endOfFrame);
public: public:
void initializeController(); void InitializeController();
void initializeEngine(); void InitializeEngine();
void initializeGameLoop(); void InitializeGameLoop();
void cleanupEngine(); void CleanupEngine();
float getDeltaTime() const; float GetDeltaTime() const;
void pressButton(Controller::Button button, std::function<void()> callback); void PressButton(Controller::Button button, std::function<void()> callback);
virtual ~Engine(); virtual ~Engine();
Controller* controller = nullptr; Controller* p_controller = nullptr;
struct Vector3 { };
Vector3 operator/(float amount) const;
void operator/=(float amount); struct Vector3 {
Vector3 operator+(Vector3 vector) const; public:
float x; Vector3 operator/(float amount) const;
float y; void operator/=(float amount);
float z; Vector3 operator+(Vector3 vector) const;
static const Vector3 zero; float x;
}; float y;
float z;
static const Vector3 zero;
};
}; };
#endif #endif

View file

@ -1,81 +1,94 @@
#include "entity.hh" #include "entity.hh"
#include <cstdio> #include <cstdio>
void Entity::DestroyChildren() { namespace engine {
void Entity::DestroyChildren()
{
for (Entity* childEntity : children) { for (Entity* childEntity : children) {
delete childEntity; delete childEntity;
} }
} }
Entity::~Entity() { Entity::~Entity()
{
DestroyChildren(); DestroyChildren();
} }
void Entity::drawEntity(Entity* entity) { void DrawEntity(Entity* entity)
{
glPushMatrix(); glPushMatrix();
glTranslatef(entity->position.x, entity->position.y, entity->position.z); Vector3 position = entity->GetPosition();
glScalef(entity->scale.x, entity->scale.y, entity->scale.z); Vector3 scale = entity->GetScale();
entity->draw(); glTranslatef(position.x, position.y, position.z);
glScalef(scale.x, scale.y, scale.z);
entity->Draw();
glPopMatrix(); glPopMatrix();
if (entity->children.size() > 0) { if (entity->children.size() > 0) {
drawChildrenEntity(entity); DrawChildrenEntity(entity);
} }
} }
void Entity::updateEntity(Entity* entity) { void UpdateEntity(Entity* entity)
entity->update(); {
entity->Update();
if (entity->children.size() > 0) { if (entity->children.size() > 0) {
updateChildrenEntity(entity); UpdateChildrenEntity(entity);
} }
} }
void Entity::drawChildrenEntity(Entity* entity) { void DrawChildrenEntity(Entity* entity)
{
for (Entity* childEntity : entity->children) { for (Entity* childEntity : entity->children) {
glPushMatrix(); glPushMatrix();
if (childEntity->parent == nullptr) { if (childEntity->p_parent == nullptr) {
printf("Error: Entity parent is nullptr.\n"); printf("Error: Entity parent is nullptr.\n");
} else { } else {
glTranslatef( Vector3 addedPosition = childEntity->p_parent->GetPosition() + childEntity->GetPosition();
childEntity->parent->position.x + childEntity->position.x, glTranslatef(addedPosition.x, addedPosition.y, addedPosition.z);
childEntity->parent->position.y + childEntity->position.y,
+childEntity->parent->position.z + childEntity->position.z);
} }
glScalef(childEntity->scale.x, childEntity->scale.y, Vector3 scale = childEntity->GetScale();
childEntity->scale.z); glScalef(scale.x, scale.y, scale.z);
childEntity->draw(); childEntity->Draw();
glPopMatrix(); glPopMatrix();
drawChildrenEntity(childEntity); DrawChildrenEntity(childEntity);
} }
} }
void Entity::updateChildrenEntity(Entity* entity) { void UpdateChildrenEntity(Entity* entity)
{
for (Entity* childEntity : entity->children) { for (Entity* childEntity : entity->children) {
childEntity->update(); childEntity->Update();
updateChildrenEntity(childEntity); UpdateChildrenEntity(childEntity);
} }
} }
Engine::Vector3 Entity::getPosition() const { Vector3 Entity::GetPosition() const
{
return position; return position;
} }
Engine::Vector3 Entity::getGlobalPosition() const { Vector3 Entity::GetGlobalPosition() const
{
return globalPosition; return globalPosition;
} }
void Entity::updatePosition(Engine::Vector3 position) { void Entity::UpdatePosition(Vector3 position)
{
this->position = position; this->position = position;
if (this->parent != nullptr) { if (this->p_parent != nullptr) {
globalPosition = this->position + this->parent->globalPosition; globalPosition = this->position + this->p_parent->globalPosition;
} else { } else {
globalPosition = position; globalPosition = position;
} }
} }
Engine::Vector3 Entity::getScale() const { Vector3 Entity::GetScale() const
{
return scale; return scale;
} }
void Entity::updateScale(Engine::Vector3 scale) { void Entity::UpdateScale(Vector3 scale)
{
this->scale = scale; this->scale = scale;
} }
}

View file

@ -1,34 +1,37 @@
#ifndef ENTITY_HH #ifndef ENTITY_HH
#define ENTITY_HH #define ENTITY_HH
#include "engine.hh" #include "engine.hh"
namespace engine {
class Entity { class Entity {
public: public:
Entity(Engine* engie, Entity* parent) { Entity(Engine* engie, Entity* parent)
engine = engie; {
this->parent = parent; p_engine = engie;
updatePosition(Engine::Vector3::zero); this->p_parent = parent;
UpdatePosition(Vector3::zero);
}; };
virtual ~Entity(); virtual ~Entity();
Engine* engine; Engine* p_engine;
Entity* parent = nullptr; Entity* p_parent = nullptr;
std::vector<Entity*> children = {}; std::vector<Entity*> children = {};
virtual void draw(){}; virtual void Draw() {};
virtual void update(){}; virtual void Update() {};
bool destroyed = false; bool b_destroyed = false;
static void drawEntity(Entity* entity); Vector3 GetPosition() const;
static void updateEntity(Entity* entity); Vector3 GetGlobalPosition() const;
Engine::Vector3 getPosition() const; void UpdatePosition(Vector3 position);
Engine::Vector3 getGlobalPosition() const; Vector3 GetScale() const;
void updatePosition(Engine::Vector3 position); void UpdateScale(Vector3 scale);
Engine::Vector3 getScale() const;
void updateScale(Engine::Vector3 scale);
void DestroyChildren(); void DestroyChildren();
private: private:
Engine::Vector3 position = Engine::Vector3::zero; Vector3 position = Vector3::zero;
Engine::Vector3 globalPosition = Engine::Vector3::zero; Vector3 globalPosition = Vector3::zero;
Engine::Vector3 scale = {1, 1, 1}; Vector3 scale = { 1, 1, 1 };
static void drawChildrenEntity(Entity* entity); };
static void updateChildrenEntity(Entity* entity); void DrawEntity(Entity* entity);
void UpdateEntity(Entity* entity);
void DrawChildrenEntity(Entity* entity);
void UpdateChildrenEntity(Entity* entity);
}; };
#endif #endif

View file

@ -1,13 +1,15 @@
#ifndef NATIVE_ENGINE_HH #ifndef NATIVE_ENGINE_HH
#define NATIVE_ENGINE_HH #define NATIVE_ENGINE_HH
#include "controller/controller.hh" #include "controller/controller.hh"
namespace engine {
class NativeEngine { class NativeEngine {
public: public:
virtual void initializeController(){}; virtual void InitializeController() {};
virtual void initializeEngine(){}; virtual void InitializeEngine() {};
virtual void SwapBuffers(){}; virtual void SwapBuffers() {};
virtual bool ShouldWindowClose(){return false;}; virtual bool ShouldWindowClose() { return false; };
virtual ~NativeEngine() {}; virtual ~NativeEngine() {};
Controller* controller = nullptr; Controller* p_controller = nullptr;
}; };
}
#endif #endif

View file

@ -2,7 +2,7 @@
int main() { int main() {
FlappyBird engine; FlappyBird engine;
engine.initializeEngine(); engine.InitializeEngine();
engine.initializeController(); engine.InitializeController();
engine.initializeGameLoop(); engine.InitializeGameLoop();
} }

View file

@ -2,10 +2,10 @@
#include <cstdio> #include <cstdio>
void Bird::draw() { void Bird::Draw() {
Engine::Vector3 one = {-1.0, -1.0, 0.0}; engine::Vector3 one = {-1.0, -1.0, 0.0};
Engine::Vector3 two = {0.0, 1.0, 0.0}; engine::Vector3 two = {0.0, 1.0, 0.0};
Engine::Vector3 three = {1.0, -1.0, 0.0f}; engine::Vector3 three = {1.0, -1.0, 0.0f};
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); glColor3f(1.0f, 0.0f, 0.0f);
@ -19,48 +19,47 @@ void Bird::draw() {
glEnd(); glEnd();
} }
void Bird::update() { void Bird::Update() {
if (pipes != nullptr) { if (pipes != nullptr) {
for (size_t 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 <=
pipes->children[i]->getGlobalPosition().x + 0.15f) { pipes->children[i]->GetGlobalPosition().x + 0.15f) {
Pipes* pipe = (Pipes*)pipes->children[i]; Pipes* pipe = (Pipes*)pipes->children[i];
float topPipeBottomPosition = 1 - ((Pipe*)(pipe->children[0]))->pipeLength; float topPipeBottomPosition = 1 - ((Pipe*)(pipe->children[0]))->f_pipeLength;
float bottomPipeTopPosition = -1 + ((Pipe*)(pipe->children[1]))->pipeLength; float bottomPipeTopPosition = -1 + ((Pipe*)(pipe->children[1]))->f_pipeLength;
bool betweenPipes = getGlobalPosition().y < topPipeBottomPosition && getGlobalPosition().y > bottomPipeTopPosition; bool betweenPipes = GetGlobalPosition().y < topPipeBottomPosition && GetGlobalPosition().y > bottomPipeTopPosition;
if(betweenPipes) { if(betweenPipes) {
printf("between pipes\n"); printf("between pipes\n");
} else { } else {
printf("colliding: %f\n", printf("colliding: %f\n",
pipes->children[i]->getGlobalPosition().x); pipes->children[i]->GetGlobalPosition().x);
isHit = true; b_isHit = true;
} }
} }
} }
} }
float deltaTime = engine->getDeltaTime(); float deltaTime = p_engine->GetDeltaTime();
engine->pressButton(Controller::A, [&]() { p_engine->PressButton(engine::Controller::A, [&]() {
if (verticalSpeed < 0) { if (f_verticalSpeed < 0) {
verticalSpeed = JUMP_CONST; f_verticalSpeed = JUMP_CONST;
return; 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); pos.y = std::clamp(pos.y + (f_verticalSpeed * deltaTime), -1.0f, 1.0f);
updatePosition(pos); UpdatePosition(pos);
verticalSpeed -= FALLING_CONST * deltaTime; 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) { : Entity(engie, parent) {
this->pipes = pipes; this->pipes = pipes;
} }

View file

@ -3,17 +3,17 @@
#include <engine/entity.hh> #include <engine/entity.hh>
#include "Pipes.hh" #include "Pipes.hh"
class Bird : public Entity { class Bird : public engine::Entity {
public: public:
void draw() override; void Draw() override;
void update() override; void Update() override;
Bird(Engine* engie, Entity* parent, PipesContainer* pipes); Bird(engine::Engine* engie, engine::Entity* parent, PipesContainer* pipes);
bool isHit = false; bool b_isHit = false;
private: private:
static constexpr float FALLING_CONST = 0.65; static constexpr float FALLING_CONST = 0.65;
static constexpr float JUMP_CONST = 0.5; static constexpr float JUMP_CONST = 0.5;
float verticalSpeed = JUMP_CONST; float f_verticalSpeed = JUMP_CONST;
PipesContainer *pipes = nullptr; PipesContainer *pipes = nullptr;
}; };
#endif #endif

View file

@ -6,96 +6,102 @@
#include "MainMenu.hh" #include "MainMenu.hh"
void FlappyBird::gameLoop() { void FlappyBird::GameLoop()
if (mainMenu->active) { {
mainMenuLoop(); if (p_mainMenu->b_active) {
MainMenuLoop();
return; return;
} }
if (!thread) { if (!p_thread) {
thread = new std::thread(&FlappyBird::spawnPipes, this); p_thread = new std::thread(&FlappyBird::SpawnPipes, this);
} }
mainGameLoop(); MainGameLoop();
} }
void FlappyBird::spawnPipes() { void FlappyBird::SpawnPipes()
threadRunning = true; {
while (threadRunning) { b_threadRunning = true;
doneSpawningPipes = false; while (b_threadRunning) {
pipesContainer->children.push_back( b_doneSpawningPipes = false;
Pipes::CreatePipes(this, pipesContainer)); p_pipesContainer->children.push_back(
doneSpawningPipes = true; Pipes::CreatePipes(this, p_pipesContainer));
b_doneSpawningPipes = true;
cv.notify_one(); cv.notify_one();
std::this_thread::sleep_for(std::chrono::seconds(2)); std::this_thread::sleep_for(std::chrono::seconds(2));
} }
} }
void FlappyBird::mainGameLoop() { void FlappyBird::MainGameLoop()
if (bird->isHit) { {
mainMenu->active = true; if (p_bird->b_isHit) {
bird->isHit = false; p_mainMenu->b_active = true;
p_bird->b_isHit = false;
SwapBuffers(); SwapBuffers();
return; return;
} }
for (size_t i = 0; i < pipesContainer->children.size(); i++) { for (size_t i = 0; i < p_pipesContainer->children.size(); i++) {
if (pipesContainer->children[i]->destroyed) { if (p_pipesContainer->children[i]->b_destroyed) {
delete pipesContainer->children[i]; delete p_pipesContainer->children[i];
pipesContainer->children.erase(pipesContainer->children.begin() + p_pipesContainer->children.erase(p_pipesContainer->children.begin() + i);
i);
} }
} }
std::unique_lock lock(mutex); std::unique_lock lock(mutex);
cv.wait(lock, [&]{return doneSpawningPipes;}); cv.wait(lock, [&] { return b_doneSpawningPipes; });
rendering = true; b_rendering = true;
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
controller->PollController(); p_controller->PollController();
Entity::updateEntity(this->bird); engine::UpdateEntity(p_bird);
Entity::updateEntity(this->pipesContainer); engine::UpdateEntity(p_pipesContainer);
Entity::drawEntity(this->bird); engine::DrawEntity(p_bird);
Entity::drawEntity(this->pipesContainer); engine::DrawEntity(p_pipesContainer);
SwapBuffers(); SwapBuffers();
} }
void FlappyBird::mainMenuLoop() { void FlappyBird::MainMenuLoop()
if (thread) { {
threadRunning = false; if (p_thread) {
thread->detach(); b_threadRunning = false;
delete thread; p_thread->detach();
thread = nullptr; delete p_thread;
pipesContainer->DestroyChildren(); p_thread = nullptr;
pipesContainer->children.clear(); p_pipesContainer->DestroyChildren();
p_pipesContainer->children.clear();
SwapBuffers(); SwapBuffers();
return; return;
} }
controller->PollController(); p_controller->PollController();
Entity::updateEntity(this->mainMenu); engine::UpdateEntity(p_mainMenu);
Entity::drawEntity(this->mainMenu); engine::DrawEntity(p_mainMenu);
SwapBuffers(); SwapBuffers();
} }
FlappyBird::FlappyBird() { FlappyBird::FlappyBird()
pipesContainer = new PipesContainer(this, nullptr); {
bird = new Bird(this, nullptr, pipesContainer); p_pipesContainer = new PipesContainer(this, nullptr);
Vector3 scale = bird->getScale(); p_bird = new Bird(this, nullptr, p_pipesContainer);
Vector3 position = bird->getPosition(); engine::Vector3 scale = p_bird->GetScale();
engine::Vector3 position = p_bird->GetPosition();
scale.x = 0.1; scale.x = 0.1;
scale.y = 0.1; scale.y = 0.1;
position.x = -0.75; position.x = -0.75;
bird->updateScale(scale); p_bird->UpdateScale(scale);
bird->updatePosition(position); p_bird->UpdatePosition(position);
mainMenu = new MainMenu(this, nullptr); p_mainMenu = new MainMenu(this, nullptr);
mainMenu->active = true; p_mainMenu->b_active = true;
} }
FlappyBird::~FlappyBird() { FlappyBird::~FlappyBird()
delete bird; {
delete pipesContainer; delete p_bird;
delete mainMenu; delete p_pipesContainer;
threadRunning = false; delete p_mainMenu;
if (thread != nullptr) thread->detach(); b_threadRunning = false;
delete thread; if (p_thread != nullptr)
p_thread->detach();
delete p_thread;
} }

View file

@ -9,20 +9,20 @@
#include "Pipes.hh" #include "Pipes.hh"
#include "MainMenu.hh" #include "MainMenu.hh"
class FlappyBird : public Engine { class FlappyBird : public engine::Engine {
void gameLoop() override; void GameLoop() override;
void spawnPipes(); void SpawnPipes();
void mainGameLoop(); void MainGameLoop();
void mainMenuLoop(); void MainMenuLoop();
public: public:
Bird* bird; Bird* p_bird;
PipesContainer* pipesContainer; PipesContainer* p_pipesContainer;
MainMenu* mainMenu; MainMenu* p_mainMenu;
std::thread* thread = nullptr; std::thread* p_thread = nullptr;
bool threadRunning; bool b_threadRunning;
bool doneSpawningPipes = true; bool b_doneSpawningPipes = true;
bool rendering = false; bool b_rendering = false;
std::mutex mutex; std::mutex mutex;
std::condition_variable cv; std::condition_variable cv;
FlappyBird(); FlappyBird();

View file

@ -1,12 +1,14 @@
#include "MainMenu.hh" #include "MainMenu.hh"
#include <engine/controller/controller.hh>
#include <cstdio> #include <cstdio>
#include <engine/controller/controller.hh>
void MainMenu::draw() { void MainMenu::Draw()
{
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
} }
void MainMenu::update() { void MainMenu::Update()
engine->pressButton(Controller::A, [&]() { {
active = false; p_engine->PressButton(engine::Controller::A, [&]() {
b_active = false;
}); });
} }

View file

@ -2,12 +2,12 @@
#define MAIN_MENU_HH #define MAIN_MENU_HH
#include <engine/entity.hh> #include <engine/entity.hh>
class MainMenu : public Entity { class MainMenu : public engine::Entity {
void draw() override; void Draw() override;
void update() override; void Update() override;
public: public:
MainMenu(Engine* engie, Entity* parent) : Entity(engie, parent) {}; MainMenu(engine::Engine* engie, engine::Entity* parent) : engine::Entity(engie, parent) {};
bool active; bool b_active;
}; };
#endif #endif

View file

@ -4,9 +4,9 @@
#include "engine/entity.hh" #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::random_device r;
std::default_random_engine generator(r()); std::default_random_engine generator(r());
@ -16,31 +16,31 @@ Pipes::Pipes(Engine* engie, Entity* parent) : Entity(engie, parent) {
std::uniform_real_distribution<float> offsetGenerator(0.35f, 0.8f); std::uniform_real_distribution<float> offsetGenerator(0.35f, 0.8f);
if (upOrDown) { if (upOrDown) {
pipeOffset = -offsetGenerator(generator); f_pipeOffset = -offsetGenerator(generator);
} else { } else {
pipeOffset = offsetGenerator(generator); f_pipeOffset = offsetGenerator(generator);
} }
Pipe* pipe1 = new Pipe(engine, this); Pipe* pipe1 = new Pipe(p_engine, this);
Pipe* pipe2 = new Pipe(engine, this); Pipe* pipe2 = new Pipe(p_engine, this);
Engine::Vector3 pipe1Scale = pipe1->getScale(); engine::Vector3 pipe1Scale = pipe1->GetScale();
Engine::Vector3 pipe1Position = pipe1->getPosition(); engine::Vector3 pipe1Position = pipe1->GetPosition();
Engine::Vector3 pipe2Scale = pipe2->getScale(); engine::Vector3 pipe2Scale = pipe2->GetScale();
Engine::Vector3 pipe2Position = pipe2->getPosition(); engine::Vector3 pipe2Position = pipe2->GetPosition();
pipe1Scale.x = 0.1; pipe1Scale.x = 0.1;
pipe1Position.y = 1.0f; pipe1Position.y = 1.0f;
pipe1->pipeLength = std::abs(pipeLength + pipeOffset); pipe1->f_pipeLength = std::abs(f_pipeLength + f_pipeOffset);
pipe1->updateScale(pipe1Scale); pipe1->UpdateScale(pipe1Scale);
pipe1->updatePosition(pipe1Position); pipe1->UpdatePosition(pipe1Position);
pipe2Scale.x = 0.1; pipe2Scale.x = 0.1;
pipe2Position.y = -1.0f; pipe2Position.y = -1.0f;
pipe2->pipeLength = std::abs(pipeLength - pipeOffset); pipe2->f_pipeLength = std::abs(f_pipeLength - f_pipeOffset);
pipe2->updateScale(pipe2Scale); pipe2->UpdateScale(pipe2Scale);
pipe2->updatePosition(pipe2Position); pipe2->UpdatePosition(pipe2Position);
children.push_back(pipe1); children.push_back(pipe1);
children.push_back(pipe2); children.push_back(pipe2);
@ -48,23 +48,23 @@ Pipes::Pipes(Engine* engie, Entity* parent) : Entity(engie, parent) {
// position.x -= 0.1f; // position.x -= 0.1f;
} }
void Pipes::update() { void Pipes::Update() {
Engine::Vector3 position = this->getPosition(); engine::Vector3 position = this->GetPosition();
position.x -= 0.5f * engine->getDeltaTime(); position.x -= 0.5f * p_engine->GetDeltaTime();
if (position.x < -2) { 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); Pipes* pipes = new Pipes(engie, parent);
Engine::Vector3 pipesScale = pipes->getScale(); engine::Vector3 pipesScale = pipes->GetScale();
Engine::Vector3 pipesPosition = pipes->getPosition(); engine::Vector3 pipesPosition = pipes->GetPosition();
pipesScale.x = 0.1f; pipesScale.x = 0.1f;
pipesScale.y = 0.1f; pipesScale.y = 0.1f;
pipesPosition.x = 1.25f; pipesPosition.x = 1.25f;
pipes->updateScale(pipesScale); pipes->UpdateScale(pipesScale);
pipes->updatePosition(pipesPosition); pipes->UpdatePosition(pipesPosition);
return pipes; return pipes;
} }

View file

@ -2,25 +2,25 @@
#define PIPES_HH #define PIPES_HH
#include <engine/entity.hh> #include <engine/entity.hh>
class Pipes : public Entity { class Pipes : public engine::Entity {
public: public:
Pipes(Engine* engie, Entity* parent); Pipes(engine::Engine* engie, engine::Entity* parent);
void update() override; void Update() override;
float pipeLength = 0.7f; float f_pipeLength = 0.7f;
float pipeOffset = 0.35f; float f_pipeOffset = 0.35f;
static Pipes* CreatePipes(Engine* engie, Entity* parent); static Pipes* CreatePipes(engine::Engine* engie, engine::Entity* parent);
}; };
class Pipe : public Entity { class Pipe : public engine::Entity {
void draw() override; void Draw() override;
public: public:
Pipe(Engine* engie, Entity* parent) : Entity(engie, parent) {} Pipe(engine::Engine* engie, engine::Entity* parent) : engine::Entity(engie, parent) {}
float pipeLength = 1.0f; float f_pipeLength = 1.0f;
}; };
class PipesContainer : public Entity { class PipesContainer : public engine::Entity {
public: public:
PipesContainer(Engine* engie, Entity* parent) : Entity(engie, parent) {} PipesContainer(engine::Engine* engie, engine::Entity* parent) : engine::Entity(engie, parent) {}
}; };
#endif #endif

View file

@ -2,7 +2,7 @@
int main() { int main() {
Glcc engine; Glcc engine;
engine.initializeEngine(); engine.InitializeEngine();
engine.initializeController(); engine.InitializeController();
engine.initializeGameLoop(); engine.InitializeGameLoop();
} }

View file

@ -3,13 +3,13 @@
#include "GlEntity.hh" #include "GlEntity.hh"
Glcc::Glcc() { Glcc::Glcc() {
entity = new GlEntity(this, nullptr); p_entity = new GlEntity(this, nullptr);
} }
void Glcc::gameLoop() { void Glcc::GameLoop() {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
controller->PollController(); p_controller->PollController();
Entity::updateEntity(entity); engine::UpdateEntity(p_entity);
Entity::drawEntity(entity); engine::DrawEntity(p_entity);
SwapBuffers(); SwapBuffers();
} }

View file

@ -3,12 +3,12 @@
#include <engine/engine.hh> #include <engine/engine.hh>
#include "GlEntity.hh" #include "GlEntity.hh"
class Glcc : public Engine { class Glcc : public engine::Engine {
public: public:
Glcc(); Glcc();
private: private:
void gameLoop() override; void GameLoop() override;
GlEntity* entity; GlEntity* p_entity;
}; };
#endif #endif

View file

@ -5,27 +5,27 @@
#include <kos.h> #include <kos.h>
#endif #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() { void GlEntity::Update() {
engine->pressButton(Controller::Button::DPAD_LEFT, p_engine->PressButton(engine::Controller::Button::DPAD_LEFT,
[&]() { speed -= 1.0f; }); [&]() { f_speed -= 1.0f; });
engine->pressButton(Controller::Button::DPAD_RIGHT, p_engine->PressButton(engine::Controller::Button::DPAD_RIGHT,
[&]() { speed += 1.0f; }); [&]() { f_speed += 1.0f; });
engine->pressButton(Controller::Button::A, p_engine->PressButton(engine::Controller::Button::A,
[&]() { thirtyFps = !thirtyFps; }); [&]() { 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() * f_angle += p_engine->p_controller->GetLeftJoystickXAxis() *
engine->getDeltaTime() * speed; p_engine->GetDeltaTime() * f_speed;
printf("%f\n", speed); printf("%f\n", f_speed);
} }
void GlEntity::draw() { void GlEntity::Draw() {
// clang-format off // clang-format off
glPushMatrix(); glPushMatrix();
glRotatef(angle, 0, 0, 1); glRotatef(f_angle, 0, 0, 1);
glColor3f(0.5f, 0.0f, 1.0f); glColor3f(0.5f, 0.0f, 1.0f);
glBegin(GL_POLYGON); glBegin(GL_POLYGON);
glColor3f(1.0f, 0.0f, 0.0f); glColor3f(1.0f, 0.0f, 0.0f);
@ -44,7 +44,7 @@ void GlEntity::draw() {
// clang-format on // clang-format on
#ifdef _arch_dreamcast #ifdef _arch_dreamcast
if (thirtyFps) { if (b_thirtyFps) {
vid_waitvbl(); vid_waitvbl();
vid_waitvbl(); vid_waitvbl();
} }

View file

@ -1,15 +1,15 @@
#ifndef GL_ENTITY_HH #ifndef GL_ENTITY_HH
#define GL_ENTITY_HH #define GL_ENTITY_HH
#include <engine/entity.hh> #include <engine/entity.hh>
class GlEntity : public Entity { class GlEntity : public engine::Entity {
public: public:
GlEntity(Engine* engie, Entity* parent); GlEntity(engine::Engine* engie, engine::Entity* parent);
private: private:
void update() override; void Update() override;
void draw() override; void Draw() override;
bool thirtyFps; bool b_thirtyFps;
float angle = 0; float f_angle = 0;
float speed = 5.0f; float f_speed = 5.0f;
}; };
#endif #endif

View file

@ -2,6 +2,6 @@
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
Hello engine; Hello engine;
engine.initializeEngine(); engine.InitializeEngine();
engine.initializeGameLoop(); engine.InitializeGameLoop();
} }

View file

@ -1,11 +1,11 @@
#include "Hello.hh" #include "Hello.hh"
Hello::Hello() { 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); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Entity::drawEntity(entity); engine::DrawEntity(p_entity);
SwapBuffers(); SwapBuffers();
} }

View file

@ -1,12 +1,12 @@
#ifndef HELLO_HH #ifndef HELLO_HH
#define HELLO_HH #define HELLO_HH
#include <engine/engine.hh>
#include "HelloEntity.hh" #include "HelloEntity.hh"
#include <engine/engine.hh>
class Hello : public Engine { class Hello : public engine::Engine {
public: public:
HelloEntity* entity; HelloEntity* p_entity;
void gameLoop() override; void GameLoop() override;
Hello(); Hello();
}; };
#endif #endif

View file

@ -1,6 +1,6 @@
#include "HelloEntity.hh" #include "HelloEntity.hh"
void HelloEntity::draw() { void HelloEntity::Draw() {
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
@ -13,4 +13,4 @@ void HelloEntity::draw() {
glEnd(); glEnd();
} }
HelloEntity::HelloEntity(Engine* engie, Entity* parent) : Entity(engie, parent) {} HelloEntity::HelloEntity(engine::Engine* engie, engine::Entity* parent) : engine::Entity(engie, parent) {}

View file

@ -1,9 +1,9 @@
#ifndef HELLO_ENTITY_HH #ifndef HELLO_ENTITY_HH
#define HELLO_ENTITY_HH #define HELLO_ENTITY_HH
#include <engine/entity.hh> #include <engine/entity.hh>
class HelloEntity : public Entity { class HelloEntity : public engine::Entity {
void draw() override; void Draw() override;
public: public:
HelloEntity(Engine* engine, Entity* parent); HelloEntity(engine::Engine* engine, engine::Entity* parent);
}; };
#endif #endif