From b433b09f75d12535aeabffa2a0ef1163f4ed0e23 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Thu, 25 Mar 2021 11:40:46 +0100 Subject: [PATCH] Level edition, tile picking and misc. This is the first usable version of the project! --- CMakeLists.txt | 2 ++ include/conf.h | 1 + include/mouse.h | 6 ++++++ src/editing_area/main.c | 14 +++----------- src/mouse.c | 20 ++++++++++++++++++++ src/tile_picker/main.c | 41 ++++++++++++++++++++++++++++++++++++++++- 6 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 include/mouse.h create mode 100644 src/mouse.c diff --git a/CMakeLists.txt b/CMakeLists.txt index b62f676..af94e46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ include_directories(include) set(SOURCES src/main.c + src/mouse.c src/editing_area/main.c src/editing_area/level.c src/editing_area/draw.c @@ -17,6 +18,7 @@ set(FLAGS -Wall -Wextra -Wshadow -Wswitch-default -Wswitch-enum -Wunreachable-code -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -Werror-implicit-function-declaration + -Wformat-pedantic -Werror -pedantic -std=c90 -Os) diff --git a/include/conf.h b/include/conf.h index d9a064a..d6787ac 100644 --- a/include/conf.h +++ b/include/conf.h @@ -22,6 +22,7 @@ static const int editor_window_height = 224 * EDITOR_SCALE; static const int draw_offset_x = -2 * EDITOR_SCALE; static const int draw_offset_y = -8 * EDITOR_SCALE; +#define PICKER_SCALE 1 /* don't change this yet! */ #define PICKER_PADDING 4 static const int picker_window_width = (TILE_WIDTH + PICKER_PADDING) * TILESET_WIDTH + PICKER_PADDING; diff --git a/include/mouse.h b/include/mouse.h new file mode 100644 index 0000000..2786ee6 --- /dev/null +++ b/include/mouse.h @@ -0,0 +1,6 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* Copyright (C) 2021 KikooDX */ +#pragma once + +void update_mouse_position(int *mouse_x, int *mouse_y, int max_x, + int max_y); diff --git a/src/editing_area/main.c b/src/editing_area/main.c index 4ae8608..e48fa20 100644 --- a/src/editing_area/main.c +++ b/src/editing_area/main.c @@ -4,6 +4,7 @@ #include "conf.h" #include "editing_area/draw.h" #include "editing_area/level.h" +#include "mouse.h" #include "shared_data.h" #include #include @@ -90,17 +91,8 @@ 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); - /* update mouse position */ - *mouse_x = GetMouseX(); - *mouse_y = GetMouseY(); - if (*mouse_x < 0) - *mouse_x = 0; - if (*mouse_y < 0) - *mouse_y = 0; - if (*mouse_x >= level.width) - *mouse_x = level.width - 1; - if (*mouse_y >= level.height) - *mouse_y = level.height - 1; + update_mouse_position(mouse_x, mouse_y, level.width - 1, + level.height - 1); /* set tile */ if (left_click) { diff --git a/src/mouse.c b/src/mouse.c new file mode 100644 index 0000000..a7c4971 --- /dev/null +++ b/src/mouse.c @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* Copyright (C) 2021 KikooDX */ + +#include "mouse.h" +#include + +void update_mouse_position(int *mouse_x, int *mouse_y, int max_x, + int max_y) +{ + *mouse_x = GetMouseX(); + *mouse_y = GetMouseY(); + if (*mouse_x < 0) + *mouse_x = 0; + if (*mouse_y < 0) + *mouse_y = 0; + if (*mouse_x >= max_x) + *mouse_x = max_x; + if (*mouse_y >= max_y) + *mouse_y = max_y; +} diff --git a/src/tile_picker/main.c b/src/tile_picker/main.c index 0990735..706f865 100644 --- a/src/tile_picker/main.c +++ b/src/tile_picker/main.c @@ -2,24 +2,39 @@ /* Copyright (C) 2021 KikooDX */ #include "conf.h" +#include "mouse.h" #include "shared_data.h" #include "tile_picker/draw.h" +#include +#include #include #include +static void init_mouse(void); +static void update_mouse(int *mouse_x, int *mouse_y, + struct SharedData *shared_data); + int tile_picker_main(int argc, char **argv, struct SharedData *shared_data) { + int mouse_x; + int mouse_y; + Texture2D tileset; + /* initialize raylib */ InitWindow(picker_window_width, picker_window_height, "SLE secondary window"); SetTargetFPS(picker_target_fps); + init_mouse(); /* load textures */ - const Texture2D tileset = LoadTexture(argv[1]); + tileset = LoadTexture(argv[1]); /* only proceed if tileset is well loaded */ if (tileset.width > 0) { while (!shared_data->end_child) { + /* update */ + update_mouse(&mouse_x, &mouse_y, shared_data); + /* draw */ BeginDrawing(); @@ -38,3 +53,27 @@ int tile_picker_main(int argc, char **argv, return EXIT_SUCCESS; } + +static void init_mouse(void) +{ + SetMouseOffset(-PICKER_PADDING, -PICKER_PADDING); + SetMouseScale( + 1.0 / ((TILE_WIDTH + PICKER_PADDING) * PICKER_SCALE), + 1.0 / ((TILE_HEIGHT + PICKER_PADDING) * PICKER_SCALE)); +} + +static void update_mouse(int *mouse_x, int *mouse_y, + struct SharedData *shared_data) +{ + const bool left_click = IsMouseButtonDown(0); + const bool right_click = IsMouseButtonDown(1); + + update_mouse_position( + mouse_x, mouse_y, + TILESET_MIN_WIDTH_PX + TILESET_WIDTH * PICKER_PADDING, + TILESET_MIN_HEIGHT_PX + TILESET_HEIGHT * PICKER_PADDING); + + /* set left button tile */ + if (left_click) + shared_data->selected_tile = *mouse_x + *mouse_y * TILESET_WIDTH; +}