diff --git a/engine.cc b/engine.cc new file mode 100644 index 0000000..26d2c67 --- /dev/null +++ b/engine.cc @@ -0,0 +1,73 @@ +#include "engine.hh" + +#include +#include + +#include +#include + +void Engine::initScreen() { + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glViewport(0, 0, 640, 480); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glOrtho(0, 640, 0, 480, -100, 100); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); +} + +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)); + printf("GL_EXTENSIONS: %s\n", glGetString(GL_EXTENSIONS)); +} + +void Engine::initializeEngine() { + glKosInit(); + initScreen(); + printSystemInformation(); +} + +template +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); + if (time > 0.0f) { + deltaTime = time; + } +} + +void Engine::initializeGameLoop() { + struct timeval beginningOfFrame, endOfFrame; + + while (1) { + deltaTimeLoop(&Engine::gameLoop, beginningOfFrame, endOfFrame); + } +} + +void Engine::pressButton(cont_state_t* controller_state, int button, + std::function callback) { + if (controller_state->buttons & button) { + if (!(buttonsPressed & button)) { + buttonsPressed |= button; + + callback(); + } + } else if ((buttonsPressed & button) && + ~(controller_state->buttons & button)) { + buttonsPressed &= ~button; + } +} + +void Engine::glVertex3fNormalized(float x, float y, float z) { + int xSize, ySize; + xSize = 640; + ySize = 480; + + glVertex3f(x * xSize, y * ySize, z); +} diff --git a/engine.hh b/engine.hh new file mode 100644 index 0000000..3d4e0f4 --- /dev/null +++ b/engine.hh @@ -0,0 +1,36 @@ +#ifndef ENGINE_HH +#define ENGINE_HH + +#include +#include + +#include + +class Engine { + protected: + const float microSecond = 0.000001f; + + float angle = 0; + float deltaTime = 1.0 / 60.0; + float speed = 5.0f; + + bool thirtyfps; + unsigned int buttonsPressed; + + void initScreen(); + void printSystemInformation(); + virtual void gameLoop(){}; + void pressButton(cont_state_t* controller_state, int button, + std::function callback); + void glVertex3fNormalized(float x, float y, float z); + + private: + template + void deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame, + struct timeval& endOfFrame); + + public: + void initializeEngine(); + void initializeGameLoop(); +}; +#endif diff --git a/gl.cc b/gl.cc index 9c0dacb..8d480c4 100644 --- a/gl.cc +++ b/gl.cc @@ -1,53 +1,22 @@ #include #include -#include -#include #include #include -const float microSecond = 0.000001f; +#include "engine.hh" -float angle = 0; -float deltaTime = 1.0 / 60.0; -float speed = 5.0f; +class Glcc : public Engine { + void process_input(); + void displayStuff(); -bool thirtyfps; -uint32 buttonsPressed; - -template -void pressButton(cont_state_t* controller_state, int button, T&& callback) { - if (controller_state->buttons & button) { - if (!(buttonsPressed & button)) { - buttonsPressed |= button; - - callback(); - } - } else if ((buttonsPressed & button) && - ~(controller_state->buttons & button)) { - buttonsPressed &= ~button; + void gameLoop() override { + process_input(); + displayStuff(); } -} +}; -void initScreen() { - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - glViewport(0, 0, 640, 480); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glOrtho(0, 640, 0, 480, -100, 100); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); -} - -void glVertex3fNormalized(float x, float y, float z) { - int xSize, ySize; - xSize = 640; - ySize = 480; - - glVertex3f(x * xSize, y * ySize, z); -} - -void displayStuff() { +void Glcc::displayStuff() { glClear(GL_COLOR_BUFFER_BIT); // clang-format off @@ -78,7 +47,7 @@ void displayStuff() { glKosSwapBuffers(); } -void process_input() { +void Glcc::process_input() { maple_device_t* controller; cont_state_t* controller_state; @@ -89,12 +58,15 @@ void process_input() { if (!controller_state) { printf( - "An error has occured while trying to read the controller.\n"); + "An error has occured while trying to read the " + "controller.\n"); } - pressButton(controller_state, CONT_DPAD_LEFT, []() { speed -= 1.0f; }); - pressButton(controller_state, CONT_DPAD_RIGHT, []() { speed += 1.0f; }); - pressButton(controller_state, CONT_A, []() { thirtyfps = !thirtyfps; }); + pressButton(controller_state, CONT_DPAD_LEFT, [&]() { speed -= 1.0f; }); + pressButton(controller_state, CONT_DPAD_RIGHT, + [&]() { speed += 1.0f; }); + pressButton(controller_state, CONT_A, + [&]() { thirtyfps = !thirtyfps; }); speed = std::clamp(speed, 1.0f, 10.0f); @@ -102,38 +74,8 @@ void process_input() { } } -void 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)); - printf("GL_EXTENSIONS: %s\n", glGetString(GL_EXTENSIONS)); -} - -template -void deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame, - struct timeval& endOfFrame) { - gettimeofday(&beginningOfFrame, 0); - callback(); - gettimeofday(&endOfFrame, 0); - float time = ((float)(endOfFrame.tv_usec) * microSecond) - - ((float)(beginningOfFrame.tv_usec) * microSecond); - if (time > 0.0f) { - deltaTime = time; - } -} - -void gameLoop() { - process_input(); - displayStuff(); -} - int main() { - glKosInit(); - initScreen(); - printSystemInformation(); - struct timeval beginningOfFrame, endOfFrame; - - while (1) { - deltaTimeLoop(gameLoop, beginningOfFrame, endOfFrame); - } + Glcc* engine = new Glcc; + engine->initializeEngine(); + engine->initializeGameLoop(); } diff --git a/hello.c b/hello.c deleted file mode 100644 index d9b31e8..0000000 --- a/hello.c +++ /dev/null @@ -1,50 +0,0 @@ -#include - -#include -#include -#include - -void triangle() { - // glClearColor(1.0f, 1.0f, 0.0f, 0.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glBegin(GL_TRIANGLES); - glColor3f(1.0f, 0.0f, 0.0f); - glVertex3f(-1.0f, -1.0f, 0.0f); - - glColor3f(0.0f, 1.0f, 0.0f); - glVertex3f(0.0f, 1.0f, 0.0f); - - glColor3f(0.0f, 0.0f, 1.0f); - glVertex3f(1.0f, -1.0f, 0.0f); - glEnd(); -} - -int main(int argc, char *argv[]) { - maple_device_t *controller; - - glKosInit(); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); - // gluPerspective(45.0f, 640.0f/480.0f, 0.1f, 100.0f); - // glMatrixMode(GL_MODELVIEW); - // glEnable(GL_TEXTURE_2D); - // glEnable(GL_DEPTH_TEST); - - // glFrontFace(GL_CW); - // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - glClearColor(0.3f, 0.4f, 0.5f, 1.0f); - - while (1) { - // glLoadIdentity(); - // glTranslatef(-5.0f, 0.0f, 0.0f); - // glEnable(GL_CULL_FACE); - triangle(); - - glKosSwapBuffers(); - } - - return 0; -} diff --git a/hello.cc b/hello.cc new file mode 100644 index 0000000..5ef55ab --- /dev/null +++ b/hello.cc @@ -0,0 +1,34 @@ +#include +#include +#include "engine.hh" + +class Hello : public Engine { + void triangle(); + + void gameLoop() override { + triangle(); + glKosSwapBuffers(); + } +}; + +void Hello::triangle() { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glBegin(GL_TRIANGLES); + glColor3f(1.0f, 0.0f, 0.0f); + glVertex3fNormalized(-1.0f, -1.0f, 0.0f); + + glColor3f(0.0f, 1.0f, 0.0f); + glVertex3fNormalized(0.0f, 1.0f, 0.0f); + + glColor3f(0.0f, 0.0f, 1.0f); + glVertex3fNormalized(1.0f, -1.0f, 0.0f); + glEnd(); +} + + +int main(int argc, char *argv[]) { + Hello* engine = new Hello; + engine->initializeEngine(); + engine->initializeGameLoop(); +} diff --git a/meson.build b/meson.build index 32f5a2d..a6a3a2a 100644 --- a/meson.build +++ b/meson.build @@ -24,5 +24,5 @@ incdirs = [ kosportsinc ] -executable('hello.elf', ['hello.c'], dependencies: deps, include_directories: incdirs) -executable('gl.elf', ['gl.cc'], dependencies: deps, include_directories: incdirs) +executable('hello.elf', ['hello.cc', 'engine.cc'], dependencies: deps, include_directories: incdirs) +executable('gl.elf', ['gl.cc', 'engine.cc'], dependencies: deps, include_directories: incdirs)