From c8390bdb1233fb7edababe2580b31fe01ee04833 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Thu, 21 Aug 2025 07:33:08 -0500 Subject: [PATCH] Committing progress. --- src/display/display.c | 2 +- src/display/display.h | 2 + src/display/mesh/CMakeLists.txt | 11 +++++ src/display/mesh/mesh.c | 80 +++++++++++++++++++++++++++++++++ src/display/mesh/mesh.h | 66 +++++++++++++++++++++++++++ src/display/mesh/quad.c | 62 +++++++++++++++++++++++++ src/display/mesh/quad.h | 45 +++++++++++++++++++ 7 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 src/display/mesh/CMakeLists.txt create mode 100644 src/display/mesh/mesh.c create mode 100644 src/display/mesh/mesh.h create mode 100644 src/display/mesh/quad.c create mode 100644 src/display/mesh/quad.h diff --git a/src/display/display.c b/src/display/display.c index e19842d..7aafd16 100644 --- a/src/display/display.c +++ b/src/display/display.c @@ -21,7 +21,7 @@ errorret_t displayInit(void) { // Create window with OpenGL flag. DISPLAY.window = SDL_CreateWindow( - "DuskSDL2", + "Dusk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, DISPLAY_WINDOW_WIDTH_DEFAULT, diff --git a/src/display/display.h b/src/display/display.h index 6da90b7..f34c152 100644 --- a/src/display/display.h +++ b/src/display/display.h @@ -13,6 +13,8 @@ #define GL_GLEXT_PROTOTYPES #include #include +#else + #error "Need to specify display backend." #endif #ifndef DISPLAY_WIDTH diff --git a/src/display/mesh/CMakeLists.txt b/src/display/mesh/CMakeLists.txt new file mode 100644 index 0000000..9befe9a --- /dev/null +++ b/src/display/mesh/CMakeLists.txt @@ -0,0 +1,11 @@ +# Copyright (c) 2025 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DUSK_TARGET_NAME} + PRIVATE + mesh.c + quad.c +) \ No newline at end of file diff --git a/src/display/mesh/mesh.c b/src/display/mesh/mesh.c new file mode 100644 index 0000000..d5b09d4 --- /dev/null +++ b/src/display/mesh/mesh.c @@ -0,0 +1,80 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "mesh.h" +#include "util/memory.h" +#include "assert/assert.h" + +#include "console/console.h" + +void meshInit( + mesh_t *mesh, + const meshprimitivetype_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"); + + memoryZero(mesh, sizeof(mesh_t)); + + mesh->primitiveType = primitiveType; + mesh->vertexCount = vertexCount; + mesh->vertices = vertices; +} + +void 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" + ); + + #if DUSK_DISPLAY_SDL2 + // PSP style pointer legacy OpenGL + const GLsizei stride = sizeof(meshvertex_t); + + glColorPointer( + MESH_VERTEX_COLOR_SIZE, + GL_UNSIGNED_BYTE, + stride, + (const GLvoid*)&mesh->vertices[offset].color[0] + ); + 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 + ); + #endif +} + +void meshDispose(mesh_t *mesh) { + assertNotNull(mesh, "Mesh cannot be NULL"); + memoryZero(mesh, sizeof(mesh_t)); +} \ No newline at end of file diff --git a/src/display/mesh/mesh.h b/src/display/mesh/mesh.h new file mode 100644 index 0000000..1437238 --- /dev/null +++ b/src/display/mesh/mesh.h @@ -0,0 +1,66 @@ +// Copyright (c) 2025 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "display/render.h" + +typedef enum { + #if DUSK_DISPLAY_SDL2 + MESH_PRIMITIVE_TRIANGLES = GL_TRIANGLES, + MESH_PRIMITIVE_LINES = GL_LINES, + MESH_PRIMITIVE_POINTS = GL_POINTS, + #endif +} meshprimitivetype_t; + +#define MESH_VERTEX_COLOR_SIZE 4 +#define MESH_VERTEX_UV_SIZE 2 +#define MESH_VERTEX_POS_SIZE 3 + +typedef struct { + GLubyte color[MESH_VERTEX_COLOR_SIZE]; + GLfloat uv[MESH_VERTEX_UV_SIZE]; + GLfloat pos[MESH_VERTEX_POS_SIZE]; +} meshvertex_t; + +typedef struct { + const meshvertex_t *vertices; + int32_t vertexCount; + meshprimitivetype_t primitiveType; +} mesh_t; + +/** + * Initializes a mesh. + * + * @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. + */ +void meshInit( + mesh_t *mesh, + const meshprimitivetype_t primitiveType, + const int32_t vertexCount, + const meshvertex_t *vertices +); + +/** + * Draws a mesh. + * + * @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. + */ +void meshDraw( + const mesh_t *mesh, + const int32_t vertexOffset, + const int32_t vertexCount +); + +/** + * Disposes a mesh. + * + * @param mesh The mesh to dispose. + */ +void meshDispose(mesh_t *mesh); \ No newline at end of file diff --git a/src/display/mesh/quad.c b/src/display/mesh/quad.c new file mode 100644 index 0000000..acf02f8 --- /dev/null +++ b/src/display/mesh/quad.c @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "quad.h" +#include "assert/assert.h" + +void quadBuffer( + meshvertex_t *vertices, + const float_t minX, + const float_t minY, + const float_t maxX, + const float_t maxY, + const uint8_t r, + const uint8_t g, + const uint8_t b, + const uint8_t a, + const float_t u0, + const float_t v0, + const float_t u1, + const float_t v1 +) { + const float_t z = 0.0f; // Z coordinate for 2D rendering + assertNotNull(vertices, "Vertices cannot be NULL"); + + // First triangle + vertices[0] = (meshvertex_t) { + { r, g, b, a }, // Color + { u0, v0 }, // UV + { minX, minY, z } // Position + }; + vertices[1] = (meshvertex_t) { + { r, g, b, a }, // Color + { u1, v0 }, // UV + { maxX, minY, z } // Position + }; + vertices[2] = (meshvertex_t) { + { r, g, b, a }, // Color + { u1, v1 }, // UV + { maxX, maxY, z } // Position + }; + + // Second triangle + vertices[3] = (meshvertex_t) { + { r, g, b, a }, // Color + { u0, v0 }, // UV + { minX, minY, z } // Position + }; + vertices[4] = (meshvertex_t) { + { r, g, b, a }, // Color + { u1, v1 }, // UV + { maxX, maxY, z } // Position + }; + vertices[5] = (meshvertex_t) { + { r, g, b, a }, // Color + { u0, v1 }, // UV + { minX, maxY, z } // Position + }; +} \ No newline at end of file diff --git a/src/display/mesh/quad.h b/src/display/mesh/quad.h new file mode 100644 index 0000000..2bacfb5 --- /dev/null +++ b/src/display/mesh/quad.h @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "mesh.h" + +#define QUAD_VERTEX_COUNT 6 +#define QUAD_PRIMITIVE_TYPE MESH_PRIMITIVE_TRIANGLES + +/** + * Buffers a quad into the provided vertex array. + * + * @param vertices The vertex array to buffer into. + * @param minX The minimum X coordinate of the quad. + * @param minY The minimum Y coordinate of the quad. + * @param maxX The maximum X coordinate of the quad. + * @param maxY The maximum Y coordinate of the quad. + * @param r The red color component (0-255). + * @param g The green color component (0-255). + * @param b The blue color component (0-255). + * @param a The alpha color component (0-255). + * @param u0 The U texture coordinate for the first vertex. + * @param v0 The V texture coordinate for the first vertex. + * @param u1 The U texture coordinate for the second vertex. + * @param v1 The V texture coordinate for the second vertex. + */ +void quadBuffer( + meshvertex_t *vertices, + const float_t minX, + const float_t minY, + const float_t maxX, + const float_t maxY, + const uint8_t r, + const uint8_t g, + const uint8_t b, + const uint8_t a, + const float_t u0, + const float_t v0, + const float_t u1, + const float_t v1 +); \ No newline at end of file