From 7158e4447401a8aee18e82976aa0cb55569d5593 Mon Sep 17 00:00:00 2001 From: -inf Date: Wed, 8 Apr 2026 09:40:04 +0300 Subject: [PATCH] Fix a bug with Eraser working wrong. Add functionality to change size of the pixel brush with - and =, made so you can hide the guide, add so there is current selected pixel and brush size are shown --- main/classes.cpp | 49 ++++++++++++++++++++++++++++++++++------ main/include/classes.hpp | 2 ++ main/include/config.hpp | 5 +--- main/main.cpp | 8 +++++++ test/test.cpp | 2 +- 5 files changed, 54 insertions(+), 12 deletions(-) diff --git a/main/classes.cpp b/main/classes.cpp index 2f9cf81..0d2a513 100644 --- a/main/classes.cpp +++ b/main/classes.cpp @@ -18,6 +18,7 @@ #include #include +#include #include "include/config.hpp" @@ -60,6 +61,8 @@ World::World() { window_size = {640, 480}; selected_pixel_type = SAND; + brush_spread = 10; + hide_guide = false; // Initialising pixel_matrix calcPixelMatrixSize(); @@ -127,10 +130,10 @@ SDL_AppResult World::initSDL() void World::addPixel(Vector2 mouse_pos) { - for (int i = 0; i < BRUSH_DENCITY; ++i) + for (int i = 0; i < brush_spread * BRUSH_DENCITY; ++i) { - Vector2 pixel_spawn_coord = { (int)mouse_pos.x / pixel_size + (SDL_rand(BRUSH_SPREAD) * (SDL_rand(2) ? -1 : 1)) - , (int)mouse_pos.y / pixel_size + (SDL_rand(BRUSH_SPREAD) * (SDL_rand(2) ? -1 : 1))}; + Vector2 pixel_spawn_coord = { (int)mouse_pos.x / pixel_size + (SDL_rand(brush_spread) * (SDL_rand(2) ? -1 : 1)) + , (int)mouse_pos.y / pixel_size + (SDL_rand(brush_spread) * (SDL_rand(2) ? -1 : 1))}; // Fix pixel_spawn_coord if it's off the screen if (pixel_spawn_coord.x > pixel_matrix_size.x - 1) pixel_spawn_coord.x = pixel_matrix_size.x - 1; @@ -141,9 +144,14 @@ void World::addPixel(Vector2 mouse_pos) // Override pixel logic switch (selected_pixel_type) { + case VOID: + pixel_matrix[pixel_spawn_coord.x][pixel_spawn_coord.y] = { BG_COLOR, selected_pixel_type, false }; + break; + case SAND: if (pixel_matrix[pixel_spawn_coord.x][pixel_spawn_coord.y].type == VOID - || pixel_matrix[pixel_spawn_coord.x][pixel_spawn_coord.y].type == WATER) + || pixel_matrix[pixel_spawn_coord.x][pixel_spawn_coord.y].type == WATER + || pixel_matrix[pixel_spawn_coord.x][pixel_spawn_coord.y].type == LAVA) { pixel_matrix[pixel_spawn_coord.x][pixel_spawn_coord.y] = { SAND_COLOR, selected_pixel_type, false }; } @@ -468,17 +476,44 @@ SDL_AppResult World::redrawWorld() SDL_RenderTexture(renderer, texture, NULL, &dst_rect); // Text rendering - constexpr static const char* text = -R"(LMB - to start drawing pixels + std::string text = "Selected element: "; + switch (selected_pixel_type) { + case VOID: + text += "Erase"; + break; + + case WATER: + text += "Water"; + break; + + case STONE: + text += "Stone"; + break; + + case LAVA: + text += "Lava"; + break; + + default: + text += "Sand"; + } + text += "\nBrush spread: " + std::to_string(brush_spread); + text += "\nEnter - show/hide guide"; + if (!hide_guide) + text += +R"( +LMB - to start drawing pixels 1 - to select Sand 2 - to select Water 3 - to select Stone 4 - to select Lava 0 - to select Eraser +- - to make brush spread smaller += - to make brush spread bigger Backspace - to clean the screen )"; - static TTF_Text* text_text = TTF_CreateText(text_renderer, font, text, 0); + TTF_Text* text_text = TTF_CreateText(text_renderer, font, text.c_str(), 0); TTF_DrawRendererText(text_text, TEXT_SIZE, TEXT_SIZE); diff --git a/main/include/classes.hpp b/main/include/classes.hpp index e93f0a5..4ad992b 100644 --- a/main/include/classes.hpp +++ b/main/include/classes.hpp @@ -231,6 +231,8 @@ class World public: bool mouse_is_down = false; ///< Stores current state of the mouse button, used in redrawWorld() to detect then addPixel() should be inwoked. PixelType selected_pixel_type; ///< Currently selected PixelType, used in addPixel(). + int brush_spread; ///< Defines how off the mouse cursor's coordinates pixels can spawn. + bool hide_guide; ~World(); diff --git a/main/include/config.hpp b/main/include/config.hpp index 5942ce5..268b5dc 100644 --- a/main/include/config.hpp +++ b/main/include/config.hpp @@ -14,11 +14,8 @@ #define MAX_PIXEL_MATRIX_SIZE 100000 -/// Defines how off the mouse cursor's coordinates pixels can spawn. -#define BRUSH_SPREAD 7 - /// Defines how much pixels will me spawned each time World::addPixel() inwoked. -#define BRUSH_DENCITY (BRUSH_SPREAD * 3) +#define BRUSH_DENCITY 3 /// Defines with how off the default color pixel color can be. #define COLOR_SPREAD 100 diff --git a/main/main.cpp b/main/main.cpp index 0f38c95..5c9a226 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -64,6 +64,14 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) world->selected_pixel_type = LAVA; if (event->key.key == SDLK_0) world->selected_pixel_type = VOID; + + if (event->key.key == SDLK_MINUS && world->brush_spread > 1) + world->brush_spread--; + if (event->key.key == SDLK_EQUALS) + world->brush_spread++; + + if (event->key.key == SDLK_RETURN) + world->hide_guide = !world->hide_guide; if (event->key.key == SDLK_BACKSPACE) world->clearWorld(); diff --git a/test/test.cpp b/test/test.cpp index 4b63860..8589ab0 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -3,7 +3,6 @@ #define CONFIG_HPP #define MAX_PIXEL_MATRIX_SIZE 1000 -#define BRUSH_SPREAD 0 #define BRUSH_DENCITY 1 #define BG_COLOR (SDL_Color) { (Uint8) (0) \ , (Uint8) (0) \ @@ -36,6 +35,7 @@ class TestWorld : public World { { window_size = {3, 3}; selected_pixel_type = SAND; + brush_spread = 1; // Initialising pixel_matrix calcPixelMatrixSize();