Skip to content

Commit

Permalink
Fixed bug 3740 - atexit() in test/testime.c
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Aug 28, 2017
1 parent b7c5d15 commit aad997f
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions test/testime.c
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -123,15 +123,15 @@ 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);

hexFile = SDL_RWFromFile(fontname, "rb");
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. */
Expand All @@ -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. */
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -259,22 +260,22 @@ 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;

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);

Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit aad997f

Please sign in to comment.