From 8fee3bcf26c615a6f48f5beaa4da8992d1ceb51a Mon Sep 17 00:00:00 2001 From: KikooDX Date: Thu, 25 Mar 2021 01:14:39 +0100 Subject: [PATCH] Mouse update code, place and erase tile --- src/editing_area/draw.c | 6 ++++-- src/editing_area/main.c | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/editing_area/draw.c b/src/editing_area/draw.c index 4d56237..7485c00 100644 --- a/src/editing_area/draw.c +++ b/src/editing_area/draw.c @@ -28,8 +28,10 @@ void level_draw(struct Level level, Texture2D tileset) } } -void editor_mouse_draw(int x, int y) { +void editor_mouse_draw(int x, int y) +{ const int tx = x * TILE_WIDTH + draw_offset_x; const int ty = y * TILE_HEIGHT + draw_offset_y; - DrawRectangle(tx, ty, TILE_WIDTH, TILE_HEIGHT, overring_tile_color); + DrawRectangle(tx, ty, TILE_WIDTH, TILE_HEIGHT, + overring_tile_color); } diff --git a/src/editing_area/main.c b/src/editing_area/main.c index 7424c51..4ae8608 100644 --- a/src/editing_area/main.c +++ b/src/editing_area/main.c @@ -12,6 +12,8 @@ #include static void init_mouse(void); +static void update_mouse(int *mouse_x, int *mouse_y, struct Level level, + struct SharedData *shared_data); int editing_area_main(int argc, char **argv, struct SharedData *shared_data) @@ -37,8 +39,8 @@ int editing_area_main(int argc, char **argv, while (!WindowShouldClose()) { /* update */ - mouse_x = GetMouseX(); - mouse_y = GetMouseY(); + update_mouse(&mouse_x, &mouse_y, level, + shared_data); /* draw */ BeginDrawing(); @@ -75,7 +77,38 @@ int editing_area_main(int argc, char **argv, return EXIT_SUCCESS; } -static void init_mouse(void) { +static void init_mouse(void) +{ SetMouseOffset(-draw_offset_x, -draw_offset_y); - SetMouseScale(1.0 / (TILE_WIDTH * EDITOR_SCALE), 1.0 / (TILE_HEIGHT * EDITOR_SCALE)); + SetMouseScale(1.0 / (TILE_WIDTH * EDITOR_SCALE), + 1.0 / (TILE_HEIGHT * EDITOR_SCALE)); +} + +static void update_mouse(int *mouse_x, int *mouse_y, struct Level level, + struct SharedData *shared_data) +{ + 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; + + /* set tile */ + if (left_click) { + level.data[*mouse_x + *mouse_y * level.width] = + shared_data->selected_tile; + } + /* remove tile */ + if (right_click) { + level.data[*mouse_x + *mouse_y * level.width] = 0; + } }