]> git.lizzy.rs Git - nothing.git/blob - src/ui/wiggly_text.c
(#1045) Make Camera entity transparent
[nothing.git] / src / ui / wiggly_text.c
1 #include <SDL.h>
2
3 #include "math/point.h"
4
5 #include "./wiggly_text.h"
6 #include "system/lt.h"
7 #include "system/stacktrace.h"
8 #include "system/nth_alloc.h"
9 #include "system/str.h"
10 #include "game/camera.h"
11
12 int wiggly_text_render(const WigglyText *wiggly_text,
13                        const Camera *camera,
14                        Vec position)
15 {
16     trace_assert(wiggly_text);
17     trace_assert(camera);
18
19     const size_t n = strlen(wiggly_text->text);
20     char buf[2] = {0, 0};
21
22     for (size_t i = 0; i < n; ++i) {
23         buf[0] = wiggly_text->text[i];
24
25         if (camera_render_text_screen(
26                 camera,
27                 buf,
28                 wiggly_text->scale,
29                 wiggly_text->color,
30                 vec_sum(
31                     position,
32                     vec(
33                         (float) (i * FONT_CHAR_WIDTH) * wiggly_text->scale.x,
34                         sinf(wiggly_text->angle + (float) i / (float) n * 10.0f) * 20.0f))) < 0) {
35             return -1;
36         }
37     }
38
39     return 0;
40 }
41
42 int wiggly_text_update(WigglyText *wiggly_text, float delta_time)
43 {
44     trace_assert(wiggly_text);
45     wiggly_text->angle = fmodf(wiggly_text->angle + 10.0f * delta_time, 2 * PI);
46     return 0;
47 }
48
49 Vec wiggly_text_size(const WigglyText *wiggly_text, const Camera *camera)
50 {
51     trace_assert(wiggly_text);
52
53     const Rect boundary = camera_text_boundary_box(
54         camera,
55         vec(0.0f, 0.0f),
56         wiggly_text->scale,
57         wiggly_text->text);
58
59     return vec(boundary.w, boundary.h);
60 }