From 71e6079054495976c698ecdefe87b0392a11b575 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sat, 7 Mar 2026 12:09:40 -0600 Subject: [PATCH] More code moving --- cmake/targets/linux.cmake | 1 + src/dusk/display/display.c | 6 +- src/dusk/display/framebuffer/framebuffer.c | 3 +- src/dusk/display/framebuffer/framebuffer.h | 3 +- src/dusk/display/mesh/mesh.c | 93 ++++++------------- src/dusk/display/mesh/mesh.h | 57 ++++++------ src/dusk/display/mesh/meshvertex.h | 19 ++++ src/dusk/display/mesh/quad.c | 7 +- src/dusk/display/mesh/quad.h | 6 +- src/dusk/display/screen/screen.h | 10 +- src/dusk/display/texture/CMakeLists.txt | 1 - src/dusk/display/texture/palette.c | 43 --------- src/dusk/display/texture/palette.h | 59 ------------ src/dusk/display/texture/palettetexture.c | 51 ---------- src/dusk/display/texture/palettetexture.h | 28 ------ src/dusk/display/texture/texture.h | 1 - src/duskgl/display/CMakeLists.txt | 3 +- src/duskgl/display/framebuffer/CMakeLists.txt | 1 + src/duskgl/display/mesh/CMakeLists.txt | 10 ++ src/duskgl/display/mesh/meshgl.c | 67 +++++++++++++ src/duskgl/display/mesh/meshgl.h | 66 +++++++++++++ src/duskgl/display/mesh/meshplatform.h | 17 ++++ 22 files changed, 261 insertions(+), 291 deletions(-) create mode 100644 src/dusk/display/mesh/meshvertex.h delete mode 100644 src/dusk/display/texture/palette.c delete mode 100644 src/dusk/display/texture/palette.h delete mode 100644 src/dusk/display/texture/palettetexture.c delete mode 100644 src/dusk/display/texture/palettetexture.h create mode 100644 src/duskgl/display/mesh/CMakeLists.txt create mode 100644 src/duskgl/display/mesh/meshgl.c create mode 100644 src/duskgl/display/mesh/meshgl.h create mode 100644 src/duskgl/display/mesh/meshplatform.h diff --git a/cmake/targets/linux.cmake b/cmake/targets/linux.cmake index 3bb1a48..830edaf 100644 --- a/cmake/targets/linux.cmake +++ b/cmake/targets/linux.cmake @@ -17,4 +17,5 @@ target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC DUSK_DISPLAY_SIZE_DYNAMIC DUSK_DISPLAY_WIDTH_DEFAULT=640 DUSK_DISPLAY_HEIGHT_DEFAULT=480 + DUSK_DISPLAY_SCREEN_HEIGHT=240 ) \ No newline at end of file diff --git a/src/dusk/display/display.c b/src/dusk/display/display.c index 1c925dd..d7b3df8 100644 --- a/src/dusk/display/display.c +++ b/src/dusk/display/display.c @@ -29,8 +29,8 @@ errorret_t displayInit(void) { errorChain(displayPlatformInit()); #endif - quadInit(); - frameBufferInitBackbuffer(); + errorChain(quadInit()); + errorChain(frameBufferInitBackbuffer()); spriteBatchInit(); errorChain(textInit()); errorChain(assetLoad("main_palette.dpf", &PALETTES[0])); @@ -46,7 +46,7 @@ errorret_t displayUpdate(void) { // Reset state spriteBatchClear(); - frameBufferBind(NULL); + errorChain(frameBufferBind(NULL)); // Bind screen and render scene screenBind(); diff --git a/src/dusk/display/framebuffer/framebuffer.c b/src/dusk/display/framebuffer/framebuffer.c index 4c1487e..0edec96 100644 --- a/src/dusk/display/framebuffer/framebuffer.c +++ b/src/dusk/display/framebuffer/framebuffer.c @@ -17,9 +17,10 @@ errorret_t frameBufferInitBackbuffer() { memoryZero(&FRAMEBUFFER_BACKBUFFER, sizeof(framebuffer_t)); errorChain(frameBufferPlatformInitBackbuffer()); FRAMEBUFFER_BOUND = &FRAMEBUFFER_BACKBUFFER; + errorOk(); } -void frameBufferBind(framebuffer_t *framebuffer) { +errorret_t frameBufferBind(framebuffer_t *framebuffer) { if(framebuffer == NULL) { frameBufferBind(&FRAMEBUFFER_BACKBUFFER); FRAMEBUFFER_BOUND = &FRAMEBUFFER_BACKBUFFER; diff --git a/src/dusk/display/framebuffer/framebuffer.h b/src/dusk/display/framebuffer/framebuffer.h index 0a33ee7..f8fb7df 100644 --- a/src/dusk/display/framebuffer/framebuffer.h +++ b/src/dusk/display/framebuffer/framebuffer.h @@ -54,8 +54,9 @@ uint32_t frameBufferGetHeight(const framebuffer_t *framebuffer); * provided is NULL. * * @param framebuffer The framebuffer to bind, or NULL to bind the backbuffer. + * @return Error for binding the framebuffer. */ -void frameBufferBind(const framebuffer_t *framebuffer); +errorret_t frameBufferBind(const framebuffer_t *framebuffer); /** * Clears the currently bound framebuffer. diff --git a/src/dusk/display/mesh/mesh.c b/src/dusk/display/mesh/mesh.c index d8459ce..230c616 100644 --- a/src/dusk/display/mesh/mesh.c +++ b/src/dusk/display/mesh/mesh.c @@ -13,7 +13,7 @@ #include "display/texture.h" #endif -void meshInit( +errorret_t meshInit( mesh_t *mesh, const meshprimitivetype_t primitiveType, const int32_t vertexCount, @@ -25,80 +25,45 @@ void meshInit( memoryZero(mesh, sizeof(mesh_t)); - mesh->primitiveType = primitiveType; - mesh->vertexCount = vertexCount; - mesh->vertices = vertices; + errorChain(meshInitPlatform(mesh, primitiveType, vertexCount, vertices)); + errorOk(); } -void meshDraw( +errorret_t meshDraw( const mesh_t *mesh, const int32_t vertexOffset, const int32_t vertexCount ) { - const int32_t offset = vertexOffset == -1 ? 0 : vertexOffset; - const int32_t count = vertexCount == -1 ? mesh->vertexCount : vertexCount; - assertNotNull(mesh, "Mesh cannot be NULL"); - assertTrue(offset >= 0, "Vertex offset must be non-negative"); - assertTrue(count >= 0, "Vertex count must be non-negative"); - assertTrue(offset + count <= mesh->vertexCount, - "Vertex offset + count must not exceed vertex count" + assertTrue(vertexOffset >= 0, "Vertex offset must be non-negative"); + assertTrue(vertexCount >= -1, "Vertex count must be -1 or non-negative"); + + int32_t vertDrawCount = vertexCount; + if(vertexCount == -1) { + const int32_t totalVertices = meshGetVertexCount(mesh); + vertDrawCount = totalVertices - vertexOffset; + } + + if(vertDrawCount == 0) { + errorOk(); + } + assertTrue( + vertexOffset + vertDrawCount <= meshGetVertexCount(mesh), + "Vertex offset and count must be within vertex count bounds" ); - #if DISPLAY_SDL2 - // PSP style pointer legacy OpenGL - const GLsizei stride = sizeof(meshvertex_t); - - glColorPointer( - sizeof(color4b_t), - GL_UNSIGNED_BYTE, - stride, - (const GLvoid*)&mesh->vertices[offset].color - ); - glTexCoordPointer( - MESH_VERTEX_UV_SIZE, - GL_FLOAT, - stride, - (const GLvoid*)&mesh->vertices[offset].uv[0] - ); - glVertexPointer( - MESH_VERTEX_POS_SIZE, - GL_FLOAT, - stride, - (const GLvoid*)&mesh->vertices[offset].pos[0] - ); - - glDrawArrays( - mesh->primitiveType, - 0, - count - ); - - #elif DOLPHIN - // Prepare Vertex descriptor - DCFlushRange( - (void*)&mesh->vertices[offset], - sizeof(meshvertex_t) * count - ); - - const u8 stride = (u8)sizeof(meshvertex_t); - GX_SetArray(GX_VA_POS, (void*)&mesh->vertices[offset].pos[0], stride); - GX_SetArray(GX_VA_CLR0, (void*)&mesh->vertices[offset].color, stride); - GX_SetArray(GX_VA_TEX0, (void*)&mesh->vertices[offset].uv[0], stride); - - textureDolphinUploadTEV(); - - GX_Begin(mesh->primitiveType, GX_VTXFMT0, (uint16_t)count); - for(u16 i = 0; i < (u16)count; ++i) { - GX_Position1x16(i); - GX_Color1x16(i); - GX_TexCoord1x16(i); - } - GX_End(); - #endif + errorChain(meshDrawPlatform(mesh, vertexOffset, vertDrawCount)); + errorOk(); } -void meshDispose(mesh_t *mesh) { +int32_t meshGetVertexCount(const mesh_t *mesh) { assertNotNull(mesh, "Mesh cannot be NULL"); + return meshGetVertexCountPlatform(mesh); +} + +errorret_t meshDispose(mesh_t *mesh) { + assertNotNull(mesh, "Mesh cannot be NULL"); + errorChain(meshDisposePlatform(mesh)); memoryZero(mesh, sizeof(mesh_t)); + errorOk(); } \ No newline at end of file diff --git a/src/dusk/display/mesh/mesh.h b/src/dusk/display/mesh/mesh.h index f36f1d4..b580f94 100644 --- a/src/dusk/display/mesh/mesh.h +++ b/src/dusk/display/mesh/mesh.h @@ -6,33 +6,23 @@ #pragma once #include "display/display.h" #include "display/color.h" +#include "display/mesh/meshplatform.h" -typedef enum { - #if DISPLAY_SDL2 - MESH_PRIMITIVE_TRIANGLES = GL_TRIANGLES, - MESH_PRIMITIVE_LINES = GL_LINES, - MESH_PRIMITIVE_POINTS = GL_POINTS, - #elif DOLPHIN - MESH_PRIMITIVE_TRIANGLES = GX_TRIANGLES, - MESH_PRIMITIVE_LINES = GX_LINES, - MESH_PRIMITIVE_POINTS = GX_POINTS, - #endif -} meshprimitivetype_t; +#ifndef meshInitPlatform + #error "meshInitPlatform must be defined" +#endif +#ifndef meshDrawPlatform + #error "meshDrawPlatform must be defined" +#endif +#ifndef meshGetVertexCountPlatform + #error "meshGetVertexCountPlatform must be defined" +#endif +#ifndef meshDisposePlatform + #error "meshDisposePlatform must be defined" +#endif -#define MESH_VERTEX_UV_SIZE 2 -#define MESH_VERTEX_POS_SIZE 3 - -typedef struct { - color_t color; - float_t uv[MESH_VERTEX_UV_SIZE]; - float_t pos[MESH_VERTEX_POS_SIZE]; -} meshvertex_t; - -typedef struct { - const meshvertex_t *vertices; - int32_t vertexCount; - meshprimitivetype_t primitiveType; -} mesh_t; +typedef meshprimitivetypeplatform_t meshprimitivetype_t; +typedef meshplatform_t mesh_t; /** * Initializes a mesh. @@ -41,8 +31,9 @@ typedef struct { * @param primitiveType The OpenGL primitive type (e.g., GL_TRIANGLES). * @param vertexCount The number of vertices in the mesh. * @param vertices The vertex data for the mesh. + * @return An error indicating success or failure. */ -void meshInit( +errorret_t meshInit( mesh_t *mesh, const meshprimitivetype_t primitiveType, const int32_t vertexCount, @@ -55,16 +46,26 @@ void meshInit( * @param mesh The mesh to draw. * @param vertexOffset The offset in the vertex array to start drawing from. * @param vertexCount The number of vertices to draw. If -1, draws all vertices. + * @return An error indicating success or failure. */ -void meshDraw( +errorret_t meshDraw( const mesh_t *mesh, const int32_t vertexOffset, const int32_t vertexCount ); +/** + * Gets the vertex count of a mesh. + * + * @param mesh The mesh to get the vertex count from. + * @return The vertex count of the mesh. + */ +int32_t meshGetVertexCount(const mesh_t *mesh); + /** * Disposes a mesh. * * @param mesh The mesh to dispose. + * @return An error indicating success or failure. */ -void meshDispose(mesh_t *mesh); \ No newline at end of file +errorret_t meshDispose(mesh_t *mesh); \ No newline at end of file diff --git a/src/dusk/display/mesh/meshvertex.h b/src/dusk/display/mesh/meshvertex.h new file mode 100644 index 0000000..ea976ca --- /dev/null +++ b/src/dusk/display/mesh/meshvertex.h @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "dusk.h" +#include "display/color.h" + +#define MESH_VERTEX_UV_SIZE 2 +#define MESH_VERTEX_POS_SIZE 3 + +typedef struct { + color_t color; + float_t uv[MESH_VERTEX_UV_SIZE]; + float_t pos[MESH_VERTEX_POS_SIZE]; +} meshvertex_t; \ No newline at end of file diff --git a/src/dusk/display/mesh/quad.c b/src/dusk/display/mesh/quad.c index d1ffde1..f18a1ce 100644 --- a/src/dusk/display/mesh/quad.c +++ b/src/dusk/display/mesh/quad.c @@ -20,13 +20,14 @@ meshvertex_t QUAD_MESH_SIMPLE_VERTICES[QUAD_VERTEX_COUNT] = { { .color = COLOR_WHITE_4B, .uv = { 0.0f, 1.0f }, .pos = { 0.0f, 1.0f, 0.0f } } }; -void quadInit() { - meshInit( +errorret_t quadInit() { + errorChain(meshInit( &QUAD_MESH_SIMPLE, QUAD_PRIMITIVE_TYPE, QUAD_VERTEX_COUNT, QUAD_MESH_SIMPLE_VERTICES - ); + )); + errorOk(); } void quadBuffer( diff --git a/src/dusk/display/mesh/quad.h b/src/dusk/display/mesh/quad.h index 9dcd410..6c160f8 100644 --- a/src/dusk/display/mesh/quad.h +++ b/src/dusk/display/mesh/quad.h @@ -10,15 +10,17 @@ #include "display/color.h" #define QUAD_VERTEX_COUNT 6 -#define QUAD_PRIMITIVE_TYPE MESH_PRIMITIVE_TRIANGLES +#define QUAD_PRIMITIVE_TYPE MESH_PRIMITIVE_TYPE_TRIANGLES extern mesh_t QUAD_MESH_SIMPLE; extern meshvertex_t QUAD_MESH_SIMPLE_VERTICES[QUAD_VERTEX_COUNT]; /** * Initializes the quad mesh. + * + * @return Error for initialization of the quad mesh. */ -void quadInit(); +errorret_t quadInit(); /** * Buffers a quad into the provided vertex array. diff --git a/src/dusk/display/screen/screen.h b/src/dusk/display/screen/screen.h index 5c7860a..46d81d1 100644 --- a/src/dusk/display/screen/screen.h +++ b/src/dusk/display/screen/screen.h @@ -7,21 +7,21 @@ #pragma once #include "dusk.h" -#include "display/framebuffer.h" +#include "display/framebuffer/framebuffer.h" #include "display/camera/camera.h" #include "display/mesh/quad.h" #include "display/color.h" -#if DISPLAY_SIZE_DYNAMIC == 1 - #ifndef DISPLAY_SCREEN_HEIGHT_DEFAULT - #error "DISPLAY_SCREEN_HEIGHT_DEFAULT must be defined when DISPLAY_SIZE_DYNAMIC is enabled." +#if DUSK_DISPLAY_SIZE_DYNAMIC == 1 + #ifndef DUSK_DISPLAY_SCREEN_HEIGHT + #error "DUSK_DISPLAY_SCREEN_HEIGHT must be defined" #endif #endif typedef enum { SCREEN_MODE_BACKBUFFER, - #if DISPLAY_SIZE_DYNAMIC == 1 + #ifdef DUSK_DISPLAY_SIZE_DYNAMIC SCREEN_MODE_FIXED_SIZE, SCREEN_MODE_ASPECT_RATIO,// Maintains aspect at all cost SCREEN_MODE_FIXED_HEIGHT, // Fixed height, width expands/contracts as needed diff --git a/src/dusk/display/texture/CMakeLists.txt b/src/dusk/display/texture/CMakeLists.txt index 23ab16b..b31d832 100644 --- a/src/dusk/display/texture/CMakeLists.txt +++ b/src/dusk/display/texture/CMakeLists.txt @@ -8,5 +8,4 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME} PUBLIC tileset.c texture.c - palette.c ) \ No newline at end of file diff --git a/src/dusk/display/texture/palette.c b/src/dusk/display/texture/palette.c deleted file mode 100644 index 3c0b5af..0000000 --- a/src/dusk/display/texture/palette.c +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright (c) 2026 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "palette.h" -#include "assert/assert.h" -#include "util/memory.h" - -palette_t PALETTES[PALETTE_COUNT_MAX] = { 0 }; - -void paletteInit( - palette_t *palette, - const uint8_t colorCount, - const color_t *colors -) { - assertNotNull(palette, "Palette cannot be NULL"); - assertTrue(colorCount > 0, "Color count must be greater than 0"); - assertNotNull(colors, "Colors array cannot be NULL"); - assertTrue(colorCount <= PALETTE_COLOR_COUNT_MAX, "Color count too big"); - assertTrue( - palette - PALETTES < PALETTE_COUNT_MAX, - "Palette index out of range" - ); - - memoryZero(palette, sizeof(palette_t)); - - palette->colorCount = colorCount; - memoryCopy(palette->colors, colors, colorCount * sizeof(color_t)); -} - -void paletteBind(palette_t *palette, const uint8_t slot) { - assertNotNull(palette, "Palette cannot be NULL"); - assertTrue(slot < PALETTE_COUNT_MAX, "Palette slot out of range"); - - // Nothing yet. -} - -void paletteDispose(palette_t *palette) { - assertNotNull(palette, "Palette cannot be NULL"); -} \ No newline at end of file diff --git a/src/dusk/display/texture/palette.h b/src/dusk/display/texture/palette.h deleted file mode 100644 index 63e3009..0000000 --- a/src/dusk/display/texture/palette.h +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright (c) 2025 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include "display/color.h" - -#if DISPLAY_SDL2 - #if DISPLAY_SHADER == 1 - - #elif DISPLAY_COLOR_TABLE == 1 - - #else - #error "Unsupported palette mode" - #endif -#else - #error "Unsupported palette mode" -#endif - -#define PALETTE_COUNT_MAX 4 -#define PALETTE_COLOR_COUNT_MAX 0xFF - -typedef struct { - uint8_t colorCount; - color_t colors[PALETTE_COLOR_COUNT_MAX]; -} palette_t; - -extern palette_t PALETTES[PALETTE_COUNT_MAX]; - -/** - * Initializes a palette with the given colors. - * - * @param palette The palette to initialize. - * @param colorCount The number of colors in the palette. - * @param colors An array of colors for the palette. - */ -void paletteInit( - palette_t *palette, - const uint8_t colorCount, - const color_t *colors -); - -/** - * Binds a palette for use in rendering. - * - * @param palette The palette to bind. - * @param slot The slot to bind the palette to. - */ -void paletteBind(palette_t *palette, const uint8_t slot); - -/** - * Disposes of a palette, freeing any associated resources. - * - * @param palette The palette to dispose. - */ -void paletteDispose(palette_t *palette); \ No newline at end of file diff --git a/src/dusk/display/texture/palettetexture.c b/src/dusk/display/texture/palettetexture.c deleted file mode 100644 index d51bcfa..0000000 --- a/src/dusk/display/texture/palettetexture.c +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright (c) 2026 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "palettetexture.h" -#include "assert/assert.h" - -void paletteTextureInit( - palettetexture_t *texture, - const int32_t width, - const int32_t height, - const uint8_t *data -) { - assertNotNull(texture, "Palette texture cannot be NULL"); - assertTrue(width > 0 && height > 0, "width/height must be greater than 0"); - assertNotNull(data, "Palette texture data cannot be NULL"); - - #if DISPLAY_SDL2 - #if DISPLAY_SHADER == 1 - // Palette textures not supported, convert to GL_RED style texture - // so shader can perform the lookup. - uint8_t formatted[width * height]; - for(int32_t i = 0; i < width * height; i++) { - uint8_t index = data.paletteData[i]; - formatted[i] = index * 128; - } - glTexImage2D( - GL_TEXTURE_2D, 0, GL_R8, width, height, 0, - GL_RED, GL_UNSIGNED_BYTE, (void*)formatted - ); - - #else - glTexImage2D( - GL_TEXTURE_2D, - 0, GL_COLOR_INDEX8_EXT, - width, height, - 0, GL_COLOR_INDEX8_EXT, - GL_UNSIGNED_BYTE, (void*)data.paletteData - ); - // glColorTableEXT( - // GL_TEXTURE_2D, GL_RGBA, data.palette.palette->colorCount, GL_RGBA, - // GL_UNSIGNED_BYTE, (const void*)data.palette.palette->colors - // ); - #endif - #else - #error "Palette textures not supported on this platform" - #endif -} \ No newline at end of file diff --git a/src/dusk/display/texture/palettetexture.h b/src/dusk/display/texture/palettetexture.h deleted file mode 100644 index 7c4fc3e..0000000 --- a/src/dusk/display/texture/palettetexture.h +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright (c) 2026 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include "dusk.h" - -typedef struct { - -} palettetexture_t; - -/** - * Initializes a palette texture. - * - * @param texture The palette texture to initialize. - * @param width The width of the texture. Must be a power of 2. - * @param height The height of the texture. Must be a power of 2. - * @param data The palette index data for the texture. - */ -void paletteTextureInit( - palettetexture_t *texture, - const int32_t width, - const int32_t height, - const uint8_t *data -); \ No newline at end of file diff --git a/src/dusk/display/texture/texture.h b/src/dusk/display/texture/texture.h index 02e8bec..c760057 100644 --- a/src/dusk/display/texture/texture.h +++ b/src/dusk/display/texture/texture.h @@ -8,7 +8,6 @@ #pragma once #include "error/error.h" #include "display/color.h" -#include "display/texture/palette.h" #include "display/texture/textureplatform.h" #ifndef textureInitPlatform diff --git a/src/duskgl/display/CMakeLists.txt b/src/duskgl/display/CMakeLists.txt index 36983e2..32c23f6 100644 --- a/src/duskgl/display/CMakeLists.txt +++ b/src/duskgl/display/CMakeLists.txt @@ -11,4 +11,5 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME} # Subdirs add_subdirectory(framebuffer) -add_subdirectory(texture) \ No newline at end of file +add_subdirectory(texture) +add_subdirectory(mesh) \ No newline at end of file diff --git a/src/duskgl/display/framebuffer/CMakeLists.txt b/src/duskgl/display/framebuffer/CMakeLists.txt index 8f09211..d04ebbb 100644 --- a/src/duskgl/display/framebuffer/CMakeLists.txt +++ b/src/duskgl/display/framebuffer/CMakeLists.txt @@ -6,4 +6,5 @@ # Sources target_sources(${DUSK_LIBRARY_TARGET_NAME} PUBLIC + framebuffergl.c ) \ No newline at end of file diff --git a/src/duskgl/display/mesh/CMakeLists.txt b/src/duskgl/display/mesh/CMakeLists.txt new file mode 100644 index 0000000..5a9b256 --- /dev/null +++ b/src/duskgl/display/mesh/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (c) 2026 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DUSK_LIBRARY_TARGET_NAME} + PUBLIC + meshgl.c +) \ No newline at end of file diff --git a/src/duskgl/display/mesh/meshgl.c b/src/duskgl/display/mesh/meshgl.c new file mode 100644 index 0000000..fd68df3 --- /dev/null +++ b/src/duskgl/display/mesh/meshgl.c @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "display/mesh/mesh.h" +#include "assert/assert.h" + +errorret_t meshInitGL( + meshgl_t *mesh, + const meshprimitivetypegl_t primitiveType, + const int32_t vertexCount, + const meshvertex_t *vertices +) { + assertNotNull(mesh, "Mesh cannot be NULL"); + assertNotNull(vertices, "Vertices cannot be NULL"); + assertTrue(vertexCount > 0, "Vertex count must be greater than 0"); + + mesh->primitiveType = primitiveType; + mesh->vertexCount = vertexCount; + mesh->vertices = vertices; + + errorOk(); +} + +errorret_t meshDrawGL( + const meshgl_t *mesh, + const int32_t offset, + const int32_t count +) { + // PSP style pointer legacy OpenGL + const GLsizei stride = sizeof(meshvertex_t); + + glColorPointer( + sizeof(color4b_t), + GL_UNSIGNED_BYTE, + stride, + (const GLvoid*)&mesh->vertices[offset].color + ); + glTexCoordPointer( + MESH_VERTEX_UV_SIZE, + GL_FLOAT, + stride, + (const GLvoid*)&mesh->vertices[offset].uv + ); + glVertexPointer( + MESH_VERTEX_POS_SIZE, + GL_FLOAT, + stride, + (const GLvoid*)&mesh->vertices[offset].pos + ); + + glDrawArrays(mesh->primitiveType, offset, count); + + errorOk(); +} + +int32_t meshGetVertexCountGL(const meshgl_t *mesh) { + return mesh->vertexCount; +} + +errorret_t meshDisposeGL(meshgl_t *mesh) { + // No dynamic resources to free for this mesh implementation + errorOk(); +} \ No newline at end of file diff --git a/src/duskgl/display/mesh/meshgl.h b/src/duskgl/display/mesh/meshgl.h new file mode 100644 index 0000000..59936c1 --- /dev/null +++ b/src/duskgl/display/mesh/meshgl.h @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "error/error.h" +#include "display/mesh/meshvertex.h" + +typedef enum { + MESH_PRIMITIVE_TYPE_TRIANGLES = GL_TRIANGLES, + MESH_PRIMITIVE_TYPE_LINES = GL_LINES, + MESH_PRIMITIVE_TYPE_POINTS = GL_POINTS, +} meshprimitivetypegl_t; + +typedef struct { + +} meshgl_t; + +/** + * Initializes a mesh for OpenGL. + * + * @param mesh The mesh to initialize. + * @param primitiveType The OpenGL primitive type (e.g., GL_TRIANGLES). + * @param vertexCount The number of vertices in the mesh. + * @param vertices The vertex data for the mesh. + * @return An errorret_t indicating success or failure. + */ +errorret_t meshInitGL( + meshgl_t *mesh, + const meshprimitivetypegl_t primitiveType, + const int32_t vertexCount, + const meshvertex_t *vertices +); + +/** + * Draws a mesh using OpenGL. + * + * @param mesh The mesh to draw. + * @param vertexOffset The offset in the vertex array to start drawing from. + * @param vertexCount The number of vertices to draw. If -1, draws all vertices. + * @return An errorret_t indicating success or failure. + */ +errorret_t meshDrawGL( + const meshgl_t *mesh, + const int32_t vertexOffset, + const int32_t vertexCount +); + +/** + * Gets the vertex count of a mesh used for OpenGL. + * + * @param mesh The mesh to get the vertex count from. + * @return The vertex count of the mesh. + */ +int32_t meshGetVertexCountGL(const meshgl_t *mesh); + +/** + * Disposes a mesh used for OpenGL. + * + * @param mesh The mesh to dispose. + * @return An errorret_t indicating success or failure. + */ +errorret_t meshDisposeGL(meshgl_t *mesh); \ No newline at end of file diff --git a/src/duskgl/display/mesh/meshplatform.h b/src/duskgl/display/mesh/meshplatform.h new file mode 100644 index 0000000..0419534 --- /dev/null +++ b/src/duskgl/display/mesh/meshplatform.h @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "meshgl.h" + +typedef meshprimitivetypegl_t meshprimitivetypeplatform_t; +typedef meshgl_t meshplatform_t; + +#define meshInitPlatform meshInitGL +#define meshDrawPlatform meshDrawGL +#define meshGetVertexCountPlatform meshGetVertexCountGL +#define meshDisposePlatform meshDisposeGL \ No newline at end of file