Create an abstracted engine class for the programs.

This commit is contained in:
Fries 2024-03-13 12:53:01 -07:00
parent f919b166df
commit 3822973d6a
6 changed files with 165 additions and 130 deletions

73
engine.cc Normal file
View file

@ -0,0 +1,73 @@
#include "engine.hh"
#include <GL/gl.h>
#include <GL/glkos.h>
#include <cstdio>
#include <functional>
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 <typename T>
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<void()> 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);
}

36
engine.hh Normal file
View file

@ -0,0 +1,36 @@
#ifndef ENGINE_HH
#define ENGINE_HH
#include <kos.h>
#include <sys/time.h>
#include <functional>
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<void()> callback);
void glVertex3fNormalized(float x, float y, float z);
private:
template <typename T>
void deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame,
struct timeval& endOfFrame);
public:
void initializeEngine();
void initializeGameLoop();
};
#endif

98
gl.cc
View file

@ -1,53 +1,22 @@
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glkos.h> #include <GL/glkos.h>
#include <kos.h>
#include <sys/time.h>
#include <algorithm> #include <algorithm>
#include <cstdio> #include <cstdio>
const float microSecond = 0.000001f; #include "engine.hh"
float angle = 0; class Glcc : public Engine {
float deltaTime = 1.0 / 60.0; void process_input();
float speed = 5.0f; void displayStuff();
bool thirtyfps; void gameLoop() override {
uint32 buttonsPressed; process_input();
displayStuff();
template <typename T>
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 initScreen() { void Glcc::displayStuff() {
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() {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
// clang-format off // clang-format off
@ -78,7 +47,7 @@ void displayStuff() {
glKosSwapBuffers(); glKosSwapBuffers();
} }
void process_input() { void Glcc::process_input() {
maple_device_t* controller; maple_device_t* controller;
cont_state_t* controller_state; cont_state_t* controller_state;
@ -89,12 +58,15 @@ void process_input() {
if (!controller_state) { if (!controller_state) {
printf( 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_LEFT, [&]() { speed -= 1.0f; });
pressButton(controller_state, CONT_DPAD_RIGHT, []() { speed += 1.0f; }); pressButton(controller_state, CONT_DPAD_RIGHT,
pressButton(controller_state, CONT_A, []() { thirtyfps = !thirtyfps; }); [&]() { speed += 1.0f; });
pressButton(controller_state, CONT_A,
[&]() { thirtyfps = !thirtyfps; });
speed = std::clamp(speed, 1.0f, 10.0f); 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 <typename T>
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() { int main() {
glKosInit(); Glcc* engine = new Glcc;
initScreen(); engine->initializeEngine();
printSystemInformation(); engine->initializeGameLoop();
struct timeval beginningOfFrame, endOfFrame;
while (1) {
deltaTimeLoop(gameLoop, beginningOfFrame, endOfFrame);
}
} }

50
hello.c
View file

@ -1,50 +0,0 @@
#include <kos.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glkos.h>
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;
}

34
hello.cc Normal file
View file

@ -0,0 +1,34 @@
#include <GL/gl.h>
#include <GL/glkos.h>
#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();
}

View file

@ -24,5 +24,5 @@ incdirs = [
kosportsinc kosportsinc
] ]
executable('hello.elf', ['hello.c'], dependencies: deps, include_directories: incdirs) executable('hello.elf', ['hello.cc', 'engine.cc'], dependencies: deps, include_directories: incdirs)
executable('gl.elf', ['gl.cc'], dependencies: deps, include_directories: incdirs) executable('gl.elf', ['gl.cc', 'engine.cc'], dependencies: deps, include_directories: incdirs)