Moved render UI code

This commit is contained in:
2025-08-17 16:31:08 -05:00
parent 3e19771d8f
commit fbbd2176fd
8 changed files with 111 additions and 20 deletions

View File

@@ -7,15 +7,10 @@
#include "dusksdl2input.h"
#include "render.h"
#include "assert/assert.h"
#include "renderbackbuffer.h"
#include "display/ui/rendertext.h"
#include "display/ui/renderconsole.h"
#include "display/ui/renderfps.h"
#include "console/console.h"
#include "display/scene/renderscene.h"
#include "display/spritebatch/spritebatch.h"
#include "display/camera/camera.h"
#include "display/ui/renderui.h"
SDL_Window *RENDER_WINDOW;
SDL_GLContext RENDER_GL_CONTEXT;
@@ -72,9 +67,9 @@ errorret_t renderInit(void) {
glEnableClientState(GL_VERTEX_ARRAY);
spriteBatchInit();
renderTextInit();
renderBackBufferInit();
renderSceneInit();
renderUIInit();
RENDER_RUNNING = true;
errorOk();
@@ -97,15 +92,8 @@ errorret_t renderDraw(void) {
spriteBatchClear();
renderBackBufferBind();
// Draw the scene
renderSceneDraw();
// Draw UI
cameraUIPush();
renderConsoleDraw();
renderFPSDraw();
spriteBatchFlush();
cameraUIPop();
renderUIDraw();
// Finish rendering, now render back buffer.
renderBackBufferUnbind();
@@ -117,9 +105,9 @@ errorret_t renderDraw(void) {
}
errorret_t renderDispose(void) {
renderUIDispose();
renderSceneDispose();
renderBackBufferDispose();
renderTextDispose();
spriteBatchDispose();
// Destroy OpenGL context

View File

@@ -9,4 +9,5 @@ target_sources(${DUSK_TARGET_NAME}
renderconsole.c
renderfps.c
rendertext.c
renderui.c
)

View File

@@ -6,6 +6,7 @@
*/
#include "renderfps.h"
#include "display/render.h"
#include "display/ui/rendertext.h"
#include "time.h"
@@ -13,16 +14,19 @@ void renderFPSDraw(void) {
float_t fps = 1.0f / TIME.delta;
char_t buffer[32];
snprintf(buffer, sizeof(buffer), "FPS: %.1f", fps);
snprintf(buffer, sizeof(buffer), "%.1f FPS", fps);
int32_t width, height;
renderTextMeasure(buffer, &width, &height);
if(fps >= 50.0f) {
// Green
renderTextDraw(0, 0, buffer, 0x00, 0xFF, 0x00);
renderTextDraw(RENDER_WIDTH - width, 0, buffer, 0x00, 0xFF, 0x00);
} else if(fps >= 30.0f) {
// Yellow
renderTextDraw(0, 0, buffer, 0xFF, 0xFF, 0x00);
renderTextDraw(RENDER_WIDTH - width, 0, buffer, 0xFF, 0xFF, 0x00);
} else {
// Red
renderTextDraw(0, 0, buffer, 0xFF, 0x00, 0x00);
renderTextDraw(RENDER_WIDTH - width, 0, buffer, 0xFF, 0x00, 0x00);
}
}

View File

@@ -117,6 +117,42 @@ void renderTextDraw(
}
}
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

@@ -55,6 +55,19 @@ void renderTextDraw(
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.
*/

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 "renderui.h"
#include "display/ui/rendertext.h"
#include "display/ui/renderconsole.h"
#include "display/ui/renderfps.h"
#include "display/spritebatch/spritebatch.h"
#include "display/camera/camera.h"
void renderUIInit(void) {
renderTextInit();
}
void renderUIDraw(void) {
cameraUIPush();
renderConsoleDraw();
renderFPSDraw();
spriteBatchFlush();
cameraUIPop();
}
void renderUIDispose(void) {
renderTextDispose();
}

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
#include "dusksdl2.h"
void renderUIInit(void);
void renderUIDraw(void);
void renderUIDispose(void);

View File

@@ -46,8 +46,15 @@
{ 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_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 }