-
Notifications
You must be signed in to change notification settings - Fork 5
/
hooks.c
63 lines (54 loc) · 2.02 KB
/
hooks.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
#include "mlx_sample.h"
#include <stdio.h>
/* Any functon that you hook with the key event must be like this:
* Recibing and int for the code of the key pressed
* and a void pointer in case you need to recibe someting */
int ft_input(int key, void *param)
{
t_program *program = (t_program *)param;
// mlx function that clears the window
mlx_clear_window(program->mlx, program->window.reference);
// move in a direction based on the key
if (key == 124)
program->sprite_position.x += program->sprite.size.x;
else if (key == 123)
program->sprite_position.x -= program->sprite.size.x;
else if (key == 125)
program->sprite_position.y += program->sprite.size.y;
else if (key == 126)
program->sprite_position.y -= program->sprite.size.y;
// change color based on keys R, G and B.
else if (key == 15)
turn_img_to_color(&program->sprite, new_color(255,0,0,0));
else if (key == 5)
turn_img_to_color(&program->sprite, new_color(0,255,0,0));
else if (key == 11)
turn_img_to_color(&program->sprite, new_color(0,0,255,0));
// mlx function that puts and image into a window at a given position
// (the position 0,0 is the upper-left corner)
mlx_put_image_to_window(program->mlx, program->window.reference,
program->sprite.reference, program->sprite_position.x, program->sprite_position.y);
// print the key pressed so you know the number of each key
printf("Key pressed -> %d\n", key);
return (0);
}
int ft_update (void *param)
{
t_program *program = (t_program *)param;
// a static that saves how many frames have passed
static int frame;
// add a frame and every x frames change the position by 1 pixel
// so it looks like its animated
frame++;
if (frame == ANIMATION_FRAMES)
program->sprite_position.y += 1;
// every x*2 frames go back that pixel to loop the animation
else if (frame >= ANIMATION_FRAMES * 2)
{
program->sprite_position.y -= 1;
frame = 0;
}
mlx_put_image_to_window(program->mlx, program->window.reference,
program->sprite.reference, program->sprite_position.x, program->sprite_position.y);
return (0);
}