From e98856a8749c5256a72e124809091a76a7b1c0f4 Mon Sep 17 00:00:00 2001 From: Fries Date: Mon, 11 Mar 2024 22:41:34 -0700 Subject: [PATCH] Convert gl.c to c++ and make it nicer. --- .clang-format | 4 ++ .clangd | 4 ++ .vscode/launch.json | 33 ++++++++++ .vscode/settings.json | 5 +- dreamcast-cross-file.txt | 10 +-- gl.c | 85 -------------------------- gl.cc | 129 +++++++++++++++++++++++++++++++++++++++ meson.build | 4 +- 8 files changed, 181 insertions(+), 93 deletions(-) create mode 100644 .clang-format create mode 100644 .clangd create mode 100644 .vscode/launch.json delete mode 100644 gl.c create mode 100644 gl.cc diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..d479040 --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +BasedOnStyle: Google +UseTab: Always +IndentWidth: 4 +TabWidth: 4 diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..bfa0497 --- /dev/null +++ b/.clangd @@ -0,0 +1,4 @@ +Diagnostics: + Suppress: [pp_file_not_found] +CompileFlags: + Add: [-D_arch_dreamcast, -std=c++20] diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..a0a74ee --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,33 @@ +{ + "configurations": [ + { + "name": "(gdb) Launch gl.elf", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/gl.elf", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "/opt/toolchains/dc/sh-elf/bin/sh-elf-gdb", + "miDebuggerServerAddress": "localhost:3263", + "debugServerPath": "/usr/bin/flycast", + "debugServerArgs": "${workspaceFolder}/build/gl.elf", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set SuperH architechture", + "text": "set architecture sh4" + } + ], + "avoidWindowsConsoleRedirection": false, + "internalConsoleOptions": "openOnSessionStart" + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json index 0150950..ffa95f9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,5 +19,8 @@ "args": ["--init-file", "${workspaceFolder}/.vscode/kos_environ.sh" ], "overrideName": true } - } + }, + "clangd.arguments": [ + "--query-driver=\"/opt/toolchains/dc/kos/utils/gnu_wrappers/kos-cc,kos-cc\"" + ] } diff --git a/dreamcast-cross-file.txt b/dreamcast-cross-file.txt index 866c8b7..2080752 100644 --- a/dreamcast-cross-file.txt +++ b/dreamcast-cross-file.txt @@ -1,8 +1,8 @@ [binaries] -c = 'kos-cc' -cpp = 'kos-c++' -ar = 'kos-ar' -strip = 'kos-strip' +c = '/opt/toolchains/dc/kos/utils/gnu_wrappers/kos-cc' +cpp = '/opt/toolchains/dc/kos/utils/gnu_wrappers/kos-c++' +ar = '/opt/toolchains/dc/kos/utils/gnu_wrappers/kos-ar' +strip = '/opt/toolchains/dc/kos/utils/gnu_wrappers/kos-strip' [properties] sizeof_int = 4 @@ -17,7 +17,7 @@ has_function_printf = true sys_root = '/opt/toolchains/dc/sh-elf/' [built-in options] -c_args = ['-D_arch_dreamcast', '-D_arch_sub_pristine', '-ml', '-m4-single-only', '-ffunction-sections', '-fdata-sections', '-s', '-fno-builtin', '-fno-stack-protector' ] +c_args = ['-D_arch_dreamcast', '-D_arch_sub_pristine', '-ml', '-m4-single-only', '-ffunction-sections', '-fdata-sections', '-s', '-fno-builtin', '-fno-stack-protector'] [host_machine] system = 'dreamcast' diff --git a/gl.c b/gl.c deleted file mode 100644 index 2c581a0..0000000 --- a/gl.c +++ /dev/null @@ -1,85 +0,0 @@ -#include -#include -#include -#include -#include -#include - -float angle = 0; -float deltaTime = 1.0/60.0; -const float microSecond = 0.000001f; -float speed = 5.0f; - -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); - - glPushMatrix(); - glRotatef(angle, 0, 0, 1); - glColor3f(1.0f, 1.0f, 1.0f); - glBegin(GL_POLYGON); - glVertex3f(0.25f, 0.25f, 0.0f); - glVertex3f(0.75f, 0.25f, 0.0f); - glVertex3f(0.75f, 0.75f, 0.0f); - glVertex3f(0.25f, 0.75f, 0.0f); - glEnd(); - glPopMatrix(); - - // glFlush(); - 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"); - } - - if (controller_state->buttons & CONT_DPAD_RIGHT) { - speed += 1.0f; - } else if (controller_state->buttons & CONT_DPAD_LEFT) { - speed -= 1.0f; - } - - if (speed < 1.0f) { - speed = 1.0f; - } - - angle += (controller_state->joyx / 127.0f) * deltaTime * speed; - } -} - -int main() { - glKosInit(); - initScreen(); - 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)); - struct timeval beginningOfFrame, endOfFrame; - - while (1) { - gettimeofday(&beginningOfFrame, 0); - process_input(); - displayStuff(); - gettimeofday(&endOfFrame, 0); - float time = ((float)(endOfFrame.tv_usec) * microSecond) - ((float)(beginningOfFrame.tv_usec) * microSecond); - if (time > 0.0f) { - deltaTime = time; - } - } -} diff --git a/gl.cc b/gl.cc new file mode 100644 index 0000000..93c1c2f --- /dev/null +++ b/gl.cc @@ -0,0 +1,129 @@ +#include +#include +#include +#include + +#include +#include + +const float microSecond = 0.000001f; + +float angle = 0; +float deltaTime = 1.0 / 60.0; +float speed = 5.0f; + +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 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 +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); + } +} diff --git a/meson.build b/meson.build index 917a694..32f5a2d 100644 --- a/meson.build +++ b/meson.build @@ -1,4 +1,4 @@ -project('dreamcast-opengl', 'c') +project('dreamcast-opengl', 'c', 'cpp', default_options: ['cpp_std=c++20']) cc = meson.get_compiler('c') @@ -25,4 +25,4 @@ incdirs = [ ] executable('hello.elf', ['hello.c'], dependencies: deps, include_directories: incdirs) -executable('gl.elf', ['gl.c'], dependencies: deps, include_directories: incdirs) +executable('gl.elf', ['gl.cc'], dependencies: deps, include_directories: incdirs)