render: Add SDL_RenderDebugText().

Fixes #11201.
This commit is contained in:
Ryan C. Gordon 2024-10-13 21:48:11 -04:00
parent 44bc19b592
commit c20918b0fb
11 changed files with 2594 additions and 3354 deletions

View file

@ -23,6 +23,7 @@
// The SDL 2D rendering system
#include "SDL_sysrender.h"
#include "SDL_render_debug_font.h"
#include "software/SDL_render_sw_c.h"
#include "../video/SDL_pixels_c.h"
#include "../video/SDL_video_c.h"
@ -5097,6 +5098,9 @@ void SDL_DestroyRendererWithoutFreeing(SDL_Renderer *renderer)
SDL_DiscardAllCommands(renderer);
SDL_DestroyTexture(renderer->debug_char_texture_atlas);
renderer->debug_char_texture_atlas = NULL;
// Free existing textures for this renderer
while (renderer->textures) {
SDL_Texture *tex = renderer->textures;
@ -5346,3 +5350,119 @@ bool SDL_GetRenderVSync(SDL_Renderer *renderer, int *vsync)
}
return true;
}
#define SDL_DEBUG_FONT_GLYPHS_PER_ROW 14
static bool CreateDebugTextAtlas(SDL_Renderer *renderer)
{
SDL_assert(renderer->debug_char_texture_atlas == NULL); // don't double-create it!
const int charWidth = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
const int charHeight = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
// actually make each glyph two pixels taller/wider, to prevent scaling artifacts.
const int rows = (SDL_DEBUG_FONT_NUM_GLYPHS / SDL_DEBUG_FONT_GLYPHS_PER_ROW) + 1;
SDL_Surface *atlas = SDL_CreateSurface((charWidth + 2) * SDL_DEBUG_FONT_GLYPHS_PER_ROW, rows * (charHeight + 2), SDL_PIXELFORMAT_RGBA8888);
if (!atlas) {
return false;
}
const int pitch = atlas->pitch;
SDL_memset(atlas->pixels, '\0', atlas->h * atlas->pitch);
int column = 0;
int row = 0;
for (int glyph = 0; glyph < SDL_DEBUG_FONT_NUM_GLYPHS; glyph++) {
// find top-left of this glyph in destination surface. The +2's account for glyph padding.
Uint8 *linepos = (((Uint8 *)atlas->pixels) + ((row * (charHeight + 2) + 1) * pitch)) + ((column * (charWidth + 2) + 1) * sizeof (Uint32));
const Uint8 *charpos = SDL_RenderDebugTextFontData + (glyph * 8);
// Draw the glyph to the surface...
for (int iy = 0; iy < charHeight; iy++) {
Uint32 *curpos = (Uint32 *)linepos;
for (int ix = 0; ix < charWidth; ix++) {
if ((*charpos) & (1 << ix)) {
*curpos = 0xffffffff;
} else {
*curpos = 0;
}
++curpos;
}
linepos += pitch;
++charpos;
}
// move to next position (and if too far, start the next row).
column++;
if (column >= SDL_DEBUG_FONT_GLYPHS_PER_ROW) {
row++;
column = 0;
}
}
SDL_assert((row < rows) || ((row == rows) && (column == 0))); // make sure we didn't overflow the surface.
// Convert temp surface into texture
renderer->debug_char_texture_atlas = SDL_CreateTextureFromSurface(renderer, atlas);
SDL_DestroySurface(atlas);
return (renderer->debug_char_texture_atlas != NULL);
}
static bool DrawDebugCharacter(SDL_Renderer *renderer, float x, float y, Uint32 c)
{
SDL_assert(renderer->debug_char_texture_atlas != NULL); // should have been created by now!
const int charWidth = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
const int charHeight = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
// Character index in cache
Uint32 ci = c;
if ((ci <= 32) || ((ci >= 127) && (ci <= 160))) {
return true; // these are just completely blank chars, don't bother doing anything.
} else if (ci >= SDL_DEBUG_FONT_NUM_GLYPHS) {
ci = SDL_DEBUG_FONT_NUM_GLYPHS - 1; // use our "not a valid/supported character" glyph.
} else if (ci < 127) {
ci -= 33; // adjust for the 33 blank glyphs at the start
} else {
ci -= 67; // adjust for the 33 blank glyphs at the start AND the 34 gap in the middle.
}
const float src_x = (float) (((ci % SDL_DEBUG_FONT_GLYPHS_PER_ROW) * (charWidth + 2)) + 1);
const float src_y = (float) (((ci / SDL_DEBUG_FONT_GLYPHS_PER_ROW) * (charHeight + 2)) + 1);
// Draw texture onto destination
const SDL_FRect srect = { src_x, src_y, (float) charWidth, (float) charHeight };
const SDL_FRect drect = { x, y, (float) charWidth, (float) charHeight };
return SDL_RenderTexture(renderer, renderer->debug_char_texture_atlas, &srect, &drect);
}
bool SDL_RenderDebugText(SDL_Renderer *renderer, float x, float y, const char *s)
{
CHECK_RENDERER_MAGIC(renderer, false);
// Allocate a texture atlas for this renderer if needed.
if (!renderer->debug_char_texture_atlas) {
if (!CreateDebugTextAtlas(renderer)) {
return false;
}
}
bool result = true;
Uint8 r, g, b, a;
result &= SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
result &= SDL_SetTextureColorMod(renderer->debug_char_texture_atlas, r, g, b);
result &= SDL_SetTextureAlphaMod(renderer->debug_char_texture_atlas, a);
float curx = x;
Uint32 ch;
while (result && ((ch = SDL_StepUTF8(&s, NULL)) != 0)) {
result &= DrawDebugCharacter(renderer, curx, y, ch);
curx += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
}
return result;
}