]> git.lizzy.rs Git - nothing.git/blobdiff - src/game/level/labels.c
Merge pull request #1237 from tsoding/remove-lt
[nothing.git] / src / game / level / labels.c
index 9de6d3ca625406b8c893cf62e587758dc6b21c77..9526463c1f58c02a57cd631dafafa0214e2a7801 100644 (file)
@@ -1,17 +1,15 @@
-#include "system/stacktrace.h"
+#include <stdio.h>
 #include <stdbool.h>
 
+#include "config.h"
 #include "game/camera.h"
 #include "game/level/labels.h"
-#include "str.h"
-#include "system/line_stream.h"
+#include "game/level/level_editor/label_layer.h"
+#include "system/log.h"
 #include "system/lt.h"
 #include "system/nth_alloc.h"
-#include "system/log.h"
-#include "ebisp/interpreter.h"
-#include "broadcast.h"
-
-#define LABEL_MAX_ID_SIZE 36
+#include "system/stacktrace.h"
+#include "system/str.h"
 
 enum LabelState
 {
@@ -24,115 +22,83 @@ struct Labels
 {
     Lt *lt;
     size_t count;
-    char **ids;
-    Vec *positions;
+    char *ids;
+    Vec2f *positions;
     Color *colors;
     char **texts;
+
+    /* Animation state */
     float *alphas;
     float *delta_alphas;
     enum LabelState *states;
 };
 
-Labels *create_labels_from_line_stream(LineStream *line_stream)
+Labels *create_labels_from_label_layer(const LabelLayer *label_layer)
 {
-    trace_assert(line_stream);
+    trace_assert(label_layer);
 
-    Lt *const lt = create_lt();
-    if (lt == NULL) {
-        return NULL;
-    }
+    Lt *lt = create_lt();
 
-    Labels * const labels = PUSH_LT(lt, nth_alloc(sizeof(Labels)), free);
+    Labels *labels = PUSH_LT(lt, nth_calloc(1, sizeof(Labels)), free);
     if (labels == NULL) {
         RETURN_LT(lt, NULL);
     }
     labels->lt = lt;
 
-    if (sscanf(
-            line_stream_next(line_stream),
-            "%lu",
-            &labels->count) == EOF) {
-        log_fail("Could not read amount of labels\n");
-        RETURN_LT(lt, NULL);
-    }
+    labels->count = label_layer_count(label_layer);
 
-    labels->ids = PUSH_LT(lt, nth_alloc(sizeof(char*) * labels->count), free);
+    labels->ids = PUSH_LT(lt, nth_calloc(labels->count, sizeof(char) * ENTITY_MAX_ID_SIZE), free);
     if (labels->ids == NULL) {
         RETURN_LT(lt, NULL);
     }
+    memcpy(labels->ids,
+           label_layer_ids(label_layer),
+           labels->count * sizeof(char) * ENTITY_MAX_ID_SIZE);
 
-    labels->positions = PUSH_LT(lt, nth_alloc(sizeof(Vec) * labels->count), free);
+    labels->positions = PUSH_LT(lt, nth_calloc(1, sizeof(Vec2f) * labels->count), free);
     if (labels->positions == NULL) {
         RETURN_LT(lt, NULL);
     }
+    memcpy(labels->positions,
+           label_layer_positions(label_layer),
+           labels->count * sizeof(Vec2f));
 
-    labels->colors = PUSH_LT(lt, nth_alloc(sizeof(Color) * labels->count), free);
+    labels->colors = PUSH_LT(lt, nth_calloc(1, sizeof(Color) * labels->count), free);
     if (labels->colors == NULL) {
         RETURN_LT(lt, NULL);
     }
+    memcpy(labels->colors,
+           label_layer_colors(label_layer),
+           labels->count * sizeof(Color));
 
-    labels->texts = PUSH_LT(lt, nth_alloc(sizeof(char*) * labels->count), free);
+    labels->texts = PUSH_LT(lt, nth_calloc(1, sizeof(char*) * labels->count), free);
     if (labels->texts == NULL) {
         RETURN_LT(lt, NULL);
     }
 
-    labels->alphas = PUSH_LT(lt, nth_alloc(sizeof(float) * labels->count), free);
+    char *texts = labels_layer_texts(label_layer);
+    for (size_t i = 0; i < labels->count; ++i) {
+        labels->texts[i] = PUSH_LT(
+            labels->lt,
+            string_duplicate(texts + i * LABEL_LAYER_TEXT_MAX_SIZE, NULL),
+            free);
+    }
+
+    labels->alphas = PUSH_LT(lt, nth_calloc(1, sizeof(float) * labels->count), free);
     if (labels->alphas == NULL) {
         RETURN_LT(lt, NULL);
     }
 
-    labels->delta_alphas = PUSH_LT(lt, nth_alloc(sizeof(float) * labels->count), free);
+    labels->delta_alphas = PUSH_LT(lt, nth_calloc(1, sizeof(float) * labels->count), free);
     if (labels->delta_alphas == NULL) {
         RETURN_LT(lt, NULL);
     }
 
-    labels->states = PUSH_LT(lt, nth_alloc(sizeof(enum LabelState) * labels->count), free);
+    labels->states = PUSH_LT(lt, nth_calloc(1, sizeof(enum LabelState) * labels->count), free);
     if (labels->states == NULL) {
         RETURN_LT(lt, NULL);
     }
 
-    char color[7];
-    for (size_t i = 0; i < labels->count; ++i) {
-        labels->alphas[i] = 0.0f;
-        labels->delta_alphas[i] = 0.0f;
-        labels->states[i] = LABEL_STATE_VIRGIN;
-        labels->texts[i] = NULL;
-
-        labels->ids[i] = PUSH_LT(lt, nth_alloc(sizeof(char) * LABEL_MAX_ID_SIZE), free);
-        if (labels->ids[i] == NULL) {
-            RETURN_LT(lt, NULL);
-        }
-
-        if (sscanf(
-                line_stream_next(line_stream),
-                "%" STRINGIFY(LABEL_MAX_ID_SIZE) "s%f%f%6s\n",
-                labels->ids[i],
-                &labels->positions[i].x,
-                &labels->positions[i].y,
-                color) == EOF) {
-            log_fail("Could not read position and color of %dth label\n", i);
-            RETURN_LT(lt, NULL);
-        }
-
-        labels->colors[i] = hexstr(color);
-
-        const char *label_text = line_stream_next(line_stream);
-        if (label_text == NULL) {
-            log_fail("Could not read text of %dth label\n", i);
-            RETURN_LT(lt, NULL);
-        }
-
-        labels->texts[i] = PUSH_LT(
-            lt,
-            string_duplicate(label_text, NULL),
-            free);
-        if (labels->texts[i] == NULL) {
-            RETURN_LT(lt, NULL);
-        }
-
-        trim_endline(labels->texts[i]);
-    }
-
     return labels;
 }
 
@@ -143,7 +109,7 @@ void destroy_labels(Labels *label)
 }
 
 int labels_render(const Labels *label,
-                 Camera *camera)
+                  const Camera *camera)
 {
     trace_assert(label);
     trace_assert(camera);
@@ -154,7 +120,7 @@ int labels_render(const Labels *label,
 
         if (camera_render_text(camera,
                                label->texts[i],
-                               vec(2.0f, 2.0f),
+                               LABELS_SIZE,
                                rgba(label->colors[i].r,
                                     label->colors[i].g,
                                     label->colors[i].b,
@@ -210,55 +176,19 @@ void labels_enter_camera_event(Labels *labels,
     }
 }
 
-static struct EvalResult
-labels_action(Labels *labels,
-              size_t index,
-              Gc *gc,
-              struct Scope *scope,
-              struct Expr path)
+void labels_hide(Labels *labels, char id[ENTITY_MAX_ID_SIZE])
 {
     trace_assert(labels);
-    trace_assert(gc);
-    trace_assert(scope);
-
-    const char *target = NULL;
-    struct Expr rest = void_expr();
-    struct EvalResult res = match_list(gc, "q*", path, &target, &rest);
-    if (res.is_error) {
-        return res;
-    }
-
-    if (strcmp(target, "hide") == 0) {
-        if (labels->states[index] != LABEL_STATE_HIDDEN) {
-            labels->states[index] = LABEL_STATE_HIDDEN;
-            labels->alphas[index] = 1.0f;
-            labels->delta_alphas[index] = -3.0f;
-        }
-        return eval_success(NIL(gc));
-    }
-
-    return unknown_target(gc, labels->ids[index], target);
-}
-
-struct EvalResult
-labels_send(Labels *labels, Gc *gc, struct Scope *scope, struct Expr path)
-{
-    trace_assert(labels);
-    trace_assert(gc);
-    trace_assert(scope);
-
-    const char *target = NULL;
-    struct Expr rest = void_expr();
-    struct EvalResult res = match_list(gc, "s*", path, &target, &rest);
-    if (res.is_error) {
-        return res;
-    }
+    trace_assert(id);
 
     for (size_t i = 0; i < labels->count; ++i) {
-        if (strcmp(target, labels->ids[i]) == 0) {
-            return labels_action(labels, i, gc, scope, rest);
+        if (strncmp(id, labels->ids + i * ENTITY_MAX_ID_SIZE, ENTITY_MAX_ID_SIZE) == 0) {
+            if (labels->states[i] != LABEL_STATE_HIDDEN) {
+                labels->states[i] = LABEL_STATE_HIDDEN;
+                labels->alphas[i] = 1.0f;
+                labels->delta_alphas[i] = -3.0f;
+            }
+            return;
         }
     }
-
-    return unknown_target(gc, "label", target);
 }