SDL_test_font.c: add ability to draw on different SDL_Renderers fixes `testwm2 --windows 2`
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
diff --git a/src/test/SDL_test_font.c b/src/test/SDL_test_font.c
index a3c0e95..044b0df 100644
--- a/src/test/SDL_test_font.c
+++ b/src/test/SDL_test_font.c
@@ -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: */