From: rexim Date: Sun, 15 Dec 2019 19:13:31 +0000 (+0700) Subject: Implement chop_background_layer X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=9f47008ae084fa7cf1168bde87c9e8abe31a4ded;p=nothing.git Implement chop_background_layer --- diff --git a/src/color.c b/src/color.c index b0fc3728..eb03f55f 100644 --- a/src/color.c +++ b/src/color.c @@ -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 = { diff --git a/src/color.h b/src/color.h index 93778225..cffb7cf6 100644 --- a/src/color.h +++ b/src/color.h @@ -3,6 +3,7 @@ #include #include +#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); diff --git a/src/game/level/level_editor/background_layer.c b/src/game/level/level_editor/background_layer.c index 158eff00..74945dcf 100644 --- a/src/game/level/level_editor/background_layer.c +++ b/src/game/level/level_editor/background_layer.c @@ -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) diff --git a/src/game/level/level_editor/background_layer.h b/src/game/level/level_editor/background_layer.h index f63d3615..bb01f338 100644 --- a/src/game/level/level_editor/background_layer.h +++ b/src/game/level/level_editor/background_layer.h @@ -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) diff --git a/src/system/s.h b/src/system/s.h index 76678924..f79ae001 100644 --- a/src/system/s.h +++ b/src/system/s.h @@ -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_