-
Notifications
You must be signed in to change notification settings - Fork 1
/
prodScreen_buttonsTest.c
215 lines (182 loc) · 8.01 KB
/
prodScreen_buttonsTest.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "funkey_prod_screens.h"
/// Defines
#define time_left_FAIL 101 // in seconds
#define NB_KEYS 13
/// Static variables
static SDL_Surface *img_console_layout;
static SDL_Surface *img_button_LR_green;
static SDL_Surface *img_button_normal_green;
static int keys[NB_KEYS] = {SDLK_m, SDLK_n, SDLK_l, SDLK_u, SDLK_r, SDLK_d, SDLK_b, SDLK_y, SDLK_x, SDLK_a, SDLK_s, SDLK_k, SDLK_q};
static char *keys_str[NB_KEYS] = {"m (L1)", "n (R1)", "l (LEFT)", "u (UP)", "r (RIGHT)", "d (DOWN)", "b (B)", "y (Y)", "x (X)", "a (A)", "s (START)", "k (Select)", "q (POWER ON/OFF)"};
static int keys_pushed[NB_KEYS] = {0};
/// -------------- FUNCTIONS IMPLEMENTATION --------------
int launch_prod_screen_buttons(int argc, char *argv[]){
/* Declare Vars */
SDL_Surface *text_surface = NULL;
SDL_Rect text_pos;
SDL_Event event;
int stop_menu_loop = 0;
int res = EXIT_FAILURE;
int render = 0;
int time_left = time_left_FAIL;
int prev_ms = 0;
int i;
char str_title_buttons[50];
int nb_keys_pushed = 0;
/* Load images */
img_console_layout = IMG_Load(IMG_CONSOLE_LAYOUT);
if(!img_console_layout) {
printf("Error IMG_Load: %s\n", IMG_GetError());
exit(EXIT_FAILURE);
}
img_button_LR_green = IMG_Load(IMG_BUTTON_LR_GREEN);
if(!img_button_LR_green) {
printf("Error IMG_Load: %s\n", IMG_GetError());
exit(EXIT_FAILURE);
}
img_button_normal_green = IMG_Load(IMG_BUTTON_NORMAL_GREEN);
if(!img_button_normal_green) {
printf("Error IMG_Load: %s\n", IMG_GetError());
exit(EXIT_FAILURE);
}
/* Main loop */
while (!stop_menu_loop && time_left)
{
if(SDL_GetTicks() - prev_ms > 1000){
/* Update time*/
prev_ms = SDL_GetTicks();
time_left--;
/* Fill screen white */
SDL_FillRect(hw_surface, NULL, SDL_MapRGBA(hw_surface->format, bg_color.r, bg_color.g, bg_color.b, 0) );
/* Background image */
SDL_BlitSurface(img_console_layout, NULL, hw_surface, NULL);
/* Write Title */
text_surface = TTF_RenderText_Shaded(font_title, prog_title, text_color, bg_color);
text_pos.x = SCREEN_HORIZONTAL_SIZE/2 - text_surface->w/2;
text_pos.y = Y_PADDING;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);
/* Write "Screen ok ? */
sprintf(str_title_buttons, "Press all buttons...%ds", time_left);
text_surface = TTF_RenderText_Shaded(font_info, str_title_buttons, text_color, bg_color);
text_pos.x = SCREEN_HORIZONTAL_SIZE/2 - text_surface->w/2;
text_pos.y = 2*Y_PADDING + text_surface->h/2;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);
/* Write key SDLK_m*/
if(keys_pushed[0]){
SDL_Rect rect_tmp = {13, 68, img_button_LR_green->w, img_button_LR_green->h};
SDL_BlitSurface(img_button_LR_green, NULL, hw_surface, &rect_tmp);
}
/* Write key SDLK_n*/
if(keys_pushed[1]){
SDL_Rect rect_tmp = {207, 68, img_button_LR_green->w, img_button_LR_green->h};
SDL_BlitSurface(img_button_LR_green, NULL, hw_surface, &rect_tmp);
}
/* Write key SDLK_l*/
if(keys_pushed[2]){
SDL_Rect rect_tmp = {21, 145, img_button_normal_green->w, img_button_normal_green->h};
SDL_BlitSurface(img_button_normal_green, NULL, hw_surface, &rect_tmp);
}
/* Write key SDLK_u*/
if(keys_pushed[3]){
SDL_Rect rect_tmp = {47, 118, img_button_normal_green->w, img_button_normal_green->h};
SDL_BlitSurface(img_button_normal_green, NULL, hw_surface, &rect_tmp);
}
/* Write key SDLK_r*/
if(keys_pushed[4]){
SDL_Rect rect_tmp = {74, 143, img_button_normal_green->w, img_button_normal_green->h};
SDL_BlitSurface(img_button_normal_green, NULL, hw_surface, &rect_tmp);
}
/* Write key SDLK_d*/
if(keys_pushed[5]){
SDL_Rect rect_tmp = {48, 169, img_button_normal_green->w, img_button_normal_green->h};
SDL_BlitSurface(img_button_normal_green, NULL, hw_surface, &rect_tmp);
}
/* Write key SDLK_b*/
if(keys_pushed[6]){
SDL_Rect rect_tmp = {169, 169, img_button_normal_green->w, img_button_normal_green->h};
SDL_BlitSurface(img_button_normal_green, NULL, hw_surface, &rect_tmp);
}
/* Write key SDLK_y*/
if(keys_pushed[7]){
SDL_Rect rect_tmp = {143, 144, img_button_normal_green->w, img_button_normal_green->h};
SDL_BlitSurface(img_button_normal_green, NULL, hw_surface, &rect_tmp);
}
/* Write key SDLK_x*/
if(keys_pushed[8]){
SDL_Rect rect_tmp = {169, 117, img_button_normal_green->w, img_button_normal_green->h};
SDL_BlitSurface(img_button_normal_green, NULL, hw_surface, &rect_tmp);
}
/* Write key SDLK_a*/
if(keys_pushed[9]){
SDL_Rect rect_tmp = {195, 143, img_button_normal_green->w, img_button_normal_green->h};
SDL_BlitSurface(img_button_normal_green, NULL, hw_surface, &rect_tmp);
}
/* Write key SDLK_s*/
if(keys_pushed[10]){
SDL_Rect rect_tmp = {125, 203, img_button_normal_green->w, img_button_normal_green->h};
SDL_BlitSurface(img_button_normal_green, NULL, hw_surface, &rect_tmp);
}
/* Write key SDLK_f*/
if(keys_pushed[11]){
SDL_Rect rect_tmp = {92, 204, img_button_normal_green->w, img_button_normal_green->h};
SDL_BlitSurface(img_button_normal_green, NULL, hw_surface, &rect_tmp);
}
/* Write key SDLK_q*/
if(keys_pushed[12]){
SDL_Rect rect_tmp = {106, 73, img_button_normal_green->w, img_button_normal_green->h};
SDL_BlitSurface(img_button_normal_green, NULL, hw_surface, &rect_tmp);
}
/* Render screen */
render = 1;
}
/* Handle Keyboard Events */
while (SDL_PollEvent(&event))
switch(event.type)
{
case SDL_QUIT:
stop_menu_loop = 1;
break;
case SDL_KEYDOWN:
for (i = 0; i < NB_KEYS; i++){
if(event.key.keysym.sym != keys[i]) continue;
/* Key pushed for the 1st time */
if(!keys_pushed[i]){
nb_keys_pushed++;
keys_pushed[i] = 1;
}
/* All keys pushed = exit with success */
if(nb_keys_pushed == NB_KEYS){
stop_menu_loop = 1;
res = 0;
}
/* Render screen */
render = 1;
break;
}
}
if(render){
/// Render screen
SDL_Flip(hw_surface);
render = 0;
}
/* Sleep for some time */
SDL_Delay(SLEEP_PERIOD_MS);
}
/* Print not touched buttons if time_left */
if(!time_left){
res = ERROR_MANUAL_FAIL;
printf(" Missing Keys: ");
for (i = 0; i < NB_KEYS; i++){
if(keys_pushed[i]) continue;
printf("%s,", keys_str[i]);
}
printf("\n");
}
/*Free surfaces */
SDL_FreeSurface(img_console_layout);
SDL_FreeSurface(img_button_LR_green);
SDL_FreeSurface(img_button_normal_green);
return res;
}