additional movie tweaking
[libpicofe.git] / gp2x / menu.c
... / ...
CommitLineData
1// (c) Copyright 2006 notaz, All rights reserved.\r
2// Free for non-commercial use.\r
3\r
4// For commercial use, separate licencing terms must be obtained.\r
5\r
6#include <stdio.h>\r
7#include <string.h>\r
8#include <stdlib.h>\r
9#include <stdarg.h>\r
10#include <unistd.h>\r
11#include <dirent.h>\r
12\r
13#include "gp2x.h"\r
14#include "emu.h"\r
15#include "menu.h"\r
16#include "usbjoy.h"\r
17#include "version.h"\r
18\r
19#include "Pico/PicoInt.h"\r
20\r
21#ifndef _DIRENT_HAVE_D_TYPE\r
22#error "need d_type for file browser\r
23#endif\r
24\r
25extern char *actionNames[];\r
26extern char romFileName[PATH_MAX];\r
27extern char *rom_data;\r
28extern int mmuhack_status;\r
29extern int state_slot;\r
30\r
31static char *gp2xKeyNames[] = {\r
32 "UP", "???", "LEFT", "???", "DOWN", "???", "RIGHT", "???",\r
33 "START", "SELECT", "L", "R", "A", "B", "X", "Y",\r
34 "???", "???", "???", "???", "???", "???", "VOL DOWN", "VOL UP",\r
35 "???", "???", "???", "PUSH", "???", "???", "???", "???"\r
36};\r
37\r
38char menuErrorMsg[40] = {0, };\r
39\r
40\r
41static unsigned char fontdata8x8[] =\r
42{\r
43 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\r
44 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\r
45 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\r
46 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\r
47 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\r
48 0x3C,0x42,0x99,0xBD,0xBD,0x99,0x42,0x3C,0x3C,0x42,0x81,0x81,0x81,0x81,0x42,0x3C,\r
49 0xFE,0x82,0x8A,0xD2,0xA2,0x82,0xFE,0x00,0xFE,0x82,0x82,0x82,0x82,0x82,0xFE,0x00,\r
50 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x64,0x74,0x7C,0x38,0x00,0x00,\r
51 0x80,0xC0,0xF0,0xFC,0xF0,0xC0,0x80,0x00,0x01,0x03,0x0F,0x3F,0x0F,0x03,0x01,0x00,\r
52 0x18,0x3C,0x7E,0x18,0x7E,0x3C,0x18,0x00,0xEE,0xEE,0xEE,0xCC,0x00,0xCC,0xCC,0x00,\r
53 0x00,0x00,0x30,0x68,0x78,0x30,0x00,0x00,0x00,0x38,0x64,0x74,0x7C,0x38,0x00,0x00,\r
54 0x3C,0x66,0x7A,0x7A,0x7E,0x7E,0x3C,0x00,0x0E,0x3E,0x3A,0x22,0x26,0x6E,0xE4,0x40,\r
55 0x18,0x3C,0x7E,0x3C,0x3C,0x3C,0x3C,0x00,0x3C,0x3C,0x3C,0x3C,0x7E,0x3C,0x18,0x00,\r
56 0x08,0x7C,0x7E,0x7E,0x7C,0x08,0x00,0x00,0x10,0x3E,0x7E,0x7E,0x3E,0x10,0x00,0x00,\r
57 0x58,0x2A,0xDC,0xC8,0xDC,0x2A,0x58,0x00,0x24,0x66,0xFF,0xFF,0x66,0x24,0x00,0x00,\r
58 0x00,0x10,0x10,0x38,0x38,0x7C,0xFE,0x00,0xFE,0x7C,0x38,0x38,0x10,0x10,0x00,0x00,\r
59 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x1C,0x1C,0x18,0x00,0x18,0x18,0x00,\r
60 0x6C,0x6C,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x28,0x7C,0x28,0x7C,0x28,0x00,0x00,\r
61 0x10,0x38,0x60,0x38,0x0C,0x78,0x10,0x00,0x40,0xA4,0x48,0x10,0x24,0x4A,0x04,0x00,\r
62 0x18,0x34,0x18,0x3A,0x6C,0x66,0x3A,0x00,0x18,0x18,0x20,0x00,0x00,0x00,0x00,0x00,\r
63 0x30,0x60,0x60,0x60,0x60,0x60,0x30,0x00,0x0C,0x06,0x06,0x06,0x06,0x06,0x0C,0x00,\r
64 0x10,0x54,0x38,0x7C,0x38,0x54,0x10,0x00,0x00,0x18,0x18,0x7E,0x18,0x18,0x00,0x00,\r
65 0x00,0x00,0x00,0x00,0x18,0x18,0x30,0x00,0x00,0x00,0x00,0x00,0x3E,0x00,0x00,0x00,\r
66 0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x04,0x08,0x10,0x20,0x40,0x00,0x00,\r
67 0x38,0x4C,0xC6,0xC6,0xC6,0x64,0x38,0x00,0x18,0x38,0x18,0x18,0x18,0x18,0x7E,0x00,\r
68 0x7C,0xC6,0x0E,0x3C,0x78,0xE0,0xFE,0x00,0x7E,0x0C,0x18,0x3C,0x06,0xC6,0x7C,0x00,\r
69 0x1C,0x3C,0x6C,0xCC,0xFE,0x0C,0x0C,0x00,0xFC,0xC0,0xFC,0x06,0x06,0xC6,0x7C,0x00,\r
70 0x3C,0x60,0xC0,0xFC,0xC6,0xC6,0x7C,0x00,0xFE,0xC6,0x0C,0x18,0x30,0x30,0x30,0x00,\r
71 0x78,0xC4,0xE4,0x78,0x86,0x86,0x7C,0x00,0x7C,0xC6,0xC6,0x7E,0x06,0x0C,0x78,0x00,\r
72 0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x18,0x18,0x30,\r
73 0x1C,0x38,0x70,0xE0,0x70,0x38,0x1C,0x00,0x00,0x7C,0x00,0x00,0x7C,0x00,0x00,0x00,\r
74 0x70,0x38,0x1C,0x0E,0x1C,0x38,0x70,0x00,0x7C,0xC6,0xC6,0x1C,0x18,0x00,0x18,0x00,\r
75 0x3C,0x42,0x99,0xA1,0xA5,0x99,0x42,0x3C,0x38,0x6C,0xC6,0xC6,0xFE,0xC6,0xC6,0x00,\r
76 0xFC,0xC6,0xC6,0xFC,0xC6,0xC6,0xFC,0x00,0x3C,0x66,0xC0,0xC0,0xC0,0x66,0x3C,0x00,\r
77 0xF8,0xCC,0xC6,0xC6,0xC6,0xCC,0xF8,0x00,0xFE,0xC0,0xC0,0xFC,0xC0,0xC0,0xFE,0x00,\r
78 0xFE,0xC0,0xC0,0xFC,0xC0,0xC0,0xC0,0x00,0x3E,0x60,0xC0,0xCE,0xC6,0x66,0x3E,0x00,\r
79 0xC6,0xC6,0xC6,0xFE,0xC6,0xC6,0xC6,0x00,0x7E,0x18,0x18,0x18,0x18,0x18,0x7E,0x00,\r
80 0x06,0x06,0x06,0x06,0xC6,0xC6,0x7C,0x00,0xC6,0xCC,0xD8,0xF0,0xF8,0xDC,0xCE,0x00,\r
81 0x60,0x60,0x60,0x60,0x60,0x60,0x7E,0x00,0xC6,0xEE,0xFE,0xFE,0xD6,0xC6,0xC6,0x00,\r
82 0xC6,0xE6,0xF6,0xFE,0xDE,0xCE,0xC6,0x00,0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,\r
83 0xFC,0xC6,0xC6,0xC6,0xFC,0xC0,0xC0,0x00,0x7C,0xC6,0xC6,0xC6,0xDE,0xCC,0x7A,0x00,\r
84 0xFC,0xC6,0xC6,0xCE,0xF8,0xDC,0xCE,0x00,0x78,0xCC,0xC0,0x7C,0x06,0xC6,0x7C,0x00,\r
85 0x7E,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,\r
86 0xC6,0xC6,0xC6,0xEE,0x7C,0x38,0x10,0x00,0xC6,0xC6,0xD6,0xFE,0xFE,0xEE,0xC6,0x00,\r
87 0xC6,0xEE,0x3C,0x38,0x7C,0xEE,0xC6,0x00,0x66,0x66,0x66,0x3C,0x18,0x18,0x18,0x00,\r
88 0xFE,0x0E,0x1C,0x38,0x70,0xE0,0xFE,0x00,0x3C,0x30,0x30,0x30,0x30,0x30,0x3C,0x00,\r
89 0x60,0x60,0x30,0x18,0x0C,0x06,0x06,0x00,0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00,\r
90 0x18,0x3C,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,\r
91 0x30,0x30,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x06,0x3E,0x66,0x66,0x3C,0x00,\r
92 0x60,0x7C,0x66,0x66,0x66,0x66,0x7C,0x00,0x00,0x3C,0x66,0x60,0x60,0x66,0x3C,0x00,\r
93 0x06,0x3E,0x66,0x66,0x66,0x66,0x3E,0x00,0x00,0x3C,0x66,0x66,0x7E,0x60,0x3C,0x00,\r
94 0x1C,0x30,0x78,0x30,0x30,0x30,0x30,0x00,0x00,0x3E,0x66,0x66,0x66,0x3E,0x06,0x3C,\r
95 0x60,0x7C,0x76,0x66,0x66,0x66,0x66,0x00,0x18,0x00,0x38,0x18,0x18,0x18,0x18,0x00,\r
96 0x0C,0x00,0x1C,0x0C,0x0C,0x0C,0x0C,0x38,0x60,0x60,0x66,0x6C,0x78,0x6C,0x66,0x00,\r
97 0x38,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0xEC,0xFE,0xFE,0xFE,0xD6,0xC6,0x00,\r
98 0x00,0x7C,0x76,0x66,0x66,0x66,0x66,0x00,0x00,0x3C,0x66,0x66,0x66,0x66,0x3C,0x00,\r
99 0x00,0x7C,0x66,0x66,0x66,0x7C,0x60,0x60,0x00,0x3E,0x66,0x66,0x66,0x3E,0x06,0x06,\r
100 0x00,0x7E,0x70,0x60,0x60,0x60,0x60,0x00,0x00,0x3C,0x60,0x3C,0x06,0x66,0x3C,0x00,\r
101 0x30,0x78,0x30,0x30,0x30,0x30,0x1C,0x00,0x00,0x66,0x66,0x66,0x66,0x6E,0x3E,0x00,\r
102 0x00,0x66,0x66,0x66,0x66,0x3C,0x18,0x00,0x00,0xC6,0xD6,0xFE,0xFE,0x7C,0x6C,0x00,\r
103 0x00,0x66,0x3C,0x18,0x3C,0x66,0x66,0x00,0x00,0x66,0x66,0x66,0x66,0x3E,0x06,0x3C,\r
104 0x00,0x7E,0x0C,0x18,0x30,0x60,0x7E,0x00,0x0E,0x18,0x0C,0x38,0x0C,0x18,0x0E,0x00,\r
105 0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x00,0x70,0x18,0x30,0x1C,0x30,0x18,0x70,0x00,\r
106 0x00,0x00,0x76,0xDC,0x00,0x00,0x00,0x00,0x10,0x28,0x10,0x54,0xAA,0x44,0x00,0x00,\r
107};\r
108\r
109static void gp2x_text(unsigned char *screen, int x, int y, char *text, int color)\r
110{\r
111 int i,l;\r
112\r
113 screen = screen + x + y*320;\r
114\r
115 for (i = 0; i < strlen(text); i++)\r
116 {\r
117 for (l=0;l<8;l++)\r
118 {\r
119 if(fontdata8x8[((text[i])*8)+l]&0x80) screen[l*320+0]=color;\r
120 if(fontdata8x8[((text[i])*8)+l]&0x40) screen[l*320+1]=color;\r
121 if(fontdata8x8[((text[i])*8)+l]&0x20) screen[l*320+2]=color;\r
122 if(fontdata8x8[((text[i])*8)+l]&0x10) screen[l*320+3]=color;\r
123 if(fontdata8x8[((text[i])*8)+l]&0x08) screen[l*320+4]=color;\r
124 if(fontdata8x8[((text[i])*8)+l]&0x04) screen[l*320+5]=color;\r
125 if(fontdata8x8[((text[i])*8)+l]&0x02) screen[l*320+6]=color;\r
126 if(fontdata8x8[((text[i])*8)+l]&0x01) screen[l*320+7]=color;\r
127 }\r
128 screen += 8;\r
129 }\r
130}\r
131\r
132// draws white text to current bbp15 screen\r
133void gp2x_text_out15(int x, int y, char *text)\r
134{\r
135 int i,l;\r
136 unsigned short *screen = gp2x_screen;\r
137\r
138 screen = screen + x + y*320;\r
139\r
140 for (i = 0; i < strlen(text); i++)\r
141 {\r
142 for (l=0;l<8;l++)\r
143 {\r
144 if(fontdata8x8[((text[i])*8)+l]&0x80) screen[l*320+0]=0xffff;\r
145 if(fontdata8x8[((text[i])*8)+l]&0x40) screen[l*320+1]=0xffff;\r
146 if(fontdata8x8[((text[i])*8)+l]&0x20) screen[l*320+2]=0xffff;\r
147 if(fontdata8x8[((text[i])*8)+l]&0x10) screen[l*320+3]=0xffff;\r
148 if(fontdata8x8[((text[i])*8)+l]&0x08) screen[l*320+4]=0xffff;\r
149 if(fontdata8x8[((text[i])*8)+l]&0x04) screen[l*320+5]=0xffff;\r
150 if(fontdata8x8[((text[i])*8)+l]&0x02) screen[l*320+6]=0xffff;\r
151 if(fontdata8x8[((text[i])*8)+l]&0x01) screen[l*320+7]=0xffff;\r
152 }\r
153 screen += 8;\r
154 }\r
155}\r
156\r
157\r
158void gp2x_text_out8(int x, int y, char *texto, ...)\r
159{\r
160 va_list args;\r
161 char buffer[512];\r
162\r
163 va_start(args,texto);\r
164 vsprintf(buffer,texto,args);\r
165 va_end(args);\r
166\r
167 gp2x_text(gp2x_screen,x,y,buffer,1);\r
168}\r
169\r
170\r
171void gp2x_text_out8_2(int x, int y, char *texto, int color)\r
172{\r
173 gp2x_text(gp2x_screen, x, y, texto, color);\r
174}\r
175\r
176void gp2x_text_out8_lim(int x, int y, char *texto, int max)\r
177{\r
178 char buffer[320/8+1];\r
179\r
180 strncpy(buffer, texto, 320/8);\r
181 if (max > 320/8) max = 320/8;\r
182 if (max < 0) max = 0;\r
183 buffer[max] = 0;\r
184\r
185 gp2x_text(gp2x_screen,x,y,buffer,1);\r
186}\r
187\r
188\r
189static unsigned long inp_prev = 0;\r
190static int inp_prevjoy = 0;\r
191\r
192static unsigned long wait_for_input(unsigned long interesting)\r
193{\r
194 unsigned long ret;\r
195 static int repeats = 0, wait = 300*1000;\r
196 int release = 0, i;\r
197\r
198 if (repeats == 5 || repeats == 15 || repeats == 30) wait /= 2;\r
199\r
200 for (i = 0; i < 6 && inp_prev == gp2x_joystick_read(1); i++) {\r
201 if(i == 0) repeats++;\r
202 usleep(wait/6);\r
203 }\r
204\r
205 while ( !((ret = gp2x_joystick_read(1)) & interesting) ) {\r
206 usleep(50000);\r
207 release = 1;\r
208 }\r
209\r
210 if (release || ret != inp_prev) {\r
211 repeats = 0;\r
212 wait = 300*1000;\r
213 }\r
214 inp_prev = ret;\r
215 inp_prevjoy = 0;\r
216\r
217 // we don't need diagonals in menus\r
218 if ((ret&GP2X_UP) && (ret&GP2X_LEFT)) ret &= ~GP2X_LEFT;\r
219 if ((ret&GP2X_UP) && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;\r
220 if ((ret&GP2X_DOWN) && (ret&GP2X_LEFT)) ret &= ~GP2X_LEFT;\r
221 if ((ret&GP2X_DOWN) && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;\r
222\r
223 return ret;\r
224}\r
225\r
226static unsigned long input2_read(unsigned long interesting, int *joy)\r
227{\r
228 unsigned long ret;\r
229 int i;\r
230\r
231 do\r
232 {\r
233 *joy = 0;\r
234 if ((ret = gp2x_joystick_read(0) & interesting)) break;\r
235 gp2x_usbjoy_update();\r
236 for (i = 0; i < num_of_joys; i++) {\r
237 ret = gp2x_usbjoy_check2(i);\r
238 if (ret) { *joy = i + 1; break; }\r
239 }\r
240 if (ret) break;\r
241 }\r
242 while(0);\r
243\r
244 return ret;\r
245}\r
246\r
247// similar to wait_for_input(), but returns joy num\r
248static unsigned long wait_for_input_usbjoy(unsigned long interesting, int *joy)\r
249{\r
250 unsigned long ret;\r
251 const int wait = 300*1000;\r
252 int i;\r
253\r
254 if (inp_prevjoy == 0) inp_prev &= interesting;\r
255 for (i = 0; i < 6; i++) {\r
256 ret = input2_read(interesting, joy);\r
257 if (*joy != inp_prevjoy || ret != inp_prev) break;\r
258 usleep(wait/6);\r
259 }\r
260\r
261 while ( !(ret = input2_read(interesting, joy)) ) {\r
262 usleep(50000);\r
263 }\r
264\r
265 inp_prev = ret;\r
266 inp_prevjoy = *joy;\r
267\r
268 return ret;\r
269}\r
270\r
271\r
272\r
273// -------------- ROM selector --------------\r
274\r
275static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)\r
276{\r
277 int start, i, pos;\r
278\r
279 start = 12 - sel;\r
280 n--; // exclude current dir (".")\r
281\r
282 memset(gp2x_screen, 0, 320*240);\r
283\r
284 if(start - 2 >= 0)\r
285 gp2x_text_out8_lim(14, (start - 2)*10, curdir, 38);\r
286 for (i = 0; i < n; i++) {\r
287 pos = start + i;\r
288 if (pos < 0) continue;\r
289 if (pos > 23) break;\r
290 if (namelist[i+1]->d_type == DT_DIR) {\r
291 gp2x_text_out8_lim(14, pos*10, "/", 1);\r
292 gp2x_text_out8_lim(14+8, pos*10, namelist[i+1]->d_name, 37);\r
293 } else {\r
294 gp2x_text_out8_lim(14, pos*10, namelist[i+1]->d_name, 38);\r
295 }\r
296 }\r
297 gp2x_text_out8(5, 120, ">");\r
298 gp2x_video_flip();\r
299}\r
300\r
301static int scandir_cmp(const void *p1, const void *p2)\r
302{\r
303 struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2;\r
304 if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2);\r
305 if ((*d1)->d_type == DT_DIR) return -1; // put before\r
306 if ((*d2)->d_type == DT_DIR) return 1;\r
307 return alphasort(d1, d2);\r
308}\r
309\r
310\r
311static char *romsel_loop(char *curr_path)\r
312{\r
313 struct dirent **namelist;\r
314 DIR *dir;\r
315 int n, sel = 0;\r
316 unsigned long inp = 0;\r
317 char *ret = NULL, *fname = NULL;\r
318\r
319 // is this a dir or a full path?\r
320 if ((dir = opendir(curr_path))) {\r
321 closedir(dir);\r
322 } else {\r
323 char *p;\r
324 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);\r
325 *p = 0;\r
326 fname = p+1;\r
327 }\r
328\r
329 n = scandir(curr_path, &namelist, 0, scandir_cmp);\r
330 if (n < 0) {\r
331 // try root\r
332 n = scandir(curr_path, &namelist, 0, scandir_cmp);\r
333 if (n < 0) {\r
334 // oops, we failed\r
335 printf("dir: "); printf(curr_path); printf("\n");\r
336 perror("scandir");\r
337 return NULL;\r
338 }\r
339 }\r
340\r
341 // try to find sel\r
342 if (fname != NULL) {\r
343 int i;\r
344 for (i = 1; i < n; i++) {\r
345 if (strcmp(namelist[i]->d_name, fname) == 0) {\r
346 sel = i - 1;\r
347 break;\r
348 }\r
349 }\r
350 }\r
351\r
352 for (;;)\r
353 {\r
354 draw_dirlist(curr_path, namelist, n, sel);\r
355 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X);\r
356 if(inp & GP2X_UP ) { sel--; if (sel < 0) sel = n-2; }\r
357 if(inp & GP2X_DOWN) { sel++; if (sel > n-2) sel = 0; }\r
358 if(inp & GP2X_LEFT) { sel-=10; if (sel < 0) sel = 0; }\r
359 if(inp & GP2X_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }\r
360 if(inp & GP2X_B) { // enter dir/select\r
361 again:\r
362 if (namelist[sel+1]->d_type == DT_REG) {\r
363 strcpy(romFileName, curr_path);\r
364 strcat(romFileName, "/");\r
365 strcat(romFileName, namelist[sel+1]->d_name);\r
366 ret = romFileName;\r
367 break;\r
368 } else if (namelist[sel+1]->d_type == DT_DIR) {\r
369 int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r
370 char *p, *newdir = malloc(newlen);\r
371 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {\r
372 char *start = curr_path;\r
373 p = start + strlen(start) - 1;\r
374 while (*p == '/' && p > start) p--;\r
375 while (*p != '/' && p > start) p--;\r
376 if (p <= start) strcpy(newdir, "/");\r
377 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }\r
378 } else {\r
379 strcpy(newdir, curr_path);\r
380 p = newdir + strlen(newdir) - 1;\r
381 while (*p == '/' && p >= newdir) *p-- = 0;\r
382 strcat(newdir, "/");\r
383 strcat(newdir, namelist[sel+1]->d_name);\r
384 }\r
385 ret = romsel_loop(newdir);\r
386 free(newdir);\r
387 break;\r
388 } else {\r
389 // unknown file type, happens on NTFS mounts. Try to guess.\r
390 FILE *tstf; int tmp;\r
391 strcpy(romFileName, curr_path);\r
392 strcat(romFileName, "/");\r
393 strcat(romFileName, namelist[sel+1]->d_name);\r
394 tstf = fopen(romFileName, "rb");\r
395 if (tstf != NULL)\r
396 {\r
397 if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)\r
398 namelist[sel+1]->d_type = DT_REG;\r
399 else namelist[sel+1]->d_type = DT_DIR;\r
400 fclose(tstf);\r
401 goto again;\r
402 }\r
403 }\r
404 }\r
405 if(inp & GP2X_X) break; // cancel\r
406 }\r
407\r
408 if (n > 0) {\r
409 while(n--) free(namelist[n]);\r
410 free(namelist);\r
411 }\r
412\r
413 return ret;\r
414}\r
415\r
416// -------------- key config --------------\r
417\r
418static char *usb_joy_key_name(int joy, int num)\r
419{\r
420 static char name[16];\r
421 switch (num)\r
422 {\r
423 case 0: sprintf(name, "Joy%i UP", joy); break;\r
424 case 1: sprintf(name, "Joy%i DOWN", joy); break;\r
425 case 2: sprintf(name, "Joy%i LEFT", joy); break;\r
426 case 3: sprintf(name, "Joy%i RIGHT", joy); break;\r
427 default:sprintf(name, "Joy%i b%i", joy, num-3); break;\r
428 }\r
429 return name;\r
430}\r
431\r
432static void draw_key_config(int curr_act, int is_p2)\r
433{\r
434 char strkeys[32*5];\r
435 int joy, i;\r
436\r
437 strkeys[0] = 0;\r
438 for (i = 0; i < 32; i++)\r
439 {\r
440 if (currentConfig.KeyBinds[i] & (1 << curr_act))\r
441 {\r
442 if (curr_act < 16 && (currentConfig.KeyBinds[i] & (1 << 16)) != (is_p2 << 16)) continue;\r
443 if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, gp2xKeyNames[i]); break; }\r
444 else strcpy(strkeys, gp2xKeyNames[i]);\r
445 }\r
446 }\r
447 for (joy = 0; joy < num_of_joys; joy++)\r
448 {\r
449 for (i = 0; i < 32; i++)\r
450 {\r
451 if (currentConfig.JoyBinds[joy][i] & (1 << curr_act))\r
452 {\r
453 if (curr_act < 16 && (currentConfig.JoyBinds[joy][i] & (1 << 16)) != (is_p2 << 16)) continue;\r
454 if (strkeys[0]) {\r
455 strcat(strkeys, ", "); strcat(strkeys, usb_joy_key_name(joy + 1, i));\r
456 break;\r
457 }\r
458 else strcpy(strkeys, usb_joy_key_name(joy + 1, i));\r
459 }\r
460 }\r
461 }\r
462\r
463 memset(gp2x_screen, 0, 320*240);\r
464 gp2x_text_out8(60, 40, "Action: %s", actionNames[curr_act]);\r
465 gp2x_text_out8(60, 60, "Keys: %s", strkeys);\r
466\r
467 gp2x_text_out8(30, 180, "Use SELECT to change action");\r
468 gp2x_text_out8(30, 190, "Press a key to bind/unbind");\r
469 gp2x_text_out8(30, 200, "Select \"Done\" action and");\r
470 gp2x_text_out8(30, 210, " press any key to finish");\r
471 gp2x_video_flip();\r
472}\r
473\r
474static void key_config_loop(int is_p2)\r
475{\r
476 int curr_act = 0, joy = 0, i;\r
477 unsigned long inp = 0;\r
478\r
479 for (;;)\r
480 {\r
481 draw_key_config(curr_act, is_p2);\r
482 inp = wait_for_input_usbjoy(CONFIGURABLE_KEYS, &joy);\r
483 // printf("got %08lX from joy %i\n", inp, joy);\r
484 if (joy == 0) {\r
485 if (inp & GP2X_SELECT) {\r
486 curr_act++;\r
487 while (!actionNames[curr_act] && curr_act < 32) curr_act++;\r
488 if (curr_act > 31) curr_act = 0;\r
489 }\r
490 inp &= CONFIGURABLE_KEYS;\r
491 inp &= ~GP2X_SELECT;\r
492 }\r
493 if (curr_act == 31 && inp) break;\r
494 if (joy == 0) {\r
495 for (i = 0; i < 32; i++)\r
496 if (inp & (1 << i)) {\r
497 currentConfig.KeyBinds[i] ^= (1 << curr_act);\r
498 if (is_p2) currentConfig.KeyBinds[i] |= (1 << 16); // player 2 flag\r
499 else currentConfig.KeyBinds[i] &= ~(1 << 16);\r
500 }\r
501 } else {\r
502 for (i = 0; i < 32; i++)\r
503 if (inp & (1 << i)) {\r
504 currentConfig.JoyBinds[joy-1][i] ^= (1 << curr_act);\r
505 if (is_p2) currentConfig.JoyBinds[joy-1][i] |= (1 << 16);\r
506 else currentConfig.JoyBinds[joy-1][i] &= ~(1 << 16);\r
507 }\r
508 }\r
509 }\r
510}\r
511\r
512static void draw_kc_sel(int menu_sel)\r
513{\r
514 int tl_x = 25+40, tl_y = 60, y, i;\r
515 char joyname[36];\r
516\r
517 y = tl_y;\r
518 memset(gp2x_screen, 0, 320*240);\r
519 gp2x_text_out8(tl_x, y, "Player 1");\r
520 gp2x_text_out8(tl_x, (y+=10), "Player 2");\r
521 gp2x_text_out8(tl_x, (y+=10), "Done");\r
522\r
523 // draw cursor\r
524 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
525\r
526 tl_x = 25;\r
527 gp2x_text_out8(tl_x, (y=110), "USB joys detected:");\r
528 if (num_of_joys > 0) {\r
529 for (i = 0; i < num_of_joys; i++) {\r
530 strncpy(joyname, joy_name(joys[i]), 33); joyname[33] = 0;\r
531 gp2x_text_out8(tl_x, (y+=10), "%i: %s", i+1, joyname);\r
532 }\r
533 } else {\r
534 gp2x_text_out8(tl_x, (y+=10), "none");\r
535 }\r
536\r
537\r
538 gp2x_video_flip();\r
539}\r
540\r
541static void kc_sel_loop(void)\r
542{\r
543 int menu_sel = 2, menu_sel_max = 2;\r
544 unsigned long inp = 0;\r
545\r
546 for(;;)\r
547 {\r
548 draw_kc_sel(menu_sel);\r
549 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
550 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
551 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
552 if(inp & GP2X_B) {\r
553 switch (menu_sel) {\r
554 case 0: key_config_loop(0); return;\r
555 case 1: key_config_loop(1); return;\r
556 default: return;\r
557 }\r
558 }\r
559 if(inp & GP2X_X) return;\r
560 }\r
561}\r
562\r
563\r
564\r
565\r
566// --------- advanced options ----------\r
567\r
568// order must match that of currentConfig_t\r
569struct {\r
570 int EmuOpt;\r
571 int PicoOpt;\r
572 int PsndRate;\r
573 int PicoRegion;\r
574 int Frameskip;\r
575 int CPUclock;\r
576} tmp_opts;\r
577int tmp_gamma;\r
578\r
579static void draw_amenu_options(int menu_sel)\r
580{\r
581 int tl_x = 25, tl_y = 60, y;\r
582 char *mms = mmuhack_status ? "active) " : "inactive)";\r
583\r
584 y = tl_y;\r
585 memset(gp2x_screen, 0, 320*240);\r
586 gp2x_text_out8(tl_x, y, "Scale 32 column mode %s", (tmp_opts.PicoOpt&0x100)?"ON":"OFF"); // 0\r
587 gp2x_text_out8(tl_x, (y+=10), "Gamma correction %i.%02i", tmp_gamma / 100, tmp_gamma%100); // 1\r
588 gp2x_text_out8(tl_x, (y+=10), "Emulate Z80 %s", (tmp_opts.PicoOpt&0x004)?"ON":"OFF"); // 2\r
589 gp2x_text_out8(tl_x, (y+=10), "Emulate YM2612 (FM) %s", (tmp_opts.PicoOpt&0x001)?"ON":"OFF"); // 3\r
590 gp2x_text_out8(tl_x, (y+=10), "Emulate SN76496 (PSG) %s", (tmp_opts.PicoOpt&0x002)?"ON":"OFF"); // 4\r
591 gp2x_text_out8(tl_x, (y+=10), "gzip savestates %s", (tmp_opts.EmuOpt &0x008)?"ON":"OFF"); // 5\r
592 gp2x_text_out8(tl_x, (y+=10), "Don't save config on exit %s", (tmp_opts.EmuOpt &0x020)?"ON":"OFF"); // 6\r
593 gp2x_text_out8(tl_x, (y+=10), "needs restart:");\r
594 gp2x_text_out8(tl_x, (y+=10), "craigix's RAM timings %s", (tmp_opts.EmuOpt &0x100)?"ON":"OFF"); // 8\r
595 gp2x_text_out8(tl_x, (y+=10), "squidgehack (now %s %s", mms, (tmp_opts.EmuOpt &0x010)?"ON":"OFF"); // 9\r
596 gp2x_text_out8(tl_x, (y+=10), "Done");\r
597\r
598 // draw cursor\r
599 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
600\r
601 gp2x_video_flip();\r
602}\r
603\r
604static void amenu_loop_options(void)\r
605{\r
606 int menu_sel = 0, menu_sel_max = 10;\r
607 unsigned long inp = 0;\r
608\r
609 for(;;)\r
610 {\r
611 draw_amenu_options(menu_sel);\r
612 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
613 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
614 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
615 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
616 switch (menu_sel) {\r
617 case 0: tmp_opts.PicoOpt^=0x100; break;\r
618 case 2: tmp_opts.PicoOpt^=0x004; break;\r
619 case 3: tmp_opts.PicoOpt^=0x001; break;\r
620 case 4: tmp_opts.PicoOpt^=0x002; break;\r
621 case 5: tmp_opts.EmuOpt ^=0x008; break;\r
622 case 6: tmp_opts.EmuOpt ^=0x020; break;\r
623 case 8: tmp_opts.EmuOpt ^=0x100; break;\r
624 case 9: tmp_opts.EmuOpt ^=0x010; break;\r
625 case 10: return;\r
626 }\r
627 }\r
628 if(inp & (GP2X_X|GP2X_A)) return;\r
629 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
630 switch (menu_sel) {\r
631 case 1:\r
632 while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
633 tmp_gamma += (inp & GP2X_LEFT) ? -1 : 1;\r
634 if (tmp_gamma < 1) tmp_gamma = 1;\r
635 if (tmp_gamma > 300) tmp_gamma = 300;\r
636 draw_amenu_options(menu_sel);\r
637 usleep(18*1000);\r
638 }\r
639 break;\r
640 }\r
641 }\r
642 }\r
643}\r
644\r
645// -------------- options --------------\r
646\r
647static char *region_name(unsigned int code)\r
648{\r
649 char *names[] = { "Auto", "Japan NTSC", "Japan PAL", "USA", "Europe" };\r
650 int i = 0;\r
651 code <<= 1;\r
652 while((code >>=1)) i++;\r
653 if (i > 4) return "unknown";\r
654 return names[i];\r
655}\r
656\r
657static void draw_menu_options(int menu_sel)\r
658{\r
659 int tl_x = 25, tl_y = 40, y;\r
660 char monostereo[8], strframeskip[8], *strrend;\r
661\r
662 strcpy(monostereo, (tmp_opts.PicoOpt&0x08)?"stereo":"mono");\r
663 if (tmp_opts.Frameskip < 0)\r
664 strcpy(strframeskip, "Auto");\r
665 else sprintf(strframeskip, "%i", tmp_opts.Frameskip);\r
666 if (tmp_opts.PicoOpt&0x10) {\r
667 strrend = " 8bit fast";\r
668 } else if (tmp_opts.EmuOpt&0x80) {\r
669 strrend = "16bit accurate";\r
670 } else {\r
671 strrend = " 8bit accurate";\r
672 }\r
673\r
674 y = tl_y;\r
675 memset(gp2x_screen, 0, 320*240);\r
676 gp2x_text_out8(tl_x, y, "Renderer: %s", strrend); // 0\r
677 gp2x_text_out8(tl_x, (y+=10), "Accurate timing (slower) %s", (tmp_opts.PicoOpt&0x040)?"ON":"OFF"); // 1\r
678 gp2x_text_out8(tl_x, (y+=10), "Accurate sprites (slower) %s", (tmp_opts.PicoOpt&0x080)?"ON":"OFF"); // 2\r
679 gp2x_text_out8(tl_x, (y+=10), "Show FPS %s", (tmp_opts.EmuOpt &0x002)?"ON":"OFF"); // 3\r
680 gp2x_text_out8(tl_x, (y+=10), "Frameskip %s", strframeskip);\r
681 gp2x_text_out8(tl_x, (y+=10), "Enable sound %s", (tmp_opts.EmuOpt &0x004)?"ON":"OFF"); // 5\r
682 gp2x_text_out8(tl_x, (y+=10), "Sound Quality: %5iHz %s", tmp_opts.PsndRate, monostereo);\r
683 gp2x_text_out8(tl_x, (y+=10), "Use ARM940 core for sound %s", (tmp_opts.PicoOpt&0x200)?"ON":"OFF"); // 7\r
684 gp2x_text_out8(tl_x, (y+=10), "6 button pad %s", (tmp_opts.PicoOpt&0x020)?"ON":"OFF"); // 8\r
685 gp2x_text_out8(tl_x, (y+=10), "Genesis Region: %s", region_name(tmp_opts.PicoRegion));\r
686 gp2x_text_out8(tl_x, (y+=10), "Use SRAM savestates %s", (tmp_opts.EmuOpt &0x001)?"ON":"OFF"); // 10\r
687 gp2x_text_out8(tl_x, (y+=10), "Confirm save overwrites %s", (tmp_opts.EmuOpt &0x200)?"ON":"OFF"); // 11\r
688 gp2x_text_out8(tl_x, (y+=10), "Save slot %i", state_slot); // 12\r
689 gp2x_text_out8(tl_x, (y+=10), "GP2X CPU clocks %iMhz", tmp_opts.CPUclock);\r
690 gp2x_text_out8(tl_x, (y+=10), "[advanced options]");\r
691 gp2x_text_out8(tl_x, (y+=10), "Save cfg as default");\r
692 if (rom_data)\r
693 gp2x_text_out8(tl_x, (y+=10), "Save cfg for current game only");\r
694\r
695 // draw cursor\r
696 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
697\r
698 gp2x_video_flip();\r
699}\r
700\r
701static int sndrate_prevnext(int rate, int dir)\r
702{\r
703 int i, rates[] = { 8000, 11025, 16000, 22050, 44100 };\r
704\r
705 for (i = 0; i < 5; i++)\r
706 if (rates[i] == rate) break;\r
707\r
708 i += dir ? 1 : -1;\r
709 if (i > 4) return dir ? 44100 : 22050;\r
710 if (i < 0) return dir ? 11025 : 8000;\r
711 return rates[i];\r
712}\r
713\r
714static void menu_options_save(void)\r
715{\r
716 memcpy(&currentConfig.EmuOpt, &tmp_opts.EmuOpt, sizeof(tmp_opts));\r
717 currentConfig.gamma = tmp_gamma;\r
718 PicoOpt = currentConfig.PicoOpt;\r
719 PsndRate = currentConfig.PsndRate;\r
720 PicoRegionOverride = currentConfig.PicoRegion;\r
721 if (PicoOpt & 0x20) {\r
722 actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
723 actionNames[10] = "X"; actionNames[11] = "MODE";\r
724 } else {\r
725 actionNames[8] = actionNames[9] = actionNames[10] = actionNames[11] = 0;\r
726 }\r
727}\r
728\r
729static void menu_loop_options(void)\r
730{\r
731 int menu_sel = 0, menu_sel_max = 15;\r
732 unsigned long inp = 0;\r
733\r
734 if (rom_data) menu_sel_max++;\r
735 memcpy(&tmp_opts.EmuOpt, &currentConfig.EmuOpt, sizeof(tmp_opts));\r
736 tmp_gamma = currentConfig.gamma;\r
737 tmp_opts.PicoOpt = PicoOpt;\r
738 tmp_opts.PsndRate = PsndRate;\r
739 tmp_opts.PicoRegion = PicoRegionOverride;\r
740\r
741 for(;;)\r
742 {\r
743 draw_menu_options(menu_sel);\r
744 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
745 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
746 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
747 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
748 switch (menu_sel) {\r
749 case 1: tmp_opts.PicoOpt^=0x040; break;\r
750 case 2: tmp_opts.PicoOpt^=0x080; break;\r
751 case 3: tmp_opts.EmuOpt ^=0x002; break;\r
752 case 5: tmp_opts.EmuOpt ^=0x004; break;\r
753 case 7: tmp_opts.PicoOpt^=0x200; break;\r
754 case 8: tmp_opts.PicoOpt^=0x020; break;\r
755 case 10: tmp_opts.EmuOpt ^=0x001; break;\r
756 case 11: tmp_opts.EmuOpt ^=0x200; break;\r
757 case 14: amenu_loop_options(); break;\r
758 case 15: // done (save)\r
759 menu_options_save();\r
760 emu_WriteConfig(0);\r
761 return;\r
762 case 16: // done (save for current game)\r
763 menu_options_save();\r
764 emu_WriteConfig(1);\r
765 return;\r
766 }\r
767 }\r
768 if(inp & GP2X_X) return; // done (no save)\r
769 if(inp & GP2X_A) {\r
770 menu_options_save();\r
771 return; // done (save)\r
772 }\r
773 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
774 switch (menu_sel) {\r
775 case 0:\r
776 if (inp & GP2X_LEFT) {\r
777 if ( tmp_opts.PicoOpt&0x10) tmp_opts.PicoOpt&= ~0x10;\r
778 else if (!(tmp_opts.EmuOpt &0x80))tmp_opts.EmuOpt |= 0x80;\r
779 else if ( tmp_opts.EmuOpt &0x80) break;\r
780 } else {\r
781 if ( tmp_opts.PicoOpt&0x10) break;\r
782 else if (!(tmp_opts.EmuOpt &0x80))tmp_opts.PicoOpt|= 0x10;\r
783 else if ( tmp_opts.EmuOpt &0x80) tmp_opts.EmuOpt &= ~0x80;\r
784 }\r
785 break;\r
786 case 4:\r
787 tmp_opts.Frameskip += (inp & GP2X_LEFT) ? -1 : 1;\r
788 if (tmp_opts.Frameskip < 0) tmp_opts.Frameskip = -1;\r
789 if (tmp_opts.Frameskip > 32) tmp_opts.Frameskip = 32;\r
790 break;\r
791 case 6:\r
792 if ((inp & GP2X_RIGHT) && tmp_opts.PsndRate == 44100 && !(tmp_opts.PicoOpt&0x08)) {\r
793 tmp_opts.PsndRate = 8000; tmp_opts.PicoOpt|= 0x08;\r
794 } else if ((inp & GP2X_LEFT) && tmp_opts.PsndRate == 8000 && (tmp_opts.PicoOpt&0x08)) {\r
795 tmp_opts.PsndRate = 44100; tmp_opts.PicoOpt&=~0x08;\r
796 } else tmp_opts.PsndRate = sndrate_prevnext(tmp_opts.PsndRate, inp & GP2X_RIGHT);\r
797 break;\r
798 case 9:\r
799 if (inp & GP2X_RIGHT) {\r
800 if (tmp_opts.PicoRegion) tmp_opts.PicoRegion<<=1; else tmp_opts.PicoRegion=1;\r
801 if (tmp_opts.PicoRegion > 8) tmp_opts.PicoRegion = 8;\r
802 } else tmp_opts.PicoRegion>>=1;\r
803 break;\r
804 case 12:\r
805 if (inp & GP2X_RIGHT) {\r
806 state_slot++; if (state_slot > 9) state_slot = 0;\r
807 } else {state_slot--; if (state_slot < 0) state_slot = 9;\r
808 }\r
809 break;\r
810 case 13:\r
811 while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
812 tmp_opts.CPUclock += (inp & GP2X_LEFT) ? -1 : 1;\r
813 if (tmp_opts.CPUclock < 1) tmp_opts.CPUclock = 1;\r
814 draw_menu_options(menu_sel);\r
815 usleep(50*1000);\r
816 }\r
817 break;\r
818 }\r
819 }\r
820 }\r
821}\r
822\r
823// -------------- credits --------------\r
824\r
825static void draw_menu_credits(void)\r
826{\r
827 int tl_x = 15, tl_y = 70, y;\r
828 memset(gp2x_screen, 0, 320*240);\r
829\r
830 gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION " (c) notaz, 2006");\r
831 y = tl_y;\r
832 gp2x_text_out8(tl_x, y, "Credits:");\r
833 gp2x_text_out8(tl_x, (y+=10), "Dave: Cyclone 68000 core,");\r
834 gp2x_text_out8(tl_x, (y+=10), " base code of PicoDrive");\r
835 gp2x_text_out8(tl_x, (y+=10), "Reesy & FluBBa: DrZ80 core");\r
836 gp2x_text_out8(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");\r
837 gp2x_text_out8(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");\r
838 gp2x_text_out8(tl_x, (y+=10), "Stephane Dallongeville:");\r
839 gp2x_text_out8(tl_x, (y+=10), " opensource Gens");\r
840 gp2x_text_out8(tl_x, (y+=10), "Haze: Genesis hw info");\r
841 gp2x_text_out8(tl_x, (y+=10), "rlyeh and others: minimal SDK");\r
842 gp2x_text_out8(tl_x, (y+=10), "Squidge: squidgehack");\r
843 gp2x_text_out8(tl_x, (y+=10), "Dzz: ARM940 sample");\r
844 gp2x_text_out8(tl_x, (y+=10), "GnoStiC / Puck2099: USB joystick");\r
845 gp2x_text_out8(tl_x, (y+=10), "craigix: GP2X hardware");\r
846\r
847 gp2x_video_flip();\r
848}\r
849\r
850\r
851// -------------- root menu --------------\r
852\r
853static void draw_menu_root(int menu_sel)\r
854{\r
855 int tl_x = 70, tl_y = 70, y;\r
856 memset(gp2x_screen, 0, 320*240);\r
857\r
858 gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION);\r
859\r
860 y = tl_y;\r
861 if (rom_data) {\r
862 gp2x_text_out8(tl_x, y, "Resume game");\r
863 gp2x_text_out8(tl_x, (y+=10), "Save State");\r
864 gp2x_text_out8(tl_x, (y+=10), "Load State");\r
865 gp2x_text_out8(tl_x, (y+=10), "Reset game");\r
866 } else {\r
867 y += 30;\r
868 }\r
869 gp2x_text_out8(tl_x, (y+=10), "Load new ROM");\r
870 gp2x_text_out8(tl_x, (y+=10), "Change options");\r
871 gp2x_text_out8(tl_x, (y+=10), "Configure controls");\r
872 gp2x_text_out8(tl_x, (y+=10), "Credits");\r
873 gp2x_text_out8(tl_x, (y+=10), "Exit");\r
874\r
875 // draw cursor\r
876 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
877 // error\r
878 if (menuErrorMsg[0]) gp2x_text_out8(5, 226, menuErrorMsg);\r
879 gp2x_video_flip();\r
880}\r
881\r
882\r
883static void menu_loop_root(void)\r
884{\r
885 int menu_sel = 4, menu_sel_max = 8, menu_sel_min = 4;\r
886 unsigned long inp = 0;\r
887 char curr_path[PATH_MAX], *selfname;\r
888 FILE *tstf;\r
889\r
890 if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )\r
891 {\r
892 fclose(tstf);\r
893 strcpy(curr_path, currentConfig.lastRomFile);\r
894 }\r
895 else\r
896 {\r
897 getcwd(curr_path, PATH_MAX);\r
898 }\r
899\r
900 if (rom_data) menu_sel = menu_sel_min = 0;\r
901\r
902 for(;;)\r
903 {\r
904 draw_menu_root(menu_sel);\r
905 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X|GP2X_SELECT);\r
906 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < menu_sel_min) menu_sel = menu_sel_max; }\r
907 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = menu_sel_min; }\r
908 if(inp &(GP2X_SELECT|GP2X_X)){\r
909 if (rom_data) {\r
910 while (gp2x_joystick_read(1) & (GP2X_SELECT|GP2X_X)) usleep(50*1000); // wait until select is released\r
911 engineState = PGS_Running;\r
912 break;\r
913 }\r
914 }\r
915 if(inp & GP2X_B ) {\r
916 switch (menu_sel) {\r
917 case 0: // resume game\r
918 if (rom_data) { engineState = PGS_Running; return; }\r
919 break;\r
920 case 1: // save state\r
921 if (rom_data) {\r
922 if(emu_SaveLoadGame(0, 0)) {\r
923 strcpy(menuErrorMsg, "save failed");\r
924 continue;\r
925 }\r
926 engineState = PGS_Running;\r
927 return;\r
928 }\r
929 break;\r
930 case 2: // load state\r
931 if (rom_data) {\r
932 if(emu_SaveLoadGame(1, 0)) {\r
933 strcpy(menuErrorMsg, "load failed");\r
934 continue;\r
935 }\r
936 engineState = PGS_Running;\r
937 return;\r
938 }\r
939 break;\r
940 case 3: // reset game\r
941 if (rom_data) {\r
942 emu_ResetGame();\r
943 engineState = PGS_Running;\r
944 return;\r
945 }\r
946 break;\r
947 case 4: // select rom\r
948 selfname = romsel_loop(curr_path);\r
949 if (selfname) {\r
950 printf("selected file: %s\n", selfname);\r
951 strncpy(currentConfig.lastRomFile, selfname, sizeof(currentConfig.lastRomFile)-1);\r
952 currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;\r
953 engineState = PGS_ReloadRom;\r
954 }\r
955 return;\r
956 case 5: // options\r
957 menu_loop_options();\r
958 break;\r
959 case 6: // controls\r
960 kc_sel_loop();\r
961 break;\r
962 case 7: // credits\r
963 draw_menu_credits();\r
964 usleep(500*1000);\r
965 inp = wait_for_input(GP2X_B|GP2X_X);\r
966 break;\r
967 case 8: // exit\r
968 engineState = PGS_Quit;\r
969 return;\r
970 }\r
971 }\r
972 menuErrorMsg[0] = 0; // clear error msg\r
973 }\r
974}\r
975\r
976\r
977void menu_loop(void)\r
978{\r
979 int pal[2];\r
980\r
981 // switch to 8bpp\r
982 gp2x_video_changemode(8);\r
983 gp2x_video_RGB_setscaling(320, 240);\r
984 // set pal\r
985 pal[0] = 0;\r
986 pal[1] = 0x00ffffff;\r
987 gp2x_video_setpalette(pal, 2);\r
988\r
989 menu_loop_root();\r
990\r
991 menuErrorMsg[0] = 0;\r
992}\r