1 #include "fonts.h"
2 
3 #define BGCOLOR 254
4 #define FGCOLOR 255
5 
6 #define FONT_DIM 7 /* Fonts are 7x7 (5x5 + space) */
7 #define LINE_SPACE 8 /* Space from line to line (in pixels) */
8 #define CURSOR_SPACE 10 /* Left space for the menu cursor */
9 
10 extern char *tela;
11 
12 extern char ogl_ctable[256][3];
13 extern void fb2gl_palette(int index, int r, int g, int b);
14 extern void fb2gl_set_palette();
15 
16 SDL_Event menu_event;
17 SDL_Event clean_event;
18 Uint8 *menu_key;
19 int halfx, halfy;
20 
21 struct {
22 	char newf;
23 	int x,y;
24 	int hw,hh; /* half width and half height */
25 	int lines;
26 	char quit;
27 	char open;
28 } menu;
29 
30 struct {
31 	int r,g,b;
32 } old_bg, old_fg; /* Old BG color and FG color */
33 
34 char pos=0; /* Position of select cursor */
35 char old_pos=4; /* Old Cursor Position */
36 
add_item(char * text)37 void add_item(char *text) { /* Add item to the menu */
38   int i=0;
39   int j,k;
40   unsigned char *p;
41   int sp; /* Space */
42   char draw=1;
43   static int x,y;
44 
45   if (menu.newf) { /* New menu drawn? */
46 	menu.newf=0;
47 	x=menu.x + CURSOR_SPACE; /* Enough space for the menu cursor */
48 	y=menu.y + 2; /* Space from frame */
49   }
50   else {
51 	y+=LINE_SPACE; /* Space beetwen vertical text */
52   }
53 
54   sp=i=0;
55   while (text[i]!='\0') {
56 	switch (text[i]) {
57 		case 'A': case 'a': p=font_a; break;
58 		case 'B': case 'b': p=font_b; break;
59 		case 'C': case 'c': p=font_c; break;
60 		case 'D': case 'd': p=font_d; break;
61 		case 'E': case 'e': p=font_e; break;
62 		case 'F': case 'f': p=font_f; break;
63 		case 'G': case 'g': p=font_g; break;
64 		case 'H': case 'h': p=font_h; break;
65 		case 'I': case 'i': p=font_i; break;
66 		case 'J': case 'j': p=font_j; break;
67 		case 'K': case 'k': p=font_k; break;
68 		case 'L': case 'l': p=font_l; break;
69 		case 'M': case 'm': p=font_m; break;
70 		case 'N': case 'n': p=font_n; break;
71 		case 'O': case 'o': p=font_o; break;
72 		case 'P': case 'p': p=font_p; break;
73 		case 'Q': case 'q': p=font_q; break;
74 		case 'R': case 'r': p=font_r; break;
75 		case 'S': case 's': p=font_s; break;
76 		case 'T': case 't': p=font_t; break;
77 		case 'U': case 'u': p=font_u; break;
78 		case 'V': case 'v': p=font_v; break;
79 		case 'W': case 'w': p=font_w; break;
80 		case 'X': case 'x': p=font_x; break;
81 		case 'Y': case 'y': p=font_y; break;
82 		case 'Z': case 'z': p=font_z; break;
83 		case '0': p=font_0; break;
84 		case '1': p=font_1; break;
85 		case '2': p=font_2; break;
86 		case '3': p=font_3; break;
87 		case '4': p=font_4; break;
88 		case '5': p=font_5; break;
89 		case '6': p=font_6; break;
90 		case '7': p=font_7; break;
91 		case '8': p=font_8; break;
92 		case '9': p=font_9; break;
93 		case '*': p=font_ast; break;
94 		case '.': p = font_per; break;
95 		case '\"': p=font_quot; break;
96 		case '!': p=font_ex; break;
97 		case '\'': p=font_apos; break;
98 		case ' ': draw=0; break;
99 		default: draw=0; break;
100         };
101 	if (draw) {
102 		for (j=0;j<5;j++)
103 		  for (k=0;k<5;k++)
104 		    if (p[k+j*5]) tela[(k+x+sp)+(j+y)*io.screen_w] = FGCOLOR;
105 	}
106 	else draw=1;
107 	sp+=7;
108 	i++;
109   }
110 }
111 
create_menu(int letters,int lines)112 void create_menu(int letters, int lines) {
113 	int i,j;
114 	int half_let = (CURSOR_SPACE+letters*FONT_DIM)/2;
115 	int half_lin = lines*LINE_SPACE/2;
116 
117 	halfx = io.screen_w/2;
118 	halfy = io.screen_h/2;
119 
120 	menu.newf = 1; /* It's a new Menu */
121 	menu.quit = 0;
122 	menu.x = halfx - half_let;
123 	menu.y = halfy - half_lin;
124 	menu.hw = half_let;
125 	menu.hh = half_lin;
126 	menu.lines = lines;
127 
128 	menu.open=1;
129 	pos=0;
130 	old_pos=menu.lines-1;
131 
132 	/* Backup old RGB for color BGCOLOR (index == 0) */
133 	old_bg.r = ogl_ctable[BGCOLOR][0];
134 	old_bg.g = ogl_ctable[BGCOLOR][1];
135 	old_bg.b = ogl_ctable[BGCOLOR][2];
136 
137 	/* Backup old RGB for color FGCOLOR (index == 1) */
138 	old_fg.r = ogl_ctable[FGCOLOR][0];
139 	old_fg.g = ogl_ctable[FGCOLOR][1];
140 	old_fg.b = ogl_ctable[FGCOLOR][2];
141 
142 	fb2gl_palette(BGCOLOR,50,50,50); /* Set RGB for BGCOLOR */
143 	fb2gl_palette(FGCOLOR,255,255,255); /* Set RGB for FGCOLOR */
144 	fb2gl_set_palette(); /* Update video palette */
145 
146 
147 	/* Clear the background and add a frame with the FGCOLOR */
148 	for (j=halfy-half_lin; j<halfy+half_lin+1; j++) {
149 		for (i=halfx-half_let; i<halfx+half_let+1; i++) {
150 			tela[i+j*(io.screen_w)] = BGCOLOR; /* Clearing Bg */
151 			if ( j == halfy - half_lin || j == halfy + half_lin ||
152 			     i == halfx - half_let || i == halfx + half_let )
153 			{
154 			     tela[i+j*(io.screen_w)] = FGCOLOR; /* Frame */
155 			}
156 		}
157 	}
158 
159 }
160 
draw_menu_cursor(int pos)161 void draw_menu_cursor(int pos) {
162 	int i,j;
163 	for (j=0;j<5;j++)
164 	  for (i=0;i<5;i++) {
165 	    if (font_ast[i+j*5]) { /* +2 is to keep away from the frame */
166 	      tela[ ( i + menu.x + 2 ) +
167 		   ( j + menu.y + 2 + (pos*LINE_SPACE) ) *
168 		   io.screen_w ] = FGCOLOR;
169 	    }
170 	    /* Clean the old cursor */
171 	    tela[ ( i + menu.x + 2) +
172 		  ( j + menu.y + 2 + (old_pos*8) ) *
173 		  io.screen_w] = BGCOLOR;
174 	  }
175 
176 }
177 
update_menu()178 void update_menu() {
179 	draw_menu_cursor(pos);
180 	fb2gl_update(tela,io.screen_w,io.screen_h,io.screen_w,0,0);
181 }
182 
menu_wait(int this_key)183 void menu_wait(int this_key) {
184 	SDL_PumpEvents();
185 	menu_key=SDL_GetKeyState(NULL);
186 	while (menu_key[this_key] != SDL_RELEASED) {
187 		SDL_PumpEvents();
188 		menu_key=SDL_GetKeyState(NULL);
189 	}
190 }
191 
list_rom()192 void list_rom() {
193 	printf("List ROMs in dir\n");
194 }
195 
menu_handle_select(int p)196 int menu_handle_select(int p) {
197 	// switch(menu_level) {
198 	switch(p) {
199 		case 0: list_rom(); break;
200 		case 4: menu.quit=1; break;
201 		default: menu.open=0;
202 	};
203 	// menu_level
204 }
205 
menu_main_loop()206 void menu_main_loop() {
207 
208 	menu_wait(SDLK_ESCAPE);
209 
210 	while (menu.open) {
211 		SDL_PumpEvents();
212 		menu_key=SDL_GetKeyState(NULL);
213 		if(menu_key[SDLK_ESCAPE] == SDL_PRESSED) {
214 			menu.open=0;
215 			menu_wait(SDLK_ESCAPE);
216 		}
217 		if(menu_key[SDLK_DOWN] == SDL_PRESSED) {
218 			old_pos=pos;
219 			pos++;
220 			if (pos>menu.lines-1) pos=0;
221 			update_menu();
222 			menu_wait(SDLK_DOWN);
223 		}
224 		if(menu_key[SDLK_UP] == SDL_PRESSED) {
225 			old_pos=pos;
226 			pos--;
227 			if (pos<0) pos=menu.lines-1;
228 			update_menu();
229 			menu_wait(SDLK_UP);
230 		}
231 		if(menu_key[SDLK_RETURN] == SDL_PRESSED) {
232 			menu_handle_select(pos);
233 			menu_wait(SDLK_RETURN);
234 			if (menu.quit) return;
235 		}
236 
237 	}
238 
239 }
240 
241 
open_menu()242 int open_menu() {
243 
244 	create_menu(10,5);
245 	add_item("load rom");
246 	add_item("save state");
247 	add_item("load state");
248 	add_item("options");
249 	add_item("quit");
250 	update_menu();
251 
252 	menu_main_loop();
253 
254 	fb2gl_palette(BGCOLOR,old_bg.r,old_bg.g,old_bg.b);
255 	fb2gl_palette(FGCOLOR,old_fg.r,old_fg.g,old_fg.b);
256 	fb2gl_set_palette();
257 
258 	if (menu.quit) return 0; /* Leave the emulator */
259 	else return 1;
260 
261 }
262