]> git.lizzy.rs Git - nothing.git/commitdiff
Implement chop_background_layer
authorrexim <reximkut@gmail.com>
Sun, 15 Dec 2019 19:13:31 +0000 (02:13 +0700)
committerrexim <reximkut@gmail.com>
Sat, 4 Jan 2020 17:08:51 +0000 (00:08 +0700)
src/color.c
src/color.h
src/game/level/level_editor/background_layer.c
src/game/level/level_editor/background_layer.h
src/system/s.h

index b0fc3728ceb0e8b39b5eddec55e3b4eef7c08f32..eb03f55f928a573b9a11d547e136b1efd5d0fbe4 100644 (file)
@@ -83,6 +83,17 @@ Color hexstr(const char *hexstr)
         1.0f);
 }
 
+Color hexs(String input)
+{
+    if (input.count < 6) return COLOR_BLACK;
+
+    return rgba(
+        parse_color_component(input.data) / 255.0f,
+        parse_color_component(input.data + 2) / 255.0f,
+        parse_color_component(input.data + 4) / 255.0f,
+        1.0f);
+}
+
 SDL_Color color_for_sdl(Color color)
 {
     const SDL_Color result = {
index 937782256b1057093ed08d4501ed89120654c91a..cffb7cf613f7e787d84fc6d76f58cb3447a27935 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <stdio.h>
 #include <SDL.h>
+#include "./system/s.h"
 
 #define COLOR_BLACK rgba(0.0f, 0.0f, 0.0f, 1.0f)
 #define COLOR_WHITE rgba(1.0f, 1.0f, 1.0f, 1.0f)
@@ -16,6 +17,7 @@ Color rgba(float r, float g, float b, float a);
 Color hsla(float h, float s, float l, float a);
 Color rgba_to_hsla(Color color);
 Color hexstr(const char *hexstr);
+Color hexs(String input);
 SDL_Color color_for_sdl(Color color);
 
 int color_hex_to_stream(Color color, FILE *stream);
index 158eff00a673ecf2c7ac854427e48a51cef40063..74945dcf7360bb64abd58eaa5370639534eaa22d 100644 (file)
@@ -31,6 +31,12 @@ int background_layer_read_from_line_stream(BackgroundLayer *layer,
     return 0;
 }
 
+BackgroundLayer chop_background_layer(String *input)
+{
+    String line = trim(chop_by_delim(input, '\n'));
+    return create_background_layer(hexs(line));
+}
+
 int background_layer_render(BackgroundLayer *layer,
                             const Camera *camera,
                             int active)
index f63d361505135a14b8a975c32c1cd7d9957efc1c..bb01f338b72e27f1a599f9ef2bf6ecf251fa3634 100644 (file)
@@ -2,6 +2,7 @@
 #define BACKGROUND_LAYER_H_
 
 #include "color_picker.h"
+#include "system/s.h"
 
 typedef struct {
     ColorPicker color_picker;
@@ -11,6 +12,7 @@ typedef struct {
 BackgroundLayer create_background_layer(Color color);
 int background_layer_read_from_line_stream(BackgroundLayer *layer,
                                            LineStream *line_stream);
+BackgroundLayer chop_background_layer(String *input);
 
 static inline
 LayerPtr background_layer_as_layer(BackgroundLayer *layer)
index 766789241e993a223adb16ef2c1d78915474f94c..f79ae00165b7292580629c081ccf05b1f87888f1 100644 (file)
@@ -62,4 +62,31 @@ int string_equal(String a, String b)
     return memcmp(a.data, b.data, a.count) == 0;
 }
 
+static inline
+String trim_begin(String input)
+{
+    while (input.count > 0 && isspace(*input.data)) {
+        input.data += 1;
+        input.count -= 1;
+    }
+
+    return input;
+}
+
+static inline
+String trim_end(String input)
+{
+    while (input.count > 0 && isspace(*(input.data + input.count - 1))) {
+        input.count -= 1;
+    }
+
+    return input;
+}
+
+static inline
+String trim(String input)
+{
+    return trim_end(trim_begin(input));
+}
+
 #endif  // S_H_