From 4d4582831c3b3bfde6786abfa5c6ca042c20a977 Mon Sep 17 00:00:00 2001 From: Fries Date: Sun, 10 Mar 2024 21:39:58 -0700 Subject: [PATCH] Make some Dreamcast OpenGL programs. --- .gitignore | 2 + .vscode/kos_environ.sh | 4 ++ .vscode/kos_make.sh | 8 ++++ .vscode/settings.json | 23 +++++++++++ .vscode/tasks.json | 17 ++++++++ dreamcast-cross-file.txt | 26 ++++++++++++ gl.c | 85 ++++++++++++++++++++++++++++++++++++++++ hello.c | 50 +++++++++++++++++++++++ meson.build | 28 +++++++++++++ 9 files changed, 243 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/kos_environ.sh create mode 100644 .vscode/kos_make.sh create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 dreamcast-cross-file.txt create mode 100644 gl.c create mode 100644 hello.c create mode 100644 meson.build diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a6c2198 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.cache +/build diff --git a/.vscode/kos_environ.sh b/.vscode/kos_environ.sh new file mode 100644 index 0000000..8dc1282 --- /dev/null +++ b/.vscode/kos_environ.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +#set the KOS environtment variables +source /opt/toolchains/dc/kos/environ.sh diff --git a/.vscode/kos_make.sh b/.vscode/kos_make.sh new file mode 100644 index 0000000..c94d677 --- /dev/null +++ b/.vscode/kos_make.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +#set the KOS environtment variables +source /opt/toolchains/dc/kos/environ.sh + +make + +exit diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0150950 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,23 @@ +{ + "C_Cpp.default.includePath": [ + "${workspaceFolder}/**", + "/opt/toolchains/dc/kos/**", + "/opt/toolchains/dc/kos-ports/**" + ], + "C_Cpp.default.defines": [ + "_arch_dreamcast" + ], + "C_Cpp.default.compilerPath": "/opt/toolchains/dc/sh-elf/bin/sh-elf-gcc", + "C_Cpp.default.cStandard": "c11", + "C_Cpp.default.cppStandard": "c++17", + "C_Cpp.default.intelliSenseMode": "", + + "terminal.integrated.defaultProfile.linux": "kos-bash", + "terminal.integrated.profiles.linux": { + "kos-bash": { + "path": "/usr/bin/bash", + "args": ["--init-file", "${workspaceFolder}/.vscode/kos_environ.sh" ], + "overrideName": true + } + } +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..b95ba77 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,17 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Build", + "type": "process", + "command": "bash", + "args": [ + "--init-file", "${workspaceFolder}/.vscode/kos_make.sh" + ], + "group": { + "kind": "build", + "isDefault": true + }, + }, + ] +} diff --git a/dreamcast-cross-file.txt b/dreamcast-cross-file.txt new file mode 100644 index 0000000..866c8b7 --- /dev/null +++ b/dreamcast-cross-file.txt @@ -0,0 +1,26 @@ +[binaries] +c = 'kos-cc' +cpp = 'kos-c++' +ar = 'kos-ar' +strip = 'kos-strip' + +[properties] +sizeof_int = 4 +sizeof_wchar_t = 4 +sizeof_void* = 4 + +alignment_char = 1 +alignment_void* = 4 +alignment_double = 4 + +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' ] + +[host_machine] +system = 'dreamcast' +cpu_family = 'sh4' +cpu = 'sh4' +endian = 'little' diff --git a/gl.c b/gl.c new file mode 100644 index 0000000..2c581a0 --- /dev/null +++ b/gl.c @@ -0,0 +1,85 @@ +#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/hello.c b/hello.c new file mode 100644 index 0000000..d9b31e8 --- /dev/null +++ b/hello.c @@ -0,0 +1,50 @@ +#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/meson.build b/meson.build new file mode 100644 index 0000000..917a694 --- /dev/null +++ b/meson.build @@ -0,0 +1,28 @@ +project('dreamcast-opengl', 'c') + +cc = meson.get_compiler('c') + +kosinc = include_directories('/opt/toolchains/dc/kos/include') +koskernelinc = include_directories('/opt/toolchains/dc/kos/kernel/arch/dreamcast/include') +kosaddonsinc = include_directories('/opt/toolchains/dc/kos/addons/include') +kosportsinc = include_directories('/opt/toolchains/dc/kos-ports/include') + +GL = cc.find_library('GL', required: true) +math = cc.find_library('m', required: true) +# pcx = cc.find_library('pcx', required: true) +# kosutils = cc.find_library('kosutils', required: true) + +deps = [ + GL, + math +] + +incdirs = [ + kosinc, + koskernelinc, + kosaddonsinc, + kosportsinc +] + +executable('hello.elf', ['hello.c'], dependencies: deps, include_directories: incdirs) +executable('gl.elf', ['gl.c'], dependencies: deps, include_directories: incdirs)