Skip to content
This repository was archived by the owner on May 7, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions main/classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <algorithm>
#include <utility>
#include <string>

#include "include/config.hpp"

Expand Down Expand Up @@ -60,6 +61,8 @@ World::World()
{
window_size = {640, 480};
selected_pixel_type = SAND;
brush_spread = 10;
hide_guide = false;

// Initialising pixel_matrix
calcPixelMatrixSize();
Expand Down Expand Up @@ -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;
Expand All @@ -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 };
}
Expand Down Expand Up @@ -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);


Expand Down
2 changes: 2 additions & 0 deletions main/include/classes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
5 changes: 1 addition & 4 deletions main/include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -36,6 +35,7 @@ class TestWorld : public World {
{
window_size = {3, 3};
selected_pixel_type = SAND;
brush_spread = 1;

// Initialising pixel_matrix
calcPixelMatrixSize();
Expand Down
Loading