Moved render UI code
This commit is contained in:
@@ -7,15 +7,10 @@
|
|||||||
|
|
||||||
#include "dusksdl2input.h"
|
#include "dusksdl2input.h"
|
||||||
#include "render.h"
|
#include "render.h"
|
||||||
#include "assert/assert.h"
|
|
||||||
#include "renderbackbuffer.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/scene/renderscene.h"
|
||||||
#include "display/spritebatch/spritebatch.h"
|
#include "display/spritebatch/spritebatch.h"
|
||||||
#include "display/camera/camera.h"
|
#include "display/ui/renderui.h"
|
||||||
|
|
||||||
SDL_Window *RENDER_WINDOW;
|
SDL_Window *RENDER_WINDOW;
|
||||||
SDL_GLContext RENDER_GL_CONTEXT;
|
SDL_GLContext RENDER_GL_CONTEXT;
|
||||||
@@ -72,9 +67,9 @@ errorret_t renderInit(void) {
|
|||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
|
||||||
spriteBatchInit();
|
spriteBatchInit();
|
||||||
renderTextInit();
|
|
||||||
renderBackBufferInit();
|
renderBackBufferInit();
|
||||||
renderSceneInit();
|
renderSceneInit();
|
||||||
|
renderUIInit();
|
||||||
|
|
||||||
RENDER_RUNNING = true;
|
RENDER_RUNNING = true;
|
||||||
errorOk();
|
errorOk();
|
||||||
@@ -97,15 +92,8 @@ errorret_t renderDraw(void) {
|
|||||||
spriteBatchClear();
|
spriteBatchClear();
|
||||||
renderBackBufferBind();
|
renderBackBufferBind();
|
||||||
|
|
||||||
// Draw the scene
|
|
||||||
renderSceneDraw();
|
renderSceneDraw();
|
||||||
|
renderUIDraw();
|
||||||
// Draw UI
|
|
||||||
cameraUIPush();
|
|
||||||
renderConsoleDraw();
|
|
||||||
renderFPSDraw();
|
|
||||||
spriteBatchFlush();
|
|
||||||
cameraUIPop();
|
|
||||||
|
|
||||||
// Finish rendering, now render back buffer.
|
// Finish rendering, now render back buffer.
|
||||||
renderBackBufferUnbind();
|
renderBackBufferUnbind();
|
||||||
@@ -117,9 +105,9 @@ errorret_t renderDraw(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
errorret_t renderDispose(void) {
|
errorret_t renderDispose(void) {
|
||||||
|
renderUIDispose();
|
||||||
renderSceneDispose();
|
renderSceneDispose();
|
||||||
renderBackBufferDispose();
|
renderBackBufferDispose();
|
||||||
renderTextDispose();
|
|
||||||
spriteBatchDispose();
|
spriteBatchDispose();
|
||||||
|
|
||||||
// Destroy OpenGL context
|
// Destroy OpenGL context
|
||||||
|
@@ -9,4 +9,5 @@ target_sources(${DUSK_TARGET_NAME}
|
|||||||
renderconsole.c
|
renderconsole.c
|
||||||
renderfps.c
|
renderfps.c
|
||||||
rendertext.c
|
rendertext.c
|
||||||
|
renderui.c
|
||||||
)
|
)
|
@@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "renderfps.h"
|
#include "renderfps.h"
|
||||||
|
#include "display/render.h"
|
||||||
#include "display/ui/rendertext.h"
|
#include "display/ui/rendertext.h"
|
||||||
#include "time.h"
|
#include "time.h"
|
||||||
|
|
||||||
@@ -13,16 +14,19 @@ void renderFPSDraw(void) {
|
|||||||
float_t fps = 1.0f / TIME.delta;
|
float_t fps = 1.0f / TIME.delta;
|
||||||
|
|
||||||
char_t buffer[32];
|
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) {
|
if(fps >= 50.0f) {
|
||||||
// Green
|
// Green
|
||||||
renderTextDraw(0, 0, buffer, 0x00, 0xFF, 0x00);
|
renderTextDraw(RENDER_WIDTH - width, 0, buffer, 0x00, 0xFF, 0x00);
|
||||||
} else if(fps >= 30.0f) {
|
} else if(fps >= 30.0f) {
|
||||||
// Yellow
|
// Yellow
|
||||||
renderTextDraw(0, 0, buffer, 0xFF, 0xFF, 0x00);
|
renderTextDraw(RENDER_WIDTH - width, 0, buffer, 0xFF, 0xFF, 0x00);
|
||||||
} else {
|
} else {
|
||||||
// Red
|
// Red
|
||||||
renderTextDraw(0, 0, buffer, 0xFF, 0x00, 0x00);
|
renderTextDraw(RENDER_WIDTH - width, 0, buffer, 0xFF, 0x00, 0x00);
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -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) {
|
void renderTextDispose(void) {
|
||||||
textureDispose(&RENDER_TEXT_TEXTURE);
|
textureDispose(&RENDER_TEXT_TEXTURE);
|
||||||
}
|
}
|
@@ -55,6 +55,19 @@ void renderTextDraw(
|
|||||||
const uint8_t b
|
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.
|
* Disposes of the text rendering system, freeing any allocated resources.
|
||||||
*/
|
*/
|
||||||
|
29
src/dusksdl2/display/ui/renderui.c
Normal file
29
src/dusksdl2/display/ui/renderui.c
Normal 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();
|
||||||
|
}
|
13
src/dusksdl2/display/ui/renderui.h
Normal file
13
src/dusksdl2/display/ui/renderui.h
Normal 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);
|
@@ -46,8 +46,15 @@
|
|||||||
{ SDL_SCANCODE_S, INPUT_BIND_DOWN },
|
{ SDL_SCANCODE_S, INPUT_BIND_DOWN },
|
||||||
{ SDL_SCANCODE_A, INPUT_BIND_LEFT },
|
{ SDL_SCANCODE_A, INPUT_BIND_LEFT },
|
||||||
{ SDL_SCANCODE_D, INPUT_BIND_RIGHT },
|
{ 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_SPACE, INPUT_BIND_ACTION },
|
||||||
{ SDL_SCANCODE_ESCAPE, INPUT_BIND_CANCEL },
|
{ 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_GRAVE, INPUT_BIND_CONSOLE },
|
||||||
{ SDL_SCANCODE_Q, INPUT_BIND_QUIT },
|
{ SDL_SCANCODE_Q, INPUT_BIND_QUIT },
|
||||||
{ 0, 0 }
|
{ 0, 0 }
|
||||||
|
Reference in New Issue
Block a user