3 #include "game/camera.h"
4 #include "game/level/labels.h"
5 #include "system/error.h"
8 #define LABEL_TEXT_MAX_LENGTH 64
19 static char *trim_endline(char *s)
21 const size_t n = strlen(s);
27 if (s[n - 1] == '\n') {
34 /* TODO(#256): labels don't play appear animation when they get into the camera view */
36 labels_t *create_labels_from_stream(FILE *stream)
40 lt_t *const lt = create_lt();
45 labels_t * const labels = PUSH_LT(lt, malloc(sizeof(labels_t)), free);
47 throw_error(ERROR_TYPE_LIBC);
52 if (fscanf(stream, "%lu", &labels->count) == EOF) {
53 throw_error(ERROR_TYPE_LIBC);
57 labels->positions = PUSH_LT(lt, malloc(sizeof(vec_t) * labels->count), free);
58 if (labels->positions == NULL) {
59 throw_error(ERROR_TYPE_LIBC);
63 labels->colors = PUSH_LT(lt, malloc(sizeof(color_t) * labels->count), free);
64 if (labels->colors == NULL) {
65 throw_error(ERROR_TYPE_LIBC);
69 labels->texts = PUSH_LT(lt, malloc(sizeof(char*) * labels->count), free);
70 if (labels->texts == NULL) {
71 throw_error(ERROR_TYPE_LIBC);
75 for (size_t i = 0; i < labels->count; ++i) {
76 labels->texts[i] = PUSH_LT(lt, malloc(sizeof(char) * (LABEL_TEXT_MAX_LENGTH + 1)), free);
77 if (labels->texts[i] == NULL) {
78 throw_error(ERROR_TYPE_LIBC);
84 for (size_t i = 0; i < labels->count; ++i) {
85 if (fscanf(stream, "%f%f%6s\n",
86 &labels->positions[i].x,
87 &labels->positions[i].y,
89 throw_error(ERROR_TYPE_LIBC);
93 labels->colors[i] = color_from_hexstr(color);
95 if (fgets(labels->texts[i], LABEL_TEXT_MAX_LENGTH, stream) == NULL) {
96 throw_error(ERROR_TYPE_LIBC);
100 trim_endline(labels->texts[i]);
106 void destroy_labels(labels_t *label)
109 RETURN_LT0(label->lt);
112 int labels_render(const labels_t *label,
118 for (size_t i = 0; i < label->count; ++i) {
119 if (camera_render_text(camera,
123 label->positions[i]) < 0) {
131 void labels_update(labels_t *label,