More code moving
This commit is contained in:
@@ -17,4 +17,5 @@ target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
|||||||
DUSK_DISPLAY_SIZE_DYNAMIC
|
DUSK_DISPLAY_SIZE_DYNAMIC
|
||||||
DUSK_DISPLAY_WIDTH_DEFAULT=640
|
DUSK_DISPLAY_WIDTH_DEFAULT=640
|
||||||
DUSK_DISPLAY_HEIGHT_DEFAULT=480
|
DUSK_DISPLAY_HEIGHT_DEFAULT=480
|
||||||
|
DUSK_DISPLAY_SCREEN_HEIGHT=240
|
||||||
)
|
)
|
||||||
@@ -29,8 +29,8 @@ errorret_t displayInit(void) {
|
|||||||
errorChain(displayPlatformInit());
|
errorChain(displayPlatformInit());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
quadInit();
|
errorChain(quadInit());
|
||||||
frameBufferInitBackbuffer();
|
errorChain(frameBufferInitBackbuffer());
|
||||||
spriteBatchInit();
|
spriteBatchInit();
|
||||||
errorChain(textInit());
|
errorChain(textInit());
|
||||||
errorChain(assetLoad("main_palette.dpf", &PALETTES[0]));
|
errorChain(assetLoad("main_palette.dpf", &PALETTES[0]));
|
||||||
@@ -46,7 +46,7 @@ errorret_t displayUpdate(void) {
|
|||||||
|
|
||||||
// Reset state
|
// Reset state
|
||||||
spriteBatchClear();
|
spriteBatchClear();
|
||||||
frameBufferBind(NULL);
|
errorChain(frameBufferBind(NULL));
|
||||||
|
|
||||||
// Bind screen and render scene
|
// Bind screen and render scene
|
||||||
screenBind();
|
screenBind();
|
||||||
|
|||||||
@@ -17,9 +17,10 @@ errorret_t frameBufferInitBackbuffer() {
|
|||||||
memoryZero(&FRAMEBUFFER_BACKBUFFER, sizeof(framebuffer_t));
|
memoryZero(&FRAMEBUFFER_BACKBUFFER, sizeof(framebuffer_t));
|
||||||
errorChain(frameBufferPlatformInitBackbuffer());
|
errorChain(frameBufferPlatformInitBackbuffer());
|
||||||
FRAMEBUFFER_BOUND = &FRAMEBUFFER_BACKBUFFER;
|
FRAMEBUFFER_BOUND = &FRAMEBUFFER_BACKBUFFER;
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
void frameBufferBind(framebuffer_t *framebuffer) {
|
errorret_t frameBufferBind(framebuffer_t *framebuffer) {
|
||||||
if(framebuffer == NULL) {
|
if(framebuffer == NULL) {
|
||||||
frameBufferBind(&FRAMEBUFFER_BACKBUFFER);
|
frameBufferBind(&FRAMEBUFFER_BACKBUFFER);
|
||||||
FRAMEBUFFER_BOUND = &FRAMEBUFFER_BACKBUFFER;
|
FRAMEBUFFER_BOUND = &FRAMEBUFFER_BACKBUFFER;
|
||||||
|
|||||||
@@ -54,8 +54,9 @@ uint32_t frameBufferGetHeight(const framebuffer_t *framebuffer);
|
|||||||
* provided is NULL.
|
* provided is NULL.
|
||||||
*
|
*
|
||||||
* @param framebuffer The framebuffer to bind, or NULL to bind the backbuffer.
|
* @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.
|
* Clears the currently bound framebuffer.
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
#include "display/texture.h"
|
#include "display/texture.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void meshInit(
|
errorret_t meshInit(
|
||||||
mesh_t *mesh,
|
mesh_t *mesh,
|
||||||
const meshprimitivetype_t primitiveType,
|
const meshprimitivetype_t primitiveType,
|
||||||
const int32_t vertexCount,
|
const int32_t vertexCount,
|
||||||
@@ -25,80 +25,45 @@ void meshInit(
|
|||||||
|
|
||||||
memoryZero(mesh, sizeof(mesh_t));
|
memoryZero(mesh, sizeof(mesh_t));
|
||||||
|
|
||||||
mesh->primitiveType = primitiveType;
|
errorChain(meshInitPlatform(mesh, primitiveType, vertexCount, vertices));
|
||||||
mesh->vertexCount = vertexCount;
|
errorOk();
|
||||||
mesh->vertices = vertices;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void meshDraw(
|
errorret_t meshDraw(
|
||||||
const mesh_t *mesh,
|
const mesh_t *mesh,
|
||||||
const int32_t vertexOffset,
|
const int32_t vertexOffset,
|
||||||
const int32_t vertexCount
|
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");
|
assertNotNull(mesh, "Mesh cannot be NULL");
|
||||||
assertTrue(offset >= 0, "Vertex offset must be non-negative");
|
assertTrue(vertexOffset >= 0, "Vertex offset must be non-negative");
|
||||||
assertTrue(count >= 0, "Vertex count must be non-negative");
|
assertTrue(vertexCount >= -1, "Vertex count must be -1 or non-negative");
|
||||||
assertTrue(offset + count <= mesh->vertexCount,
|
|
||||||
"Vertex offset + count must not exceed vertex count"
|
|
||||||
);
|
|
||||||
|
|
||||||
#if DISPLAY_SDL2
|
int32_t vertDrawCount = vertexCount;
|
||||||
// PSP style pointer legacy OpenGL
|
if(vertexCount == -1) {
|
||||||
const GLsizei stride = sizeof(meshvertex_t);
|
const int32_t totalVertices = meshGetVertexCount(mesh);
|
||||||
|
vertDrawCount = totalVertices - vertexOffset;
|
||||||
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
|
if(vertDrawCount == 0) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
assertTrue(
|
||||||
|
vertexOffset + vertDrawCount <= meshGetVertexCount(mesh),
|
||||||
|
"Vertex offset and count must be within vertex count bounds"
|
||||||
|
);
|
||||||
|
|
||||||
|
errorChain(meshDrawPlatform(mesh, vertexOffset, vertDrawCount));
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
void meshDispose(mesh_t *mesh) {
|
int32_t meshGetVertexCount(const mesh_t *mesh) {
|
||||||
assertNotNull(mesh, "Mesh cannot be NULL");
|
assertNotNull(mesh, "Mesh cannot be NULL");
|
||||||
memoryZero(mesh, sizeof(mesh_t));
|
return meshGetVertexCountPlatform(mesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t meshDispose(mesh_t *mesh) {
|
||||||
|
assertNotNull(mesh, "Mesh cannot be NULL");
|
||||||
|
errorChain(meshDisposePlatform(mesh));
|
||||||
|
memoryZero(mesh, sizeof(mesh_t));
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
@@ -6,33 +6,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "display/display.h"
|
#include "display/display.h"
|
||||||
#include "display/color.h"
|
#include "display/color.h"
|
||||||
|
#include "display/mesh/meshplatform.h"
|
||||||
|
|
||||||
typedef enum {
|
#ifndef meshInitPlatform
|
||||||
#if DISPLAY_SDL2
|
#error "meshInitPlatform must be defined"
|
||||||
MESH_PRIMITIVE_TRIANGLES = GL_TRIANGLES,
|
#endif
|
||||||
MESH_PRIMITIVE_LINES = GL_LINES,
|
#ifndef meshDrawPlatform
|
||||||
MESH_PRIMITIVE_POINTS = GL_POINTS,
|
#error "meshDrawPlatform must be defined"
|
||||||
#elif DOLPHIN
|
#endif
|
||||||
MESH_PRIMITIVE_TRIANGLES = GX_TRIANGLES,
|
#ifndef meshGetVertexCountPlatform
|
||||||
MESH_PRIMITIVE_LINES = GX_LINES,
|
#error "meshGetVertexCountPlatform must be defined"
|
||||||
MESH_PRIMITIVE_POINTS = GX_POINTS,
|
#endif
|
||||||
#endif
|
#ifndef meshDisposePlatform
|
||||||
} meshprimitivetype_t;
|
#error "meshDisposePlatform must be defined"
|
||||||
|
#endif
|
||||||
|
|
||||||
#define MESH_VERTEX_UV_SIZE 2
|
typedef meshprimitivetypeplatform_t meshprimitivetype_t;
|
||||||
#define MESH_VERTEX_POS_SIZE 3
|
typedef meshplatform_t mesh_t;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a mesh.
|
* Initializes a mesh.
|
||||||
@@ -41,8 +31,9 @@ typedef struct {
|
|||||||
* @param primitiveType The OpenGL primitive type (e.g., GL_TRIANGLES).
|
* @param primitiveType The OpenGL primitive type (e.g., GL_TRIANGLES).
|
||||||
* @param vertexCount The number of vertices in the mesh.
|
* @param vertexCount The number of vertices in the mesh.
|
||||||
* @param vertices The vertex data for 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,
|
mesh_t *mesh,
|
||||||
const meshprimitivetype_t primitiveType,
|
const meshprimitivetype_t primitiveType,
|
||||||
const int32_t vertexCount,
|
const int32_t vertexCount,
|
||||||
@@ -55,16 +46,26 @@ void meshInit(
|
|||||||
* @param mesh The mesh to draw.
|
* @param mesh The mesh to draw.
|
||||||
* @param vertexOffset The offset in the vertex array to start drawing from.
|
* @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.
|
* @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 mesh_t *mesh,
|
||||||
const int32_t vertexOffset,
|
const int32_t vertexOffset,
|
||||||
const int32_t vertexCount
|
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.
|
* Disposes a mesh.
|
||||||
*
|
*
|
||||||
* @param mesh The mesh to dispose.
|
* @param mesh The mesh to dispose.
|
||||||
|
* @return An error indicating success or failure.
|
||||||
*/
|
*/
|
||||||
void meshDispose(mesh_t *mesh);
|
errorret_t meshDispose(mesh_t *mesh);
|
||||||
19
src/dusk/display/mesh/meshvertex.h
Normal file
19
src/dusk/display/mesh/meshvertex.h
Normal file
@@ -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;
|
||||||
@@ -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 } }
|
{ .color = COLOR_WHITE_4B, .uv = { 0.0f, 1.0f }, .pos = { 0.0f, 1.0f, 0.0f } }
|
||||||
};
|
};
|
||||||
|
|
||||||
void quadInit() {
|
errorret_t quadInit() {
|
||||||
meshInit(
|
errorChain(meshInit(
|
||||||
&QUAD_MESH_SIMPLE,
|
&QUAD_MESH_SIMPLE,
|
||||||
QUAD_PRIMITIVE_TYPE,
|
QUAD_PRIMITIVE_TYPE,
|
||||||
QUAD_VERTEX_COUNT,
|
QUAD_VERTEX_COUNT,
|
||||||
QUAD_MESH_SIMPLE_VERTICES
|
QUAD_MESH_SIMPLE_VERTICES
|
||||||
);
|
));
|
||||||
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
void quadBuffer(
|
void quadBuffer(
|
||||||
|
|||||||
@@ -10,15 +10,17 @@
|
|||||||
#include "display/color.h"
|
#include "display/color.h"
|
||||||
|
|
||||||
#define QUAD_VERTEX_COUNT 6
|
#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 mesh_t QUAD_MESH_SIMPLE;
|
||||||
extern meshvertex_t QUAD_MESH_SIMPLE_VERTICES[QUAD_VERTEX_COUNT];
|
extern meshvertex_t QUAD_MESH_SIMPLE_VERTICES[QUAD_VERTEX_COUNT];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the quad mesh.
|
* 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.
|
* Buffers a quad into the provided vertex array.
|
||||||
|
|||||||
@@ -7,21 +7,21 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "dusk.h"
|
#include "dusk.h"
|
||||||
#include "display/framebuffer.h"
|
#include "display/framebuffer/framebuffer.h"
|
||||||
#include "display/camera/camera.h"
|
#include "display/camera/camera.h"
|
||||||
#include "display/mesh/quad.h"
|
#include "display/mesh/quad.h"
|
||||||
#include "display/color.h"
|
#include "display/color.h"
|
||||||
|
|
||||||
#if DISPLAY_SIZE_DYNAMIC == 1
|
#if DUSK_DISPLAY_SIZE_DYNAMIC == 1
|
||||||
#ifndef DISPLAY_SCREEN_HEIGHT_DEFAULT
|
#ifndef DUSK_DISPLAY_SCREEN_HEIGHT
|
||||||
#error "DISPLAY_SCREEN_HEIGHT_DEFAULT must be defined when DISPLAY_SIZE_DYNAMIC is enabled."
|
#error "DUSK_DISPLAY_SCREEN_HEIGHT must be defined"
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SCREEN_MODE_BACKBUFFER,
|
SCREEN_MODE_BACKBUFFER,
|
||||||
|
|
||||||
#if DISPLAY_SIZE_DYNAMIC == 1
|
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
||||||
SCREEN_MODE_FIXED_SIZE,
|
SCREEN_MODE_FIXED_SIZE,
|
||||||
SCREEN_MODE_ASPECT_RATIO,// Maintains aspect at all cost
|
SCREEN_MODE_ASPECT_RATIO,// Maintains aspect at all cost
|
||||||
SCREEN_MODE_FIXED_HEIGHT, // Fixed height, width expands/contracts as needed
|
SCREEN_MODE_FIXED_HEIGHT, // Fixed height, width expands/contracts as needed
|
||||||
|
|||||||
@@ -8,5 +8,4 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
|||||||
PUBLIC
|
PUBLIC
|
||||||
tileset.c
|
tileset.c
|
||||||
texture.c
|
texture.c
|
||||||
palette.c
|
|
||||||
)
|
)
|
||||||
@@ -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");
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -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
|
|
||||||
);
|
|
||||||
@@ -8,7 +8,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.h"
|
#include "error/error.h"
|
||||||
#include "display/color.h"
|
#include "display/color.h"
|
||||||
#include "display/texture/palette.h"
|
|
||||||
#include "display/texture/textureplatform.h"
|
#include "display/texture/textureplatform.h"
|
||||||
|
|
||||||
#ifndef textureInitPlatform
|
#ifndef textureInitPlatform
|
||||||
|
|||||||
@@ -12,3 +12,4 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
|||||||
# Subdirs
|
# Subdirs
|
||||||
add_subdirectory(framebuffer)
|
add_subdirectory(framebuffer)
|
||||||
add_subdirectory(texture)
|
add_subdirectory(texture)
|
||||||
|
add_subdirectory(mesh)
|
||||||
@@ -6,4 +6,5 @@
|
|||||||
# Sources
|
# Sources
|
||||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
PUBLIC
|
PUBLIC
|
||||||
|
framebuffergl.c
|
||||||
)
|
)
|
||||||
10
src/duskgl/display/mesh/CMakeLists.txt
Normal file
10
src/duskgl/display/mesh/CMakeLists.txt
Normal file
@@ -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
|
||||||
|
)
|
||||||
67
src/duskgl/display/mesh/meshgl.c
Normal file
67
src/duskgl/display/mesh/meshgl.c
Normal file
@@ -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();
|
||||||
|
}
|
||||||
66
src/duskgl/display/mesh/meshgl.h
Normal file
66
src/duskgl/display/mesh/meshgl.h
Normal file
@@ -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);
|
||||||
17
src/duskgl/display/mesh/meshplatform.h
Normal file
17
src/duskgl/display/mesh/meshplatform.h
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user