menu changes
[libpicofe.git] / gp2x / menu.c
CommitLineData
720ee7f6 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
0805b5b4 16#include "fonts.h"\r
720ee7f6 17#include "usbjoy.h"\r
18#include "version.h"\r
19\r
70d2ecc5 20#include <Pico/PicoInt.h>\r
21#include <Pico/Patch.h>\r
22#include <zlib/zlib.h>\r
720ee7f6 23\r
24#ifndef _DIRENT_HAVE_D_TYPE\r
25#error "need d_type for file browser\r
26#endif\r
27\r
28extern char *actionNames[];\r
29extern char romFileName[PATH_MAX];\r
30extern char *rom_data;\r
31extern int mmuhack_status;\r
32extern int state_slot;\r
33\r
34static char *gp2xKeyNames[] = {\r
35 "UP", "???", "LEFT", "???", "DOWN", "???", "RIGHT", "???",\r
36 "START", "SELECT", "L", "R", "A", "B", "X", "Y",\r
37 "???", "???", "???", "???", "???", "???", "VOL DOWN", "VOL UP",\r
38 "???", "???", "???", "PUSH", "???", "???", "???", "???"\r
39};\r
40\r
41char menuErrorMsg[40] = {0, };\r
42\r
43\r
0805b5b4 44static void gp2x_text(unsigned char *screen, int x, int y, const char *text, int color)\r
720ee7f6 45{\r
46 int i,l;\r
47\r
48 screen = screen + x + y*320;\r
49\r
50 for (i = 0; i < strlen(text); i++)\r
51 {\r
52 for (l=0;l<8;l++)\r
53 {\r
54 if(fontdata8x8[((text[i])*8)+l]&0x80) screen[l*320+0]=color;\r
55 if(fontdata8x8[((text[i])*8)+l]&0x40) screen[l*320+1]=color;\r
56 if(fontdata8x8[((text[i])*8)+l]&0x20) screen[l*320+2]=color;\r
57 if(fontdata8x8[((text[i])*8)+l]&0x10) screen[l*320+3]=color;\r
58 if(fontdata8x8[((text[i])*8)+l]&0x08) screen[l*320+4]=color;\r
59 if(fontdata8x8[((text[i])*8)+l]&0x04) screen[l*320+5]=color;\r
60 if(fontdata8x8[((text[i])*8)+l]&0x02) screen[l*320+6]=color;\r
61 if(fontdata8x8[((text[i])*8)+l]&0x01) screen[l*320+7]=color;\r
62 }\r
63 screen += 8;\r
64 }\r
65}\r
66\r
67// draws white text to current bbp15 screen\r
0805b5b4 68void gp2x_text_out15(int x, int y, const char *text)\r
720ee7f6 69{\r
70 int i,l;\r
71 unsigned short *screen = gp2x_screen;\r
72\r
73 screen = screen + x + y*320;\r
74\r
75 for (i = 0; i < strlen(text); i++)\r
76 {\r
77 for (l=0;l<8;l++)\r
78 {\r
79 if(fontdata8x8[((text[i])*8)+l]&0x80) screen[l*320+0]=0xffff;\r
80 if(fontdata8x8[((text[i])*8)+l]&0x40) screen[l*320+1]=0xffff;\r
81 if(fontdata8x8[((text[i])*8)+l]&0x20) screen[l*320+2]=0xffff;\r
82 if(fontdata8x8[((text[i])*8)+l]&0x10) screen[l*320+3]=0xffff;\r
83 if(fontdata8x8[((text[i])*8)+l]&0x08) screen[l*320+4]=0xffff;\r
84 if(fontdata8x8[((text[i])*8)+l]&0x04) screen[l*320+5]=0xffff;\r
85 if(fontdata8x8[((text[i])*8)+l]&0x02) screen[l*320+6]=0xffff;\r
86 if(fontdata8x8[((text[i])*8)+l]&0x01) screen[l*320+7]=0xffff;\r
87 }\r
88 screen += 8;\r
89 }\r
90}\r
91\r
92\r
0805b5b4 93void gp2x_text_out8(int x, int y, const char *texto, ...)\r
720ee7f6 94{\r
95 va_list args;\r
96 char buffer[512];\r
97\r
98 va_start(args,texto);\r
99 vsprintf(buffer,texto,args);\r
100 va_end(args);\r
101\r
e5d315a5 102 gp2x_text(gp2x_screen,x,y,buffer,0xf0);\r
720ee7f6 103}\r
104\r
105\r
0805b5b4 106void gp2x_text_out8_2(int x, int y, const char *texto, int color)\r
720ee7f6 107{\r
108 gp2x_text(gp2x_screen, x, y, texto, color);\r
109}\r
110\r
0805b5b4 111void gp2x_text_out8_lim(int x, int y, const char *texto, int max)\r
720ee7f6 112{\r
113 char buffer[320/8+1];\r
114\r
115 strncpy(buffer, texto, 320/8);\r
116 if (max > 320/8) max = 320/8;\r
117 if (max < 0) max = 0;\r
118 buffer[max] = 0;\r
119\r
e5d315a5 120 gp2x_text(gp2x_screen,x,y,buffer,0xf0);\r
720ee7f6 121}\r
122\r
0805b5b4 123static void gp2x_smalltext8(int x, int y, const char *texto)\r
124{\r
125 int i;\r
126 unsigned char *src, *dst;\r
127\r
128 for (i = 0;; i++, x += 6)\r
129 {\r
130 unsigned char c = (unsigned char) texto[i];\r
131 int h = 8;\r
132\r
133 if (!c) break;\r
134\r
135 src = fontdata6x8[c];\r
136 dst = (unsigned char *)gp2x_screen + x + y*320;\r
137\r
138 while (h--)\r
139 {\r
140 int w = 0x20;\r
141 while (w)\r
142 {\r
143 if( *src & w ) *dst = 0xf0;\r
144 dst++;\r
145 w>>=1;\r
146 }\r
147 src++;\r
148\r
149 dst += 320-6;\r
150 }\r
151 }\r
152}\r
153\r
154static void gp2x_smalltext8_lim(int x, int y, const char *texto, int max)\r
155{\r
156 char buffer[320/6+1];\r
157\r
158 strncpy(buffer, texto, 320/6);\r
159 if (max > 320/6) max = 320/6;\r
160 if (max < 0) max = 0;\r
161 buffer[max] = 0;\r
162\r
163 gp2x_smalltext8(x, y, buffer);\r
164}\r
165\r
720ee7f6 166\r
167static unsigned long inp_prev = 0;\r
168static int inp_prevjoy = 0;\r
169\r
170static unsigned long wait_for_input(unsigned long interesting)\r
171{\r
172 unsigned long ret;\r
173 static int repeats = 0, wait = 300*1000;\r
174 int release = 0, i;\r
175\r
176 if (repeats == 5 || repeats == 15 || repeats == 30) wait /= 2;\r
177\r
178 for (i = 0; i < 6 && inp_prev == gp2x_joystick_read(1); i++) {\r
179 if(i == 0) repeats++;\r
180 usleep(wait/6);\r
181 }\r
182\r
183 while ( !((ret = gp2x_joystick_read(1)) & interesting) ) {\r
184 usleep(50000);\r
185 release = 1;\r
186 }\r
187\r
188 if (release || ret != inp_prev) {\r
189 repeats = 0;\r
190 wait = 300*1000;\r
191 }\r
192 inp_prev = ret;\r
193 inp_prevjoy = 0;\r
194\r
195 // we don't need diagonals in menus\r
196 if ((ret&GP2X_UP) && (ret&GP2X_LEFT)) ret &= ~GP2X_LEFT;\r
197 if ((ret&GP2X_UP) && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;\r
198 if ((ret&GP2X_DOWN) && (ret&GP2X_LEFT)) ret &= ~GP2X_LEFT;\r
199 if ((ret&GP2X_DOWN) && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;\r
200\r
201 return ret;\r
202}\r
203\r
204static unsigned long input2_read(unsigned long interesting, int *joy)\r
205{\r
206 unsigned long ret;\r
207 int i;\r
208\r
209 do\r
210 {\r
211 *joy = 0;\r
212 if ((ret = gp2x_joystick_read(0) & interesting)) break;\r
213 gp2x_usbjoy_update();\r
214 for (i = 0; i < num_of_joys; i++) {\r
215 ret = gp2x_usbjoy_check2(i);\r
216 if (ret) { *joy = i + 1; break; }\r
217 }\r
218 if (ret) break;\r
219 }\r
220 while(0);\r
221\r
222 return ret;\r
223}\r
224\r
225// similar to wait_for_input(), but returns joy num\r
226static unsigned long wait_for_input_usbjoy(unsigned long interesting, int *joy)\r
227{\r
228 unsigned long ret;\r
229 const int wait = 300*1000;\r
230 int i;\r
231\r
232 if (inp_prevjoy == 0) inp_prev &= interesting;\r
233 for (i = 0; i < 6; i++) {\r
234 ret = input2_read(interesting, joy);\r
235 if (*joy != inp_prevjoy || ret != inp_prev) break;\r
236 usleep(wait/6);\r
237 }\r
238\r
239 while ( !(ret = input2_read(interesting, joy)) ) {\r
240 usleep(50000);\r
241 }\r
242\r
243 inp_prev = ret;\r
244 inp_prevjoy = *joy;\r
245\r
246 return ret;\r
247}\r
248\r
249\r
250\r
251// -------------- ROM selector --------------\r
252\r
253static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)\r
254{\r
255 int start, i, pos;\r
256\r
257 start = 12 - sel;\r
258 n--; // exclude current dir (".")\r
259\r
e5d315a5 260 //memset(gp2x_screen, 0, 320*240);\r
261 gp2x_pd_clone_buffer2();\r
720ee7f6 262\r
263 if(start - 2 >= 0)\r
0805b5b4 264 gp2x_smalltext8_lim(14, (start - 2)*10, curdir, 53-2);\r
720ee7f6 265 for (i = 0; i < n; i++) {\r
266 pos = start + i;\r
267 if (pos < 0) continue;\r
268 if (pos > 23) break;\r
269 if (namelist[i+1]->d_type == DT_DIR) {\r
0805b5b4 270 gp2x_smalltext8_lim(14, pos*10, "/", 1);\r
271 gp2x_smalltext8_lim(14+6, pos*10, namelist[i+1]->d_name, 53-3);\r
720ee7f6 272 } else {\r
0805b5b4 273 gp2x_smalltext8_lim(14, pos*10, namelist[i+1]->d_name, 53-2);\r
720ee7f6 274 }\r
275 }\r
276 gp2x_text_out8(5, 120, ">");\r
e5d315a5 277 gp2x_video_flip2();\r
720ee7f6 278}\r
279\r
280static int scandir_cmp(const void *p1, const void *p2)\r
281{\r
282 struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2;\r
283 if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2);\r
284 if ((*d1)->d_type == DT_DIR) return -1; // put before\r
285 if ((*d2)->d_type == DT_DIR) return 1;\r
286 return alphasort(d1, d2);\r
287}\r
288\r
17fa1e44 289static char *filter_exts[] = { ".mp3", ".MP3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html", ".jpg", ".gpe" };\r
0d0558bd 290\r
291static int scandir_filter(const struct dirent *ent)\r
292{\r
293 const char *p;\r
294 int i;\r
295\r
296 if (ent == NULL || ent->d_name == NULL) return 0;\r
297 if (strlen(ent->d_name) < 5) return 1;\r
298\r
299 p = ent->d_name + strlen(ent->d_name) - 4;\r
300\r
301 for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)\r
302 {\r
303 if (strcmp(p, filter_exts[i]) == 0) return 0;\r
304 }\r
305\r
306 return 1;\r
307}\r
720ee7f6 308\r
309static char *romsel_loop(char *curr_path)\r
310{\r
311 struct dirent **namelist;\r
312 DIR *dir;\r
313 int n, sel = 0;\r
314 unsigned long inp = 0;\r
315 char *ret = NULL, *fname = NULL;\r
316\r
317 // is this a dir or a full path?\r
318 if ((dir = opendir(curr_path))) {\r
319 closedir(dir);\r
320 } else {\r
321 char *p;\r
322 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);\r
323 *p = 0;\r
324 fname = p+1;\r
325 }\r
326\r
0d0558bd 327 n = scandir(curr_path, &namelist, scandir_filter, scandir_cmp);\r
720ee7f6 328 if (n < 0) {\r
329 // try root\r
0d0558bd 330 n = scandir("/", &namelist, scandir_filter, scandir_cmp);\r
720ee7f6 331 if (n < 0) {\r
332 // oops, we failed\r
333 printf("dir: "); printf(curr_path); printf("\n");\r
334 perror("scandir");\r
335 return NULL;\r
336 }\r
337 }\r
338\r
339 // try to find sel\r
340 if (fname != NULL) {\r
341 int i;\r
342 for (i = 1; i < n; i++) {\r
343 if (strcmp(namelist[i]->d_name, fname) == 0) {\r
344 sel = i - 1;\r
345 break;\r
346 }\r
347 }\r
348 }\r
349\r
350 for (;;)\r
351 {\r
352 draw_dirlist(curr_path, namelist, n, sel);\r
5111820c 353 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);\r
720ee7f6 354 if(inp & GP2X_UP ) { sel--; if (sel < 0) sel = n-2; }\r
355 if(inp & GP2X_DOWN) { sel++; if (sel > n-2) sel = 0; }\r
5111820c 356 if(inp &(GP2X_LEFT|GP2X_L)) { sel-=10; if (sel < 0) sel = 0; }\r
357 if(inp &(GP2X_RIGHT|GP2X_R)) { sel+=10; if (sel > n-2) sel = n-2; }\r
720ee7f6 358 if(inp & GP2X_B) { // enter dir/select\r
359 again:\r
360 if (namelist[sel+1]->d_type == DT_REG) {\r
361 strcpy(romFileName, curr_path);\r
362 strcat(romFileName, "/");\r
363 strcat(romFileName, namelist[sel+1]->d_name);\r
364 ret = romFileName;\r
365 break;\r
366 } else if (namelist[sel+1]->d_type == DT_DIR) {\r
367 int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r
368 char *p, *newdir = malloc(newlen);\r
369 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {\r
370 char *start = curr_path;\r
371 p = start + strlen(start) - 1;\r
372 while (*p == '/' && p > start) p--;\r
373 while (*p != '/' && p > start) p--;\r
374 if (p <= start) strcpy(newdir, "/");\r
375 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }\r
376 } else {\r
377 strcpy(newdir, curr_path);\r
378 p = newdir + strlen(newdir) - 1;\r
379 while (*p == '/' && p >= newdir) *p-- = 0;\r
380 strcat(newdir, "/");\r
381 strcat(newdir, namelist[sel+1]->d_name);\r
382 }\r
383 ret = romsel_loop(newdir);\r
384 free(newdir);\r
385 break;\r
386 } else {\r
387 // unknown file type, happens on NTFS mounts. Try to guess.\r
388 FILE *tstf; int tmp;\r
389 strcpy(romFileName, curr_path);\r
390 strcat(romFileName, "/");\r
391 strcat(romFileName, namelist[sel+1]->d_name);\r
392 tstf = fopen(romFileName, "rb");\r
393 if (tstf != NULL)\r
394 {\r
395 if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)\r
396 namelist[sel+1]->d_type = DT_REG;\r
397 else namelist[sel+1]->d_type = DT_DIR;\r
398 fclose(tstf);\r
399 goto again;\r
400 }\r
401 }\r
402 }\r
403 if(inp & GP2X_X) break; // cancel\r
404 }\r
405\r
406 if (n > 0) {\r
407 while(n--) free(namelist[n]);\r
408 free(namelist);\r
409 }\r
410\r
411 return ret;\r
412}\r
413\r
70d2ecc5 414// ------------ patch/gg menu ------------\r
415\r
416static void draw_patchlist(int sel)\r
417{\r
418 int start, i, pos;\r
419\r
420 start = 12 - sel;\r
421\r
422 gp2x_pd_clone_buffer2();\r
423\r
424 for (i = 0; i < PicoPatchCount; i++) {\r
425 pos = start + i;\r
426 if (pos < 0) continue;\r
427 if (pos > 23) break;\r
428 gp2x_smalltext8_lim(14, pos*10, PicoPatches[i].active ? "ON " : "OFF", 3);\r
af6e9c49 429 gp2x_smalltext8_lim(14+6*4, pos*10, PicoPatches[i].name, 53-6);\r
70d2ecc5 430 }\r
431 pos = start + i;\r
432 if (pos < 24) gp2x_smalltext8_lim(14, pos*10, "done", 4);\r
433\r
434 gp2x_text_out8(5, 120, ">");\r
435 gp2x_video_flip2();\r
436}\r
437\r
438\r
439void patches_menu_loop(void)\r
440{\r
441 int menu_sel = 0;\r
442 unsigned long inp = 0;\r
443\r
444 for(;;)\r
445 {\r
446 draw_patchlist(menu_sel);\r
447 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);\r
448 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }\r
449 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }\r
450 if(inp &(GP2X_LEFT|GP2X_L)) { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }\r
451 if(inp &(GP2X_RIGHT|GP2X_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }\r
452 if(inp & GP2X_B) { // action\r
453 if (menu_sel < PicoPatchCount)\r
454 PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;\r
455 else return;\r
456 }\r
457 if(inp & GP2X_X) return;\r
458 }\r
459\r
460}\r
461\r
4ffd2858 462// ------------ savestate loader ------------\r
463\r
464static void menu_prepare_bg(void);\r
465\r
466static int state_slot_flags = 0;\r
467\r
468static void state_check_slots(void)\r
469{\r
470 int slot;\r
471\r
472 state_slot_flags = 0;\r
473\r
474 for (slot = 0; slot < 10; slot++)\r
475 {\r
476 if (emu_check_save_file(slot))\r
477 {\r
478 state_slot_flags |= 1 << slot;\r
479 }\r
480 }\r
481}\r
482\r
483static void draw_savestate_bg(int slot)\r
484{\r
485 struct PicoVideo tmp_pv;\r
486 unsigned short tmp_cram[0x40];\r
487 unsigned short tmp_vsram[0x40];\r
488 void *tmp_vram, *file;\r
489 char *fname;\r
490\r
491 fname = emu_GetSaveFName(1, 0, slot);\r
492 if (!fname) return;\r
493\r
494 tmp_vram = malloc(sizeof(Pico.vram));\r
495 if (tmp_vram == NULL) return;\r
496\r
497 memcpy(tmp_vram, Pico.vram, sizeof(Pico.vram));\r
498 memcpy(tmp_cram, Pico.cram, sizeof(Pico.cram));\r
499 memcpy(tmp_vsram, Pico.vsram, sizeof(Pico.vsram));\r
500 memcpy(&tmp_pv, &Pico.video, sizeof(Pico.video));\r
501\r
502 if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {\r
503 file = gzopen(fname, "rb");\r
504 emu_set_save_cbs(1);\r
505 } else {\r
506 file = fopen(fname, "rb");\r
507 emu_set_save_cbs(0);\r
508 }\r
509\r
510 if (file) {\r
511 if (PicoMCD & 1) {\r
512 PicoCdLoadStateGfx(file);\r
513 } else {\r
514 areaSeek(file, 0x10020, SEEK_SET); // skip header and RAM in state file\r
515 areaRead(Pico.vram, 1, sizeof(Pico.vram), file);\r
516 areaSeek(file, 0x2000, SEEK_CUR);\r
517 areaRead(Pico.cram, 1, sizeof(Pico.cram), file);\r
518 areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);\r
519 areaSeek(file, 0x221a0, SEEK_SET);\r
520 areaRead(&Pico.video, 1, sizeof(Pico.video), file);\r
521 }\r
522 areaClose(file);\r
523 }\r
524\r
525 emu_forced_frame();\r
526 gp2x_memcpy_buffers((1<<2), gp2x_screen, 0, 320*240*2);\r
527 menu_prepare_bg();\r
528\r
529 memcpy(Pico.vram, tmp_vram, sizeof(Pico.vram));\r
530 memcpy(Pico.cram, tmp_cram, sizeof(Pico.cram));\r
531 memcpy(Pico.vsram, tmp_vsram, sizeof(Pico.vsram));\r
532 memcpy(&Pico.video, &tmp_pv, sizeof(Pico.video));\r
533 free(tmp_vram);\r
534}\r
535\r
536static void draw_savestate_menu(int menu_sel, int is_loading)\r
537{\r
538 int tl_x = 25, tl_y = 60, y, i;\r
539\r
540 if (state_slot_flags & (1 << menu_sel))\r
541 draw_savestate_bg(menu_sel);\r
542 gp2x_pd_clone_buffer2();\r
543\r
544 gp2x_text_out8(tl_x, 30, is_loading ? "Load state" : "Save state");\r
545\r
546 /* draw all 10 slots */\r
547 y = tl_y;\r
548 for (i = 0; i < 10; i++, y+=10)\r
549 {\r
550 gp2x_text_out8(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");\r
551 }\r
552 gp2x_text_out8(tl_x, y, "back");\r
553\r
554 // draw cursor\r
555 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
556\r
557 gp2x_video_flip2();\r
558}\r
559\r
560static int savestate_menu_loop(int is_loading)\r
561{\r
562 int menu_sel = 10, menu_sel_max = 10;\r
563 unsigned long inp = 0;\r
564\r
565 state_check_slots();\r
566\r
567 for(;;)\r
568 {\r
569 draw_savestate_menu(menu_sel, is_loading);\r
570 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
571 if(inp & GP2X_UP ) {\r
572 do {\r
573 menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;\r
574 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
575 }\r
576 if(inp & GP2X_DOWN) {\r
577 do {\r
578 menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;\r
579 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
580 }\r
581 if(inp & GP2X_B) { // save/load\r
582 if (menu_sel < 10) {\r
583 state_slot = menu_sel;\r
584 if (emu_SaveLoadGame(is_loading, 0)) {\r
585 strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");\r
586 return 1;\r
587 }\r
588 return 0;\r
589 } else return 1;\r
590 }\r
591 if(inp & GP2X_X) return 1;\r
592 }\r
593}\r
594\r
720ee7f6 595// -------------- key config --------------\r
596\r
597static char *usb_joy_key_name(int joy, int num)\r
598{\r
599 static char name[16];\r
600 switch (num)\r
601 {\r
602 case 0: sprintf(name, "Joy%i UP", joy); break;\r
603 case 1: sprintf(name, "Joy%i DOWN", joy); break;\r
604 case 2: sprintf(name, "Joy%i LEFT", joy); break;\r
605 case 3: sprintf(name, "Joy%i RIGHT", joy); break;\r
606 default:sprintf(name, "Joy%i b%i", joy, num-3); break;\r
607 }\r
608 return name;\r
609}\r
610\r
611static void draw_key_config(int curr_act, int is_p2)\r
612{\r
613 char strkeys[32*5];\r
614 int joy, i;\r
615\r
616 strkeys[0] = 0;\r
617 for (i = 0; i < 32; i++)\r
618 {\r
619 if (currentConfig.KeyBinds[i] & (1 << curr_act))\r
620 {\r
621 if (curr_act < 16 && (currentConfig.KeyBinds[i] & (1 << 16)) != (is_p2 << 16)) continue;\r
622 if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, gp2xKeyNames[i]); break; }\r
623 else strcpy(strkeys, gp2xKeyNames[i]);\r
624 }\r
625 }\r
626 for (joy = 0; joy < num_of_joys; joy++)\r
627 {\r
628 for (i = 0; i < 32; i++)\r
629 {\r
630 if (currentConfig.JoyBinds[joy][i] & (1 << curr_act))\r
631 {\r
632 if (curr_act < 16 && (currentConfig.JoyBinds[joy][i] & (1 << 16)) != (is_p2 << 16)) continue;\r
633 if (strkeys[0]) {\r
634 strcat(strkeys, ", "); strcat(strkeys, usb_joy_key_name(joy + 1, i));\r
635 break;\r
636 }\r
637 else strcpy(strkeys, usb_joy_key_name(joy + 1, i));\r
638 }\r
639 }\r
640 }\r
641\r
e5d315a5 642 //memset(gp2x_screen, 0, 320*240);\r
643 gp2x_pd_clone_buffer2();\r
720ee7f6 644 gp2x_text_out8(60, 40, "Action: %s", actionNames[curr_act]);\r
645 gp2x_text_out8(60, 60, "Keys: %s", strkeys);\r
646\r
647 gp2x_text_out8(30, 180, "Use SELECT to change action");\r
648 gp2x_text_out8(30, 190, "Press a key to bind/unbind");\r
649 gp2x_text_out8(30, 200, "Select \"Done\" action and");\r
650 gp2x_text_out8(30, 210, " press any key to finish");\r
e5d315a5 651 gp2x_video_flip2();\r
720ee7f6 652}\r
653\r
654static void key_config_loop(int is_p2)\r
655{\r
656 int curr_act = 0, joy = 0, i;\r
657 unsigned long inp = 0;\r
658\r
659 for (;;)\r
660 {\r
661 draw_key_config(curr_act, is_p2);\r
662 inp = wait_for_input_usbjoy(CONFIGURABLE_KEYS, &joy);\r
663 // printf("got %08lX from joy %i\n", inp, joy);\r
664 if (joy == 0) {\r
665 if (inp & GP2X_SELECT) {\r
666 curr_act++;\r
667 while (!actionNames[curr_act] && curr_act < 32) curr_act++;\r
668 if (curr_act > 31) curr_act = 0;\r
669 }\r
670 inp &= CONFIGURABLE_KEYS;\r
671 inp &= ~GP2X_SELECT;\r
672 }\r
673 if (curr_act == 31 && inp) break;\r
674 if (joy == 0) {\r
675 for (i = 0; i < 32; i++)\r
676 if (inp & (1 << i)) {\r
677 currentConfig.KeyBinds[i] ^= (1 << curr_act);\r
678 if (is_p2) currentConfig.KeyBinds[i] |= (1 << 16); // player 2 flag\r
679 else currentConfig.KeyBinds[i] &= ~(1 << 16);\r
680 }\r
681 } else {\r
682 for (i = 0; i < 32; i++)\r
683 if (inp & (1 << i)) {\r
684 currentConfig.JoyBinds[joy-1][i] ^= (1 << curr_act);\r
685 if (is_p2) currentConfig.JoyBinds[joy-1][i] |= (1 << 16);\r
686 else currentConfig.JoyBinds[joy-1][i] &= ~(1 << 16);\r
687 }\r
688 }\r
689 }\r
690}\r
691\r
692static void draw_kc_sel(int menu_sel)\r
693{\r
694 int tl_x = 25+40, tl_y = 60, y, i;\r
695 char joyname[36];\r
696\r
697 y = tl_y;\r
e5d315a5 698 //memset(gp2x_screen, 0, 320*240);\r
699 gp2x_pd_clone_buffer2();\r
720ee7f6 700 gp2x_text_out8(tl_x, y, "Player 1");\r
701 gp2x_text_out8(tl_x, (y+=10), "Player 2");\r
702 gp2x_text_out8(tl_x, (y+=10), "Done");\r
703\r
704 // draw cursor\r
705 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
706\r
707 tl_x = 25;\r
708 gp2x_text_out8(tl_x, (y=110), "USB joys detected:");\r
709 if (num_of_joys > 0) {\r
710 for (i = 0; i < num_of_joys; i++) {\r
711 strncpy(joyname, joy_name(joys[i]), 33); joyname[33] = 0;\r
712 gp2x_text_out8(tl_x, (y+=10), "%i: %s", i+1, joyname);\r
713 }\r
714 } else {\r
715 gp2x_text_out8(tl_x, (y+=10), "none");\r
716 }\r
717\r
718\r
e5d315a5 719 gp2x_video_flip2();\r
720ee7f6 720}\r
721\r
722static void kc_sel_loop(void)\r
723{\r
724 int menu_sel = 2, menu_sel_max = 2;\r
725 unsigned long inp = 0;\r
726\r
727 for(;;)\r
728 {\r
729 draw_kc_sel(menu_sel);\r
730 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
731 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
732 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
733 if(inp & GP2X_B) {\r
734 switch (menu_sel) {\r
735 case 0: key_config_loop(0); return;\r
736 case 1: key_config_loop(1); return;\r
737 default: return;\r
738 }\r
739 }\r
740 if(inp & GP2X_X) return;\r
741 }\r
742}\r
743\r
744\r
745\r
daf91588 746// --------- sega/mega cd options ----------\r
747\r
748static void draw_cd_menu_options(int menu_sel, char *b_us, char *b_eu, char *b_jp)\r
749{\r
750 int tl_x = 25, tl_y = 60, y;\r
5f9922e6 751 char ra_buff[16];\r
752\r
753 if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);\r
754 else strcpy(ra_buff, " OFF");\r
daf91588 755\r
756 y = tl_y;\r
e5d315a5 757 //memset(gp2x_screen, 0, 320*240);\r
758 gp2x_pd_clone_buffer2();\r
759\r
daf91588 760 gp2x_text_out8(tl_x, y, "USA BIOS: %s", b_us); // 0\r
761 gp2x_text_out8(tl_x, (y+=10), "EUR BIOS: %s", b_eu); // 1\r
762 gp2x_text_out8(tl_x, (y+=10), "JAP BIOS: %s", b_jp); // 2\r
dccc2bd0 763 gp2x_text_out8(tl_x, (y+=10), "CD LEDs %s", (currentConfig.EmuOpt &0x0400)?"ON":"OFF"); // 3\r
764 gp2x_text_out8(tl_x, (y+=10), "CDDA audio (using mp3s) %s", (currentConfig.PicoOpt&0x0800)?"ON":"OFF"); // 4\r
765 gp2x_text_out8(tl_x, (y+=10), "PCM audio %s", (currentConfig.PicoOpt&0x0400)?"ON":"OFF"); // 5\r
7789349f 766 gp2x_text_out8(tl_x, (y+=10), "ReadAhead buffer %s", ra_buff); // 6\r
17fa1e44 767 gp2x_text_out8(tl_x, (y+=10), "Scale/Rot. fx (slow) %s", (currentConfig.PicoOpt&0x1000)?"ON":"OFF"); // 7\r
7789349f 768 gp2x_text_out8(tl_x, (y+=10), "Better sync (slow) %s", (currentConfig.PicoOpt&0x2000)?"ON":"OFF"); // 8\r
daf91588 769 gp2x_text_out8(tl_x, (y+=10), "Done");\r
770\r
771 // draw cursor\r
772 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
773\r
774 if ((menu_sel == 0 && strcmp(b_us, "NOT FOUND")) ||\r
775 (menu_sel == 1 && strcmp(b_eu, "NOT FOUND")) ||\r
776 (menu_sel == 2 && strcmp(b_jp, "NOT FOUND")))\r
777 gp2x_text_out8(tl_x, 220, "Press start to test selected BIOS");\r
778\r
e5d315a5 779 gp2x_video_flip2();\r
daf91588 780}\r
781\r
782static void cd_menu_loop_options(void)\r
783{\r
7789349f 784 int menu_sel = 0, menu_sel_max = 9;\r
daf91588 785 unsigned long inp = 0;\r
786 char bios_us[32], bios_eu[32], bios_jp[32], *bios, *p;\r
787\r
788 if (find_bios(4, &bios)) { // US\r
789 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
790 strncpy(bios_us, p, 31); bios_us[31] = 0;\r
791 } else strcpy(bios_us, "NOT FOUND");\r
792\r
793 if (find_bios(8, &bios)) { // EU\r
794 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
795 strncpy(bios_eu, p, 31); bios_eu[31] = 0;\r
796 } else strcpy(bios_eu, "NOT FOUND");\r
797\r
798 if (find_bios(1, &bios)) { // JP\r
799 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
800 strncpy(bios_jp, p, 31); bios_jp[31] = 0;\r
801 } else strcpy(bios_jp, "NOT FOUND");\r
802\r
803 for(;;)\r
804 {\r
805 draw_cd_menu_options(menu_sel, bios_us, bios_eu, bios_jp);\r
806 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A|GP2X_START);\r
807 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
808 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
809 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
810 switch (menu_sel) {\r
dccc2bd0 811 case 3: currentConfig.EmuOpt ^=0x0400; break;\r
812 case 4: currentConfig.PicoOpt^=0x0800; break;\r
813 case 5: currentConfig.PicoOpt^=0x0400; break;\r
7789349f 814 case 6:\r
5f9922e6 815 if (inp & GP2X_LEFT) {\r
816 PicoCDBuffers >>= 1;\r
817 if (PicoCDBuffers < 64) PicoCDBuffers = 0;\r
818 } else {\r
819 if (PicoCDBuffers < 64) PicoCDBuffers = 64;\r
820 else PicoCDBuffers <<= 1;\r
53c908bb 821 if (PicoCDBuffers > 8*1024) PicoCDBuffers = 8*1024; // 16M\r
5f9922e6 822 }\r
823 break;\r
7789349f 824 case 7: currentConfig.PicoOpt^=0x1000; break;\r
825 case 8: currentConfig.PicoOpt^=0x2000; break;\r
826 case 9: return;\r
daf91588 827 }\r
828 }\r
829 if(inp & (GP2X_X|GP2X_A)) return;\r
830 if(inp & GP2X_START) { // BIOS testers\r
831 switch (menu_sel) {\r
832 case 0: if (find_bios(4, &bios)) { // test US\r
833 strcpy(romFileName, bios);\r
834 engineState = PGS_ReloadRom;\r
835 return;\r
836 }\r
837 break;\r
838 case 1: if (find_bios(8, &bios)) { // test EU\r
839 strcpy(romFileName, bios);\r
840 engineState = PGS_ReloadRom;\r
841 return;\r
842 }\r
843 break;\r
844 case 2: if (find_bios(1, &bios)) { // test JP\r
845 strcpy(romFileName, bios);\r
846 engineState = PGS_ReloadRom;\r
847 return;\r
848 }\r
849 break;\r
850 }\r
851 }\r
852 }\r
853}\r
854\r
855\r
856// --------- advanced options ----------\r
857\r
720ee7f6 858static void draw_amenu_options(int menu_sel)\r
859{\r
860 int tl_x = 25, tl_y = 60, y;\r
861 char *mms = mmuhack_status ? "active) " : "inactive)";\r
862\r
863 y = tl_y;\r
e5d315a5 864 //memset(gp2x_screen, 0, 320*240);\r
865 gp2x_pd_clone_buffer2();\r
866\r
979ba09f 867 gp2x_text_out8(tl_x, y, "Scale 32 column mode %s", (currentConfig.PicoOpt&0x100)?"ON":"OFF"); // 0\r
868 gp2x_text_out8(tl_x, (y+=10), "Gamma correction %i.%02i", currentConfig.gamma / 100, currentConfig.gamma%100); // 1\r
869 gp2x_text_out8(tl_x, (y+=10), "Emulate Z80 %s", (currentConfig.PicoOpt&0x004)?"ON":"OFF"); // 2\r
870 gp2x_text_out8(tl_x, (y+=10), "Emulate YM2612 (FM) %s", (currentConfig.PicoOpt&0x001)?"ON":"OFF"); // 3\r
871 gp2x_text_out8(tl_x, (y+=10), "Emulate SN76496 (PSG) %s", (currentConfig.PicoOpt&0x002)?"ON":"OFF"); // 4\r
872 gp2x_text_out8(tl_x, (y+=10), "gzip savestates %s", (currentConfig.EmuOpt &0x008)?"ON":"OFF"); // 5\r
873 gp2x_text_out8(tl_x, (y+=10), "Don't save config on exit %s", (currentConfig.EmuOpt &0x020)?"ON":"OFF"); // 6\r
720ee7f6 874 gp2x_text_out8(tl_x, (y+=10), "needs restart:");\r
979ba09f 875 gp2x_text_out8(tl_x, (y+=10), "craigix's RAM timings %s", (currentConfig.EmuOpt &0x100)?"ON":"OFF"); // 8\r
876 gp2x_text_out8(tl_x, (y+=10), "squidgehack (now %s %s", mms, (currentConfig.EmuOpt &0x010)?"ON":"OFF"); // 9\r
720ee7f6 877 gp2x_text_out8(tl_x, (y+=10), "Done");\r
878\r
879 // draw cursor\r
880 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
881\r
e5d315a5 882 gp2x_video_flip2();\r
720ee7f6 883}\r
884\r
885static void amenu_loop_options(void)\r
886{\r
47f22a1f 887 int menu_sel = 0, menu_sel_max = 10;\r
720ee7f6 888 unsigned long inp = 0;\r
889\r
890 for(;;)\r
891 {\r
892 draw_amenu_options(menu_sel);\r
893 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
894 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
895 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
896 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
897 switch (menu_sel) {\r
979ba09f 898 case 0: currentConfig.PicoOpt^=0x100; break;\r
899 case 2: currentConfig.PicoOpt^=0x004; break;\r
900 case 3: currentConfig.PicoOpt^=0x001; break;\r
901 case 4: currentConfig.PicoOpt^=0x002; break;\r
902 case 5: currentConfig.EmuOpt ^=0x008; break;\r
903 case 6: currentConfig.EmuOpt ^=0x020; break;\r
904 case 8: currentConfig.EmuOpt ^=0x100; break;\r
905 case 9: currentConfig.EmuOpt ^=0x010; break;\r
720ee7f6 906 case 10: return;\r
907 }\r
908 }\r
909 if(inp & (GP2X_X|GP2X_A)) return;\r
910 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
911 switch (menu_sel) {\r
912 case 1:\r
913 while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
979ba09f 914 currentConfig.gamma += (inp & GP2X_LEFT) ? -1 : 1;\r
915 if (currentConfig.gamma < 1) currentConfig.gamma = 1;\r
916 if (currentConfig.gamma > 300) currentConfig.gamma = 300;\r
720ee7f6 917 draw_amenu_options(menu_sel);\r
918 usleep(18*1000);\r
919 }\r
920 break;\r
921 }\r
922 }\r
923 }\r
924}\r
925\r
926// -------------- options --------------\r
927\r
979ba09f 928static const char *region_name(unsigned int code)\r
720ee7f6 929{\r
979ba09f 930 static const char *names[] = { "Auto", " Japan NTSC", " Japan PAL", " USA", " Europe" };\r
931 static const char *names_short[] = { "", " JP", " JP", " US", " EU" };\r
932 int u, i = 0;\r
933 if (code) {\r
934 code <<= 1;\r
935 while((code >>= 1)) i++;\r
936 if (i > 4) return "unknown";\r
937 return names[i];\r
938 } else {\r
939 static char name[24];\r
940 strcpy(name, "Auto:");\r
941 for (u = 0; u < 3; u++) {\r
942 i = 0; code = ((PicoAutoRgnOrder >> u*4) & 0xf) << 1;\r
943 while((code >>= 1)) i++;\r
944 strcat(name, names_short[i]);\r
945 }\r
946 return name;\r
947 }\r
720ee7f6 948}\r
949\r
950static void draw_menu_options(int menu_sel)\r
951{\r
952 int tl_x = 25, tl_y = 40, y;\r
953 char monostereo[8], strframeskip[8], *strrend;\r
954\r
979ba09f 955 strcpy(monostereo, (currentConfig.PicoOpt&0x08)?"stereo":"mono");\r
956 if (currentConfig.Frameskip < 0)\r
720ee7f6 957 strcpy(strframeskip, "Auto");\r
979ba09f 958 else sprintf(strframeskip, "%i", currentConfig.Frameskip);\r
959 if (currentConfig.PicoOpt&0x10) {\r
720ee7f6 960 strrend = " 8bit fast";\r
979ba09f 961 } else if (currentConfig.EmuOpt&0x80) {\r
720ee7f6 962 strrend = "16bit accurate";\r
963 } else {\r
964 strrend = " 8bit accurate";\r
965 }\r
966\r
967 y = tl_y;\r
e5d315a5 968 //memset(gp2x_screen, 0, 320*240);\r
969 gp2x_pd_clone_buffer2();\r
970\r
720ee7f6 971 gp2x_text_out8(tl_x, y, "Renderer: %s", strrend); // 0\r
979ba09f 972 gp2x_text_out8(tl_x, (y+=10), "Accurate timing (slower) %s", (currentConfig.PicoOpt&0x040)?"ON":"OFF"); // 1\r
973 gp2x_text_out8(tl_x, (y+=10), "Accurate sprites (slower) %s", (currentConfig.PicoOpt&0x080)?"ON":"OFF"); // 2\r
974 gp2x_text_out8(tl_x, (y+=10), "Show FPS %s", (currentConfig.EmuOpt &0x002)?"ON":"OFF"); // 3\r
720ee7f6 975 gp2x_text_out8(tl_x, (y+=10), "Frameskip %s", strframeskip);\r
979ba09f 976 gp2x_text_out8(tl_x, (y+=10), "Enable sound %s", (currentConfig.EmuOpt &0x004)?"ON":"OFF"); // 5\r
977 gp2x_text_out8(tl_x, (y+=10), "Sound Quality: %5iHz %s", currentConfig.PsndRate, monostereo);\r
978 gp2x_text_out8(tl_x, (y+=10), "Use ARM940 core for sound %s", (currentConfig.PicoOpt&0x200)?"ON":"OFF"); // 7\r
979 gp2x_text_out8(tl_x, (y+=10), "6 button pad %s", (currentConfig.PicoOpt&0x020)?"ON":"OFF"); // 8\r
980 gp2x_text_out8(tl_x, (y+=10), "Genesis Region: %s", region_name(currentConfig.PicoRegion));\r
981 gp2x_text_out8(tl_x, (y+=10), "Use SRAM/BRAM savestates %s", (currentConfig.EmuOpt &0x001)?"ON":"OFF"); // 10\r
982 gp2x_text_out8(tl_x, (y+=10), "Confirm save overwrites %s", (currentConfig.EmuOpt &0x200)?"ON":"OFF"); // 11\r
720ee7f6 983 gp2x_text_out8(tl_x, (y+=10), "Save slot %i", state_slot); // 12\r
979ba09f 984 gp2x_text_out8(tl_x, (y+=10), "GP2X CPU clocks %iMhz", currentConfig.CPUclock);\r
daf91588 985 gp2x_text_out8(tl_x, (y+=10), "[Sega/Mega CD options]");\r
986 gp2x_text_out8(tl_x, (y+=10), "[advanced options]"); // 15\r
720ee7f6 987 gp2x_text_out8(tl_x, (y+=10), "Save cfg as default");\r
988 if (rom_data)\r
989 gp2x_text_out8(tl_x, (y+=10), "Save cfg for current game only");\r
990\r
991 // draw cursor\r
992 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
993\r
e5d315a5 994 gp2x_video_flip2();\r
720ee7f6 995}\r
996\r
997static int sndrate_prevnext(int rate, int dir)\r
998{\r
999 int i, rates[] = { 8000, 11025, 16000, 22050, 44100 };\r
1000\r
1001 for (i = 0; i < 5; i++)\r
1002 if (rates[i] == rate) break;\r
1003\r
1004 i += dir ? 1 : -1;\r
1005 if (i > 4) return dir ? 44100 : 22050;\r
1006 if (i < 0) return dir ? 11025 : 8000;\r
1007 return rates[i];\r
1008}\r
1009\r
979ba09f 1010static void region_prevnext(int right)\r
1011{\r
1012 // jp_ntsc=1, jp_pal=2, usa=4, eu=8\r
1013 static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };\r
1014 int i;\r
1015 if (right) {\r
1016 if (!currentConfig.PicoRegion) {\r
1017 for (i = 0; i < 6; i++)\r
1018 if (rgn_orders[i] == PicoAutoRgnOrder) break;\r
1019 if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];\r
1020 else currentConfig.PicoRegion=1;\r
1021 }\r
1022 else currentConfig.PicoRegion<<=1;\r
1023 if (currentConfig.PicoRegion > 8) currentConfig.PicoRegion = 8;\r
1024 } else {\r
1025 if (!currentConfig.PicoRegion) {\r
1026 for (i = 0; i < 6; i++)\r
1027 if (rgn_orders[i] == PicoAutoRgnOrder) break;\r
1028 if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];\r
1029 }\r
1030 else currentConfig.PicoRegion>>=1;\r
1031 }\r
1032}\r
1033\r
720ee7f6 1034static void menu_options_save(void)\r
1035{\r
720ee7f6 1036 PicoOpt = currentConfig.PicoOpt;\r
1037 PsndRate = currentConfig.PsndRate;\r
1038 PicoRegionOverride = currentConfig.PicoRegion;\r
1039 if (PicoOpt & 0x20) {\r
1040 actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
1041 actionNames[10] = "X"; actionNames[11] = "MODE";\r
1042 } else {\r
1043 actionNames[8] = actionNames[9] = actionNames[10] = actionNames[11] = 0;\r
1044 }\r
1045}\r
1046\r
daf91588 1047static int menu_loop_options(void)\r
720ee7f6 1048{\r
daf91588 1049 int menu_sel = 0, menu_sel_max = 16;\r
720ee7f6 1050 unsigned long inp = 0;\r
1051\r
1052 if (rom_data) menu_sel_max++;\r
979ba09f 1053 currentConfig.PicoOpt = PicoOpt;\r
1054 currentConfig.PsndRate = PsndRate;\r
1055 currentConfig.PicoRegion = PicoRegionOverride;\r
720ee7f6 1056\r
1057 for(;;)\r
1058 {\r
1059 draw_menu_options(menu_sel);\r
1060 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
1061 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
1062 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
1063 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
1064 switch (menu_sel) {\r
979ba09f 1065 case 1: currentConfig.PicoOpt^=0x040; break;\r
1066 case 2: currentConfig.PicoOpt^=0x080; break;\r
1067 case 3: currentConfig.EmuOpt ^=0x002; break;\r
1068 case 5: currentConfig.EmuOpt ^=0x004; break;\r
1069 case 7: currentConfig.PicoOpt^=0x200; break;\r
1070 case 8: currentConfig.PicoOpt^=0x020; break;\r
1071 case 10: currentConfig.EmuOpt ^=0x001; break;\r
1072 case 11: currentConfig.EmuOpt ^=0x200; break;\r
daf91588 1073 case 14: cd_menu_loop_options();\r
1074 if (engineState == PGS_ReloadRom)\r
1075 return 0; // test BIOS\r
1076 break;\r
1077 case 15: amenu_loop_options(); break;\r
1078 case 16: // done (update and write)\r
720ee7f6 1079 menu_options_save();\r
daf91588 1080 if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");\r
1081 else strcpy(menuErrorMsg, "failed to write config");\r
1082 return 1;\r
1083 case 17: // done (update and write for current game)\r
720ee7f6 1084 menu_options_save();\r
daf91588 1085 if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");\r
1086 else strcpy(menuErrorMsg, "failed to write config");\r
1087 return 1;\r
720ee7f6 1088 }\r
1089 }\r
979ba09f 1090 if(inp & (GP2X_X|GP2X_A)) {\r
720ee7f6 1091 menu_options_save();\r
daf91588 1092 return 0; // done (update, no write)\r
720ee7f6 1093 }\r
1094 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
1095 switch (menu_sel) {\r
1096 case 0:\r
1097 if (inp & GP2X_LEFT) {\r
979ba09f 1098 if ( currentConfig.PicoOpt&0x10) currentConfig.PicoOpt&= ~0x10;\r
1099 else if (!(currentConfig.EmuOpt &0x80))currentConfig.EmuOpt |= 0x80;\r
1100 else if ( currentConfig.EmuOpt &0x80) break;\r
720ee7f6 1101 } else {\r
979ba09f 1102 if ( currentConfig.PicoOpt&0x10) break;\r
1103 else if (!(currentConfig.EmuOpt &0x80))currentConfig.PicoOpt|= 0x10;\r
1104 else if ( currentConfig.EmuOpt &0x80) currentConfig.EmuOpt &= ~0x80;\r
720ee7f6 1105 }\r
1106 break;\r
1107 case 4:\r
979ba09f 1108 currentConfig.Frameskip += (inp & GP2X_LEFT) ? -1 : 1;\r
1109 if (currentConfig.Frameskip < 0) currentConfig.Frameskip = -1;\r
1110 if (currentConfig.Frameskip > 32) currentConfig.Frameskip = 32;\r
720ee7f6 1111 break;\r
1112 case 6:\r
979ba09f 1113 if ((inp & GP2X_RIGHT) && currentConfig.PsndRate == 44100 && !(currentConfig.PicoOpt&0x08)) {\r
1114 currentConfig.PsndRate = 8000; currentConfig.PicoOpt|= 0x08;\r
1115 } else if ((inp & GP2X_LEFT) && currentConfig.PsndRate == 8000 && (currentConfig.PicoOpt&0x08)) {\r
1116 currentConfig.PsndRate = 44100; currentConfig.PicoOpt&=~0x08;\r
1117 } else currentConfig.PsndRate = sndrate_prevnext(currentConfig.PsndRate, inp & GP2X_RIGHT);\r
720ee7f6 1118 break;\r
1119 case 9:\r
979ba09f 1120 region_prevnext(inp & GP2X_RIGHT);\r
720ee7f6 1121 break;\r
1122 case 12:\r
1123 if (inp & GP2X_RIGHT) {\r
1124 state_slot++; if (state_slot > 9) state_slot = 0;\r
1125 } else {state_slot--; if (state_slot < 0) state_slot = 9;\r
1126 }\r
1127 break;\r
1128 case 13:\r
1129 while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
979ba09f 1130 currentConfig.CPUclock += (inp & GP2X_LEFT) ? -1 : 1;\r
1131 if (currentConfig.CPUclock < 1) currentConfig.CPUclock = 1;\r
720ee7f6 1132 draw_menu_options(menu_sel);\r
1133 usleep(50*1000);\r
1134 }\r
1135 break;\r
1136 }\r
1137 }\r
1138 }\r
1139}\r
1140\r
1141// -------------- credits --------------\r
1142\r
1143static void draw_menu_credits(void)\r
1144{\r
1145 int tl_x = 15, tl_y = 70, y;\r
e5d315a5 1146 //memset(gp2x_screen, 0, 320*240);\r
1147 gp2x_pd_clone_buffer2();\r
720ee7f6 1148\r
5111820c 1149 gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");\r
720ee7f6 1150 y = tl_y;\r
1151 gp2x_text_out8(tl_x, y, "Credits:");\r
1152 gp2x_text_out8(tl_x, (y+=10), "Dave: Cyclone 68000 core,");\r
1153 gp2x_text_out8(tl_x, (y+=10), " base code of PicoDrive");\r
1154 gp2x_text_out8(tl_x, (y+=10), "Reesy & FluBBa: DrZ80 core");\r
1155 gp2x_text_out8(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");\r
1156 gp2x_text_out8(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");\r
1157 gp2x_text_out8(tl_x, (y+=10), "Stephane Dallongeville:");\r
1158 gp2x_text_out8(tl_x, (y+=10), " opensource Gens");\r
1159 gp2x_text_out8(tl_x, (y+=10), "Haze: Genesis hw info");\r
1160 gp2x_text_out8(tl_x, (y+=10), "rlyeh and others: minimal SDK");\r
1161 gp2x_text_out8(tl_x, (y+=10), "Squidge: squidgehack");\r
1162 gp2x_text_out8(tl_x, (y+=10), "Dzz: ARM940 sample");\r
1163 gp2x_text_out8(tl_x, (y+=10), "GnoStiC / Puck2099: USB joystick");\r
1164 gp2x_text_out8(tl_x, (y+=10), "craigix: GP2X hardware");\r
1165\r
e5d315a5 1166 gp2x_video_flip2();\r
720ee7f6 1167}\r
1168\r
1169\r
1170// -------------- root menu --------------\r
1171\r
1172static void draw_menu_root(int menu_sel)\r
1173{\r
1174 int tl_x = 70, tl_y = 70, y;\r
e5d315a5 1175 //memset(gp2x_screen, 0, 320*240);\r
1176 gp2x_pd_clone_buffer2();\r
720ee7f6 1177\r
1178 gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION);\r
1179\r
1180 y = tl_y;\r
1181 if (rom_data) {\r
1182 gp2x_text_out8(tl_x, y, "Resume game");\r
1183 gp2x_text_out8(tl_x, (y+=10), "Save State");\r
1184 gp2x_text_out8(tl_x, (y+=10), "Load State");\r
1185 gp2x_text_out8(tl_x, (y+=10), "Reset game");\r
1186 } else {\r
1187 y += 30;\r
1188 }\r
8dfb9fd5 1189 gp2x_text_out8(tl_x, (y+=10), "Load new ROM/ISO");\r
720ee7f6 1190 gp2x_text_out8(tl_x, (y+=10), "Change options");\r
1191 gp2x_text_out8(tl_x, (y+=10), "Configure controls");\r
1192 gp2x_text_out8(tl_x, (y+=10), "Credits");\r
1193 gp2x_text_out8(tl_x, (y+=10), "Exit");\r
70d2ecc5 1194 if (PicoPatches)\r
1195 gp2x_text_out8(tl_x, (y+=10), "Patches / GameGenie");\r
720ee7f6 1196\r
1197 // draw cursor\r
1198 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
1199 // error\r
1200 if (menuErrorMsg[0]) gp2x_text_out8(5, 226, menuErrorMsg);\r
e5d315a5 1201 gp2x_video_flip2();\r
720ee7f6 1202}\r
1203\r
1204\r
1205static void menu_loop_root(void)\r
1206{\r
daf91588 1207 int ret, menu_sel = 4, menu_sel_max = 8, menu_sel_min = 4;\r
720ee7f6 1208 unsigned long inp = 0;\r
1209 char curr_path[PATH_MAX], *selfname;\r
1210 FILE *tstf;\r
1211\r
1212 if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )\r
1213 {\r
1214 fclose(tstf);\r
1215 strcpy(curr_path, currentConfig.lastRomFile);\r
1216 }\r
1217 else\r
1218 {\r
1219 getcwd(curr_path, PATH_MAX);\r
1220 }\r
1221\r
1222 if (rom_data) menu_sel = menu_sel_min = 0;\r
70d2ecc5 1223 if (PicoPatches) menu_sel_max = 9;\r
720ee7f6 1224\r
1225 for(;;)\r
1226 {\r
1227 draw_menu_root(menu_sel);\r
1228 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X|GP2X_SELECT);\r
1229 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < menu_sel_min) menu_sel = menu_sel_max; }\r
1230 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = menu_sel_min; }\r
1231 if(inp &(GP2X_SELECT|GP2X_X)){\r
1232 if (rom_data) {\r
1233 while (gp2x_joystick_read(1) & (GP2X_SELECT|GP2X_X)) usleep(50*1000); // wait until select is released\r
1234 engineState = PGS_Running;\r
1235 break;\r
1236 }\r
1237 }\r
1238 if(inp & GP2X_B ) {\r
1239 switch (menu_sel) {\r
1240 case 0: // resume game\r
1241 if (rom_data) { engineState = PGS_Running; return; }\r
1242 break;\r
1243 case 1: // save state\r
1244 if (rom_data) {\r
4ffd2858 1245 if(savestate_menu_loop(0))\r
720ee7f6 1246 continue;\r
720ee7f6 1247 engineState = PGS_Running;\r
1248 return;\r
1249 }\r
1250 break;\r
1251 case 2: // load state\r
1252 if (rom_data) {\r
4ffd2858 1253 if(savestate_menu_loop(1))\r
720ee7f6 1254 continue;\r
720ee7f6 1255 engineState = PGS_Running;\r
1256 return;\r
1257 }\r
1258 break;\r
1259 case 3: // reset game\r
1260 if (rom_data) {\r
1261 emu_ResetGame();\r
1262 engineState = PGS_Running;\r
1263 return;\r
1264 }\r
1265 break;\r
1266 case 4: // select rom\r
1267 selfname = romsel_loop(curr_path);\r
1268 if (selfname) {\r
1269 printf("selected file: %s\n", selfname);\r
720ee7f6 1270 engineState = PGS_ReloadRom;\r
1271 }\r
1272 return;\r
1273 case 5: // options\r
daf91588 1274 ret = menu_loop_options();\r
1275 if (ret == 1) continue; // status update\r
1276 if (engineState == PGS_ReloadRom)\r
1277 return; // BIOS test\r
720ee7f6 1278 break;\r
1279 case 6: // controls\r
1280 kc_sel_loop();\r
1281 break;\r
1282 case 7: // credits\r
1283 draw_menu_credits();\r
1284 usleep(500*1000);\r
1285 inp = wait_for_input(GP2X_B|GP2X_X);\r
1286 break;\r
1287 case 8: // exit\r
1288 engineState = PGS_Quit;\r
1289 return;\r
70d2ecc5 1290 case 9: // patches/gg\r
1291 if (rom_data && PicoPatches) {\r
1292 patches_menu_loop();\r
1293 PicoPatchApply();\r
1294 strcpy(menuErrorMsg, "Patches applied");\r
1295 continue;\r
1296 }\r
1297 break;\r
720ee7f6 1298 }\r
1299 }\r
1300 menuErrorMsg[0] = 0; // clear error msg\r
1301 }\r
1302}\r
1303\r
1304\r
4ffd2858 1305static void menu_prepare_bg(void)\r
720ee7f6 1306{\r
e5d315a5 1307 extern int localPal[0x100];\r
4ffd2858 1308 int c, i;\r
e5d315a5 1309\r
1310 // don't clear old palette just for fun (but make it dark)\r
4ffd2858 1311 for (i = 0x100-1; i >= 0; i--) {\r
1312 c = localPal[i];\r
1313 localPal[i] = ((c >> 1) & 0x007f7f7f) - ((c >> 3) & 0x001f1f1f);\r
1314 }\r
e5d315a5 1315 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
1316 localPal[0xf0] = 0x00ffffff;\r
720ee7f6 1317\r
4ffd2858 1318 gp2x_video_setpalette(localPal, 0x100);\r
1319}\r
1320\r
1321static void menu_gfx_prepare(void)\r
1322{\r
1323 menu_prepare_bg();\r
1324\r
720ee7f6 1325 // switch to 8bpp\r
e5d315a5 1326 gp2x_video_changemode2(8);\r
720ee7f6 1327 gp2x_video_RGB_setscaling(320, 240);\r
e5d315a5 1328 gp2x_video_flip2();\r
1329}\r
1330\r
1331\r
1332void menu_loop(void)\r
1333{\r
1334 menu_gfx_prepare();\r
720ee7f6 1335\r
1336 menu_loop_root();\r
1337\r
1338 menuErrorMsg[0] = 0;\r
1339}\r