diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f72794..fea9dca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(SOURCES src/main.c src/options.c src/mouse.c + src/strtoint.c src/editing_area/main.c src/editing_area/level.c src/editing_area/draw.c diff --git a/include/options.h b/include/options.h index c9ee047..c45a92e 100644 --- a/include/options.h +++ b/include/options.h @@ -11,8 +11,8 @@ struct Options { int tile_height; int tileset_width; int tileset_height; - int editor_window_width; - int editor_window_height; + int editor_width; + int editor_height; int editor_target_fps; int editor_draw_offset_x; int editor_draw_offset_y; diff --git a/include/strtoint.h b/include/strtoint.h new file mode 100644 index 0000000..b5b9d13 --- /dev/null +++ b/include/strtoint.h @@ -0,0 +1,6 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* Copyright (C) 2021 KikooDX */ +#pragma once + +/* Attempt to convert a string to positive integer. Return -1 on error. */ +int strtoint(char *string); diff --git a/src/editing_area/main.c b/src/editing_area/main.c index ed4b6eb..6f7b046 100644 --- a/src/editing_area/main.c +++ b/src/editing_area/main.c @@ -27,8 +27,8 @@ int editing_area_main(struct Options options, Texture2D tileset; /* initialize raylib */ - InitWindow(options.editor_window_width, - options.editor_window_height, "SLE main window"); + InitWindow(options.editor_width, + options.editor_height, "SLE main window"); SetTargetFPS(options.editor_target_fps); init_mouse(options); /* load textures */ diff --git a/src/main.c b/src/main.c index 131b29f..efde1ac 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ #include "options.h" #include "shared_data.h" #include "tile_picker/main.h" +#include "strtoint.h" #include #include #include @@ -89,6 +90,10 @@ static void process_arguments(int argc, char **argv, static struct option long_options[] = { {"level", required_argument, 0, 'l'}, {"tileset", required_argument, 0, 't'}, + {"tile-width", required_argument, 0, 'w'}, + {"tile-height", required_argument, 0, 'h'}, + {"editor-width", required_argument, 0, 'W'}, + {"editor-height", required_argument, 0, 'H'}, {0, 0, 0, 0}, }; /* getopt_long stores the option index here */ @@ -101,14 +106,34 @@ static void process_arguments(int argc, char **argv, break; switch (opt) { - case 'l': + case 'l': /* level */ overflow_prevention(); strcpy(options->level_path, optarg); break; - case 't': + case 't': /* tileset */ overflow_prevention(); strcpy(options->tileset_path, optarg); break; + case 'w': /* tile width */ + options->tile_width = strtoint(optarg); + if (options->tile_width <= 0) + exit(EXIT_FAILURE); + break; + case 'h': /* tile height */ + options->tile_height = strtoint(optarg); + if (options->tile_height <= 0) + exit(EXIT_FAILURE); + break; + case 'W': /* editor width */ + options->editor_width = strtoint(optarg); + if (options->editor_width <= 0) + exit(EXIT_FAILURE); + break; + case 'H': /* editor height */ + options->editor_height = strtoint(optarg); + if (options->editor_height <= 0) + exit(EXIT_FAILURE); + break; case '?': /* getopt_long already printed an error message */ diff --git a/src/options.c b/src/options.c index f8fd94d..220b74c 100644 --- a/src/options.c +++ b/src/options.c @@ -10,8 +10,8 @@ struct Options options_defaults(void) .tile_height = TILE_HEIGHT, .tileset_width = 0, .tileset_height = 0, - .editor_window_width = EDITOR_WINDOW_WIDTH, - .editor_window_height = EDITOR_WINDOW_HEIGHT, + .editor_width = EDITOR_WINDOW_WIDTH, + .editor_height = EDITOR_WINDOW_HEIGHT, .editor_target_fps = EDITOR_TARGET_FPS, .editor_draw_offset_x = EDITOR_DRAW_OFFSET_X, .editor_draw_offset_y = EDITOR_DRAW_OFFSET_Y, diff --git a/src/strtoint.c b/src/strtoint.c new file mode 100644 index 0000000..87eb414 --- /dev/null +++ b/src/strtoint.c @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* Copyright (C) 2021 KikooDX */ + +#include "strtoint.h" +#include +#include + +/* Attempt to convert a string to positive integer. Return -1 on error. */ +int strtoint(char *string) { + char character; + int i; + const size_t string_len = strlen(string); + int sum = 0; + int multiplier = 1; + for (i = string_len - 1; i >= 0; i -= 1) { + character = string[i]; + if (character < '0' || character > '9') { + fprintf(stderr, "ERROR: flag expected a positive number argument, got \"%s\"\n", string); + return -1; + } + sum += multiplier * (character - '0'); + multiplier *= 10; + } + return sum; +}