]> git.lizzy.rs Git - nothing.git/blobdiff - src/game/sprite_font.c
(#1153) Remove TODO for #1157 (sorry!) and cleanup key handling.
[nothing.git] / src / game / sprite_font.c
index 48996f3870e91092c138db8ad6965f165dfb4f0d..0c32f3073b6345087018a99391916b4724b1cc97 100644 (file)
@@ -1,14 +1,14 @@
-#include <SDL2/SDL.h>
-#include <assert.h>
+#include <SDL.h>
+#include "system/stacktrace.h"
 #include <stdio.h>
 #include <string.h>
 
 #include "math/rect.h"
 #include "sdl/renderer.h"
 #include "sprite_font.h"
-#include "system/error.h"
 #include "system/lt.h"
 #include "system/nth_alloc.h"
+#include "system/log.h"
 
 #define FONT_ROW_SIZE 18
 
@@ -21,23 +21,19 @@ struct Sprite_font
 Sprite_font *create_sprite_font_from_file(const char *bmp_file_path,
                                             SDL_Renderer *renderer)
 {
-    assert(bmp_file_path);
-    assert(renderer);
+    trace_assert(bmp_file_path);
+    trace_assert(renderer);
 
-    Lt * const lt = create_lt();
-    if (lt == NULL) {
-        return NULL;
-    }
+    Lt *lt = create_lt();
 
-    Sprite_font * const sprite_font = PUSH_LT(lt, nth_alloc(sizeof(Sprite_font)), free);
+    Sprite_font * const sprite_font = PUSH_LT(lt, nth_calloc(1, sizeof(Sprite_font)), free);
     if (sprite_font == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
 
     SDL_Surface * const surface = PUSH_LT(lt, SDL_LoadBMP(bmp_file_path), SDL_FreeSurface);
     if (surface == NULL) {
-        throw_error(ERROR_TYPE_SDL2);
+        log_fail("Could not load %s: %s\n", bmp_file_path, SDL_GetError());
         RETURN_LT(lt, NULL);
     }
 
@@ -45,7 +41,7 @@ Sprite_font *create_sprite_font_from_file(const char *bmp_file_path,
                         SDL_TRUE,
                         SDL_MapRGB(surface->format,
                                    0, 0, 0)) < 0) {
-        throw_error(ERROR_TYPE_SDL2);
+        log_fail("SDL_SetColorKey: %s\n", SDL_GetError());
         RETURN_LT(lt, NULL);
     }
 
@@ -54,7 +50,7 @@ Sprite_font *create_sprite_font_from_file(const char *bmp_file_path,
         SDL_CreateTextureFromSurface(renderer, surface),
         SDL_DestroyTexture);
     if (sprite_font->texture == NULL) {
-        throw_error(ERROR_TYPE_SDL2);
+        log_fail("SDL_CreateTextureFromSurface: %s\n", SDL_GetError());
         RETURN_LT(lt, NULL);
     }
 
@@ -67,13 +63,13 @@ Sprite_font *create_sprite_font_from_file(const char *bmp_file_path,
 
 void destroy_sprite_font(Sprite_font *sprite_font)
 {
-    assert(sprite_font);
+    trace_assert(sprite_font);
     RETURN_LT0(sprite_font->lt);
 }
 
 static SDL_Rect sprite_font_char_rect(const Sprite_font *sprite_font, char x)
 {
-    assert(sprite_font);
+    trace_assert(sprite_font);
 
     if (32 <= x && x <= 126) {
         const SDL_Rect rect = {
@@ -90,24 +86,24 @@ static SDL_Rect sprite_font_char_rect(const Sprite_font *sprite_font, char x)
 
 int sprite_font_render_text(const Sprite_font *sprite_font,
                             SDL_Renderer *renderer,
-                            Vec position,
-                            Vec size,
+                            Vec2f position,
+                            Vec2f size,
                             Color color,
                             const char *text)
 {
-    assert(sprite_font);
-    assert(renderer);
-    assert(text);
+    trace_assert(sprite_font);
+    trace_assert(renderer);
+    trace_assert(text);
 
     const SDL_Color sdl_color = color_for_sdl(color);
 
     if (SDL_SetTextureColorMod(sprite_font->texture, sdl_color.r, sdl_color.g, sdl_color.b) < 0) {
-        throw_error(ERROR_TYPE_SDL2);
+        log_fail("SDL_SetTextureColorMod: %s\n", SDL_GetError());
         return -1;
     }
 
     if (SDL_SetTextureAlphaMod(sprite_font->texture, sdl_color.a) < 0) {
-        throw_error(ERROR_TYPE_SDL2);
+        log_fail("SDL_SetTextureAlphaMod: %s\n", SDL_GetError());
         return -1;
     }
 
@@ -129,12 +125,12 @@ int sprite_font_render_text(const Sprite_font *sprite_font,
 }
 
 Rect sprite_font_boundary_box(const Sprite_font *sprite_font,
-                                Vec position,
-                                Vec size,
+                                Vec2f position,
+                                Vec2f size,
                                 const char *text)
 {
-    assert(sprite_font);
-    assert(text);
+    trace_assert(sprite_font);
+    trace_assert(text);
     return rect(
         position.x, position.y,
         size.x * FONT_CHAR_WIDTH * (float) strlen(text),