From b88e37fadffdca79428d632308de474c2eb91041 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Sat, 27 Mar 2021 12:19:10 +0100 Subject: [PATCH] Level creation flag & cleanup and improved README --- .clang-format | 1 + README | 33 +++++++++++++++++++++++----- include/conf.h | 14 +++++++----- include/editing_area/level.h | 4 ++++ include/options.h | 3 +++ src/editing_area/level.c | 42 +++++++++++++++++++++++++++++++++--- src/editing_area/main.c | 7 ++++-- src/main.c | 14 ++++++++++-- src/options.c | 3 +++ 9 files changed, 104 insertions(+), 17 deletions(-) diff --git a/.clang-format b/.clang-format index 8574fb9..ba34d8a 100644 --- a/.clang-format +++ b/.clang-format @@ -5,3 +5,4 @@ BreakBeforeBraces: Linux AllowShortIfStatementsOnASingleLine: false IndentCaseLabels: false ColumnLimit: 72 +AlignConsecutiveMacros: true diff --git a/README b/README index 1b5b375..758b85b 100644 --- a/README +++ b/README @@ -45,22 +45,25 @@ Close/kill the main window. The secondary window will close automaticly and your modifications to the level file will be writen to disk. FLAGS -====== +===== -Mendatory flags +Mandatory flags --------------- All these options take a path to a file as argument. -tileset Tileset image -level KBLE file -Optionnal flags ---------------- +Optional flags +-------------- These flags are boolean toggles. They take no option. --verbose +-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. -tile-width -tile-height +-level-width : used by -create +-level-height : used by -create -editor-width -editor-height -editor-target-fps @@ -69,6 +72,26 @@ All these flags take integers as argument. -picker-target-fps -picker-padding +Example aliases +--------------- +By using the flags, it's very easy to create specific configurations. +Following are various aliases. They assume than sle is in your PATH. + +# PICO-8-like configuration +alias sle-p8='sle -tile-width 8 -tile-height 8 -level-width 16 \ + -level-height 16 -editor-width 128 -editor-height 128 \ + -editor-off-x 0 -editor-off-y 0' + +# fx-CG50 configuration +alias sle-cg='sle -tile-width 16 -tile-height 16 -level-width 25 \ + -level-height 14 -editor-width 396 -editor-height 224 \ + -editor-off-x -2 -editor-off-y 0' + +# 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' + DEFAULT CONFIGURATION ===================== Edit include/conf.h to change default configuration. Read the comments! diff --git a/include/conf.h b/include/conf.h index 9045c22..bcec615 100644 --- a/include/conf.h +++ b/include/conf.h @@ -4,7 +4,7 @@ #include /* for the color struct */ -#define TILE_WIDTH 16 +#define TILE_WIDTH 16 #define TILE_HEIGHT 16 #define UNSELECTED_TILE_COLOR \ @@ -12,14 +12,18 @@ #define OVERRING_TILE_COLOR \ (Color) { 255, 255, 255, 80 } -#define EDITOR_WINDOW_WIDTH 396 +#define NEW_LEVEL_WIDTH 25 +#define NEW_LEVEL_HEIGHT 14 + +#define EDITOR_WINDOW_WIDTH 396 #define EDITOR_WINDOW_HEIGHT 224 -#define EDITOR_TARGET_FPS 60 +#define EDITOR_TARGET_FPS 60 #define EDITOR_DRAW_OFFSET_X -2 -#define EDITOR_DRAW_OFFSET_Y -8 +#define EDITOR_DRAW_OFFSET_Y 0 #define PICKER_TARGET_FPS 30 -#define PICKER_PADDING 4 +#define PICKER_PADDING 4 + #define PICKER_WINDOW_WIDTH \ ((TILE_WIDTH + PICKER_PADDING) * TILESET_WIDTH + PICKER_PADDING) #define PICKER_WINDOW_HEIGHT \ diff --git a/include/editing_area/level.h b/include/editing_area/level.h index 9856f8c..fd874d7 100644 --- a/include/editing_area/level.h +++ b/include/editing_area/level.h @@ -2,6 +2,8 @@ /* Copyright (C) 2021 KikooDX */ #pragma once +#include "options.h" + typedef unsigned int Tile; struct Level { int width; @@ -11,6 +13,8 @@ struct Level { /* return -1 if error */ int level_read(struct Level *, char *path); +/* return -1 if error */ +int level_create(struct Level *, struct Options options); void level_free(struct Level *); /* return -1 if error */ int level_write(struct Level level, char *path); diff --git a/include/options.h b/include/options.h index 91c3ead..0528c61 100644 --- a/include/options.h +++ b/include/options.h @@ -11,6 +11,8 @@ struct Options { /* optionnal arguments, default values in conf.h */ int tile_width; int tile_height; + int new_level_width; + int new_level_height; int editor_width; int editor_height; int editor_target_fps; @@ -23,6 +25,7 @@ struct Options { int tileset_height; int picker_window_width; int picker_window_height; + int level_create; }; struct Options options_defaults(void); diff --git a/src/editing_area/level.c b/src/editing_area/level.c index f6e44ff..f02f30d 100644 --- a/src/editing_area/level.c +++ b/src/editing_area/level.c @@ -3,6 +3,7 @@ #include "editing_area/level.h" #include "info.h" +#include "options.h" #include #include #include @@ -64,12 +65,12 @@ int level_read(struct Level *level, char *path) /* allocate memory for data */ level_size = level->width * level->height; - if (level->data == NULL) { + if (level->data == NULL) level->data = malloc(level_size * sizeof(Tile)); - } else { + else level->data = realloc(level->data, level_size * sizeof(Tile)); - } + /* check for allocation failure */ if (level->data == NULL) { fprintf(stderr, "ERROR: memory allocation failure\n"); @@ -88,6 +89,41 @@ int level_read(struct Level *level, char *path) return 0; } +int level_create(struct Level *level, struct Options options) +{ + int level_size; + int i; + + /* set width */ + level->width = options.new_level_width; + if (level->width == (Tile)-1) + return -1; + /* set height */ + level->height = options.new_level_height; + if (level->height == (Tile)-1) + return -1; + + /* allocate memory for data */ + level_size = level->width * level->height; + if (level->data == NULL) + level->data = malloc(level_size * sizeof(Tile)); + else + level->data = + realloc(level->data, level_size * sizeof(Tile)); + + /* check for allocation failure */ + if (level->data == NULL) { + fprintf(stderr, "ERROR: memory allocation failure\n"); + return -1; + } + + /* file the level with zeros */ + for (i = 0; i < level_size; i += 1) + level->data[i] = 0; + + return 0; +} + void level_free(struct Level *level) { free(level->data); } int level_write(struct Level level, char *path) diff --git a/src/editing_area/main.c b/src/editing_area/main.c index 7de979c..292b766 100644 --- a/src/editing_area/main.c +++ b/src/editing_area/main.c @@ -46,8 +46,11 @@ int editing_area_main(struct Options options, goto panic; } - /* load level */ - if (level_read(&level, options.level_path)) + /* load level or create empty one */ + if (options.level_create) { + if (level_create(&level, options)) + goto panic; + } else if (level_read(&level, options.level_path)) goto panic; while (!WindowShouldClose()) { diff --git a/src/main.c b/src/main.c index ff9751c..63b6fd1 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ /* Copyright (C) 2021 KikooDX */ #include "editing_area/main.h" +#include "info.h" #include "options.h" #include "shared_data.h" #include "strtoint.h" @@ -40,7 +41,7 @@ int main(int argc, char **argv) shared_data->end_child = false; shared_data->selected_tile = 1; - /* set log level */ + /* set log level for raylib */ SetTraceLogLevel(LOG_ERROR); /* process arguments */ @@ -90,12 +91,15 @@ static void process_arguments(int argc, char **argv, { int opt; while (1) { - static struct option long_options[] = { + const struct option long_options[] = { {"verbose", no_argument, &verbose, 1}, + {"create", no_argument, &options->level_create, 1}, {"level", required_argument, 0, 'l'}, {"tileset", required_argument, 0, 't'}, {"tile-width", required_argument, 0, 'w'}, {"tile-height", required_argument, 0, 'h'}, + {"level-width", required_argument, 0, 'i'}, + {"level-height", required_argument, 0, 'e'}, {"editor-width", required_argument, 0, 'W'}, {"editor-height", required_argument, 0, 'H'}, {"editor-fps", required_argument, 0, 'f'}, @@ -129,6 +133,12 @@ static void process_arguments(int argc, char **argv, case 'h': /* tile height */ options->tile_height = int_arg_min(opt, 1); break; + case 'i': /* new level width */ + options->new_level_width = int_arg_min(opt, 1); + break; + case 'e': /* new level height */ + options->new_level_height = int_arg_min(opt, 1); + break; case 'W': /* editor width */ options->editor_width = int_arg_min(opt, 1); break; diff --git a/src/options.c b/src/options.c index 220b74c..ed0ff74 100644 --- a/src/options.c +++ b/src/options.c @@ -10,6 +10,8 @@ struct Options options_defaults(void) .tile_height = TILE_HEIGHT, .tileset_width = 0, .tileset_height = 0, + .new_level_width = NEW_LEVEL_WIDTH, + .new_level_height = NEW_LEVEL_HEIGHT, .editor_width = EDITOR_WINDOW_WIDTH, .editor_height = EDITOR_WINDOW_HEIGHT, .editor_target_fps = EDITOR_TARGET_FPS, @@ -19,5 +21,6 @@ struct Options options_defaults(void) .picker_window_height = 0, .picker_target_fps = PICKER_TARGET_FPS, .picker_padding = PICKER_PADDING, + .level_create = 0, }; }