]> git.lizzy.rs Git - nothing.git/commitdiff
Implement chop_rect_layer
authorrexim <reximkut@gmail.com>
Sun, 22 Dec 2019 20:25:37 +0000 (03:25 +0700)
committerrexim <reximkut@gmail.com>
Sat, 4 Jan 2020 17:08:51 +0000 (00:08 +0700)
src/game/level/level_editor/rect_layer.c
src/game/level/level_editor/rect_layer.h

index aa4f75c8f40d07d6e53979415b5267ec92c71088..871197687bbdab1f5e1bb69f1bae11d160f08810 100644 (file)
@@ -962,6 +962,71 @@ RectLayer *create_rect_layer_from_line_stream(LineStream *line_stream,
     return layer;
 }
 
+RectLayer *chop_rect_layer(Memory *memory,
+                           String *input,
+                           const char *id_name_prefix,
+                           Cursor *cursor)
+{
+    trace_assert(memory);
+    trace_assert(input);
+
+    RectLayer *layer = create_rect_layer(id_name_prefix, cursor);
+    trace_assert(layer);
+
+    int n = atoi(string_to_cstr(memory, trim(chop_by_delim(input, '\n'))));
+    char id[ENTITY_MAX_ID_SIZE];
+    for (int i = 0; i < n; ++i) {
+        Rect rect;
+        String line = trim(chop_by_delim(input, '\n'));
+        String string_id = trim(chop_word(&line));
+        rect.x = strtof(string_to_cstr(memory, trim(chop_word(&line))), NULL);
+        rect.y = strtof(string_to_cstr(memory, trim(chop_word(&line))), NULL);
+        rect.w = strtof(string_to_cstr(memory, trim(chop_word(&line))), NULL);
+        rect.h = strtof(string_to_cstr(memory, trim(chop_word(&line))), NULL);
+        Color color = hexs(trim(chop_word(&line)));
+
+        memset(id, 0, ENTITY_MAX_ID_SIZE);
+        memcpy(
+            id,
+            string_id.data,
+            min_size_t(ENTITY_MAX_ID_SIZE - 1, string_id.count));
+
+        dynarray_push(&layer->rects, &rect);
+        dynarray_push(&layer->colors, &color);
+        dynarray_push(&layer->ids, id);
+
+        Action action = {
+            .type = ACTION_NONE,
+            .entity_id = {0}
+        };
+
+        String action_string = trim(chop_word(&line));
+        if (action_string.count > 0) {
+            action.type = atoi(string_to_cstr(memory, action_string));
+            switch (action.type) {
+            case ACTION_NONE: break;
+            case ACTION_TOGGLE_GOAL:
+            case ACTION_HIDE_LABEL: {
+                String label_id = trim(chop_word(&line));
+                trace_assert(label_id.count > 0);
+                memset(action.entity_id, 0, ENTITY_MAX_ID_SIZE);
+                memcpy(action.entity_id,
+                       label_id.data,
+                       min_size_t(
+                           ENTITY_MAX_ID_SIZE - 1,
+                           label_id.count));
+            } break;
+
+            case ACTION_N: break;
+            }
+        }
+
+        dynarray_push(&layer->actions, &action);
+    }
+
+    return layer;
+}
+
 void destroy_rect_layer(RectLayer *layer)
 {
     trace_assert(layer);
index c0b2803045396751d3a17624c5e3ab4af1d9563b..85bf7979246ee8f397d2e6fb447139a9ad57b317 100644 (file)
@@ -17,6 +17,10 @@ RectLayer *create_rect_layer_from_line_stream(
     LineStream *line_stream,
     const char *id_name_prefix,
     Cursor *cursor);
+RectLayer *chop_rect_layer(Memory *memory,
+                           String *input,
+                           const char *id_name_prefix,
+                           Cursor *cursor);
 void destroy_rect_layer(RectLayer *layer);
 
 int rect_layer_render(const RectLayer *layer, const Camera *camera, int active);