-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.c
70 lines (62 loc) · 1.63 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include "../mlx/mlx.h"
#define X_EVENT_KEY_PRESS 2
#define X_EVENT_KEY_release 3
#define X_EVENT_KEY_EXIT 17 //exit key code
//Mac key code example
//All the key code example other than below is described on the site linked in READEME.md
#define KEY_ESC 53
# define KEY_Q 12
# define KEY_W 13
# define KEY_E 14
# define KEY_R 15
# define KEY_A 0
# define KEY_S 1
# define KEY_D 2
//Since key_press() can recieve only one argument, all the argument shold be gathered in one structure
//x,y and str are meaningless variables.
typedef struct s_param{
int x;
int y;
char str[3];
} t_param;
//Only param->x will be used.
void param_init(t_param *param)
{
param->x = 3;
param->y = 4;
param->str[0] = 'a';
param->str[1] = 'b';
param->str[2] = '\0';
}
int key_press(int keycode, t_param *param)
{
static int a = 0;
if (keycode == KEY_W)//Action when W key pressed
param->x++;
else if (keycode == KEY_S) //Action when S key pressed
param->x--;
else if (keycode == KEY_ESC) //Quit the program when ESC key pressed
exit(0);
printf("x: %d\n", param->x);
return (0);
}
int main(void)
{
void *mlx;
void *win;
t_param param;
param_init(¶m);
mlx = mlx_init();
win = mlx_new_window(mlx, 500, 500, "mlx_project");
printf("-------------------------------\n");
printf("'W key': Add 1 to x.\n");
printf("'S key': Subtract 1 from x\n");
printf("'ESC key': Exit this program\n");
printf("'Other keys': print current x \n");
printf("-------------------------------\n");
printf("Current x = 3\n");
mlx_hook(win, X_EVENT_KEY_PRESS, 0, &key_press, ¶m);
mlx_loop(mlx);
}