-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdl_handlers.c
101 lines (92 loc) · 2.59 KB
/
sdl_handlers.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sdl_handlers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pvan-erp <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/03 21:40:34 by pvan-erp #+# #+# */
/* Updated: 2017/04/04 23:14:13 by pvan-erp ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
static void zoom(int button, int x, int y, t_sdl *sdl)
{
long double oldscale;
oldscale = sdl->scale;
if (button == 5)
sdl->scale *= sdl->scalefr;
else if (button == 4)
sdl->scale /= sdl->scalefr;
sdl->offset.a += x * (sdl->scale - oldscale);
sdl->offset.b += y * (sdl->scale - oldscale);
}
int expose_hook(t_sdl *sdl)
{
draw(sdl);
return (0);
}
int motion_hook(int x, int y, t_sdl *sdl)
{
((t_frac *)sdl->data)->c.a = x * sdl->scale - sdl->offset.a;
((t_frac *)sdl->data)->c.b = -(y * sdl->scale - sdl->offset.b);
draw(sdl);
return (0);
}
int wheel_hook(int button, t_sdl *sdl)
{
int x;
int y;
SDL_GetMouseState(&x, &y);
if (button > 0)
zoom(4, x, y, sdl);
else if (button < 0)
zoom(5, x, y, sdl);
draw(sdl);
return (0);
}
int mouse_hook(int button, int x, int y, t_sdl *sdl)
{
if (y >= 0)
{
if (button == SDL_BUTTON_LEFT)
((t_frac *)sdl->data)->i_max++;
else if (button == SDL_BUTTON_RIGHT)
((t_frac *)sdl->data)->i_max--;
else if (button == 4 || button == 5)
zoom(button, x, y, sdl);
else if (button == SDL_BUTTON_MIDDLE)
init_scale(sdl);
if (button >= 1 && button <= 5)
{
if (((t_frac *)sdl->data)->i_max == 0)
((t_frac *)sdl->data)->i_max = 1;
draw(sdl);
}
}
return (0);
}
int key_hook(int key_code, t_sdl *sdl)
{
if (key_code == SDLK_ESCAPE)
{
free(sdl->img.id);
free(sdl->data);
free(sdl->win);
free(sdl->id);
exit(0);
}
else
{
if (key_code == SDLK_LEFT)
sdl->offset.a -= 10 * sdl->scale;
else if (key_code == SDLK_RIGHT)
sdl->offset.a += 10 * sdl->scale;
else if (key_code == SDLK_DOWN)
sdl->offset.b += 10 * sdl->scale;
else if (key_code == SDLK_UP)
sdl->offset.b -= 10 * sdl->scale;
draw(sdl);
}
return (0);
}