129 lines
2.8 KiB
C++
129 lines
2.8 KiB
C++
#include <GL/gl.h>
|
|
#include <GL/glkos.h>
|
|
#include <kos.h>
|
|
#include <sys/time.h>
|
|
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
|
|
const float microSecond = 0.000001f;
|
|
|
|
float angle = 0;
|
|
float deltaTime = 1.0 / 60.0;
|
|
float speed = 5.0f;
|
|
|
|
bool thirtyfps;
|
|
uint32 buttonsPressed;
|
|
|
|
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() {
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
|
|
}
|
|
|
|
void displayStuff() {
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
// clang-format off
|
|
glPushMatrix();
|
|
glRotatef(angle, 0, 0, 1);
|
|
glColor3f(0.5f, 0.0f, 1.0f);
|
|
glBegin(GL_POLYGON);
|
|
glColor3f(1.0f, 0.0f, 0.0f);
|
|
glVertex3f(0.25f, 0.25f, 0.0f);
|
|
|
|
glColor3f(0.0f, 1.0f, 0.0f);
|
|
glVertex3f(0.75f, 0.25f, 0.0f);
|
|
|
|
glColor3f(0.0f, 0.0f, 1.0f);
|
|
glVertex3f(0.75f, 0.75f, 0.0f);
|
|
|
|
glColor3f(1.0f, 1.0f, 1.0f);
|
|
glVertex3f(0.25f, 0.75f, 0.0f);
|
|
glEnd();
|
|
glPopMatrix();
|
|
// clang-format on
|
|
|
|
if (thirtyfps) {
|
|
vid_waitvbl();
|
|
vid_waitvbl();
|
|
}
|
|
|
|
glKosSwapBuffers();
|
|
}
|
|
|
|
void process_input() {
|
|
maple_device_t* controller;
|
|
cont_state_t* controller_state;
|
|
|
|
controller = maple_enum_type(0, MAPLE_FUNC_CONTROLLER);
|
|
|
|
if (controller) {
|
|
controller_state = (cont_state_t*)maple_dev_status(controller);
|
|
|
|
if (!controller_state) {
|
|
printf(
|
|
"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; });
|
|
|
|
speed = std::clamp(speed, 1.0f, 10.0f);
|
|
|
|
angle += (controller_state->joyx / 127.0f) * deltaTime * speed;
|
|
}
|
|
}
|
|
|
|
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() {
|
|
glKosInit();
|
|
initScreen();
|
|
printSystemInformation();
|
|
struct timeval beginningOfFrame, endOfFrame;
|
|
|
|
while (1) {
|
|
deltaTimeLoop(gameLoop, beginningOfFrame, endOfFrame);
|
|
}
|
|
}
|