archivemuh

This commit is contained in:
2025-08-20 19:18:38 -05:00
parent 1411c2e96b
commit fbfcbe9578
173 changed files with 2802 additions and 13 deletions

View File

@@ -0,0 +1,41 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Compile defs
target_compile_definitions(${DUSK_TARGET_NAME}
PUBLIC
# DUSK_KEYBOARD_SUPPORT=1
)
# Libs
find_package(SDL2 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(cglm REQUIRED)
target_link_libraries(${DUSK_TARGET_NAME}
PUBLIC
SDL2::SDL2
OpenGL::GL
GL
cglm
)
# Includes
target_include_directories(${DUSK_TARGET_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
dusksdl2input.c
main.c
time.c
)
# Subdirs
add_subdirectory(display)

View File

@@ -0,0 +1,21 @@
# 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
render.c
renderbackbuffer.c
)
# Subdirs
add_subdirectory(camera)
add_subdirectory(framebuffer)
add_subdirectory(mesh)
add_subdirectory(overworld)
add_subdirectory(texture)
add_subdirectory(spritebatch)
add_subdirectory(scene)
add_subdirectory(ui)

View File

@@ -0,0 +1,10 @@
# 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
camera.c
)

View File

@@ -0,0 +1,125 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "camera.h"
#include "display/render.h"
#include "world/overworld.h"
void cameraUIPush(void) {
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, RENDER_WIDTH, RENDER_HEIGHT);
mat4 ortho;
glm_ortho(
0.0f, (float_t)RENDER_WIDTH,
(float_t)RENDER_HEIGHT, 0.0f,
-1.0f, 1.0f,
ortho
);
glLoadMatrixf((const GLfloat*)ortho);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void cameraUIPop(void) {
glPopMatrix();
}
void cameraScreenPush(void) {
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
mat4 ortho;
#if RENDER_USE_FRAMEBUFFER
int32_t windowWidth, windowHeight;
SDL_GetWindowSize(RENDER_WINDOW, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
glm_ortho(
0.0f, (float_t) windowWidth,
(float_t)windowHeight, 0.0f,
-1.0f, 1.0f,
ortho
);
#else
glm_ortho(
0.0f, (float_t)RENDER_WIDTH,
(float_t)RENDER_HEIGHT, 0.0f,
-1.0f, 1.0f,
ortho
);
#endif
glLoadMatrixf((const GLfloat*)ortho);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void cameraScreenPop(void) {
glPopMatrix();
}
void cameraOverworldPush(void) {
glPushMatrix();
glLoadIdentity();
#if RENDER_USE_FRAMEBUFFER
glViewport(0, 0, RENDER_WIDTH, RENDER_HEIGHT);
#endif
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const float_t fov = glm_rad(75.0f);
const float_t camOffset = 12.0f;
const float_t aspect = (float_t)RENDER_WIDTH / (float_t)RENDER_HEIGHT;
const float_t pixelPerfectOffset = (
tanf((glm_rad(180) - fov) / 2.0f) *
((float_t)RENDER_HEIGHT/ 2.0f)
);
vec3 look = {
OVERWORLD_CAMERA_X,
OVERWORLD_CAMERA_Y,
0.0f
};
vec3 eye = {
look[0],
look[1] + camOffset,
look[2] + pixelPerfectOffset
};
vec3 up = { 0.0f, 1.0f, 0.0f };
mat4 proj;
glm_perspective(fov, aspect, 0.1f, 1000.0f, proj);
// Flips rendering on the Y axis, so that it is still right-down even in 3D;
proj[1][1] = -proj[1][1];
mat4 view;
glm_lookat(eye, look, up, view);
mat4 pv;
glm_mat4_mul(proj, view, pv);
glLoadMatrixf((const GLfloat*)pv);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void cameraOverworldPop(void) {
glPopMatrix();
}

View File

@@ -0,0 +1,39 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusksdl2.h"
/**
* Pushes the UI camera matrix onto the stack.
*/
void cameraUIPush(void);
/**
* Pops the UI camera matrix from the stack.
*/
void cameraUIPop(void);
/**
* Pushes the screen space camera matrix onto the stack.
*/
void cameraScreenPush(void);
/**
* Pops the screen space camera matrix.
*/
void cameraScreenPop(void);
/**
* Pushes the overworld camera matrix onto the stack.
*/
void cameraOverworldPush(void);
/**
* Pops the overworld camera matrix.
*/
void cameraOverworldPop(void);

View File

@@ -0,0 +1,10 @@
# 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
framebuffer.c
)

View File

@@ -0,0 +1,59 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "framebuffer.h"
#include "assert/assert.h"
#include "util/memory.h"
#if RENDER_USE_FRAMEBUFFER
void frameBufferInit(
framebuffer_t *framebuffer,
const uint32_t width,
const uint32_t height
) {
assertNotNull(framebuffer, "Framebuffer cannot be NULL");
assertTrue(width > 0 && height > 0, "Width & height must be greater than 0");
memoryZero(framebuffer, sizeof(framebuffer_t));
textureInit(&framebuffer->texture, width, height, GL_RGBA, NULL);
// Generate the framebuffer object using EXT
glGenFramebuffersEXT(1, &framebuffer->id);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer->id);
// Attach the texture to the framebuffer
glFramebufferTexture2DEXT(
GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, framebuffer->texture.id, 0
);
// Check if the framebuffer is complete
if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT) {
assertUnreachable("Framebuffer is not complete");
}
// Unbind the framebuffer
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
void frameBufferBind(const framebuffer_t *framebuffer) {
if(framebuffer == NULL) {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
return;
}
// Bind the framebuffer for rendering
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer->id);
}
void frameBufferDispose(framebuffer_t *framebuffer) {
assertNotNull(framebuffer, "Framebuffer cannot be NULL");
glDeleteFramebuffersEXT(1, &framebuffer->id);
textureDispose(&framebuffer->texture);
}
#endif

View File

@@ -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 "display/render.h"
#include "display/texture/texture.h"
#if RENDER_USE_FRAMEBUFFER
typedef struct {
GLuint id;
texture_t texture;
} framebuffer_t;
/**
* Initializes a framebuffer using EXT methods.
*
* @param framebuffer The framebuffer to initialize.
* @param width The width of the framebuffer.
* @param height The height of the framebuffer.
* @return An error code indicating success or failure.
*/
void frameBufferInit(
framebuffer_t *framebuffer,
const uint32_t width,
const uint32_t height
);
/**
* Binds the framebuffer for rendering using EXT methods.
*
* @param framebuffer The framebuffer to bind, or NULL to unbind.
*/
void frameBufferBind(const framebuffer_t *framebuffer);
/**
* Disposes of the framebuffer using EXT methods.
*
* @param framebuffer The framebuffer to dispose of.
*/
void frameBufferDispose(framebuffer_t *framebuffer);
#endif

View File

@@ -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
)

View File

@@ -0,0 +1,82 @@
/**
* 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 GLenum 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 1
// 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
);
#else
#error "Need to support modern OpenGL with VAOs and VBOs"
#endif
}
void meshDispose(mesh_t *mesh) {
assertNotNull(mesh, "Mesh cannot be NULL");
memoryZero(mesh, sizeof(mesh_t));
}

View File

@@ -0,0 +1,58 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dusksdl2.h"
#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;
GLenum 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 GLenum 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);

View File

@@ -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
};
}

View File

@@ -0,0 +1,44 @@
/**
* 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
/**
* 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
);

View File

@@ -0,0 +1,10 @@
# 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
renderoverworld.c
)

View File

@@ -0,0 +1,125 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "renderoverworld.h"
#include "util/memory.h"
#include "assert/assert.h"
#include "display/camera/camera.h"
#include "entity/entity.h"
#include "display/spritebatch/spritebatch.h"
renderoverworld_t RENDER_OVERWORLD;
void renderOverworldInit(void) {
memoryZero(&RENDER_OVERWORLD, sizeof(RENDER_OVERWORLD));
for(uint8_t i = 0; i < CHUNK_MAP_COUNT; i++) {
renderchunk_t *chunk = &RENDER_OVERWORLD.chunks[i];
meshInit(
&chunk->meshBase,
GL_TRIANGLES,
CHUNK_TILE_COUNT * QUAD_VERTEX_COUNT,
chunk->verticesBase
);
meshInit(
&chunk->meshBaseOverlay,
GL_TRIANGLES,
CHUNK_TILE_COUNT,
chunk->verticesBaseOverlay
);
}
}
void renderOverworldDraw(void) {
cameraOverworldPush();
for(uint8_t i = 0; i < CHUNK_MAP_COUNT; i++) {
renderchunk_t *chunk = &RENDER_OVERWORLD.chunks[i];
meshDraw(&chunk->meshBase, -1, -1);
}
for(uint8_t i = 0; i < ENTITY_COUNT_MAX; i++) {
entity_t *entity = &ENTITIES[i];
if(entity->type == ENTITY_TYPE_NULL) continue;
float_t x = (entity->x * TILE_WIDTH_HEIGHT) + entity->subX;
float_t y = (entity->y * TILE_WIDTH_HEIGHT) + entity->subY;
// Draw the entity
spriteBatchPush(
NULL,
x, y,
x + TILE_WIDTH_HEIGHT, y + TILE_WIDTH_HEIGHT,
0xFF, 0x00, 0xFF, 0XFF,
0.0f, 0.0f, 1.0f, 1.0f
);
}
spriteBatchFlush();
cameraOverworldPop();
}
void renderChunkUpdated(chunk_t *chunk) {
uint8_t r, g, b;
assertNotNull(chunk, "Chunk pointer is null");
int32_t chunkIndex = chunk - CHUNK_MAP.chunks;
assertTrue(
chunkIndex >= 0 && chunkIndex < CHUNK_MAP_COUNT,
"Chunk index out of bounds"
);
for(uint32_t i = 0; i < CHUNK_TILE_COUNT; i++) {
tile_t base = chunk->tilesBase[i];
tile_t overlay = chunk->tilesBaseOverlay[i];
float_t posX = (i % CHUNK_WIDTH) + (chunk->x * CHUNK_WIDTH);
float_t posY = (i / CHUNK_WIDTH) + (chunk->y * CHUNK_HEIGHT);
switch(base) {
case 0:
r = 0; g = 0; b = 0; // Black for empty
break;
case 1:
r = 34; g = 139; b = 34; // Forest Green
break;
case 2:
r = 0; g = 191; b = 255; // Deep Sky Blue
break;
case 3:
r = 139; g = 69; b = 19; // Saddle Brown
break;
case 4:
r = 255; g = 255; b = 0; // Yellow
break;
default:
r = 255; g = 20; b = 147; // Pink for unknown
break;
}
quadBuffer(
&RENDER_OVERWORLD.chunks[chunkIndex].verticesBase[i * QUAD_VERTEX_COUNT],
posX * TILE_WIDTH_HEIGHT,
posY * TILE_WIDTH_HEIGHT,
(posX + 1) * TILE_WIDTH_HEIGHT,
(posY + 1) * TILE_WIDTH_HEIGHT,
r, g, b, 255,
0, 0, 1, 1
);
}
}
void renderOverworldDispose(void) {
// Clean up overworld rendering resources here
for(uint8_t i = 0; i < CHUNK_MAP_COUNT; i++) {
renderchunk_t *chunk = &RENDER_OVERWORLD.chunks[i];
meshDispose(&chunk->meshBase);
meshDispose(&chunk->meshBaseOverlay);
}
}

View File

@@ -0,0 +1,39 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "world/chunk.h"
#include "display/mesh/quad.h"
typedef struct {
mesh_t meshBase;
meshvertex_t verticesBase[CHUNK_TILE_COUNT * QUAD_VERTEX_COUNT];
mesh_t meshBaseOverlay;
meshvertex_t verticesBaseOverlay[CHUNK_TILE_COUNT];
} renderchunk_t;
typedef struct {
renderchunk_t chunks[CHUNK_MAP_COUNT];
} renderoverworld_t;
extern renderoverworld_t RENDER_OVERWORLD;
/**
* Initializes the render overworld.
*/
void renderOverworldInit(void);
/**
* Draws the render overworld.
*/
void renderOverworldDraw(void);
/**
* Disposes of the render overworld.
*/
void renderOverworldDispose(void);

View File

@@ -0,0 +1,119 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dusksdl2input.h"
#include "render.h"
#include "renderbackbuffer.h"
#include "display/scene/renderscene.h"
#include "display/spritebatch/spritebatch.h"
#include "display/ui/renderui.h"
SDL_Window *RENDER_WINDOW;
SDL_GLContext RENDER_GL_CONTEXT;
bool_t RENDER_RUNNING;
errorret_t renderInit(void) {
// Init SDL
uint32_t flags = SDL_INIT_VIDEO;
#if INPUT_SUPPORT_GAMEPAD
flags |= SDL_INIT_GAMECONTROLLER;
#endif
if(SDL_Init(flags) != 0) {
errorThrow(
"SDL Failed to Initialize: %s",
SDL_GetError()
);
}
// Set OpenGL attributes (Needs to be done now or later?)
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// Create window with OpenGL flag.
RENDER_WINDOW = SDL_CreateWindow(
"DuskSDL2",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
RENDER_WINDOW_WIDTH_DEFAULT,
RENDER_WINDOW_HEIGHT_DEFAULT,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI |
SDL_WINDOW_OPENGL
);
if(!RENDER_WINDOW) {
errorThrow("SDL_CreateWindow failed: %s", SDL_GetError());
}
// Create OpenGL context
RENDER_GL_CONTEXT = SDL_GL_CreateContext(RENDER_WINDOW);
if(!RENDER_GL_CONTEXT) {
errorThrow("SDL_GL_CreateContext failed: %s", SDL_GetError());
}
SDL_GL_SetSwapInterval(1);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);// PSP defaults this on?
glShadeModel(GL_SMOOTH); // Fixes color on PSP?
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glEnableClientState(GL_COLOR_ARRAY);// To confirm: every frame on PSP?
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
spriteBatchInit();
renderBackBufferInit();
renderSceneInit();
renderUIInit();
RENDER_RUNNING = true;
errorOk();
}
errorret_t renderDraw(void) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
RENDER_RUNNING = false;
break;
default:
break;
}
}
// Reset the state
spriteBatchClear();
renderBackBufferBind();
renderSceneDraw();
renderUIDraw();
// Finish rendering, now render back buffer.
renderBackBufferUnbind();
renderBackBufferDraw();
textureBind(NULL);
SDL_GL_SwapWindow(RENDER_WINDOW);
errorOk();
}
errorret_t renderDispose(void) {
renderUIDispose();
renderSceneDispose();
renderBackBufferDispose();
spriteBatchDispose();
// Destroy OpenGL context
SDL_GL_DeleteContext(RENDER_GL_CONTEXT);
SDL_DestroyWindow(RENDER_WINDOW);
SDL_Quit();
errorOk();
}

View File

@@ -0,0 +1,27 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusksdl2.h"
#include "display/renderbase.h"
#ifndef RENDER_WINDOW_WIDTH_DEFAULT
#define RENDER_WINDOW_WIDTH_DEFAULT RENDER_WIDTH * 3
#endif
#ifndef RENDER_WINDOW_HEIGHT_DEFAULT
#define RENDER_WINDOW_HEIGHT_DEFAULT RENDER_HEIGHT * 3
#endif
#if RENDER_WIDTH == RENDER_WINDOW_WIDTH_DEFAULT && RENDER_HEIGHT == RENDER_WINDOW_HEIGHT_DEFAULT
#define RENDER_USE_FRAMEBUFFER 0
#else
#define RENDER_USE_FRAMEBUFFER 1
#endif
extern SDL_Window *RENDER_WINDOW;
extern SDL_Renderer *RENDER_RENDERER;
extern bool_t RENDER_RUNNING;

View File

@@ -0,0 +1,93 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "renderbackbuffer.h"
#include "render.h"
#include "display/spritebatch/spritebatch.h"
#include "display/camera/camera.h"
#if RENDER_USE_FRAMEBUFFER
framebuffer_t RENDER_BACKBUFFER;
#endif
errorret_t renderBackBufferInit(void) {
#if RENDER_USE_FRAMEBUFFER
frameBufferInit(
&RENDER_BACKBUFFER,
RENDER_WIDTH,
RENDER_HEIGHT
);
#else
// No back buffer needed for window rendering
#endif
errorOk();
}
void renderBackBufferBind(void) {
#if RENDER_USE_FRAMEBUFFER
frameBufferBind(&RENDER_BACKBUFFER);
#endif
// Fill background with cornflower blue.
glClearColor(0.392f, 0.584f, 0.929f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void renderBackBufferUnbind(void) {
#if RENDER_USE_FRAMEBUFFER
frameBufferBind(NULL);
#endif
}
void renderBackBufferDraw(void) {
#if RENDER_USE_FRAMEBUFFER
int32_t windowWidth, windowHeight;
SDL_GetWindowSize(RENDER_WINDOW, &windowWidth, &windowHeight);
// Set viewport to match window size
cameraScreenPush();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Create a quad that is scaled to fit but maintain original aspect ratio
int32_t renderWidth, renderHeight, renderX, renderY;
if(RENDER_WIDTH * windowHeight > RENDER_HEIGHT * windowWidth) {
renderWidth = windowWidth;
renderHeight = (RENDER_HEIGHT * windowWidth) / RENDER_WIDTH;
renderX = 0;
renderY = (windowHeight - renderHeight) / 2;
} else {
renderWidth = (RENDER_WIDTH * windowHeight) / RENDER_HEIGHT;
renderHeight = windowHeight;
renderX = (windowWidth - renderWidth) / 2;
renderY = 0;
}
// Draw the back buffer texture
spriteBatchClear();
spriteBatchPush(
&RENDER_BACKBUFFER.texture,
renderX, renderY,
renderX+renderWidth, renderY+renderHeight,
0xFF, 0xFF, 0xFF, 0xFF,
0, 1, 1, 0
);
spriteBatchFlush();
cameraScreenPop();
#else
// No back buffer to draw
#endif
}
errorret_t renderBackBufferDispose(void) {
#if RENDER_USE_FRAMEBUFFER
frameBufferDispose(&RENDER_BACKBUFFER);
#endif
errorOk();
}

View File

@@ -0,0 +1,42 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/renderbase.h"
#include "display/framebuffer/framebuffer.h"
#if RENDER_USE_FRAMEBUFFER
extern framebuffer_t RENDER_BACKBUFFER;
#endif
/**
* Initializes the render back buffer. May be either a framebuffer or a texture
* depending on the render settings.
*/
errorret_t renderBackBufferInit(void);
/**
* Binds the render back buffer as the current render target.
*/
void renderBackBufferBind(void);
/**
* Unbinds the render back buffer, returning to the default render target.
*/
void renderBackBufferUnbind(void);
/**
* Draws the render back buffer to the screen, scaling it to fit the window.
*/
void renderBackBufferDraw(void);
/**
* Disposes of the render back buffer, freeing any resources it holds.
*
* @return An error state if an error occurred, otherwise OK.
*/
errorret_t renderBackBufferDispose(void);

View File

@@ -0,0 +1,13 @@
# 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
renderscene.c
)
# Subdirs
# add_subdirectory(draw)

View File

@@ -0,0 +1,42 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "renderscene.h"
#include "display/overworld/renderoverworld.h"
renderscenecallback_t RENDER_SCENE_CALLBACKS[SCENE_COUNT] = {
[SCENE_INITIAL] = {
.init = NULL,
.draw = NULL,
.dispose = NULL
},
[SCENE_OVERWORLD] = {
.init = renderOverworldInit,
.draw = renderOverworldDraw,
.dispose = renderOverworldDispose
},
};
void renderSceneInit(void) {
for(int32_t i = 0; i < SCENE_COUNT; i++) {
if(!RENDER_SCENE_CALLBACKS[i].init) continue;
RENDER_SCENE_CALLBACKS[i].init();
}
}
void renderSceneDraw(void) {
if(!RENDER_SCENE_CALLBACKS[SCENE_CURRENT].draw) return;
RENDER_SCENE_CALLBACKS[SCENE_CURRENT].draw();
}
void renderSceneDispose(void) {
for(int32_t i = 0; i < SCENE_COUNT; i++) {
if(!RENDER_SCENE_CALLBACKS[i].dispose) continue;
RENDER_SCENE_CALLBACKS[i].dispose();
}
}

View File

@@ -0,0 +1,32 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/scene.h"
typedef struct {
void (*init)(void);
void (*draw)(void);
void (*dispose)(void);
} renderscenecallback_t;
extern renderscenecallback_t RENDER_SCENE_CALLBACKS[SCENE_COUNT];
/**
* Initializes the render scene module.
*/
void renderSceneInit(void);
/**
* Draws the current scene.
*/
void renderSceneDraw(void);
/**
* Disposes of the render scene module.
*/
void renderSceneDispose(void);

View File

@@ -0,0 +1,13 @@
# 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
spritebatch.c
)
# Subdirs
# add_subdirectory(draw)

View File

@@ -0,0 +1,75 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "spritebatch.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "console/console.h"
spritebatch_t SPRITEBATCH;
void spriteBatchInit() {
SPRITEBATCH.spriteCount = 0;
SPRITEBATCH.currentTexture = NULL;
meshInit(
&SPRITEBATCH.mesh,
GL_TRIANGLES,
SPRITEBATCH_VERTEX_COUNT,
&SPRITEBATCH.vertices[0]
);
}
void spriteBatchPush(
texture_t *texture,
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
) {
// Need to flush?
if(
SPRITEBATCH.currentTexture != texture ||
SPRITEBATCH.spriteCount >= SPRITEBATCH_SPRITES_MAX
) {
spriteBatchFlush();
SPRITEBATCH.currentTexture = texture;
}
quadBuffer(
&SPRITEBATCH.vertices[SPRITEBATCH.spriteCount * QUAD_VERTEX_COUNT],
minX, minY, maxX, maxY,
r, g, b, a,
u0, v0, u1, v1
);
SPRITEBATCH.spriteCount++;
}
void spriteBatchClear() {
SPRITEBATCH.spriteCount = 0;
SPRITEBATCH.currentTexture = NULL;
}
void spriteBatchFlush() {
if(SPRITEBATCH.spriteCount == 0) return;
textureBind(SPRITEBATCH.currentTexture);
meshDraw(&SPRITEBATCH.mesh, 0, QUAD_VERTEX_COUNT * SPRITEBATCH.spriteCount);
spriteBatchClear();
}
void spriteBatchDispose() {
meshDispose(&SPRITEBATCH.mesh);
}

View File

@@ -0,0 +1,52 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/mesh/quad.h"
#include "display/texture/texture.h"
#define SPRITEBATCH_SPRITES_MAX 1
#define SPRITEBATCH_VERTEX_COUNT (SPRITEBATCH_SPRITES_MAX * QUAD_VERTEX_COUNT)
typedef struct {
mesh_t mesh;
int32_t spriteCount;
texture_t *currentTexture;
meshvertex_t vertices[SPRITEBATCH_VERTEX_COUNT];
} spritebatch_t;
extern spritebatch_t SPRITEBATCH;
/**
* Initializes a sprite batch.
*
* @param spriteBatch The sprite batch to initialize.
*/
void spriteBatchInit();
void spriteBatchPush(
texture_t *texture,
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
);
void spriteBatchClear();
void spriteBatchFlush();
void spriteBatchDispose();

View File

@@ -0,0 +1,13 @@
# 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
texture.c
)
# Subdirs
# add_subdirectory(draw)

View File

@@ -0,0 +1,78 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "texture.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "util/math.h"
void textureInit(
texture_t *texture,
const int32_t width,
const int32_t height,
const GLenum format,
const uint8_t *data
) {
assertNotNull(texture, "Texture cannot be NULL");
assertTrue(width > 0 && height > 0, "Width and height must be greater than 0");
#if PSP
assertTrue(
width == mathNextPowTwo(width),
"Width must be powers of 2 for PSP"
);
assertTrue(
height == mathNextPowTwo(height),
"Height must be powers of 2 for PSP"
);
#endif
memoryZero(texture, sizeof(texture_t));
texture->width = width;
texture->height = height;
glGenTextures(1, &texture->id);
glBindTexture(GL_TEXTURE_2D, texture->id);
glTexImage2D(
GL_TEXTURE_2D, 0, format, width, height, 0,
format, GL_UNSIGNED_BYTE, data
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
}
void textureBind(const texture_t *texture) {
if(texture == NULL) {
glDisable(GL_TEXTURE_2D);
// glBindTexture(GL_TEXTURE_2D, 0);
return;
}
assertTrue(
texture->id != 0,
"Texture ID must not be 0"
);
assertTrue(
texture->width > 0 && texture->height > 0,
"Texture width and height must be greater than 0"
);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture->id);
}
void textureDispose(texture_t *texture) {
assertNotNull(texture, "Texture cannot be NULL");
assertTrue(texture->id != 0, "Texture ID must not be 0");
glDeleteTextures(1, &texture->id);
}

View File

@@ -0,0 +1,46 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusksdl2.h"
typedef struct {
GLuint id;
int32_t width;
int32_t height;
} texture_t;
/**
* Initializes a texture.
*
* @param texture The texture to initialize.
* @param width The width of the texture.
* @param height The height of the texture.
* @param format The format of the texture (e.g., GL_RGBA, GL_ALPHA).
* @param data The pixel data for the texture.
*/
void textureInit(
texture_t *texture,
const int32_t width,
const int32_t height,
const GLenum format,
const uint8_t *data
);
/**
* Binds a texture for rendering.
*
* @param texture The texture to bind.
*/
void textureBind(const texture_t *texture);
/**
* Disposes a texture.
*
* @param texture The texture to dispose.
*/
void textureDispose(texture_t *texture);

View File

@@ -0,0 +1,14 @@
# 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
renderconsole.c
renderfps.c
rendertext.c
renderui.c
rendertextbox.c
)

View File

@@ -0,0 +1,29 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "renderconsole.h"
#include "console/console.h"
#include "display/ui/rendertext.h"
void renderConsoleDraw(void) {
if(!CONSOLE.visible) return;
int32_t i = 0;
char_t *line;
do {
line = CONSOLE.line[i];
if(line[0] == '\0') {
i++;
continue;
}
renderTextDraw(
0, (CONSOLE_HISTORY_MAX - i - 1) * FONT_TILE_HEIGHT, line,
0xFF, 0xFF, 0xFF
);
i++;
} while(i < CONSOLE_HISTORY_MAX);
}

View File

@@ -0,0 +1,14 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusksdl2.h"
/**
* Draws the console overlay.
*/
void renderConsoleDraw(void);

View File

@@ -0,0 +1,46 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "renderfps.h"
#include "display/render.h"
#include "display/ui/rendertext.h"
#include "time.h"
#include "game.h"
float_t RENDER_FPS_AVG = -1.0f;
float_t RENDER_TPS_AVG = -1.0f;
void renderFPSDraw(void) {
if(TIME.delta > 0) {
float_t fps = 1.0f / TIME.realDelta;
if(RENDER_FPS_AVG == -1.0f) {
RENDER_FPS_AVG = fps;
} else {
RENDER_FPS_AVG = (RENDER_FPS_AVG + fps) / 2.0f;
}
}
if(TIME.time != TIME.lastTick) {
float_t timeSinceLastTick = TIME.realTime - TIME.lastTick;
float_t tps = 1.0f / timeSinceLastTick;
if(RENDER_TPS_AVG == -1.0f) {
RENDER_TPS_AVG = tps;
} else {
RENDER_TPS_AVG = (RENDER_TPS_AVG + tps) / 2.0f;
}
}
char_t buffer[64];
snprintf(buffer, sizeof(buffer), "%.1f/%.1f", RENDER_FPS_AVG, RENDER_TPS_AVG);
int32_t width, height;
renderTextMeasure(buffer, &width, &height);
renderTextDraw(RENDER_WIDTH - width, 0, buffer, 0x00, 0xFF, 0x00);
}

View File

@@ -0,0 +1,13 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
/**
* Draws the FPS overlay.
*/
void renderFPSDraw(void);

View File

@@ -0,0 +1,159 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "rendertext.h"
#include "display/render.h"
#include "assert/assert.h"
#include "display/spritebatch/spritebatch.h"
#include "util/memory.h"
#include "util/math.h"
texture_t RENDER_TEXT_TEXTURE;
static mesh_t RENDER_TEXT_QUAD_MESH;
void renderTextInit(void) {
const int32_t cols = FONT_COLUMN_COUNT;
const int32_t rows = (FONT_TILE_COUNT + cols - 1) / cols;
const int32_t inputFontWidth = cols * FONT_TILE_WIDTH;
const int32_t inputFontHeight = rows * FONT_TILE_HEIGHT;
int32_t outputFontWidth = inputFontWidth;
int32_t outputFontHeight = inputFontHeight;
// Round up to nearest power of 2
#if PSP
outputFontWidth = mathNextPowTwo(inputFontWidth);
outputFontHeight = mathNextPowTwo(inputFontHeight);
#endif
uint8_t *pixels = (uint8_t *)memoryAllocate(
outputFontWidth * outputFontHeight *
sizeof(uint8_t)
);
// Buffer the pixels.
for(int tileIndex = 0; tileIndex < FONT_TILE_COUNT; ++tileIndex) {
const int32_t tileX = (tileIndex % FONT_COLUMN_COUNT) * FONT_TILE_WIDTH;
const int32_t tileY = (tileIndex / FONT_COLUMN_COUNT) * FONT_TILE_HEIGHT;
const uint8_t* tile = TILE_PIXEL_DATA[tileIndex];
for (int y = 0; y < FONT_TILE_HEIGHT; ++y) {
for (int x = 0; x < FONT_TILE_WIDTH; ++x) {
const int32_t pixel = (tileY + y) * outputFontWidth + (tileX + x);
const int32_t pixelOffset = pixel;
uint8_t value = tile[y * FONT_TILE_WIDTH + x];
pixels[pixel] = value ? 0xFF : 0x00; // Alpha channel
}
}
}
textureInit(
&RENDER_TEXT_TEXTURE,
outputFontWidth, outputFontHeight,
GL_ALPHA, pixels
);
memoryFree(pixels);
}
void renderTextDrawChar(
const float_t x,
const float_t y,
const char_t c,
const uint8_t r,
const uint8_t g,
const uint8_t b
) {
int32_t tileIndex = (int32_t)(c) - FONT_CHAR_START;
assertTrue(
tileIndex >= 0 && tileIndex < FONT_TILE_COUNT,
"Character is out of bounds for font tiles"
);
const float_t w = (float)RENDER_TEXT_TEXTURE.width;
const float_t h = (float)RENDER_TEXT_TEXTURE.height;
const int32_t tileX = (tileIndex % FONT_COLUMN_COUNT);
const int32_t tileY = (tileIndex / FONT_COLUMN_COUNT);
spriteBatchPush(
&RENDER_TEXT_TEXTURE,
x, y,
x + FONT_TILE_WIDTH, y + FONT_TILE_HEIGHT,
r, g, b, 0xFF,
(tileX * FONT_TILE_WIDTH) / w,
(tileY * FONT_TILE_HEIGHT) / h,
((tileX + 1) * FONT_TILE_WIDTH) / w,
((tileY + 1) * FONT_TILE_HEIGHT) / h
);
}
void renderTextDraw(
const float_t x,
const float_t y,
const char_t *text,
const uint8_t r,
const uint8_t g,
const uint8_t b
) {
assertNotNull(text, "Text cannot be NULL");
float_t posX = x;
float_t posY = y;
char_t c;
int32_t i = 0;
while((c = text[i++]) != '\0') {
if(c == '\n') {
posX = x;
posY += FONT_TILE_HEIGHT;
continue;
}
renderTextDrawChar(posX, posY, c, r, g, b);
posX += FONT_TILE_WIDTH;
}
}
void renderTextMeasure(
const char_t *text,
int32_t *outWidth,
int32_t *outHeight
) {
assertNotNull(text, "Text cannot be NULL");
assertNotNull(outWidth, "Output width pointer cannot be NULL");
assertNotNull(outHeight, "Output height pointer cannot be NULL");
int32_t width = 0;
int32_t height = FONT_TILE_HEIGHT;
int32_t lineWidth = 0;
char_t c;
int32_t i = 0;
while((c = text[i++]) != '\0') {
if(c == '\n') {
if(lineWidth > width) {
width = lineWidth;
}
lineWidth = 0;
height += FONT_TILE_HEIGHT;
continue;
}
lineWidth += FONT_TILE_WIDTH;
}
if(lineWidth > width) {
width = lineWidth;
}
*outWidth = width;
*outHeight = height;
}
void renderTextDispose(void) {
textureDispose(&RENDER_TEXT_TEXTURE);
}

View File

@@ -0,0 +1,74 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusksdl2.h"
#include "ui/font.h"
#include "display/texture/texture.h"
extern texture_t RENDER_TEXT_TEXTURE;
/**
* Initializes the text rendering system.
*/
void renderTextInit(void);
/**
* Draws a single character at the specified position.
*
* @param x The x-coordinate to draw the character at.
* @param y The y-coordinate to draw the character at.
* @param c The character to draw.
* @param r The red component of the color (0-255).
* @param g The green component of the color (0-255).
* @param b The blue component of the color (0-255).
*/
void renderTextDrawChar(
const float_t x,
const float_t y,
const char_t c,
const uint8_t r,
const uint8_t g,
const uint8_t b
);
/**
* Draws a string of text at the specified position.
*
* @param x The x-coordinate to draw the text at.
* @param y The y-coordinate to draw the text at.
* @param text The null-terminated string of text to draw.
* @param r The red component of the color (0-255).
* @param g The green component of the color (0-255).
* @param b The blue component of the color (0-255).
*/
void renderTextDraw(
const float_t x,
const float_t y,
const char_t *text,
const uint8_t r,
const uint8_t g,
const uint8_t b
);
/**
* Measures the width and height of the given text string when rendered.
*
* @param text The null-terminated string of text to measure.
* @param outWidth Pointer to store the measured width in pixels.
* @param outHeight Pointer to store the measured height in pixels.
*/
void renderTextMeasure(
const char_t *text,
int32_t *outWidth,
int32_t *outHeight
);
/**
* Disposes of the text rendering system, freeing any allocated resources.
*/
void renderTextDispose(void);

View File

@@ -0,0 +1,70 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "rendertextbox.h"
#include "ui/uitextbox.h"
#include "display/ui/rendertext.h"
#include "display/spritebatch/spritebatch.h"
#include "assert/assert.h"
void renderTextboxDraw(void) {
if(!UI_TEXTBOX.visible) return;
// Background
spriteBatchPush(
NULL,
0, RENDER_HEIGHT - UI_TEXTBOX_HEIGHT,
RENDER_WIDTH, RENDER_HEIGHT,
0x00, 0x00, 0x00, 0xFF,
0.0f, 0.0f, 1.0f, 1.0f
);
uint32_t x = 0;
uint32_t y = RENDER_HEIGHT - UI_TEXTBOX_HEIGHT;
if(UI_TEXTBOX.charsRevealed > 0) {
uint8_t charsRendered = 0;
// For each line
for(uint8_t i = 0; i < UI_TEXTBOX_LINES_PER_PAGE; i++) {
// Get count of chars in the line
uint8_t lineLength = UI_TEXTBOX.lineLengths[
i + (UI_TEXTBOX.page * UI_TEXTBOX_LINES_PER_PAGE)
];
if(lineLength == 0) continue;
// Determine how many chars left to render
uint8_t lineChars = UI_TEXTBOX.charsRevealed - charsRendered;
// Don't render more than in line
if(lineChars > lineLength) lineChars = lineLength;
assertTrue(lineChars > 0, "Line chars must be greater than 0");
// Update how many rendered
charsRendered += lineChars;
for(uint8_t j = 0; j < lineChars; j++) {
renderTextDrawChar(
x + UI_TEXTBOX_PADDING_X + UI_TEXTBOX_BORDER_WIDTH + (
j * FONT_TILE_WIDTH
),
y + UI_TEXTBOX_PADDING_Y + UI_TEXTBOX_BORDER_HEIGHT + (
i * FONT_TILE_HEIGHT
),
UI_TEXTBOX.text[
(i * UI_TEXTBOX_CHARS_PER_LINE) + j +
(UI_TEXTBOX.page * UI_TEXTBOX_CHARS_PER_PAGE)
],
0xFF, 0xFF, 0xFF
);
}
// Check if we're done rendering text
if(UI_TEXTBOX.charsRevealed - charsRendered == 0) break;
}
}
}

View File

@@ -0,0 +1,10 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
void renderTextboxDraw(void);

View File

@@ -0,0 +1,63 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "renderui.h"
#include "display/ui/rendertext.h"
#include "display/ui/renderconsole.h"
#include "display/ui/renderfps.h"
#include "display/ui/rendertextbox.h"
#include "display/spritebatch/spritebatch.h"
#include "display/camera/camera.h"
renderuicallback_t RENDER_UI_CALLBACKS[] = {
{
.init = renderTextInit,
.dispose = renderTextDispose
},
{
.draw = renderTextboxDraw,
},
{
.draw = renderConsoleDraw,
},
{
.draw = renderFPSDraw,
},
};
#define RENDER_UI_CALLBACKS_COUNT ( \
sizeof(RENDER_UI_CALLBACKS) / sizeof(RENDER_UI_CALLBACKS[0]) \
)
void renderUIInit(void) {
for (int32_t i = 0; i < RENDER_UI_CALLBACKS_COUNT; i++) {
if(!RENDER_UI_CALLBACKS[i].init) continue;
RENDER_UI_CALLBACKS[i].init();
}
}
void renderUIDraw(void) {
cameraUIPush();
for (int32_t i = 0; i < RENDER_UI_CALLBACKS_COUNT; i++) {
if(!RENDER_UI_CALLBACKS[i].draw) continue;
RENDER_UI_CALLBACKS[i].draw();
}
spriteBatchFlush();
cameraUIPop();
}
void renderUIDispose(void) {
for (int32_t i = 0; i < RENDER_UI_CALLBACKS_COUNT; i++) {
if(!RENDER_UI_CALLBACKS[i].dispose) continue;
RENDER_UI_CALLBACKS[i].dispose();
}
}

View File

@@ -0,0 +1,32 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusksdl2.h"
typedef struct {
void (*init)(void);
void (*draw)(void);
void (*dispose)(void);
} renderuicallback_t;
extern renderuicallback_t RENDER_UI_CALLBACKS[];
/**
* Initialize the UI rendering system.
*/
void renderUIInit(void);
/**
* Draw the UI elements.
*/
void renderUIDraw(void);
/**
* Dispose of the UI rendering system.
*/
void renderUIDispose(void);

View File

@@ -0,0 +1,16 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
#include <SDL2/SDL.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
#include <cglm/cglm.h>

View File

@@ -0,0 +1,44 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dusksdl2input.h"
inputstate_t inputStateGet() {
inputstate_t state = 0;
#if INPUT_SUPPORT_GAMEPAD
// Get gamepad state.
for(int32_t i = 0; i < SDL_NumJoysticks(); i++) {
if(!SDL_IsGameController(i)) continue;
SDL_GameController *controller = SDL_GameControllerOpen(i);
if(!controller) continue;
inputsdlbuttonmap_t *map = INPUT_SDL_BUTTON_MAP;
do {
if(SDL_GameControllerGetButton(controller, map->button)) {
state |= map->bind;
}
map++;
} while(map->bind != 0);
}
#endif
// Get keyboard state.
#if INPUT_SUPPORT_KEYBOARD
const uint8_t *keyboardState = SDL_GetKeyboardState(NULL);
inputsdlkbmap_t *kbmap = INPUT_SDL_KEYBOARD_MAP;
do {
if(keyboardState[kbmap->code]) {
state |= kbmap->bind;
}
kbmap++;
} while(kbmap->bind != 0);
#endif
return state;
}

View File

@@ -0,0 +1,63 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusksdl2.h"
#include "input.h"
#ifndef INPUT_SUPPORT_GAMEPAD
#define INPUT_SUPPORT_GAMEPAD 1
#endif
#ifndef INPUT_SUPPORT_KEYBOARD
#define INPUT_SUPPORT_KEYBOARD 1
#endif
#if INPUT_SUPPORT_GAMEPAD
typedef struct {
const SDL_GameControllerButton button;
const inputbind_t bind;
} inputsdlbuttonmap_t;
static const inputsdlbuttonmap_t INPUT_SDL_BUTTON_MAP[] = {
{ SDL_CONTROLLER_BUTTON_DPAD_UP, INPUT_BIND_UP },
{ SDL_CONTROLLER_BUTTON_DPAD_DOWN, INPUT_BIND_DOWN },
{ SDL_CONTROLLER_BUTTON_DPAD_LEFT, INPUT_BIND_LEFT },
{ SDL_CONTROLLER_BUTTON_DPAD_RIGHT, INPUT_BIND_RIGHT },
{ SDL_CONTROLLER_BUTTON_A, INPUT_BIND_ACTION },
{ SDL_CONTROLLER_BUTTON_B, INPUT_BIND_CANCEL },
{ SDL_CONTROLLER_BUTTON_BACK, INPUT_BIND_CONSOLE },
{ 0, 0 }
};
#endif
#if INPUT_SUPPORT_KEYBOARD
typedef struct {
SDL_Scancode code;
inputbind_t bind;
} inputsdlkbmap_t;
static const inputsdlkbmap_t INPUT_SDL_KEYBOARD_MAP[] = {
{ SDL_SCANCODE_W, INPUT_BIND_UP },
{ SDL_SCANCODE_S, INPUT_BIND_DOWN },
{ SDL_SCANCODE_A, INPUT_BIND_LEFT },
{ SDL_SCANCODE_D, INPUT_BIND_RIGHT },
{ SDL_SCANCODE_LEFT, INPUT_BIND_LEFT },
{ SDL_SCANCODE_RIGHT, INPUT_BIND_RIGHT },
{ SDL_SCANCODE_UP, INPUT_BIND_UP },
{ SDL_SCANCODE_DOWN, INPUT_BIND_DOWN },
{ SDL_SCANCODE_RETURN, INPUT_BIND_ACTION },
{ SDL_SCANCODE_SPACE, INPUT_BIND_ACTION },
{ SDL_SCANCODE_E, INPUT_BIND_ACTION },
{ SDL_SCANCODE_ESCAPE, INPUT_BIND_CANCEL },
{ SDL_SCANCODE_BACKSPACE, INPUT_BIND_CANCEL },
{ SDL_SCANCODE_TAB, INPUT_BIND_CONSOLE },
{ SDL_SCANCODE_GRAVE, INPUT_BIND_CONSOLE },
{ SDL_SCANCODE_Q, INPUT_BIND_QUIT },
{ 0, 0 }
};
#endif

35
archive/dusksdl2/main.c Normal file
View File

@@ -0,0 +1,35 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "display/render.h"
#include "game.h"
#include "input.h"
#define mainError(ret) \
if((ret).code != ERROR_OK) { \
errorPrint(ret); \
return (ret).code; \
}
int main(int argc, char *argv[]) {
errorret_t ret;
mainError(renderInit());
gameInit();
while(RENDER_RUNNING) {
gameUpdate();
mainError(renderDraw());
if(!GAME.running) break;
}
gameDispose();
mainError(renderDispose());
return 0;
}

19
archive/dusksdl2/time.c Normal file
View File

@@ -0,0 +1,19 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "time.h"
#include "dusksdl2.h"
uint32_t TIME_LAST = 0;
float_t timeDeltaGet(void) {
// Get the current time in milliseconds
uint32_t currentTime = SDL_GetTicks();
float_t delta = (currentTime - TIME_LAST) / 1000.0f;
TIME_LAST = currentTime;
return delta;
}