From 0dc7fb3914dd6c587e739f838a823710e730652e Mon Sep 17 00:00:00 2001 From: KikooDX Date: Fri, 26 Mar 2021 23:25:23 +0100 Subject: [PATCH] Fix very bad casting mistake & dummy verbose mode --- README | 10 +++++++++- include/info.h | 18 ++++++++++++++++++ src/editing_area/draw.c | 6 +++--- src/editing_area/level.c | 20 +++++++++++++++----- src/editing_area/main.c | 7 +++++++ src/main.c | 10 +++++----- 6 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 include/info.h diff --git a/README b/README index 304388b..1b5b375 100644 --- a/README +++ b/README @@ -55,8 +55,10 @@ All these options take a path to a file as argument. Optionnal flags --------------- -All these options take integers as argument. +These flags are boolean toggles. They take no option. +-verbose +All these flags take integers as argument. -tile-width -tile-height -editor-width @@ -72,6 +74,12 @@ DEFAULT CONFIGURATION Edit include/conf.h to change default configuration. Read the comments! Don't forget to recompile after you made your modifications. +DEBUG FEATURES +============== +The following features are enabled with the -verbose flag. +Pressing middle mouse button in the editor will print the value of the +tile pointed to STDIN. + LICENSE ======= Copyright (C) 2021 KikooDX diff --git a/include/info.h b/include/info.h new file mode 100644 index 0000000..06f12bb --- /dev/null +++ b/include/info.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* Copyright (C) 2021 KikooDX */ +#pragma once + +#define INFO(message) \ + { \ + extern int verbose; \ + if (verbose) \ + printf("[%s:%d] " message "\n", __FILE__, \ + __LINE__); \ + } +#define INFO_VAR(format, var) \ + { \ + extern int verbose; \ + if (verbose) \ + printf("[%s:%d] " format "\n", __FILE__, \ + __LINE__, var); \ + } diff --git a/src/editing_area/draw.c b/src/editing_area/draw.c index 1f25869..4981493 100644 --- a/src/editing_area/draw.c +++ b/src/editing_area/draw.c @@ -16,13 +16,13 @@ void level_draw(struct Level level, struct Options options, for (y = 0; y < level.height; y += 1) { const int tile_index = x + y * level.width; /* if tile index is out of bound, skip */ - if (tile_index >= level.width * - level.height) + if (tile_index >= level.width * level.height) continue; const Tile tile = level.data[x + y * level.width]; /* if tile is not in tileset, skip */ - if (!tile || tile >= options.tileset_width * options.tileset_height) + if (!tile || tile >= options.tileset_width * + options.tileset_height) continue; const Rectangle tile_rect = { diff --git a/src/editing_area/level.c b/src/editing_area/level.c index e8c2719..d7c6004 100644 --- a/src/editing_area/level.c +++ b/src/editing_area/level.c @@ -2,6 +2,7 @@ /* Copyright (C) 2021 KikooDX */ #include "editing_area/level.h" +#include "info.h" #include #include #include @@ -91,10 +92,11 @@ void level_free(struct Level *level) { free(level->data); } int level_write(struct Level level, char *path) { + INFO("write begin"); FILE *file = NULL; int tile = 0; - int tile_size = 0; - int max_tile_size = 1; + unsigned int tile_size = 0; + unsigned int max_tile_size = 1; int i = 0; const int level_size = level.width * level.height; @@ -105,16 +107,23 @@ int level_write(struct Level level, char *path) path); return -1; } + INFO("file opened"); + INFO("look for longest value in data"); /* find longest value in data (in bytes) */ for (i = 0; i < level_size; i += 1) { tile = level.data[i]; tile_size = 1; - while (tile >>= 8) + while (tile >>= 8) { tile_size += 1; - if (tile_size > max_tile_size) + INFO_VAR("%d", tile); + } + if (tile_size > max_tile_size) { max_tile_size = tile_size; + } } + INFO("max tile size is"); + INFO_VAR("%d", max_tile_size); /* write KBLE format version */ write_byte(file, kble_fmt_version); @@ -133,6 +142,7 @@ int level_write(struct Level level, char *path) /* close file */ fclose(file); + INFO("write end"); return 0; } @@ -144,7 +154,7 @@ static int read_byte(FILE *file) fprintf(stderr, "ERROR: unexpected EOF\n"); return -1; } - return (char)byte; + return (unsigned char)byte; } /* Read multiple bytes and "merge" them into one integer. Return -1 if diff --git a/src/editing_area/main.c b/src/editing_area/main.c index 98de2b8..7de979c 100644 --- a/src/editing_area/main.c +++ b/src/editing_area/main.c @@ -3,6 +3,7 @@ #include "editing_area/draw.h" #include "editing_area/level.h" +#include "info.h" #include "mouse.h" #include "options.h" #include "shared_data.h" @@ -97,6 +98,7 @@ static void update_mouse(int *mouse_x, int *mouse_y, struct Level level, { const bool left_click = IsMouseButtonDown(0); const bool right_click = IsMouseButtonDown(1); + const bool middle_click = IsMouseButtonPressed(2); update_mouse_position(mouse_x, mouse_y, level.width - 1, level.height - 1); @@ -110,4 +112,9 @@ static void update_mouse(int *mouse_x, int *mouse_y, struct Level level, if (right_click) { level.data[*mouse_x + *mouse_y * level.width] = 0; } + /* get info about tile */ + if (middle_click) { + INFO_VAR("%d", + level.data[*mouse_x + *mouse_y * level.width]); + } } diff --git a/src/main.c b/src/main.c index 6836096..ff9751c 100644 --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,8 @@ #include #include +int verbose = 0; + static void *create_shared_memory(size_t size); static void overflow_prevention(void); /* work on optarg */ static void process_arguments(int argc, char **argv, @@ -89,6 +91,7 @@ static void process_arguments(int argc, char **argv, int opt; while (1) { static struct option long_options[] = { + {"verbose", no_argument, &verbose, 1}, {"level", required_argument, 0, 'l'}, {"tileset", required_argument, 0, 't'}, {"tile-width", required_argument, 0, 'w'}, @@ -157,11 +160,7 @@ static void process_arguments(int argc, char **argv, exit(EXIT_FAILURE); break; default: - fprintf(stderr, - "ERROR: option -%c non handled " - "(open an issue!)\n", - opt); - exit(EXIT_FAILURE); + break; } } /* missing arguments? */ @@ -193,6 +192,7 @@ static int int_arg_min(int opt, int min) opt, min, value); exit(EXIT_FAILURE); } + return value; } static void set_tileset_dimensions(struct Options *options)