-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
75 lines (67 loc) · 1.75 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
71
72
73
74
75
#include "main_menu.h"
void menu_img_init(t_menu *menu)
{
int width;
int height;
menu->img.img_start = mlx_xpm_file_to_image(menu->mlx, "xpm/Render01.xpm", &width, &height);
menu->img.img_exit = mlx_xpm_file_to_image(menu->mlx, "xpm/Render02.xpm", &width, &height);
}
int close_simple_menu(t_menu *menu)
{
if (menu->img.img_start)
mlx_destroy_image(menu->mlx, menu->img.img_start);
if (menu->img.img_exit)
mlx_destroy_image(menu->mlx, menu->img.img_exit);
if (menu->win)
mlx_destroy_window(menu->mlx, menu->win);
if (menu->mlx)
mlx_destroy_display(menu->mlx);
if (menu->mlx)
{
free(menu->mlx);
menu->mlx = NULL;
}
exit(EXIT_SUCCESS);
return (0);
}
int key_hook(int keycode, t_menu *menu)
{
static int current_state = 1;
if (keycode == XK_Escape)
{
close_simple_menu(menu);
}
if ((keycode == XK_Up || keycode == XK_Down) && current_state == 1)
{
mlx_put_image_to_window(menu->mlx, menu->win, menu->img.img_exit, 0, 0);
current_state = 2;
}
else if ((keycode == XK_Up || keycode == XK_Down) && current_state == 2)
{
mlx_put_image_to_window(menu->mlx, menu->win, menu->img.img_start, 0, 0);
current_state = 1;
}
if (keycode == XK_Return && current_state == 1)
{
return (0);
}
if (keycode == XK_Return && current_state == 2)
close_simple_menu(menu);
return (0);
}
void start_simple_menu(char *menu_name)
{
t_menu menu;
menu.mlx = mlx_init();
menu.win = mlx_new_window(menu.mlx, 800, 600, menu_name);
menu_img_init(&menu);
mlx_put_image_to_window(menu.mlx, menu.win, menu.img.img_start, 0, 0);
mlx_hook(menu.win, ClientMessage, StructureNotifyMask, close_simple_menu, &menu);
mlx_hook(menu.win, KeyPress, KeyPressMask, key_hook, &menu);
mlx_loop(menu.mlx);
}
int main(void)
{
start_simple_menu("Main Menu");
return (0);
}