Fixed bug 3740 - atexit() in test/testime.c
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
diff --git a/test/testime.c b/test/testime.c
index 069bed5..32f80cd 100644
--- a/test/testime.c
+++ b/test/testime.c
@@ -99,7 +99,7 @@ static Uint8 validate_hex(const char *cp, size_t len, Uint32 *np)
return 1;
}
-static void unifont_init(const char *fontname)
+static int unifont_init(const char *fontname)
{
Uint8 hexBuffer[65];
Uint32 numGlyphs = 0;
@@ -114,7 +114,7 @@ static void unifont_init(const char *fontname)
if (unifontGlyph == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for glyph data.\n", (int)(unifontGlyphSize + 1023) / 1024);
- exit(-1);
+ return -1;
}
SDL_memset(unifontGlyph, 0, unifontGlyphSize);
@@ -123,7 +123,7 @@ static void unifont_init(const char *fontname)
if (unifontTexture == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for texture pointer data.\n", (int)(unifontTextureSize + 1023) / 1024);
- exit(-1);
+ return -1;
}
SDL_memset(unifontTexture, 0, unifontTextureSize);
@@ -131,7 +131,7 @@ static void unifont_init(const char *fontname)
if (hexFile == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to open font file: %s\n", fontname);
- exit(-1);
+ return -1;
}
/* Read all the glyph data into memory to make it accessible later when textures are created. */
@@ -147,7 +147,7 @@ static void unifont_init(const char *fontname)
if ((numGlyphs == 0 && bytesRead == 0) || (numGlyphs > 0 && bytesRead < 9))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
- exit(-1);
+ return -1;
}
/* Looking for the colon that separates the codepoint and glyph data at position 2, 4, 6 and 8. */
@@ -162,13 +162,13 @@ static void unifont_init(const char *fontname)
else
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Could not find codepoint and glyph data separator symbol in hex file on line %d.\n", lineNumber);
- exit(-1);
+ return -1;
}
if (!validate_hex((const char *)hexBuffer, codepointHexSize, &codepoint))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal number in hex file on line %d.\n", lineNumber);
- exit(-1);
+ return -1;
}
if (codepoint > UNIFONT_MAX_CODEPOINT)
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Codepoint on line %d exceeded limit of 0x%x.\n", lineNumber, UNIFONT_MAX_CODEPOINT);
@@ -181,7 +181,7 @@ static void unifont_init(const char *fontname)
if (bytesRead < (33 - bytesOverread))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
- exit(-1);
+ return -1;
}
if (hexBuffer[32] == '\n')
glyphWidth = 8;
@@ -192,14 +192,14 @@ static void unifont_init(const char *fontname)
if (bytesRead < 32)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
- exit(-1);
+ return -1;
}
}
if (!validate_hex((const char *)hexBuffer, glyphWidth * 4, NULL))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal glyph data in hex file on line %d.\n", lineNumber);
- exit(-1);
+ return -1;
}
if (codepoint <= UNIFONT_MAX_CODEPOINT)
@@ -221,6 +221,7 @@ static void unifont_init(const char *fontname)
SDL_RWclose(hexFile);
SDL_Log("unifont: Loaded %u glyphs.\n", numGlyphs);
+ return 0;
}
static void unifont_make_rgba(Uint8 *src, Uint8 *dst, Uint8 width)
@@ -259,7 +260,7 @@ static void unifont_make_rgba(Uint8 *src, Uint8 *dst, Uint8 width)
}
}
-static void unifont_load_texture(Uint32 textureID)
+static int unifont_load_texture(Uint32 textureID)
{
int i;
Uint8 * textureRGBA;
@@ -267,14 +268,14 @@ static void unifont_load_texture(Uint32 textureID)
if (textureID >= UNIFONT_NUM_TEXTURES)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Tried to load out of range texture %u.\n", textureID);
- exit(-1);
+ return -1;
}
textureRGBA = (Uint8 *)SDL_malloc(UNIFONT_TEXTURE_SIZE);
if (textureRGBA == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d MiB for a texture.\n", UNIFONT_TEXTURE_SIZE / 1024 / 1024);
- exit(-1);
+ return -1;
}
SDL_memset(textureRGBA, 0, UNIFONT_TEXTURE_SIZE);
@@ -301,7 +302,7 @@ static void unifont_load_texture(Uint32 textureID)
if (tex == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to create texture %u for renderer %d.\n", textureID, i);
- exit(-1);
+ return -1;
}
unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID] = tex;
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
@@ -313,6 +314,7 @@ static void unifont_load_texture(Uint32 textureID)
SDL_free(textureRGBA);
unifontTextureLoaded[textureID] = 1;
+ return 0;
}
static Sint32 unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_Rect *dstrect)
@@ -321,10 +323,14 @@ static Sint32 unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_Rect *dst
const Uint32 textureID = codepoint / UNIFONT_GLYPHS_IN_TEXTURE;
SDL_Rect srcrect;
srcrect.w = srcrect.h = 16;
- if (codepoint > UNIFONT_MAX_CODEPOINT)
+ if (codepoint > UNIFONT_MAX_CODEPOINT) {
return 0;
- if (!unifontTextureLoaded[textureID])
- unifont_load_texture(textureID);
+ }
+ if (!unifontTextureLoaded[textureID]) {
+ if (unifont_load_texture(textureID) < 0) {
+ return 0;
+ }
+ }
texture = unifontTexture[UNIFONT_NUM_TEXTURES * rendererID + textureID];
if (texture != NULL)
{
@@ -430,12 +436,10 @@ Uint32 utf8_decode(char *p, size_t len)
void usage()
{
SDL_Log("usage: testime [--font fontfile]\n");
- exit(0);
}
void InitInput()
{
-
/* Prepare a rect for text input */
textRect.x = textRect.y = 100;
textRect.w = DEFAULT_WINDOW_WIDTH - 2 * textRect.x;
@@ -459,7 +463,8 @@ void CleanupVideo()
#endif
}
-void _Redraw(int rendererID) {
+void _Redraw(int rendererID)
+{
SDL_Renderer * renderer = state->renderers[rendererID];
SDL_Rect drawnTextRect, cursorRect, underlineRect;
drawnTextRect = textRect;
@@ -607,7 +612,8 @@ void _Redraw(int rendererID) {
SDL_SetTextInputRect(&markedRect);
}
-void Redraw() {
+void Redraw()
+{
int i;
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
@@ -623,7 +629,8 @@ void Redraw() {
}
}
-int main(int argc, char *argv[]) {
+int main(int argc, char *argv[])
+{
int i, done;
SDL_Event event;
const char *fontname = DEFAULT_FONT;
@@ -673,14 +680,15 @@ int main(int argc, char *argv[]) {
if (! font)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to find font: %s\n", TTF_GetError());
- exit(-1);
+ return -1;
}
#else
- unifont_init(fontname);
+ if (unifont_init(fontname) < 0) {
+ return -1;
+ }
#endif
SDL_Log("Using font: %s\n", fontname);
- atexit(SDL_Quit);
InitInput();
/* Create the windows and initialize the renderers */