]> git.lizzy.rs Git - nothing.git/blob - src/game/sprite_font.c
Merge pull request #618 from tsoding/broadcast
[nothing.git] / src / game / sprite_font.c
1 #include <SDL2/SDL.h>
2 #include "system/stacktrace.h"
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "math/rect.h"
7 #include "sdl/renderer.h"
8 #include "sprite_font.h"
9 #include "system/lt.h"
10 #include "system/nth_alloc.h"
11 #include "system/log.h"
12
13 #define FONT_ROW_SIZE 18
14
15 struct Sprite_font
16 {
17     Lt *lt;
18     SDL_Texture *texture;
19 };
20
21 Sprite_font *create_sprite_font_from_file(const char *bmp_file_path,
22                                             SDL_Renderer *renderer)
23 {
24     trace_assert(bmp_file_path);
25     trace_assert(renderer);
26
27     Lt * const lt = create_lt();
28     if (lt == NULL) {
29         return NULL;
30     }
31
32     Sprite_font * const sprite_font = PUSH_LT(lt, nth_alloc(sizeof(Sprite_font)), free);
33     if (sprite_font == NULL) {
34         RETURN_LT(lt, NULL);
35     }
36
37     SDL_Surface * const surface = PUSH_LT(lt, SDL_LoadBMP(bmp_file_path), SDL_FreeSurface);
38     if (surface == NULL) {
39         log_fail("Could not load %s: %s\n", bmp_file_path, SDL_GetError());
40         RETURN_LT(lt, NULL);
41     }
42
43     if (SDL_SetColorKey(surface,
44                         SDL_TRUE,
45                         SDL_MapRGB(surface->format,
46                                    0, 0, 0)) < 0) {
47         log_fail("SDL_SetColorKey: %s\n", SDL_GetError());
48         RETURN_LT(lt, NULL);
49     }
50
51     sprite_font->texture = PUSH_LT(
52         lt,
53         SDL_CreateTextureFromSurface(renderer, surface),
54         SDL_DestroyTexture);
55     if (sprite_font->texture == NULL) {
56         log_fail("SDL_CreateTextureFromSurface: %s\n", SDL_GetError());
57         RETURN_LT(lt, NULL);
58     }
59
60     SDL_FreeSurface(RELEASE_LT(lt, surface));
61
62     sprite_font->lt = lt;
63
64     return sprite_font;
65 }
66
67 void destroy_sprite_font(Sprite_font *sprite_font)
68 {
69     trace_assert(sprite_font);
70     RETURN_LT0(sprite_font->lt);
71 }
72
73 static SDL_Rect sprite_font_char_rect(const Sprite_font *sprite_font, char x)
74 {
75     trace_assert(sprite_font);
76
77     if (32 <= x && x <= 126) {
78         const SDL_Rect rect = {
79             .x = ((x - 32) % FONT_ROW_SIZE) * FONT_CHAR_WIDTH,
80             .y = ((x - 32) / FONT_ROW_SIZE) * FONT_CHAR_HEIGHT,
81             .w = FONT_CHAR_WIDTH,
82             .h = FONT_CHAR_HEIGHT
83         };
84         return rect;
85     } else {
86         return sprite_font_char_rect(sprite_font, '?');
87     }
88 }
89
90 int sprite_font_render_text(const Sprite_font *sprite_font,
91                             SDL_Renderer *renderer,
92                             Vec position,
93                             Vec size,
94                             Color color,
95                             const char *text)
96 {
97     trace_assert(sprite_font);
98     trace_assert(renderer);
99     trace_assert(text);
100
101     const SDL_Color sdl_color = color_for_sdl(color);
102
103     if (SDL_SetTextureColorMod(sprite_font->texture, sdl_color.r, sdl_color.g, sdl_color.b) < 0) {
104         log_fail("SDL_SetTextureColorMod: %s\n", SDL_GetError());
105         return -1;
106     }
107
108     if (SDL_SetTextureAlphaMod(sprite_font->texture, sdl_color.a) < 0) {
109         log_fail("SDL_SetTextureAlphaMod: %s\n", SDL_GetError());
110         return -1;
111     }
112
113     const size_t text_size = strlen(text);
114     for (size_t i = 0; i < text_size; ++i) {
115         const SDL_Rect char_rect = sprite_font_char_rect(sprite_font, text[i]);
116         const SDL_Rect dest_rect = rect_for_sdl(
117             rect(
118                 position.x + (float) FONT_CHAR_WIDTH * (float) i * size.x,
119                 position.y,
120                 (float) char_rect.w * size.x,
121                 (float) char_rect.h * size.y));
122         if (SDL_RenderCopy(renderer, sprite_font->texture, &char_rect, &dest_rect) < 0) {
123             return -1;
124         }
125     }
126
127     return 0;
128 }
129
130 Rect sprite_font_boundary_box(const Sprite_font *sprite_font,
131                                 Vec position,
132                                 Vec size,
133                                 const char *text)
134 {
135     trace_assert(sprite_font);
136     trace_assert(text);
137     return rect(
138         position.x, position.y,
139         size.x * FONT_CHAR_WIDTH * (float) strlen(text),
140         size.y * FONT_CHAR_HEIGHT);
141 }