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
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() {
Cube engine;
engine.initializeEngine();
engine.initializeGameLoop();
engine.InitializeEngine();
engine.InitializeGameLoop();
}

View file

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

View file

@ -2,12 +2,12 @@
#define CUBE_HH
#include <engine/engine.hh>
#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

View file

@ -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<Engine::Vector3> vertexes{};
std::vector<Engine::Vector3> normals{};
std::vector<Engine::Vector3> textureCoords{};
std::vector<engine::Vector3> vertexes{};
std::vector<engine::Vector3> normals{};
std::vector<engine::Vector3> 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 = "./";

View file

@ -4,13 +4,13 @@
#include <tiny_obj_loader.h>
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

View file

@ -1,5 +1,6 @@
#ifndef CONTROLLER_HH
#define CONTROLLER_HH
namespace engine {
class Controller {
public:
enum Button {
@ -22,4 +23,5 @@ class Controller {
protected:
virtual int GetButtonMask(Button button) const { return 0; }
};
}
#endif

View file

@ -4,7 +4,9 @@
#include <fstream>
#include <sstream>
bool DesktopController::InitializeController() {
namespace engine {
bool DesktopController::InitializeController()
{
std::ifstream gameControllerDB;
gameControllerDB.open("./gamecontrollerdb.txt");
if (gameControllerDB.is_open()) {
@ -20,23 +22,28 @@ 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;
@ -56,3 +63,4 @@ int DesktopController::GetButtonMask(Button button) const {
return GLFW_GAMEPAD_BUTTON_DPAD_RIGHT;
}
}
}

View file

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

View file

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

View file

@ -3,6 +3,7 @@
#include <kos.h>
#include "controller.hh"
namespace engine {
class DreamcastController : public Controller {
public:
DreamcastController();
@ -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

View file

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

View file

@ -3,12 +3,14 @@
#include "nativeEngine.hh"
#include <GLFW/glfw3.h>
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

View file

@ -1,28 +1,33 @@
#ifdef _arch_dreamcast
#include "dreamcastEngine.hh"
#include <stdexcept>
#include <GL/glkos.h>
#include "controller/dreamcastController.hh"
void DreamcastEngine::initializeEngine() {
#include <GL/glkos.h>
#include <stdexcept>
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

View file

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

View file

@ -14,27 +14,31 @@
#include <cstring>
#include <functional>
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 <typename T>
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<void()> callback) {
if (controller->IsButtonPressed(button)) {
if (!(buttonsPressed & button)) {
buttonsPressed |= button;
void Engine::PressButton(Controller::Button button,
std::function<void()> 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 {
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 {
Vector3 Vector3::operator+(Vector3 vector) const
{
return { this->x + vector.x, this->y + vector.y, this->z + vector.z };
}
};

View file

@ -14,42 +14,45 @@
#include <GLFW/glfw3.h>
#endif
namespace engine {
class Engine {
protected:
static constexpr float microSecond = 0.000001f;
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;
NativeEngine* p_engine = nullptr;
template <typename T>
void deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame,
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<void()> callback);
void InitializeController();
void InitializeEngine();
void InitializeGameLoop();
void CleanupEngine();
float GetDeltaTime() const;
void PressButton(Controller::Button button, std::function<void()> callback);
virtual ~Engine();
Controller* controller = nullptr;
Controller* p_controller = nullptr;
};
struct Vector3 {
public:
Vector3 operator/(float amount) const;
void operator/=(float amount);
Vector3 operator+(Vector3 vector) const;

View file

@ -1,81 +1,94 @@
#include "entity.hh"
#include <cstdio>
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;
}
}

View file

@ -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);
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<Entity*> 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);
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

View file

@ -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 InitializeController() {};
virtual void InitializeEngine() {};
virtual void SwapBuffers() {};
virtual bool ShouldWindowClose() { return false; };
virtual ~NativeEngine() {};
Controller* controller = nullptr;
Controller* p_controller = nullptr;
};
}
#endif

View file

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

View file

@ -2,10 +2,10 @@
#include <cstdio>
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;
}

View file

@ -3,17 +3,17 @@
#include <engine/entity.hh>
#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

View file

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

View file

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

View file

@ -1,12 +1,14 @@
#include "MainMenu.hh"
#include <engine/controller/controller.hh>
#include <cstdio>
#include <engine/controller/controller.hh>
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;
});
}

View file

@ -2,12 +2,12 @@
#define MAIN_MENU_HH
#include <engine/entity.hh>
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

View file

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

View file

@ -2,25 +2,25 @@
#define PIPES_HH
#include <engine/entity.hh>
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

View file

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

View file

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

View file

@ -3,12 +3,12 @@
#include <engine/engine.hh>
#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

View file

@ -5,27 +5,27 @@
#include <kos.h>
#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();
}

View file

@ -1,15 +1,15 @@
#ifndef GL_ENTITY_HH
#define GL_ENTITY_HH
#include <engine/entity.hh>
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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,9 +1,9 @@
#ifndef HELLO_ENTITY_HH
#define HELLO_ENTITY_HH
#include <engine/entity.hh>
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