diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-05-20 09:02:13 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-05-22 09:27:12 +0200 |
| commit | c9098d549d63b5fe18d8c95049ba13a5ae6e9eca (patch) | |
| tree | 50f78e9733764258f362e0a308a1c87a38a432a3 | |
| parent | b0998910dd974280b3c6f3f65e21bfd5859b117f (diff) | |
| download | mandelbrot_cpu-c9098d549d63b5fe18d8c95049ba13a5ae6e9eca.tar.gz mandelbrot_cpu-c9098d549d63b5fe18d8c95049ba13a5ae6e9eca.tar.bz2 mandelbrot_cpu-c9098d549d63b5fe18d8c95049ba13a5ae6e9eca.zip | |
Added multiple key pressed at once
| -rw-r--r-- | inc/mandel.h | 14 | ||||
| -rw-r--r-- | src/event.c | 128 |
2 files changed, 101 insertions, 41 deletions
diff --git a/inc/mandel.h b/inc/mandel.h index a98e007..1496aa0 100644 --- a/inc/mandel.h +++ b/inc/mandel.h @@ -13,6 +13,20 @@ error_check_sdl(#x, __FILE__, __LINE__); \ } while (0) +enum +{ + KEY_UP = 0, + KEY_DOWN, + KEY_RIGHT, + KEY_LEFT, + + KEY_INC_ITERATIONS, + KEY_DEC_ITERATIONS, + + KEY_ZOOM_IN, + KEY_ZOOM_OUT, +}; + typedef union { uint32_t data; diff --git a/src/event.c b/src/event.c index f0f0e14..9b05b76 100644 --- a/src/event.c +++ b/src/event.c @@ -1,8 +1,24 @@ #include "mandel.h" + static void st_zoom(State *state, bool zoom_in); static void st_move_horizontal(State *state, bool move_right); static void st_move_vertical(State *state, bool move_down); +static void st_set_key(SDL_Keycode sym, bool value); +static void st_apply_keys(State *state); + +static bool g_key_states[] = { + [KEY_UP] = false, + [KEY_DOWN] = false, + [KEY_RIGHT] = false, + [KEY_LEFT] = false, + + [KEY_INC_ITERATIONS] = false, + [KEY_DEC_ITERATIONS] = false, + + [KEY_ZOOM_IN] = false, + [KEY_ZOOM_OUT] = false, +}; void event_handle(State *state) { @@ -17,45 +33,11 @@ void event_handle(State *state) break; case SDL_KEYDOWN: - switch (e.key.keysym.sym) - { - case SDLK_r: - state->iterations += 10; - state->palette = color_palette_new(state->palette, state->iterations); - break; - case SDLK_e: - state->iterations -= 10; - if (state->iterations <= 0) - state->iterations = 1; - state->palette = color_palette_new(state->palette, state->iterations); - break; - case SDLK_UP: - case SDLK_k: - st_move_vertical(state, false); - break; - case SDLK_DOWN: - case SDLK_j: - st_move_vertical(state, true); - break; - case SDLK_LEFT: - case SDLK_h: - st_move_horizontal(state, false); - break; - case SDLK_RIGHT: - case SDLK_l: - st_move_horizontal(state, true); - break; - case SDLK_EQUALS: - case SDLK_f: - st_zoom(state, true); - break; - case SDLK_MINUS: - case SDLK_d: - st_zoom(state, false); - break; - case SDLK_q: - state->running = false; - } + st_set_key(e.key.keysym.sym, true); + break; + + case SDL_KEYUP: + st_set_key(e.key.keysym.sym, false); break; case SDL_MOUSEWHEEL: @@ -92,9 +74,73 @@ void event_handle(State *state) break; } } + st_apply_keys(state); +} + +static void st_set_key(SDL_Keycode sym, bool value) +{ + switch (sym) + { + case SDLK_r: g_key_states[KEY_INC_ITERATIONS] = value; break; + case SDLK_e: g_key_states[KEY_DEC_ITERATIONS] = value; break; + + case SDLK_UP: + case SDLK_k: + g_key_states[KEY_UP] = value; + break; + case SDLK_DOWN: + case SDLK_j: + g_key_states[KEY_DOWN] = value; + break; + case SDLK_LEFT: + case SDLK_h: + g_key_states[KEY_LEFT] = value; + break; + case SDLK_RIGHT: + case SDLK_l: + g_key_states[KEY_RIGHT] = value; + break; + + case SDLK_EQUALS: + case SDLK_f: + g_key_states[KEY_ZOOM_IN] = value; + break; + case SDLK_MINUS: + case SDLK_d: + g_key_states[KEY_ZOOM_OUT] = value; + break; + } +} + +#define MANDEL_ITERATIONS_DELTA 2 + +static void st_apply_keys(State *state) +{ + if (g_key_states[KEY_INC_ITERATIONS]) + state->iterations += MANDEL_ITERATIONS_DELTA; + if (g_key_states[KEY_DEC_ITERATIONS]) + { + state->iterations -= MANDEL_ITERATIONS_DELTA; + if (state->iterations <= 0) + state->iterations = 1; + } + + if (g_key_states[KEY_UP]) + st_move_vertical(state, false); + if (g_key_states[KEY_DOWN]) + st_move_vertical(state, true); + if (g_key_states[KEY_LEFT]) + st_move_horizontal(state, false); + if (g_key_states[KEY_RIGHT]) + st_move_horizontal(state, true); + + if (g_key_states[KEY_ZOOM_IN]) + st_zoom(state, true); + if (g_key_states[KEY_ZOOM_OUT]) + st_zoom(state, false); } -#define MANDEL_ZOOM_RATIO 30 +#define MANDEL_ZOOM_RATIO 64 static void st_zoom(State *state, bool zoom_in) { @@ -107,7 +153,7 @@ static void st_zoom(State *state, bool zoom_in) state->imag_end -= factor * imag_change; } -#define MANDEL_MOVE_RATIO 20 +#define MANDEL_MOVE_RATIO 64 static void st_move_horizontal(State *state, bool move_right) { |
