Fixed bug 3740 - atexit() in test/testime.c
authorSam Lantinga <slouken@libsdl.org>
Sun, 27 Aug 2017 19:00:03 -0700
changeset 11361b83e2eaed190
parent 11360 b97aaee16bc8
child 11363 2d3d35b2c95a
Fixed bug 3740 - atexit() in test/testime.c
test/testime.c
     1.1 --- a/test/testime.c	Sun Aug 27 18:53:30 2017 -0700
     1.2 +++ b/test/testime.c	Sun Aug 27 19:00:03 2017 -0700
     1.3 @@ -99,7 +99,7 @@
     1.4      return 1;
     1.5  }
     1.6  
     1.7 -static void unifont_init(const char *fontname)
     1.8 +static int unifont_init(const char *fontname)
     1.9  {
    1.10      Uint8 hexBuffer[65];
    1.11      Uint32 numGlyphs = 0;
    1.12 @@ -114,7 +114,7 @@
    1.13      if (unifontGlyph == NULL)
    1.14      {
    1.15          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for glyph data.\n", (int)(unifontGlyphSize + 1023) / 1024);
    1.16 -        exit(-1);
    1.17 +        return -1;
    1.18      }
    1.19      SDL_memset(unifontGlyph, 0, unifontGlyphSize);
    1.20  
    1.21 @@ -123,7 +123,7 @@
    1.22      if (unifontTexture == NULL)
    1.23      {
    1.24          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for texture pointer data.\n", (int)(unifontTextureSize + 1023) / 1024);
    1.25 -        exit(-1);
    1.26 +        return -1;
    1.27      }
    1.28      SDL_memset(unifontTexture, 0, unifontTextureSize);
    1.29  
    1.30 @@ -131,7 +131,7 @@
    1.31      if (hexFile == NULL)
    1.32      {
    1.33          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to open font file: %s\n", fontname);
    1.34 -        exit(-1);
    1.35 +        return -1;
    1.36      }
    1.37  
    1.38      /* Read all the glyph data into memory to make it accessible later when textures are created. */
    1.39 @@ -147,7 +147,7 @@
    1.40          if ((numGlyphs == 0 && bytesRead == 0) || (numGlyphs > 0 && bytesRead < 9))
    1.41          {
    1.42              SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
    1.43 -            exit(-1);
    1.44 +            return -1;
    1.45          }
    1.46  
    1.47          /* Looking for the colon that separates the codepoint and glyph data at position 2, 4, 6 and 8. */
    1.48 @@ -162,13 +162,13 @@
    1.49          else
    1.50          {
    1.51              SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Could not find codepoint and glyph data separator symbol in hex file on line %d.\n", lineNumber);
    1.52 -            exit(-1);
    1.53 +            return -1;
    1.54          }
    1.55  
    1.56          if (!validate_hex((const char *)hexBuffer, codepointHexSize, &codepoint))
    1.57          {
    1.58              SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal number in hex file on line %d.\n", lineNumber);
    1.59 -            exit(-1);
    1.60 +            return -1;
    1.61          }
    1.62          if (codepoint > UNIFONT_MAX_CODEPOINT)
    1.63              SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Codepoint on line %d exceeded limit of 0x%x.\n", lineNumber, UNIFONT_MAX_CODEPOINT);
    1.64 @@ -181,7 +181,7 @@
    1.65          if (bytesRead < (33 - bytesOverread))
    1.66          {
    1.67              SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
    1.68 -            exit(-1);
    1.69 +            return -1;
    1.70          }
    1.71          if (hexBuffer[32] == '\n')
    1.72              glyphWidth = 8;
    1.73 @@ -192,14 +192,14 @@
    1.74              if (bytesRead < 32)
    1.75              {
    1.76                  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
    1.77 -                exit(-1);
    1.78 +                return -1;
    1.79              }
    1.80          }
    1.81  
    1.82          if (!validate_hex((const char *)hexBuffer, glyphWidth * 4, NULL))
    1.83          {
    1.84              SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal glyph data in hex file on line %d.\n", lineNumber);
    1.85 -            exit(-1);
    1.86 +            return -1;
    1.87          }
    1.88  
    1.89          if (codepoint <= UNIFONT_MAX_CODEPOINT)
    1.90 @@ -221,6 +221,7 @@
    1.91  
    1.92      SDL_RWclose(hexFile);
    1.93      SDL_Log("unifont: Loaded %u glyphs.\n", numGlyphs);
    1.94 +    return 0;
    1.95  }
    1.96  
    1.97  static void unifont_make_rgba(Uint8 *src, Uint8 *dst, Uint8 width)
    1.98 @@ -259,7 +260,7 @@
    1.99      }
   1.100  }
   1.101  
   1.102 -static void unifont_load_texture(Uint32 textureID)
   1.103 +static int unifont_load_texture(Uint32 textureID)
   1.104  {
   1.105      int i;
   1.106      Uint8 * textureRGBA;
   1.107 @@ -267,14 +268,14 @@
   1.108      if (textureID >= UNIFONT_NUM_TEXTURES)
   1.109      {
   1.110          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Tried to load out of range texture %u.\n", textureID);
   1.111 -        exit(-1);
   1.112 +        return -1;
   1.113      }
   1.114  
   1.115      textureRGBA = (Uint8 *)SDL_malloc(UNIFONT_TEXTURE_SIZE);
   1.116      if (textureRGBA == NULL)
   1.117      {
   1.118          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d MiB for a texture.\n", UNIFONT_TEXTURE_SIZE / 1024 / 1024);
   1.119 -        exit(-1);
   1.120 +        return -1;
   1.121      }
   1.122      SDL_memset(textureRGBA, 0, UNIFONT_TEXTURE_SIZE);
   1.123  
   1.124 @@ -301,7 +302,7 @@
   1.125          if (tex == NULL)
   1.126          {
   1.127              SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to create texture %u for renderer %d.\n", textureID, i);
   1.128 -            exit(-1);
   1.129 +            return -1;
   1.130          }
   1.131          unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID] = tex;
   1.132          SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
   1.133 @@ -313,6 +314,7 @@
   1.134  
   1.135      SDL_free(textureRGBA);
   1.136      unifontTextureLoaded[textureID] = 1;
   1.137 +    return 0;
   1.138  }
   1.139  
   1.140  static Sint32 unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_Rect *dstrect)
   1.141 @@ -321,10 +323,14 @@
   1.142      const Uint32 textureID = codepoint / UNIFONT_GLYPHS_IN_TEXTURE;
   1.143      SDL_Rect srcrect;
   1.144      srcrect.w = srcrect.h = 16;
   1.145 -    if (codepoint > UNIFONT_MAX_CODEPOINT)
   1.146 +    if (codepoint > UNIFONT_MAX_CODEPOINT) {
   1.147          return 0;
   1.148 -    if (!unifontTextureLoaded[textureID])
   1.149 -        unifont_load_texture(textureID);
   1.150 +    }
   1.151 +    if (!unifontTextureLoaded[textureID]) {
   1.152 +        if (unifont_load_texture(textureID) < 0) {
   1.153 +            return 0;
   1.154 +        }
   1.155 +    }
   1.156      texture = unifontTexture[UNIFONT_NUM_TEXTURES * rendererID + textureID];
   1.157      if (texture != NULL)
   1.158      {
   1.159 @@ -430,12 +436,10 @@
   1.160  void usage()
   1.161  {
   1.162      SDL_Log("usage: testime [--font fontfile]\n");
   1.163 -    exit(0);
   1.164  }
   1.165  
   1.166  void InitInput()
   1.167  {
   1.168 -
   1.169      /* Prepare a rect for text input */
   1.170      textRect.x = textRect.y = 100;
   1.171      textRect.w = DEFAULT_WINDOW_WIDTH - 2 * textRect.x;
   1.172 @@ -459,7 +463,8 @@
   1.173  #endif
   1.174  }
   1.175  
   1.176 -void _Redraw(int rendererID) {
   1.177 +void _Redraw(int rendererID)
   1.178 +{
   1.179      SDL_Renderer * renderer = state->renderers[rendererID];
   1.180      SDL_Rect drawnTextRect, cursorRect, underlineRect;
   1.181      drawnTextRect = textRect;
   1.182 @@ -607,7 +612,8 @@
   1.183      SDL_SetTextInputRect(&markedRect);
   1.184  }
   1.185  
   1.186 -void Redraw() {
   1.187 +void Redraw()
   1.188 +{
   1.189      int i;
   1.190      for (i = 0; i < state->num_windows; ++i) {
   1.191          SDL_Renderer *renderer = state->renderers[i];
   1.192 @@ -623,7 +629,8 @@
   1.193      }
   1.194  }
   1.195  
   1.196 -int main(int argc, char *argv[]) {
   1.197 +int main(int argc, char *argv[])
   1.198 +{
   1.199      int i, done;
   1.200      SDL_Event event;
   1.201      const char *fontname = DEFAULT_FONT;
   1.202 @@ -673,14 +680,15 @@
   1.203      if (! font)
   1.204      {
   1.205          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to find font: %s\n", TTF_GetError());
   1.206 -        exit(-1);
   1.207 +        return -1;
   1.208      }
   1.209  #else
   1.210 -    unifont_init(fontname);
   1.211 +    if (unifont_init(fontname) < 0) {
   1.212 +        return -1;
   1.213 +    }
   1.214  #endif
   1.215  
   1.216      SDL_Log("Using font: %s\n", fontname);
   1.217 -    atexit(SDL_Quit);
   1.218  
   1.219      InitInput();
   1.220      /* Create the windows and initialize the renderers */