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