diff options
author | kdx <kikoodx@paranoici.org> | 2023-11-22 23:10:44 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-11-22 23:10:44 +0100 |
commit | 8cdbecd94d7c6f087f011f6d9bd4affadaedb1e2 (patch) | |
tree | f3b18f16a88576bb03f59c78309e0702c9e30281 | |
parent | 482f2e5838951d575ffcaa3785d7b28c16df2cf4 (diff) | |
download | aancyk-8cdbecd94d7c6f087f011f6d9bd4affadaedb1e2.tar.gz |
cracked tile picking
-rw-r--r-- | inc/window.h | 1 | ||||
-rw-r--r-- | src/editor.c | 43 | ||||
-rw-r--r-- | src/window.c | 7 |
3 files changed, 48 insertions, 3 deletions
diff --git a/inc/window.h b/inc/window.h index 1ab9a6a..b4e04b1 100644 --- a/inc/window.h +++ b/inc/window.h @@ -33,3 +33,4 @@ void window_event(Window *this, struct Root *root, SDL_Event *e); void window_redraw(Window *this, struct Root *root); void window_set_draw_color(Window *this, uint8_t r, uint8_t g, uint8_t b, uint8_t a); void window_fill_rect(Window *this, const SDL_Rect *rect); +void window_draw_rect(Window *this, const SDL_Rect *rect); diff --git a/src/editor.c b/src/editor.c index 640852d..fa9b209 100644 --- a/src/editor.c +++ b/src/editor.c @@ -15,6 +15,21 @@ editor_redraw(Window *this, Root *root) grid_draw(root->brush, root->tileset, this, dx, dy); tileset_set_alpha(root->tileset, 255); } + + if (this->clicked) { + const int x = min(this->clicked_x, this->mouse_x); + const int y = min(this->clicked_y, this->mouse_y); + const int w = max(this->clicked_x, this->mouse_x) - x + 1; + const int h = max(this->clicked_y, this->mouse_y) - y + 1; + const SDL_Rect rect = { + x * root->cfg->tile_width - 1, + y * root->cfg->tile_height - 1, + w * root->cfg->tile_width + 2, + h * root->cfg->tile_height + 2 + }; + window_set_draw_color(this, 255, 255, 0, 255); + window_draw_rect(this, &rect); + } } void @@ -26,6 +41,7 @@ editor_event(Window *this, Root *root, SDL_Event *e) _ev_mouse(this, root); break; case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: _ev_mouse(this, root); break; case SDL_WINDOWEVENT: @@ -55,14 +71,35 @@ _ev_mousemotion(Window *this, Root *root, SDL_MouseMotionEvent *e) static void _ev_mouse(Window *this, Root *root) { - if (input_button_down(&this->input, SDL_BUTTON_RIGHT)) { + Input *const input = &this->input; + if (input_button_pressed(input, SDL_BUTTON_LEFT) && + input_key_down(input, SDL_SCANCODE_LCTRL)) { + this->clicked = true; + this->clicked_x = this->mouse_x; + this->clicked_y = this->mouse_y; + } else if (input_button_released(input, SDL_BUTTON_LEFT) && + this->clicked) { + this->clicked = false; + const int x = min(this->clicked_x, this->mouse_x); + const int y = min(this->clicked_y, this->mouse_y); + const int w = max(this->clicked_x, this->mouse_x) - x + 1; + const int h = max(this->clicked_y, this->mouse_y) - y + 1; + grid_resize(root->brush, w, h); + forloop (ty, 0, h) { + forloop (tx, 0, w) { + int tile = grid_get(root->cell, tx + x, ty + y); + grid_set(root->brush, tx, ty, tile); + } + } + this->redraw = true; + } else if (input_button_down(input, SDL_BUTTON_RIGHT)) { if (grid_get(root->cell, this->mouse_x, this->mouse_y)) { grid_set(root->cell, this->mouse_x, this->mouse_y, 0); this->redraw = true; } - } else if (input_button_down(&this->input, SDL_BUTTON_LEFT)) { + } else if (input_button_down(input, SDL_BUTTON_LEFT)) { const int prev_tile = grid_get(root->cell, this->mouse_x, this->mouse_y); - if (input_key_down(&this->input, SDL_SCANCODE_LCTRL)) { + if (input_key_down(input, SDL_SCANCODE_LCTRL)) { grid_resize(root->brush, 1, 1); grid_set(root->brush, 0, 0, prev_tile); this->redraw = true; diff --git a/src/window.c b/src/window.c index 2ef97b7..4f73792 100644 --- a/src/window.c +++ b/src/window.c @@ -104,3 +104,10 @@ window_fill_rect(Window *this, const SDL_Rect *rect) if (SDL_RenderFillRect(this->ren, rect) < 0) pwrn("%s", SDL_GetError()); } + +void +window_draw_rect(Window *this, const SDL_Rect *rect) +{ + if (SDL_RenderDrawRect(this->ren, rect) < 0) + pwrn("%s", SDL_GetError()); +} |