From a66aaf9bdc697db6e99a93fad7ccdaae98aa7231 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Sat, 27 Mar 2021 18:34:47 +0100 Subject: [PATCH] Flags for background color configuration --- CMakeLists.txt | 1 + README | 10 +++++-- include/options.h | 4 +++ include/strtocolor.h | 8 ++++++ src/editing_area/main.c | 2 +- src/main.c | 9 +++++++ src/options.c | 2 ++ src/strtocolor.c | 59 +++++++++++++++++++++++++++++++++++++++++ src/strtoint.c | 4 +-- src/tile_picker/main.c | 2 +- 10 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 include/strtocolor.h create mode 100644 src/strtocolor.c diff --git a/CMakeLists.txt b/CMakeLists.txt index fea9dca..154e418 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ set(SOURCES src/options.c src/mouse.c src/strtoint.c + src/strtocolor.c src/editing_area/main.c src/editing_area/level.c src/editing_area/draw.c diff --git a/README b/README index 758b85b..8ea20b1 100644 --- a/README +++ b/README @@ -59,7 +59,7 @@ These flags are boolean toggles. They take no option. -verbose : print debug informations to the standard output -create : create an empty level instead of reading from the -level path -All these flags take integers as argument. +These flags take integers as argument. -tile-width -tile-height -level-width : used by -create @@ -72,6 +72,11 @@ All these flags take integers as argument. -picker-target-fps -picker-padding +These flags take colors as argument. +Color format : #RRGGBB or RRGGBB +-editor-bg-color +-picker-bg-color + Example aliases --------------- By using the flags, it's very easy to create specific configurations. @@ -90,7 +95,8 @@ alias sle-cg='sle -tile-width 16 -tile-height 16 -level-width 25 \ # fx-9860G configuration alias sle-fx='sle -tile-width 8 -tile-height 8 -level-width 16 \ -level-height 8 -editor-width 128 -editor-height 64 \ - -editor-off-x 0 -editor-off-y 0' + -editor-off-x 0 -editor-off-y 0 -editor-bg-color #b0b0b0 \ + -picker-bg-color #b0b0b0' DEFAULT CONFIGURATION ===================== diff --git a/include/options.h b/include/options.h index 0528c61..3818247 100644 --- a/include/options.h +++ b/include/options.h @@ -2,6 +2,8 @@ /* Copyright (C) 2021 KikooDX */ #pragma once +#include + #define BUFFER_SIZE 256 struct Options { @@ -18,8 +20,10 @@ struct Options { int editor_target_fps; int editor_draw_offset_x; int editor_draw_offset_y; + Color editor_bg_color; int picker_target_fps; int picker_padding; + Color picker_bg_color; /* determined after previous options */ int tileset_width; int tileset_height; diff --git a/include/strtocolor.h b/include/strtocolor.h new file mode 100644 index 0000000..7fdeccd --- /dev/null +++ b/include/strtocolor.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* Copyright (C) 2021 KikooDX */ +#pragma once + +#include + +/* Attempt to convert a string to a raylib Color. */ +Color strtocolor(char *string); diff --git a/src/editing_area/main.c b/src/editing_area/main.c index a8dae68..e4f98f5 100644 --- a/src/editing_area/main.c +++ b/src/editing_area/main.c @@ -61,7 +61,7 @@ int editing_area_main(struct Options options, /* draw */ BeginDrawing(); - ClearBackground(EDITOR_BACKGROUND_COLOR); + ClearBackground(options.editor_bg_color); level_draw(level, options, tileset); editor_mouse_draw(options, mouse_x, mouse_y); diff --git a/src/main.c b/src/main.c index 63b6fd1..1085be4 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ #include "info.h" #include "options.h" #include "shared_data.h" +#include "strtocolor.h" #include "strtoint.h" #include "tile_picker/main.h" #include @@ -105,8 +106,10 @@ static void process_arguments(int argc, char **argv, {"editor-fps", required_argument, 0, 'f'}, {"editor-off-x", required_argument, 0, 'o'}, {"editor-off-y", required_argument, 0, 'O'}, + {"editor-bg-color", required_argument, 0, 'b'}, {"picker-fps", required_argument, 0, 'F'}, {"picker-padding", required_argument, 0, 'p'}, + {"picker-bg-color", required_argument, 0, 'B'}, {0, 0, 0, 0}, }; /* getopt_long stores the option index here */ @@ -157,6 +160,9 @@ static void process_arguments(int argc, char **argv, options->editor_draw_offset_y = strtoint(optarg); break; + case 'b': /* editor background color */ + options->editor_bg_color = strtocolor(optarg); + break; case 'F': /* picker target FPS */ options->picker_target_fps = int_arg_min(opt, 1); @@ -164,6 +170,9 @@ static void process_arguments(int argc, char **argv, case 'p': /* picker padding */ options->picker_padding = int_arg_min(opt, 0); break; + case 'B': /* picker background color */ + options->picker_bg_color = strtocolor(optarg); + break; case '?': /* getopt_long already printed an error message */ diff --git a/src/options.c b/src/options.c index ed0ff74..12e92f8 100644 --- a/src/options.c +++ b/src/options.c @@ -17,10 +17,12 @@ struct Options options_defaults(void) .editor_target_fps = EDITOR_TARGET_FPS, .editor_draw_offset_x = EDITOR_DRAW_OFFSET_X, .editor_draw_offset_y = EDITOR_DRAW_OFFSET_Y, + .editor_bg_color = EDITOR_BACKGROUND_COLOR, .picker_window_width = 0, .picker_window_height = 0, .picker_target_fps = PICKER_TARGET_FPS, .picker_padding = PICKER_PADDING, + .picker_bg_color = PICKER_BACKGROUND_COLOR, .level_create = 0, }; } diff --git a/src/strtocolor.c b/src/strtocolor.c new file mode 100644 index 0000000..64cd3f0 --- /dev/null +++ b/src/strtocolor.c @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* Copyright (C) 2021 KikooDX */ + +#include "strtocolor.h" +#include "info.h" +#include +#include +#include +#include + +static const int hex_table_size = 16; +static const int hex_table[hex_table_size] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; +static const char *format_error = + "ERROR: flag expected a color argument in the form #RRGGBB, got " + "\"%s\"\n"; + +/* Attempt to convert a string to raylib Color. */ +Color strtocolor(char *string) +{ + int rgb[3] = {0, 0, 0}; + char *character = string + (string[0] == '#'); + const size_t string_len = strlen(character); + int i = 0; + int sum = 0; + + if (string_len != 6) { + fprintf(stderr, format_error, string); + exit(EXIT_FAILURE); + } + + while (*character != '\0') { + int value = -1; + int j; + for (j = 0; j < hex_table_size; j += 1) { + if (hex_table[j] == *character) { + value = j; + break; + } + } + if (value == -1) { + fprintf(stderr, format_error, string); + exit(EXIT_FAILURE); + } + sum += value * (1 + 15 * (i % 2)); + INFO_VAR("%d", sum); + if (i % 2) { + rgb[i / 2] = sum; + sum = 0; + } + i += 1; + character += 1; + } + INFO_VAR("r=%d", rgb[0]); + INFO_VAR("g=%d", rgb[1]); + INFO_VAR("b=%d", rgb[2]); + return (Color){rgb[0], rgb[1], rgb[2], 255}; +} diff --git a/src/strtoint.c b/src/strtoint.c index 953fab2..a267bca 100644 --- a/src/strtoint.c +++ b/src/strtoint.c @@ -9,9 +9,9 @@ /* Attempt to convert a string to integer. */ int strtoint(char *string) { - char character; - int i; const size_t string_len = strlen(string); + int character; + int i; int sum = 0; int multiplier = 1; int negative = string[0] == '-'; diff --git a/src/tile_picker/main.c b/src/tile_picker/main.c index ded2c19..a02a32a 100644 --- a/src/tile_picker/main.c +++ b/src/tile_picker/main.c @@ -42,7 +42,7 @@ int tile_picker_main(struct Options options, /* draw */ BeginDrawing(); - ClearBackground(PICKER_BACKGROUND_COLOR); + ClearBackground(options.picker_bg_color); tileset_draw(tileset, options, shared_data->selected_tile);