mirror of https://github.com/encounter/SDL.git
SDL_test_font.c: add ability to draw on different SDL_Renderers
fixes `testwm2 --windows 2`
This commit is contained in:
parent
6312aaea0f
commit
70f7ebe635
|
@ -3109,10 +3109,16 @@ static unsigned char SDLTest_FontData[SDL_TESTFONTDATAMAX] = {
|
|||
|
||||
/* ---- 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)
|
||||
{
|
||||
|
@ -3131,6 +3137,7 @@ int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c)
|
|||
SDL_Surface *character;
|
||||
Uint32 ci;
|
||||
Uint8 r, g, b, a;
|
||||
struct SDLTest_CharTextureCache *cache;
|
||||
|
||||
/*
|
||||
* Setup source rectangle
|
||||
|
@ -3151,10 +3158,25 @@ int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c)
|
|||
/* Character index in cache */
|
||||
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.
|
||||
*/
|
||||
if (SDLTest_CharTextureCache[ci] == NULL) {
|
||||
if (cache->charTextureCache[ci] == NULL) {
|
||||
/*
|
||||
* Redraw character into surface
|
||||
*/
|
||||
|
@ -3191,14 +3213,15 @@ int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c)
|
|||
linepos += pitch;
|
||||
}
|
||||
|
||||
|
||||
/* Convert temp surface into texture */
|
||||
SDLTest_CharTextureCache[ci] = SDL_CreateTextureFromSurface(renderer, character);
|
||||
cache->charTextureCache[ci] = SDL_CreateTextureFromSurface(renderer, character);
|
||||
SDL_FreeSurface(character);
|
||||
|
||||
/*
|
||||
* Check pointer
|
||||
*/
|
||||
if (SDLTest_CharTextureCache[ci] == NULL) {
|
||||
if (cache->charTextureCache[ci] == NULL) {
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
@ -3208,13 +3231,13 @@ int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c)
|
|||
*/
|
||||
result = 0;
|
||||
result |= SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
|
||||
result |= SDL_SetTextureColorMod(SDLTest_CharTextureCache[ci], r, g, b);
|
||||
result |= SDL_SetTextureAlphaMod(SDLTest_CharTextureCache[ci], a);
|
||||
result |= SDL_SetTextureColorMod(cache->charTextureCache[ci], r, g, b);
|
||||
result |= SDL_SetTextureAlphaMod(cache->charTextureCache[ci], a);
|
||||
|
||||
/*
|
||||
* Draw texture onto destination
|
||||
*/
|
||||
result |= SDL_RenderCopy(renderer, SDLTest_CharTextureCache[ci], &srect, &drect);
|
||||
result |= SDL_RenderCopy(renderer, cache->charTextureCache[ci], &srect, &drect);
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
@ -3239,12 +3262,23 @@ int SDLTest_DrawString(SDL_Renderer * renderer, int x, int y, const char *s)
|
|||
void SDLTest_CleanupTextDrawing(void)
|
||||
{
|
||||
unsigned int i;
|
||||
for (i = 0; i < SDL_arraysize(SDLTest_CharTextureCache); ++i) {
|
||||
if (SDLTest_CharTextureCache[i]) {
|
||||
SDL_DestroyTexture(SDLTest_CharTextureCache[i]);
|
||||
SDLTest_CharTextureCache[i] = NULL;
|
||||
struct SDLTest_CharTextureCache* cache, *next;
|
||||
|
||||
cache = SDLTest_CharTextureCacheList;
|
||||
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: */
|
||||
|
|
Loading…
Reference in New Issue