]> git.lizzy.rs Git - nothing.git/commitdiff
Implement chop_player_layer
authorrexim <reximkut@gmail.com>
Sat, 21 Dec 2019 19:20:10 +0000 (02:20 +0700)
committerrexim <reximkut@gmail.com>
Sat, 4 Jan 2020 17:08:51 +0000 (00:08 +0700)
src/game/level/level_editor/player_layer.c
src/game/level/level_editor/player_layer.h
src/system/memory.h [new file with mode: 0644]
src/system/s.h

index 711ade5b300f3e9afcd91b9f9846288a06e052ae..f3c312fe12206034a8ae944c7d85c1e551213320 100644 (file)
@@ -8,6 +8,7 @@
 #include "system/nth_alloc.h"
 #include "system/log.h"
 #include "undo_history.h"
+#include "system/memory.h"
 
 typedef struct {
     PlayerLayer *layer;
@@ -73,6 +74,19 @@ PlayerLayer create_player_layer_from_line_stream(LineStream *line_stream)
     return create_player_layer(position, hexstr(colorstr));
 }
 
+PlayerLayer chop_player_layer(Memory *memory, String *input)
+{
+    trace_assert(memory);
+    trace_assert(input);
+
+    String line = chop_by_delim(input, '\n');
+    float x = strtof(string_to_cstr(memory, chop_word(&line)), NULL);
+    float y = strtof(string_to_cstr(memory, chop_word(&line)), NULL);
+    Color color = hexs(chop_word(&line));
+
+    return create_player_layer(vec(x, y), color);
+}
+
 LayerPtr player_layer_as_layer(PlayerLayer *player_layer)
 {
     LayerPtr layer = {
index 3fa4c8e517f363a617ebe11c378bf30aedfaf0f4..f89d22f147da37fd5f67008b2a67846a9ae25940 100644 (file)
@@ -5,6 +5,8 @@
 #include "layer.h"
 #include "system/lt.h"
 #include "system/line_stream.h"
+#include "system/memory.h"
+#include "system/s.h"
 
 typedef struct {
     Vec2f position;
@@ -14,6 +16,7 @@ typedef struct {
 
 PlayerLayer create_player_layer(Vec2f position, Color color);
 PlayerLayer create_player_layer_from_line_stream(LineStream *line_stream);
+PlayerLayer chop_player_layer(Memory *memory, String *input);
 
 LayerPtr player_layer_as_layer(PlayerLayer *player_layer);
 int player_layer_render(const PlayerLayer *player_layer,
diff --git a/src/system/memory.h b/src/system/memory.h
new file mode 100644 (file)
index 0000000..d099d5f
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef MEMORY_H_
+#define MEMORY_H_
+
+#include <assert.h>
+#include <stdint.h>
+
+#define KILO 1024
+#define MEGA (1024 * KILO)
+#define GIGA (1024 * MEGA)
+
+typedef struct {
+    size_t capacity;
+    size_t size;
+    uint8_t *buffer;
+} Memory;
+
+static inline
+void *memory_alloc(Memory *memory, size_t size)
+{
+    assert(memory);
+    assert(memory->size + size <= memory->capacity);
+
+
+    void *result = memory->buffer + memory->size;
+    memory->size += size;
+
+    return result;
+}
+
+static inline
+void memory_clean(Memory *memory)
+{
+    assert(memory);
+    memory->size = 0;
+}
+
+#endif  // MEMORY_H_
index 2b3c3566524b277d7072010715648a9b04361550..688af2d1939effe22e6b3df574492956a012cda0 100644 (file)
@@ -1,8 +1,10 @@
 #ifndef S_H_
 #define S_H_
 
+#include <stdlib.h>
 #include <string.h>
 #include "system/stacktrace.h"
+#include "system/memory.h"
 
 typedef struct {
     size_t count;
@@ -106,4 +108,15 @@ String chop_word(String *input)
     return result;
 }
 
+static inline
+char *string_to_cstr(Memory *memory, String s)
+{
+    trace_assert(memory);
+
+    char *result = memory_alloc(memory, s.count + 1);
+    memset(result, 0, s.count + 1);
+    memcpy(result, s.data, s.count);
+    return result;
+}
+
 #endif  // S_H_