aboutsummaryrefslogtreecommitdiff
path: root/src/event.c
blob: 8cb0016e6e919909ee5f16a1a54f250961e7ffb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#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 void	st_resize(State *state, int new_width, int new_height);

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)
{
    SDL_Event	e;

    while (SDL_PollEvent(&e))
    {
        switch (e.type)
        {
            case SDL_QUIT:
                state->running = false;
                break;

            case SDL_KEYDOWN:
				if (e.key.keysym.sym == SDLK_ESCAPE)
				{
                    state->running = false;
                    break;
				}
				if (e.key.keysym.sym == SDLK_s)
					state->smooth = !state->smooth;
				else if (e.key.keysym.sym == SDLK_w)
					state->samples += 1.0;
				else if (e.key.keysym.sym == SDLK_q)
				{
					state->samples -= 1.0;
					if (state->samples <= 0.0)
						state->samples  = 1.0;
				}
				else
					st_set_key(e.key.keysym.sym, true);
                break;

            case SDL_KEYUP:
				st_set_key(e.key.keysym.sym, false);
                break;

            case SDL_MOUSEWHEEL:
                if (e.wheel.y == -1)
					st_zoom(state, true);
                else if (e.wheel.y == 1)
					st_zoom(state, false);
                break;

			case SDL_WINDOWEVENT:
				if (e.window.event == SDL_WINDOWEVENT_RESIZED)
					st_resize(state, e.window.data1, e.window.data2);
				break;
        }
    }
	st_apply_keys(state);
}

void		st_resize(State *state, int new_width, int new_height)
{
	double w_delta = ((double)new_width - (double)state->width) / 2.0;
	double h_delta = ((double)new_height - (double)state->height) / 2.0;

	double real_dist = state->real_end - state->real_start;
	double imag_dist = state->imag_end - state->imag_start;

	double width_real_ratio =  (double)state->width / real_dist;
	double height_imag_ratio = (double)state->height / imag_dist;

	w_delta /= width_real_ratio;
	h_delta /= height_imag_ratio;

	state->real_start -= w_delta;
	state->real_end   += w_delta;

	state->imag_start -= h_delta;
	state->imag_end   += h_delta;

	SDL_GL_GetDrawableSize(state->window, &state->width, &state->height);
	GL_CALL(glViewport(0, 0, state->width, state->height));
}

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 64

static void	st_zoom(State *state, bool zoom_in)
{
	double factor = zoom_in ? 1 : -1;
	double real_change = (state->real_end - state->real_start) / MANDEL_ZOOM_RATIO;
	double imag_change = (state->imag_end - state->imag_start) / MANDEL_ZOOM_RATIO;
	state->real_start += factor * real_change;
	state->real_end -= factor * real_change;
	state->imag_start += factor * imag_change;
	state->imag_end -= factor * imag_change;
}

#define MANDEL_MOVE_RATIO 64

static void	st_move_horizontal(State *state, bool move_right)
{
	double factor = move_right ? 1 : -1;
	double real_change = (state->real_end - state->real_start) / MANDEL_MOVE_RATIO;
	state->real_start += factor * real_change;
	state->real_end += factor * real_change;
}

static void	st_move_vertical(State *state, bool move_down)
{
	double factor = move_down ? -1 : 1;
	double imag_change = (state->imag_end - state->imag_start) / MANDEL_MOVE_RATIO;
	state->imag_start += factor * imag_change;
	state->imag_end += factor * imag_change;
}