-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu.c
70 lines (63 loc) · 1.87 KB
/
menu.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 <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "menu.h"
void menu_handleEvent(s_Game *game, s_Menu* menu, int key) {
if (
(IS_GCW && key == SDLK_LCTRL)
|| (!IS_GCW && key == SDLK_SPACE)
) {
menu->pActions[menu->iSelectedItem](game);
}
else if (key == SDLK_UP) {
menu->iSelectedItem = (menu->iNbItems + menu->iSelectedItem - 1) % menu->iNbItems;
}
else if (key == SDLK_DOWN) {
menu->iSelectedItem = (menu->iSelectedItem + 1) % menu->iNbItems;
}
}
void menu_render(s_Game* game, s_Menu* menu) {
int i;
for (i = 0; i < menu->iNbItems; ++i) {
SDL_Texture *item;
if (i == menu->iSelectedItem) {
item = menu->pSelectedItems[i];
}
else {
item = menu->pItems[i];
}
int textWidth, textHeight;
SDL_QueryTexture(item, NULL, NULL, &textWidth, &textHeight);
SDL_Rect renderQuad = {50, 30 + i * 35, textWidth, textHeight};
SDL_RenderCopy(game->renderer, item, NULL, &renderQuad);
}
}
void menu_setActionsNumber(s_Menu* menu, int nbActions) {
menu->iNbItems = nbActions;
menu->iNbActionsSet = 0;
menu->iSelectedItem = 0;
unsigned long nmemb = (unsigned long) nbActions;
menu->pActions = (void (**)(s_Game*)) malloc(nmemb * sizeof(void (*)(s_Game*)));
menu->pItems = (SDL_Texture **) malloc(nmemb * sizeof(SDL_Texture *));
menu->pSelectedItems = (SDL_Texture **) malloc(nmemb * sizeof(SDL_Texture *));
}
void menu_addAction(
s_Menu* menu,
void (*pAction)(s_Game*),
SDL_Texture *itemTexture,
SDL_Texture *selectedItemTexture
) {
menu->pActions[menu->iNbActionsSet] = pAction;
menu->pItems[menu->iNbActionsSet] = itemTexture;
menu->pSelectedItems[menu->iNbActionsSet] = selectedItemTexture;
menu->iNbActionsSet++;
}
void menu_free(s_Menu* menu) {
int t;
for (t = 0; t < menu->iNbItems; ++t) {
SDL_DestroyTexture(menu->pItems[t]);
SDL_DestroyTexture(menu->pSelectedItems[t]);
}
free(menu->pItems);
free(menu->pSelectedItems);
free(menu->pActions);
}