diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-05-20 09:02:13 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-05-20 09:02:13 +0200 |
| commit | b9915d27b0a21a97540d6ed193f338da3732e1a1 (patch) | |
| tree | 11c76553ecca1b152dcd7c992787ecc58b1941d1 | |
| parent | 09a819b2ef927adf5239a73f91cdfcefd6774688 (diff) | |
| download | mandelbrot-b9915d27b0a21a97540d6ed193f338da3732e1a1.tar.gz mandelbrot-b9915d27b0a21a97540d6ed193f338da3732e1a1.tar.bz2 mandelbrot-b9915d27b0a21a97540d6ed193f338da3732e1a1.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 ca2988e..f220132 100644 --- a/inc/mandel.h +++ b/inc/mandel.h @@ -22,6 +22,20 @@ error_check_gl(#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 struct { uint8_t r; diff --git a/src/event.c b/src/event.c index 6fd6a8c..252f9d3 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->texture = color_texture_new( state->iterations); */ - break; - case SDLK_e: - state->iterations -= 10; - if (state->iterations <= 0) - state->iterations = 1; - /* state->texture = color_texture_new( 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: @@ -80,9 +62,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) { @@ -95,7 +141,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) { |
