SDL_test_font.c: add ability to draw on different SDL_Renderers

fixes `testwm2 --windows 2`
This commit is contained in:
Eric Wasylishen 2021-11-07 01:46:04 -06:00 committed by Sam Lantinga
parent 6312aaea0f
commit 70f7ebe635
1 changed files with 47 additions and 13 deletions

View File

@ -3109,10 +3109,16 @@ static unsigned char SDLTest_FontData[SDL_TESTFONTDATAMAX] = {
/* ---- Character */ /* ---- Character */
struct SDLTest_CharTextureCache {
SDL_Renderer* renderer;
SDL_Texture* charTextureCache[256];
struct SDLTest_CharTextureCache* next;
};
/*! /*!
\brief Global cache for 8x8 pixel font textures created at runtime. \brief List of per-renderer caches for 8x8 pixel font textures created at runtime.
*/ */
static SDL_Texture *SDLTest_CharTextureCache[256]; static SDL_Texture *SDLTest_CharTextureCacheList;
int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c) int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c)
{ {
@ -3131,6 +3137,7 @@ int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c)
SDL_Surface *character; SDL_Surface *character;
Uint32 ci; Uint32 ci;
Uint8 r, g, b, a; Uint8 r, g, b, a;
struct SDLTest_CharTextureCache *cache;
/* /*
* Setup source rectangle * Setup source rectangle
@ -3151,10 +3158,25 @@ int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c)
/* Character index in cache */ /* Character index in cache */
ci = (unsigned char)c; ci = (unsigned char)c;
/* Search for this renderer's cache */
for (cache = SDLTest_CharTextureCacheList; cache != NULL; cache = cache->next) {
if (cache->renderer == renderer) {
break;
}
}
/* Allocate a new cache for this renderer if needed */
if (cache == NULL) {
cache = (struct SDLTest_CharTextureCache*)SDL_calloc(1, sizeof(struct SDLTest_CharTextureCache));
cache->renderer = renderer;
cache->next = SDLTest_CharTextureCacheList;
SDLTest_CharTextureCacheList = cache;
}
/* /*
* Create new charWidth x charHeight bitmap surface if not already present. * Create new charWidth x charHeight bitmap surface if not already present.
*/ */
if (SDLTest_CharTextureCache[ci] == NULL) { if (cache->charTextureCache[ci] == NULL) {
/* /*
* Redraw character into surface * Redraw character into surface
*/ */
@ -3191,14 +3213,15 @@ int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c)
linepos += pitch; linepos += pitch;
} }
/* Convert temp surface into texture */ /* Convert temp surface into texture */
SDLTest_CharTextureCache[ci] = SDL_CreateTextureFromSurface(renderer, character); cache->charTextureCache[ci] = SDL_CreateTextureFromSurface(renderer, character);
SDL_FreeSurface(character); SDL_FreeSurface(character);
/* /*
* Check pointer * Check pointer
*/ */
if (SDLTest_CharTextureCache[ci] == NULL) { if (cache->charTextureCache[ci] == NULL) {
return (-1); return (-1);
} }
} }
@ -3208,13 +3231,13 @@ int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c)
*/ */
result = 0; result = 0;
result |= SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a); result |= SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
result |= SDL_SetTextureColorMod(SDLTest_CharTextureCache[ci], r, g, b); result |= SDL_SetTextureColorMod(cache->charTextureCache[ci], r, g, b);
result |= SDL_SetTextureAlphaMod(SDLTest_CharTextureCache[ci], a); result |= SDL_SetTextureAlphaMod(cache->charTextureCache[ci], a);
/* /*
* Draw texture onto destination * Draw texture onto destination
*/ */
result |= SDL_RenderCopy(renderer, SDLTest_CharTextureCache[ci], &srect, &drect); result |= SDL_RenderCopy(renderer, cache->charTextureCache[ci], &srect, &drect);
return (result); return (result);
} }
@ -3239,12 +3262,23 @@ int SDLTest_DrawString(SDL_Renderer * renderer, int x, int y, const char *s)
void SDLTest_CleanupTextDrawing(void) void SDLTest_CleanupTextDrawing(void)
{ {
unsigned int i; unsigned int i;
for (i = 0; i < SDL_arraysize(SDLTest_CharTextureCache); ++i) { struct SDLTest_CharTextureCache* cache, *next;
if (SDLTest_CharTextureCache[i]) {
SDL_DestroyTexture(SDLTest_CharTextureCache[i]); cache = SDLTest_CharTextureCacheList;
SDLTest_CharTextureCache[i] = NULL; do {
for (i = 0; i < SDL_arraysize(cache->charTextureCache); ++i) {
if (cache->charTextureCache[i]) {
SDL_DestroyTexture(cache->charTextureCache[i]);
cache->charTextureCache[i] = NULL;
}
} }
}
next = cache->next;
SDL_free(cache);
cache = next;
} while (cache);
SDLTest_CharTextureCacheList = NULL;
} }
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */