Game updates

This commit is contained in:
2025-10-01 17:07:29 -05:00
parent 22e2f703db
commit a734ecaa10
21 changed files with 77 additions and 235 deletions

View File

@@ -9,7 +9,6 @@ target_sources(${DUSK_TARGET_NAME}
display.c
camera.c
tileset.c
screen.c
)
# Subdirectories
@@ -19,7 +18,6 @@ add_subdirectory(palette)
add_subdirectory(texture)
add_subdirectory(scene)
add_subdirectory(spritebatch)
add_subdirectory(ui)
if(DUSK_TARGET_SYSTEM STREQUAL "linux")
target_compile_definitions(${DUSK_TARGET_NAME}

View File

@@ -10,9 +10,8 @@
#include "display/framebuffer/framebuffer.h"
#include "display/scene/scenemanager.h"
#include "display/spritebatch/spritebatch.h"
#include "display/ui/ui.h"
#include "display/mesh/quad.h"
#include "display/screen.h"
#include "game/game.h"
display_t DISPLAY;
@@ -67,9 +66,7 @@ errorret_t displayInit(void) {
quadInit();
frameBufferInitBackbuffer();
spriteBatchInit();
errorChain(uiInit());
errorChain(sceneManagerInit());
screenInit();
errorOk();
}
@@ -107,15 +104,14 @@ errorret_t displayUpdate(void) {
#endif
spriteBatchClear();
screenBind();
frameBufferBind(NULL);
frameBufferClear(
FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH,
COLOR_CORNFLOWER_BLUE
);
sceneManagerUpdate();
uiUpdate();
sceneManagerRender();
uiRender();
screenUnbindAndRender();
gameRender();
spriteBatchFlush();
#if DISPLAY_SDL2
SDL_GL_SwapWindow(DISPLAY.window);
@@ -131,9 +127,7 @@ errorret_t displayUpdate(void) {
}
errorret_t displayDispose(void) {
screenDispose();
sceneManagerDispose();
uiDispose();
spriteBatchDispose();
#if DISPLAY_SDL2

View File

@@ -1,112 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "screen.h"
#include "assert/assert.h"
#include "display/spritebatch/spritebatch.h"
screen_t SCREEN;
void screenInit(void) {
// Virtual backbuffer for dynamic resolution scaling
#if DISPLAY_SIZE_DYNAMIC == 1
frameBufferInit(&SCREEN.frameBuffer, DISPLAY_WIDTH, DISPLAY_HEIGHT);
cameraInit(&SCREEN.frameBufferCamera);
SCREEN.frameBufferCamera.projType = CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC;
SCREEN.frameBufferCamera.viewType = CAMERA_VIEW_TYPE_MATRIX;
glm_lookat(
(vec3){0.0f, 0.0f, 1.0f},
(vec3){0.0f, 0.0f, 0.0f},
(vec3){0.0f, 1.0f, 0.0f},
SCREEN.frameBufferCamera.view
);
SCREEN.frameBufferCamera.nearClip = -1.0f;
SCREEN.frameBufferCamera.farClip = 1.0f;
#endif
}
void screenBind(void) {
#if DISPLAY_SIZE_DYNAMIC == 1
frameBufferBind(&SCREEN.frameBuffer);
#else
frameBufferBind(NULL);
#endif
frameBufferClear(
FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH,
COLOR_CORNFLOWER_BLUE
);
}
void screenUnbindAndRender(void) {
assertTrue(SPRITEBATCH.spriteCount == 0, "Sprite batch not flushed");
// Render to real backbuffer
#if DISPLAY_SIZE_DYNAMIC == 1
frameBufferBind(NULL);
frameBufferClear(
FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH,
COLOR_BLACK
);
SCREEN.frameBufferCamera.orthographic.left = 0;
SCREEN.frameBufferCamera.orthographic.right = frameBufferGetWidth(
FRAMEBUFFER_BOUND
);
SCREEN.frameBufferCamera.orthographic.bottom = frameBufferGetHeight(
FRAMEBUFFER_BOUND
);
SCREEN.frameBufferCamera.orthographic.top = 0;
cameraPushMatrix(&SCREEN.frameBufferCamera);
vec2 backbuffer = {
(float_t)frameBufferGetWidth(FRAMEBUFFER_BOUND),
(float_t)frameBufferGetHeight(FRAMEBUFFER_BOUND)
};
vec2 virtual = {
(float_t)frameBufferGetWidth(&SCREEN.frameBuffer),
(float_t)frameBufferGetHeight(&SCREEN.frameBuffer)
};
// Compare aspect ratios.
vec4 viewport;
float_t backbufferAspect = backbuffer[0] / backbuffer[1];
float_t virtualAspect = virtual[0] / virtual[1];
if (backbufferAspect > virtualAspect) {
// Backbuffer is wider: pillarbox
float_t scale = backbuffer[1] / virtual[1];
float_t width = virtual[0] * scale;
viewport[0] = (backbuffer[0] - width) * 0.5f;
viewport[1] = 0;
viewport[2] = width;
viewport[3] = backbuffer[1];
} else {
// Backbuffer is taller: letterbox
float_t scale = backbuffer[0] / virtual[0];
float_t height = virtual[1] * scale;
viewport[0] = 0;
viewport[1] = (backbuffer[1] - height) * 0.5f;
viewport[2] = backbuffer[0];
viewport[3] = height;
}
spriteBatchPush(
&SCREEN.frameBuffer.texture,
viewport[0], viewport[1],
viewport[0] + viewport[2], viewport[1] + viewport[3],
COLOR_WHITE,
0.0f, 1.0f, 1.0f, 0.0f
);
spriteBatchFlush();
cameraPopMatrix();
#endif
}
void screenDispose(void) {
frameBufferDispose(&SCREEN.frameBuffer);
}

View File

@@ -1,41 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/camera.h"
#include "display/framebuffer/framebuffer.h"
typedef struct {
#if DISPLAY_SIZE_DYNAMIC == 1
framebuffer_t frameBuffer;
camera_t frameBufferCamera;
#else
void *empty;
#endif
} screen_t;
extern screen_t SCREEN;
/**
* Initializes the screen.
*/
void screenInit(void);
/**
* Binds the screen for rendering.
*/
void screenBind(void);
/**
* Unbinds the screen and renders it.
*/
void screenUnbindAndRender(void);
/**
* Disposes of the screen.
*/
void screenDispose(void);

View File

@@ -1,13 +0,0 @@
# 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
uitext.c
ui.c
uifps.c
uiconsole.c
)

View File

@@ -1,75 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "ui.h"
#include "uitext.h"
#include "display/framebuffer/framebuffer.h"
#include "display/spritebatch/spritebatch.h"
#include "uiconsole.h"
#include "uifps.h"
#include "util/memory.h"
#include "display/tileset/tileset_minogram.h"
ui_t UI;
errorret_t uiInit(void) {
memoryZero(&UI, sizeof(ui_t));
// Load debug font.
UI.debugFontTileset = &TILESET_MINOGRAM;
errorChain(assetManagerLoadAsset(
UI.debugFontTileset->image,
&UI.debugFontAsset,
&UI.debugFontRef
));
// Setup UI Camera.
cameraInit(&UI.camera);
UI.camera.projType = CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC;
UI.camera.orthographic.left = 0.0f;
UI.camera.orthographic.top = 0.0f;
UI.camera.orthographic.right = frameBufferGetWidth(&FRAMEBUFFER_BACKBUFFER);
UI.camera.orthographic.bottom = frameBufferGetHeight(&FRAMEBUFFER_BACKBUFFER);
UI.camera.nearClip = -1.0f;
UI.camera.farClip = 1.0f;
UI.camera.viewType = CAMERA_VIEW_TYPE_MATRIX;
glm_mat4_identity(UI.camera.view);
UI.scale = 1.0f;
// Setup FPS element.
uiFPSInit();
errorOk();
}
void uiUpdate(void) {
}
void uiRender(void) {
UI.camera.orthographic.right = (
frameBufferGetWidth(FRAMEBUFFER_BOUND) / UI.scale
);
UI.camera.orthographic.bottom = (
frameBufferGetHeight(FRAMEBUFFER_BOUND) / UI.scale
);
cameraPushMatrix(&UI.camera);
uiConsoleRender();
uiFPSRender();
spriteBatchFlush();
cameraPopMatrix();
}
void uiDispose(void) {
}

View File

@@ -1,46 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#include "display/camera.h"
#include "display/texture/texture.h"
#include "display/tileset.h"
#include "asset/asset.h"
typedef struct {
camera_t camera;
float_t scale;
const tileset_t *debugFontTileset;
asset_t *debugFontAsset;
ref_t debugFontRef;
} ui_t;
extern ui_t UI;
/**
* Initializes the UI system.
*
* @return An errorret_t indicating success or failure.
*/
errorret_t uiInit(void);
/**
* Updates the UI system. Will not render anything.
*/
void uiUpdate(void);
/**
* Renders the UI system.
*/
void uiRender(void);
/**
* Disposes of the UI system.
*/
void uiDispose(void);

View File

@@ -1,32 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "uiconsole.h"
#include "uitext.h"
#include "console/console.h"
#include "ui.h"
void uiConsoleRender(void) {
if(!CONSOLE.visible) return;
consolePrint("Test\n");
int32_t i = CONSOLE_HISTORY_MAX - 1;
char_t *line;
do {
line = CONSOLE.line[i];
if(line[0] == '\0') {
i--;
continue;
}
uiTextDraw(
0, i * TILESET_MINOGRAM.tileHeight,
line, COLOR_WHITE,
UI.debugFontTileset, &UI.debugFontAsset->alphaImage.texture
);
i--;
} while(i > 0);
}

View File

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

View File

@@ -1,45 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "uifps.h"
#include "display/ui/uitext.h"
#include "time/time.h"
#include "console/console.h"
#include "util/string.h"
#include "ui.h"
void uiFPSInit(void) {
consoleRegVar("fps", "0", NULL);
}
void uiFPSRender(void) {
if(stringCompare(consoleVarGet("fps")->value, "0") == 0) {
return;
}
float_t fps = TIME.delta > 0.0f ? (1.0f / TIME.delta) : 0.0f;
char_t buffer[64];
snprintf(
buffer,
sizeof(buffer),
"%.2f/%d",
TIME.delta * 1000.0f,
(int32_t)fps
);
color_t color = (
fps >= 50.0f ? COLOR_GREEN :
fps >= 30.0f ? COLOR_YELLOW :
COLOR_RED
);
uiTextDraw(
0, 0,
buffer, color,
UI.debugFontTileset, &UI.debugFontAsset->alphaImage.texture
);
}

View File

@@ -1,12 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
void uiFPSInit(void);
void uiFPSRender(void);

View File

@@ -1,112 +0,0 @@
// /**
// * Copyright (c) 2025 Dominic Masters
// *
// * This software is released under the MIT License.
// * https://opensource.org/licenses/MIT
// */
#include "uitext.h"
#include "asset/assetmanager.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "display/spritebatch/spritebatch.h"
void uiTextDrawChar(
const float_t x,
const float_t y,
const char_t c,
const color_t color,
const tileset_t *tileset,
texture_t *texture
) {
int32_t tileIndex = (int32_t)(c) - UI_TEXT_CHAR_START;
if(tileIndex < 0 || tileIndex >= tileset->tileCount) {
tileIndex = ((int32_t)'@') - UI_TEXT_CHAR_START;
}
assertTrue(
tileIndex >= 0 && tileIndex <= tileset->tileCount,
"Character is out of bounds for font tiles"
);
vec4 uv;
tilesetTileGetUV(tileset, tileIndex, uv);
spriteBatchPush(
texture,
x, y,
x + tileset->tileWidth,
y + tileset->tileHeight,
color,
uv[0], uv[1], uv[2], uv[3]
);
}
void uiTextDraw(
const float_t x,
const float_t y,
const char_t *text,
const color_t color,
const tileset_t *tileset,
texture_t *texture
) {
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 += tileset->tileHeight;
continue;
}
if(c == ' ') {
posX += tileset->tileWidth;
continue;
}
uiTextDrawChar(posX, posY, c, color, tileset, texture);
posX += tileset->tileWidth;
}
}
void uiTextMeasure(
const char_t *text,
const tileset_t *tileset,
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 = tileset->tileHeight;
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 += tileset->tileHeight;
continue;
}
lineWidth += tileset->tileWidth;
}
if(lineWidth > width) {
width = lineWidth;
}
*outWidth = width;
*outHeight = height;
}

View File

@@ -1,65 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "asset/assetmanager.h"
#include "display/tileset/tileset_minogram.h"
#define UI_TEXT_CHAR_START '!'
/**
* 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 color The color to draw the character in.
* @param tileset Font tileset to use for rendering.
* @param texture Texture containing the font tileset image.
*/
void uiTextDrawChar(
const float_t x,
const float_t y,
const char_t c,
const color_t color,
const tileset_t *tileset,
texture_t *texture
);
/**
* 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 color The color to draw the text in.
* @param tileset Font tileset to use for rendering.
* @param texture Texture containing the font tileset image.
*/
void uiTextDraw(
const float_t x,
const float_t y,
const char_t *text,
const color_t color,
const tileset_t *tileset,
texture_t *texture
);
/**
* Measures the width and height of the given text string when rendered.
*
* @param text The null-terminated string of text to measure.
* @param tileset Font tileset to use for measurement.
* @param outWidth Pointer to store the measured width in pixels.
* @param outHeight Pointer to store the measured height in pixels.
*/
void uiTextMeasure(
const char_t *text,
const tileset_t *tileset,
int32_t *outWidth,
int32_t *outHeight
);