mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-14 05:46:00 +00:00
render: Updates to format-string versions of SDL_RenderDebugText.
- Removes SDL_RenderDebugTextV - Changes SDL_RenderDebugTextF to SDL_RenderDebugTextFormat and tweaks it to work in a world without SDL_RenderDebugTextV. - Tweaked rendering position of formatted text in the example program.
This commit is contained in:
@@ -149,15 +149,29 @@ static void SDL_InitDynamicAPI(void);
|
||||
va_end(ap); \
|
||||
return result; \
|
||||
} \
|
||||
_static bool SDLCALL SDL_RenderDebugTextF##name(SDL_Renderer *renderer, float x, float y, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
|
||||
_static bool SDLCALL SDL_RenderDebugTextFormat##name(SDL_Renderer *renderer, float x, float y, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
|
||||
{ \
|
||||
bool result; \
|
||||
char buf[128], *str = buf; \
|
||||
int result; \
|
||||
va_list ap; \
|
||||
initcall; \
|
||||
va_start(ap, fmt); \
|
||||
result = jump_table.SDL_RenderDebugTextV(renderer, x, y, fmt, ap); \
|
||||
result = jump_table.SDL_vsnprintf(buf, sizeof(buf), fmt, ap); \
|
||||
va_end(ap); \
|
||||
return result; \
|
||||
if (result >= 0 && (size_t)result >= sizeof(buf)) { \
|
||||
str = NULL; \
|
||||
va_start(ap, fmt); \
|
||||
result = jump_table.SDL_vasprintf(&str, fmt, ap); \
|
||||
va_end(ap); \
|
||||
} \
|
||||
bool retval = false; \
|
||||
if (result >= 0) { \
|
||||
retval = jump_table.SDL_RenderDebugTextFormat(renderer, x, y, "%s", str); \
|
||||
} \
|
||||
if (str != buf) { \
|
||||
jump_table.SDL_free(str); \
|
||||
} \
|
||||
return retval; \
|
||||
} \
|
||||
_static void SDLCALL SDL_Log##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
|
||||
{ \
|
||||
|
@@ -1207,8 +1207,7 @@ SDL3_0.0.0 {
|
||||
SDL_SetGPUAllowedFramesInFlight;
|
||||
SDL_RenderTextureAffine;
|
||||
SDL_WaitAndAcquireGPUSwapchainTexture;
|
||||
SDL_RenderDebugTextF;
|
||||
SDL_RenderDebugTextV;
|
||||
SDL_RenderDebugTextFormat;
|
||||
# extra symbols go here (don't modify this line)
|
||||
local: *;
|
||||
};
|
||||
|
@@ -1232,5 +1232,4 @@
|
||||
#define SDL_SetGPUAllowedFramesInFlight SDL_SetGPUAllowedFramesInFlight_REAL
|
||||
#define SDL_RenderTextureAffine SDL_RenderTextureAffine_REAL
|
||||
#define SDL_WaitAndAcquireGPUSwapchainTexture SDL_WaitAndAcquireGPUSwapchainTexture_REAL
|
||||
#define SDL_RenderDebugTextF SDL_RenderDebugTextF_REAL
|
||||
#define SDL_RenderDebugTextV SDL_RenderDebugTextV_REAL
|
||||
#define SDL_RenderDebugTextFormat SDL_RenderDebugTextFormat_REAL
|
||||
|
@@ -1239,6 +1239,5 @@ SDL_DYNAPI_PROC(bool,SDL_SetGPUAllowedFramesInFlight,(SDL_GPUDevice *a,Uint32 b)
|
||||
SDL_DYNAPI_PROC(bool,SDL_RenderTextureAffine,(SDL_Renderer *a,SDL_Texture *b,const SDL_FRect *c,const SDL_FPoint *d,const SDL_FPoint *e,const SDL_FPoint *f),(a,b,c,d,e,f),return)
|
||||
SDL_DYNAPI_PROC(bool,SDL_WaitAndAcquireGPUSwapchainTexture,(SDL_GPUCommandBuffer *a,SDL_Window *b,SDL_GPUTexture **c,Uint32 *d,Uint32 *e),(a,b,c,d,e),return)
|
||||
#ifndef SDL_DYNAPI_PROC_NO_VARARGS
|
||||
SDL_DYNAPI_PROC(bool,SDL_RenderDebugTextF,(SDL_Renderer *a,float b,float c,SDL_PRINTF_FORMAT_STRING const char *d,...),(a,b,c,d),return)
|
||||
SDL_DYNAPI_PROC(bool,SDL_RenderDebugTextFormat,(SDL_Renderer *a,float b,float c,SDL_PRINTF_FORMAT_STRING const char *d,...),(a,b,c,d),return)
|
||||
#endif
|
||||
SDL_DYNAPI_PROC(bool,SDL_RenderDebugTextV,(SDL_Renderer *a,float b,float c,SDL_PRINTF_FORMAT_STRING const char *d,va_list e),(a,b,c,d,e),return)
|
||||
|
@@ -5590,45 +5590,28 @@ bool SDL_RenderDebugText(SDL_Renderer *renderer, float x, float y, const char *s
|
||||
return result;
|
||||
}
|
||||
|
||||
bool SDL_RenderDebugTextF(SDL_Renderer *renderer, float x, float y, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
|
||||
bool SDL_RenderDebugTextFormat(SDL_Renderer *renderer, float x, float y, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
bool result = SDL_RenderDebugTextV(renderer, x, y, fmt, ap);
|
||||
|
||||
// fast path to avoid unnecessary allocation and copy. If you're going through the dynapi, there's a good chance
|
||||
// you _always_ hit this path, since it probably had to process varargs before calling into the jumptable.
|
||||
if (SDL_strcmp(fmt, "%s") == 0) {
|
||||
const char *str = va_arg(ap, const char *);
|
||||
va_end(ap);
|
||||
return SDL_RenderDebugText(renderer, x, y, str);
|
||||
}
|
||||
|
||||
char *str = NULL;
|
||||
const int rc = SDL_vasprintf(&str, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool SDL_RenderDebugTextV(SDL_Renderer *renderer, float x, float y, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
|
||||
{
|
||||
// Probably for the best to check this here, so we don't do a bunch of string formatting before realizing the renderer isn't even valid...
|
||||
CHECK_RENDERER_MAGIC(renderer, false);
|
||||
|
||||
va_list apc;
|
||||
va_copy(apc, ap); // vsnprintf mangles ap, so copy it so it can be used again later
|
||||
int len = SDL_vsnprintf(NULL, 0, fmt, apc);
|
||||
va_end(apc);
|
||||
|
||||
if (len < 0) {
|
||||
return SDL_SetError("Failed to format debug text");
|
||||
}
|
||||
|
||||
char *buf = SDL_malloc(len + 1);
|
||||
if (buf == NULL) {
|
||||
return SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
len = SDL_vsnprintf(buf, len + 1, fmt, ap);
|
||||
if (len < 0) {
|
||||
SDL_free(buf);
|
||||
return SDL_SetError("Failed to format debug text");
|
||||
}
|
||||
|
||||
bool result = SDL_RenderDebugText(renderer, x, y, buf);
|
||||
|
||||
SDL_free(buf);
|
||||
|
||||
return result;
|
||||
if (rc == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool retval = SDL_RenderDebugText(renderer, x, y, str);
|
||||
SDL_free(str);
|
||||
return retval;
|
||||
}
|
||||
|
Reference in New Issue
Block a user