diff options
author | kdx <kikoodx@paranoici.org> | 2023-11-23 03:51:18 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-11-23 03:51:18 +0100 |
commit | fc890352edb1e1d14aff51d5a3b76a99c43fabd3 (patch) | |
tree | f6bbf12ffac9816039bd103dc3bd6289ab6b20e5 | |
parent | 7e6c533236c333f191b502419e5970d4a3d722a8 (diff) | |
download | aancyk-fc890352edb1e1d14aff51d5a3b76a99c43fabd3.tar.gz |
brush x and y flipping
-rw-r--r-- | inc/grid.h | 3 | ||||
-rw-r--r-- | inc/window.h | 2 | ||||
-rw-r--r-- | src/editor.c | 17 | ||||
-rw-r--r-- | src/grid.c | 24 |
4 files changed, 45 insertions, 1 deletions
@@ -18,3 +18,6 @@ Grid *grid_load(const char *path); void grid_clear(Grid *this); void grid_resize(Grid *this, int width, int height); void grid_draw(Grid *this, Tileset *tset, Window *win, int x, int y); +void grid_swap(Grid *this, int x0, int y0, int x1, int y1); +void grid_flip_x(Grid *this); +void grid_flip_y(Grid *this); diff --git a/inc/window.h b/inc/window.h index b4e04b1..a06f48d 100644 --- a/inc/window.h +++ b/inc/window.h @@ -18,7 +18,7 @@ struct Window { bool in_focus; int mouse_x; int mouse_y; - bool clicked; + int clicked; int clicked_x; int clicked_y; void (*fn_event)(Window *this, struct Root *root, SDL_Event *e); diff --git a/src/editor.c b/src/editor.c index 664da15..2aed6be 100644 --- a/src/editor.c +++ b/src/editor.c @@ -2,6 +2,7 @@ static void _ev_mousemotion(Window *this, Root *root, SDL_MouseMotionEvent *e); static void _ev_mouse(Window *this, Root *root); +static void _ev_key(Window *this, Root *root); void editor_redraw(Window *this, Root *root) @@ -54,6 +55,9 @@ editor_event(Window *this, Root *root, SDL_Event *e) _ev_mouse(this, root); this->redraw = true; break; + case SDL_KEYDOWN: + _ev_key(this, root); + break; case SDL_WINDOWEVENT: if (e->window.event == SDL_WINDOWEVENT_LEAVE) this->clicked = false; @@ -132,3 +136,16 @@ _ev_mouse(Window *this, Root *root) } } } + +static void +_ev_key(Window *this, Root *root) +{ + Input *const input = &this->input; + if (input_key_pressed(input, SDL_SCANCODE_F)) { + if (input_key_down(input, SDL_SCANCODE_LSHIFT)) + grid_flip_y(root->brush); + else + grid_flip_x(root->brush); + this->redraw = true; + } +} @@ -160,3 +160,27 @@ grid_draw(Grid *this, Tileset *tset, Window *win, int x, int y) } } } + +void +grid_swap(Grid *this, int x0, int y0, int x1, int y1) +{ + const int tmp = grid_get(this, x0, y0); + grid_set(this, x0, y0, grid_get(this, x1, y1)); + grid_set(this, x1, y1, tmp); +} + +void +grid_flip_x(Grid *this) +{ + forloop (y, 0, this->height) + forloop (x, 0, (this->width + 1) / 2) + grid_swap(this, x, y, this->width - 1 - x, y); +} + +void +grid_flip_y(Grid *this) +{ + forloop (x, 0, this->width) + forloop (y, 0, (this->height + 1) / 2) + grid_swap(this, x, y, x, this->height - 1 - y); +} |