-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.c
92 lines (61 loc) · 1.89 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* \file menu.c
* Display and manage a menu as a line of buttons.
*/
#include "menu.h"
#define BUTTON_HEIGHT 38
#define BUTTON_WIDTH 100
static int menuHeight = 0;
/** the files for the buttons */
static char *buttonsImages[] = { "data/new_game_btn.bmp", "data/help_btn.bmp", "data/about_btn.bmp", "data/quit_btn.bmp"};
static int button_pos(SDL_Surface *screen, int index) {
int interval;
interval = (screen->w - BUTTON_WIDTH * BUTTONS_NB) / (BUTTONS_NB + 1);
return interval + index * (BUTTON_WIDTH + interval);
}
static void draw_button(SDL_Surface *screen, int index) {
SDL_Surface *buttonImg = SDL_LoadBMP(buttonsImages[index]);
SDL_Rect btnPosition;
if(buttonImg==NULL) {
fprintf(stderr, "Unable to open file %s\n", buttonsImages[index]);
return;
}
btnPosition.x = button_pos(screen, index);
btnPosition.y = menuHeight;
SDL_BlitSurface(buttonImg, NULL, screen, &btnPosition);
SDL_FreeSurface(buttonImg);
}
/**
* Display the dialog box.
* \param screen main program screen
* \param height where to place the menu : nb of pixel from the top
*/
void menu_display(SDL_Surface *screen, int height){
int i;
menuHeight = height;
for(i=0; i<BUTTONS_NB; i++) {
draw_button(screen, i);
}
}
/**
* When a mouse clic is done at position (x,y), call this function to
* know if and which button is clicked.
* \param screen main program screen
* \param x x coordinate of mouse pointer
* \param y y coordinate of mouse pointer
*/
menu_action_t menu_clicked(SDL_Surface *screen, int x, int y){
menu_action_t act = MENU_NONE;
if (y<menuHeight || y>menuHeight+BUTTON_HEIGHT) {
return MENU_NONE;
}
act++;
while(act < BUTTONS_NB) {
int x0 = button_pos(screen, act);
if(x>x0 && x<x0+BUTTON_WIDTH) {
return act;
}
act++;
}
return MENU_NONE;
}