1 // (c) Copyright 2006,2007 notaz, All rights reserved.
\r
2 // Free for non-commercial use.
\r
4 // For commercial use, separate licencing terms must be obtained.
\r
13 #include "minimal.h"
\r
15 #include "asmutils.h"
\r
21 #include "../../input.h"
\r
22 #include "../../state.h"
\r
24 #ifndef _DIRENT_HAVE_D_TYPE
\r
25 #error "need d_type for file browser
\r
28 static int GP2X_PORT_REV =
\r
32 extern char lastLoadedGameName[PATH_MAX];
\r
33 extern int mmuhack_status;
\r
34 extern int soundvol;
\r
35 extern uint8 Exit; // exit emu loop flag
\r
36 extern int InitSound(void);
\r
38 #define CONFIGURABLE_KEYS (GP2X_UP|GP2X_LEFT|GP2X_DOWN|GP2X_RIGHT|GP2X_START|GP2X_SELECT|GP2X_L|GP2X_R|GP2X_A|GP2X_B|GP2X_X|GP2X_Y|GP2X_PUSH)
\r
40 static char *gp2xKeyNames[] = {
\r
41 "UP", "01???", "LEFT", "03???", "DOWN", "05???", "RIGHT", "07???",
\r
42 "START", "SELECT", "L", "R", "A", "B", "X", "Y",
\r
43 "10???", "11???", "12???","13???", "14???","15???", "VOL DOWN", "VOL UP",
\r
44 "18???", "19???", "1a???","PUSH", "1c???","1d???", "1e???", "1f???"
\r
47 char menuErrorMsg[40] = {0, };
\r
50 void gp2x_fceu_copy_bg(void)
\r
52 memset(gp2x_screen, 0, 320*240*2);
\r
55 // draws white text to current bbp15 screen
\r
56 static void gp2x_text_out15_(int x, int y, const char *text)
\r
59 unsigned short *screen = gp2x_screen;
\r
61 screen = screen + x + y*320;
\r
63 for (i = 0; i < strlen(text); i++)
\r
67 if(fontdata8x8[((text[i])*8)+l]&0x80) screen[l*320+0]=0xffff;
\r
68 if(fontdata8x8[((text[i])*8)+l]&0x40) screen[l*320+1]=0xffff;
\r
69 if(fontdata8x8[((text[i])*8)+l]&0x20) screen[l*320+2]=0xffff;
\r
70 if(fontdata8x8[((text[i])*8)+l]&0x10) screen[l*320+3]=0xffff;
\r
71 if(fontdata8x8[((text[i])*8)+l]&0x08) screen[l*320+4]=0xffff;
\r
72 if(fontdata8x8[((text[i])*8)+l]&0x04) screen[l*320+5]=0xffff;
\r
73 if(fontdata8x8[((text[i])*8)+l]&0x02) screen[l*320+6]=0xffff;
\r
74 if(fontdata8x8[((text[i])*8)+l]&0x01) screen[l*320+7]=0xffff;
\r
80 void gp2x_text_out15(int x, int y, const char *texto, ...)
\r
85 va_start(args,texto);
\r
86 vsprintf(buffer,texto,args);
\r
89 gp2x_text_out15_(x,y,buffer);
\r
93 void gp2x_text_out15_lim(int x, int y, const char *texto, int max)
\r
95 char buffer[320/8+1];
\r
97 strncpy(buffer, texto, 320/8);
\r
98 if (max > 320/8) max = 320/8;
\r
99 if (max < 0) max = 0;
\r
102 gp2x_text_out15(x,y,buffer);
\r
105 static void gp2x_smalltext16(int x, int y, const char *texto)
\r
108 unsigned char *src;
\r
109 unsigned short *dst;
\r
111 for (i = 0;; i++, x += 6)
\r
113 unsigned char c = (unsigned char) texto[i];
\r
118 src = fontdata6x8[c];
\r
119 dst = (unsigned short *)gp2x_screen + x + y*320;
\r
126 if( *src & w ) *dst = 0xffff;
\r
137 static void gp2x_smalltext8_lim(int x, int y, const char *texto, int max)
\r
139 char buffer[320/6+1];
\r
141 strncpy(buffer, texto, 320/6);
\r
142 if (max > 320/6) max = 320/6;
\r
143 if (max < 0) max = 0;
\r
146 gp2x_smalltext16(x, y, buffer);
\r
150 static unsigned long inp_prev = 0;
\r
151 static int inp_prevjoy = 0;
\r
153 static unsigned long wait_for_input(unsigned long interesting)
\r
156 static int repeats = 0, wait = 50*1000;
\r
157 int release = 0, i;
\r
159 if (repeats == 2 || repeats == 4) wait /= 2;
\r
160 if (repeats == 6) wait = 15 * 1000;
\r
162 for (i = 0; i < 6 && inp_prev == gp2x_joystick_read(1); i++) {
\r
163 if (i == 0) repeats++;
\r
164 if (wait >= 30*1000) usleep(wait); // usleep sleeps for ~30ms minimum
\r
165 else spend_cycles(wait * Settings.cpuclock);
\r
168 while ( !((ret = gp2x_joystick_read(1)) & interesting) ) {
\r
173 if (release || ret != inp_prev) {
\r
180 // we don't need diagonals in menus
\r
181 if ((ret&GP2X_UP) && (ret&GP2X_LEFT)) ret &= ~GP2X_LEFT;
\r
182 if ((ret&GP2X_UP) && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;
\r
183 if ((ret&GP2X_DOWN) && (ret&GP2X_LEFT)) ret &= ~GP2X_LEFT;
\r
184 if ((ret&GP2X_DOWN) && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;
\r
189 static unsigned long input2_read(unsigned long interesting, int *joy)
\r
197 if ((ret = gp2x_joystick_read(0) & interesting)) break;
\r
198 gp2x_usbjoy_update();
\r
199 for (i = 0; i < num_of_joys; i++) {
\r
200 ret = gp2x_usbjoy_check2(i);
\r
201 if (ret) { *joy = i + 1; break; }
\r
210 // similar to wait_for_input(), but returns joy num
\r
211 static unsigned long wait_for_input_usbjoy(unsigned long interesting, int *joy)
\r
214 const int wait = 300*1000;
\r
217 if (inp_prevjoy == 0) inp_prev &= interesting;
\r
218 for (i = 0; i < 6; i++) {
\r
219 ret = input2_read(interesting, joy);
\r
220 if (*joy != inp_prevjoy || ret != inp_prev) break;
\r
224 while ( !(ret = input2_read(interesting, joy)) ) {
\r
229 inp_prevjoy = *joy;
\r
236 // -------------- ROM selector --------------
\r
238 static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
\r
243 n--; // exclude current dir (".")
\r
245 //memset(gp2x_screen, 0, 320*240);
\r
246 gp2x_fceu_copy_bg();
\r
249 gp2x_smalltext8_lim(14, (start - 2)*10, curdir, 53-2);
\r
250 for (i = 0; i < n; i++) {
\r
252 if (pos < 0) continue;
\r
253 if (pos > 23) break;
\r
254 if (namelist[i+1]->d_type == DT_DIR) {
\r
255 gp2x_smalltext8_lim(14, pos*10, "/", 1);
\r
256 gp2x_smalltext8_lim(14+6, pos*10, namelist[i+1]->d_name, 53-3);
\r
258 gp2x_smalltext8_lim(14, pos*10, namelist[i+1]->d_name, 53-2);
\r
261 gp2x_text_out15(5, 120, ">");
\r
265 static int scandir_cmp(const void *p1, const void *p2)
\r
267 struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2;
\r
268 if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2);
\r
269 if ((*d1)->d_type == DT_DIR) return -1; // put before
\r
270 if ((*d2)->d_type == DT_DIR) return 1;
\r
271 return alphasort(d1, d2);
\r
274 static char *filter_exts[] = {
\r
275 ".gpe", ".png", "ck.o", ".txt"
\r
278 static int scandir_filter(const struct dirent *ent)
\r
283 if (ent == NULL || ent->d_name == NULL) return 0;
\r
284 if (strlen(ent->d_name) < 5) return 1;
\r
286 p = ent->d_name + strlen(ent->d_name) - 4;
\r
288 for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)
\r
290 if (strcmp(p, filter_exts[i]) == 0) return 0;
\r
296 static char *romsel_loop(char *curr_path)
\r
298 struct dirent **namelist;
\r
301 unsigned long inp = 0;
\r
302 char *ret = NULL, *fname = NULL;
\r
304 // is this a dir or a full path?
\r
305 if ((dir = opendir(curr_path))) {
\r
309 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);
\r
314 n = scandir(curr_path, &namelist, scandir_filter, scandir_cmp);
\r
317 n = scandir("/", &namelist, scandir_filter, scandir_cmp);
\r
320 printf("dir: %s\n", curr_path);
\r
327 if (fname != NULL) {
\r
329 for (i = 1; i < n; i++) {
\r
330 if (strcmp(namelist[i]->d_name, fname) == 0) {
\r
339 draw_dirlist(curr_path, namelist, n, sel);
\r
340 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);
\r
341 if(inp & GP2X_UP ) { sel--; if (sel < 0) sel = n-2; }
\r
342 if(inp & GP2X_DOWN) { sel++; if (sel > n-2) sel = 0; }
\r
343 if(inp & GP2X_LEFT) { sel-=10; if (sel < 0) sel = 0; }
\r
344 if(inp & GP2X_L) { sel-=24; if (sel < 0) sel = 0; }
\r
345 if(inp & GP2X_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
\r
346 if(inp & GP2X_R) { sel+=24; if (sel > n-2) sel = n-2; }
\r
347 if(inp & GP2X_B) { // enter dir/select
\r
349 if (namelist[sel+1]->d_type == DT_REG) {
\r
350 strcpy(lastLoadedGameName, curr_path);
\r
351 strcat(lastLoadedGameName, "/");
\r
352 strcat(lastLoadedGameName, namelist[sel+1]->d_name);
\r
353 ret = lastLoadedGameName;
\r
355 } else if (namelist[sel+1]->d_type == DT_DIR) {
\r
356 int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
\r
357 char *p, *newdir = malloc(newlen);
\r
358 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
\r
359 char *start = curr_path;
\r
360 p = start + strlen(start) - 1;
\r
361 while (*p == '/' && p > start) p--;
\r
362 while (*p != '/' && p > start) p--;
\r
363 if (p <= start) strcpy(newdir, "/");
\r
364 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
\r
366 strcpy(newdir, curr_path);
\r
367 p = newdir + strlen(newdir) - 1;
\r
368 while (*p == '/' && p >= newdir) *p-- = 0;
\r
369 strcat(newdir, "/");
\r
370 strcat(newdir, namelist[sel+1]->d_name);
\r
372 ret = romsel_loop(newdir);
\r
376 // unknown file type, happens on NTFS mounts. Try to guess.
\r
377 FILE *tstf; int tmp;
\r
378 strcpy(lastLoadedGameName, curr_path);
\r
379 strcat(lastLoadedGameName, "/");
\r
380 strcat(lastLoadedGameName, namelist[sel+1]->d_name);
\r
381 tstf = fopen(lastLoadedGameName, "rb");
\r
384 if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)
\r
385 namelist[sel+1]->d_type = DT_REG;
\r
386 else namelist[sel+1]->d_type = DT_DIR;
\r
392 if(inp & GP2X_X) break; // cancel
\r
396 while(n--) free(namelist[n]);
\r
403 // ------------ patch/gg menu ------------
\r
406 static void draw_patchlist(int sel)
\r
412 gp2x_fceu_copy_bg();
\r
414 for (i = 0; i < PicoPatchCount; i++) {
\r
416 if (pos < 0) continue;
\r
417 if (pos > 23) break;
\r
418 gp2x_smalltext8_lim(14, pos*10, PicoPatches[i].active ? "ON " : "OFF", 3);
\r
419 gp2x_smalltext8_lim(14+6*4, pos*10, PicoPatches[i].name, 53-6);
\r
422 if (pos < 24) gp2x_smalltext8_lim(14, pos*10, "done", 4);
\r
424 gp2x_text_out15(5, 120, ">");
\r
429 void patches_menu_loop(void)
\r
432 unsigned long inp = 0;
\r
436 draw_patchlist(menu_sel);
\r
437 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);
\r
438 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }
\r
439 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }
\r
440 if(inp &(GP2X_LEFT|GP2X_L)) { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }
\r
441 if(inp &(GP2X_RIGHT|GP2X_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }
\r
442 if(inp & GP2X_B) { // action
\r
443 if (menu_sel < PicoPatchCount)
\r
444 PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;
\r
447 if(inp & GP2X_X) return;
\r
452 // ------------ savestate loader ------------
\r
454 static void menu_prepare_bg(void);
\r
456 static int state_slot_flags = 0;
\r
458 static void state_check_slots(void)
\r
462 state_slot_flags = 0;
\r
464 for (slot = 0; slot < 10; slot++)
\r
466 if (emu_check_save_file(slot))
\r
468 state_slot_flags |= 1 << slot;
\r
473 static void draw_savestate_bg(int slot)
\r
475 struct PicoVideo tmp_pv;
\r
476 unsigned short tmp_cram[0x40];
\r
477 unsigned short tmp_vsram[0x40];
\r
478 void *tmp_vram, *file;
\r
481 fname = emu_GetSaveFName(1, 0, slot);
\r
482 if (!fname) return;
\r
484 tmp_vram = malloc(sizeof(Pico.vram));
\r
485 if (tmp_vram == NULL) return;
\r
487 memcpy(tmp_vram, Pico.vram, sizeof(Pico.vram));
\r
488 memcpy(tmp_cram, Pico.cram, sizeof(Pico.cram));
\r
489 memcpy(tmp_vsram, Pico.vsram, sizeof(Pico.vsram));
\r
490 memcpy(&tmp_pv, &Pico.video, sizeof(Pico.video));
\r
492 if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {
\r
493 file = gzopen(fname, "rb");
\r
494 emu_set_save_cbs(1);
\r
496 file = fopen(fname, "rb");
\r
497 emu_set_save_cbs(0);
\r
502 PicoCdLoadStateGfx(file);
\r
504 areaSeek(file, 0x10020, SEEK_SET); // skip header and RAM in state file
\r
505 areaRead(Pico.vram, 1, sizeof(Pico.vram), file);
\r
506 areaSeek(file, 0x2000, SEEK_CUR);
\r
507 areaRead(Pico.cram, 1, sizeof(Pico.cram), file);
\r
508 areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);
\r
509 areaSeek(file, 0x221a0, SEEK_SET);
\r
510 areaRead(&Pico.video, 1, sizeof(Pico.video), file);
\r
515 emu_forced_frame();
\r
516 gp2x_memcpy_buffers((1<<2), gp2x_screen, 0, 320*240*2);
\r
519 memcpy(Pico.vram, tmp_vram, sizeof(Pico.vram));
\r
520 memcpy(Pico.cram, tmp_cram, sizeof(Pico.cram));
\r
521 memcpy(Pico.vsram, tmp_vsram, sizeof(Pico.vsram));
\r
522 memcpy(&Pico.video, &tmp_pv, sizeof(Pico.video));
\r
526 static void draw_savestate_menu(int menu_sel, int is_loading)
\r
528 int tl_x = 25, tl_y = 60, y, i;
\r
530 if (state_slot_flags & (1 << menu_sel))
\r
531 draw_savestate_bg(menu_sel);
\r
532 gp2x_fceu_copy_bg();
\r
534 gp2x_text_out15(tl_x, 30, is_loading ? "Load state" : "Save state");
\r
536 /* draw all 10 slots */
\r
538 for (i = 0; i < 10; i++, y+=10)
\r
540 gp2x_text_out15(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");
\r
542 gp2x_text_out15(tl_x, y, "back");
\r
545 gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");
\r
550 static int savestate_menu_loop(int is_loading)
\r
552 int menu_sel = 10, menu_sel_max = 10;
\r
553 unsigned long inp = 0;
\r
555 state_check_slots();
\r
559 draw_savestate_menu(menu_sel, is_loading);
\r
560 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);
\r
561 if(inp & GP2X_UP ) {
\r
563 menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;
\r
564 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
\r
566 if(inp & GP2X_DOWN) {
\r
568 menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;
\r
569 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
\r
571 if(inp & GP2X_B) { // save/load
\r
572 if (menu_sel < 10) {
\r
573 state_slot = menu_sel;
\r
574 if (emu_SaveLoadGame(is_loading, 0)) {
\r
575 strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");
\r
581 if(inp & GP2X_X) return 1;
\r
586 // -------------- key config --------------
\r
588 static char *usb_joy_key_name(int joy, int num)
\r
590 static char name[16];
\r
593 case 0: sprintf(name, "Joy%i UP", joy); break;
\r
594 case 1: sprintf(name, "Joy%i DOWN", joy); break;
\r
595 case 2: sprintf(name, "Joy%i LEFT", joy); break;
\r
596 case 3: sprintf(name, "Joy%i RIGHT", joy); break;
\r
597 default:sprintf(name, "Joy%i b%i", joy, num-3); break;
\r
602 static char *action_binds(int player_idx, int action_mask)
\r
604 static char strkeys[32*5];
\r
608 for (i = 0; i < 32; i++) // i is key index
\r
610 if (Settings.KeyBinds[i] & action_mask)
\r
612 if (player_idx >= 0 && ((Settings.KeyBinds[i] >> 16) & 3) != player_idx) continue;
\r
613 if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, gp2xKeyNames[i]); break; }
\r
614 else strcpy(strkeys, gp2xKeyNames[i]);
\r
617 for (joy = 0; joy < num_of_joys; joy++)
\r
619 for (i = 0; i < 32; i++)
\r
621 if (Settings.JoyBinds[joy][i] & action_mask)
\r
623 if (player_idx >= 0 && ((Settings.KeyBinds[i] >> 16) & 3) != player_idx) continue;
\r
625 strcat(strkeys, ", "); strcat(strkeys, usb_joy_key_name(joy + 1, i));
\r
628 else strcpy(strkeys, usb_joy_key_name(joy + 1, i));
\r
636 static void unbind_action(int action)
\r
640 for (i = 0; i < 32; i++)
\r
641 Settings.KeyBinds[i] &= ~action;
\r
642 for (u = 0; u < 4; u++)
\r
643 for (i = 0; i < 32; i++)
\r
644 Settings.JoyBinds[u][i] &= ~action;
\r
647 static int count_bound_keys(int action, int is_joy)
\r
649 int i, u, keys = 0;
\r
653 for (u = 0; u < 4; u++)
\r
654 for (i = 0; i < 32; i++)
\r
655 if (Settings.JoyBinds[u][i] & action) keys++;
\r
659 for (i = 0; i < 32; i++)
\r
660 if (Settings.KeyBinds[i] & action) keys++;
\r
665 typedef struct { char *name; int mask; } bind_action_t;
\r
667 static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_idx, int sel)
\r
669 int x, y, tl_y = 40, i;
\r
671 gp2x_fceu_copy_bg();
\r
672 if (player_idx >= 0)
\r
673 gp2x_text_out15(80, 20, "Player %i controls", player_idx + 1);
\r
674 else gp2x_text_out15(80, 20, "Emulator controls");
\r
677 for (i = 0; i < opt_cnt; i++, y+=10)
\r
678 gp2x_text_out15(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));
\r
680 gp2x_text_out15(x, y, "Done");
\r
683 gp2x_text_out15(x - 16, tl_y + sel*10, ">");
\r
686 gp2x_text_out15(30, 190, "Press a button to bind/unbind");
\r
687 gp2x_text_out15(30, 200, "Use VOL+ to clear");
\r
688 gp2x_text_out15(30, 210, "To bind UP/DOWN, hold VOL-");
\r
689 gp2x_text_out15(30, 220, "Select \"Done\" to exit");
\r
691 gp2x_text_out15(30, 220, "Press B or X to exit");
\r
696 static void key_config_loop(const bind_action_t *opts, int opt_cnt, int player_idx)
\r
698 int joy = 0, sel = 0, menu_sel_max = opt_cnt, i;
\r
699 unsigned long inp = 0;
\r
703 draw_key_config(opts, opt_cnt, player_idx, sel);
\r
704 inp = wait_for_input_usbjoy(CONFIGURABLE_KEYS|GP2X_VOL_DOWN|GP2X_VOL_UP, &joy);
\r
705 // printf("got %08lX from joy %i\n", inp, joy);
\r
707 if (!(inp & GP2X_VOL_DOWN)) {
\r
708 if(inp & GP2X_UP ) { sel--; if (sel < 0) sel = menu_sel_max; continue; }
\r
709 if(inp & GP2X_DOWN) { sel++; if (sel > menu_sel_max) sel = 0; continue; }
\r
711 if (sel >= opt_cnt) {
\r
712 if (inp & (GP2X_B|GP2X_X)) break;
\r
715 // if we are here, we want to bind/unbind something
\r
716 if (inp & GP2X_VOL_UP)
\r
717 unbind_action(opts[sel].mask);
\r
718 inp &= CONFIGURABLE_KEYS;
\r
719 for (i = 0; i < 32; i++)
\r
720 if (inp & (1 << i)) {
\r
721 if (count_bound_keys(opts[sel].mask, 0) >= 2)
\r
722 Settings.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only
\r
723 else Settings.KeyBinds[i] ^= opts[sel].mask;
\r
724 if (player_idx >= 0) {
\r
725 Settings.KeyBinds[i] &= ~(3 << 16);
\r
726 Settings.KeyBinds[i] |= player_idx << 16;
\r
729 } else if (sel < opt_cnt) {
\r
730 for (i = 0; i < 32; i++)
\r
731 if (inp & (1 << i)) {
\r
732 if (count_bound_keys(opts[sel].mask, 1) >= 1) // disallow combos for usbjoy
\r
733 Settings.JoyBinds[joy-1][i] &= ~opts[sel].mask;
\r
734 else Settings.JoyBinds[joy-1][i] ^= opts[sel].mask;
\r
735 if (player_idx >= 0) {
\r
736 Settings.JoyBinds[joy-1][i] &= ~(3 << 16);
\r
737 Settings.JoyBinds[joy-1][i] |= player_idx << 16;
\r
744 static void draw_kc_sel(int menu_sel)
\r
746 int tl_x = 25+40, tl_y = 60, y, i;
\r
750 gp2x_fceu_copy_bg();
\r
751 gp2x_text_out15(tl_x, y, "Player 1");
\r
752 gp2x_text_out15(tl_x, (y+=10), "Player 2");
\r
753 gp2x_text_out15(tl_x, (y+=10), "Emulator controls");
\r
754 gp2x_text_out15(tl_x, (y+=10), "Done");
\r
757 gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");
\r
760 gp2x_text_out15(tl_x, (y=110), "USB joys detected:");
\r
761 if (num_of_joys > 0) {
\r
762 for (i = 0; i < num_of_joys; i++) {
\r
763 strncpy(joyname, joy_name(joys[i]), 33); joyname[33] = 0;
\r
764 gp2x_text_out15(tl_x, (y+=10), "%i: %s", i+1, joyname);
\r
767 gp2x_text_out15(tl_x, (y+=10), "none");
\r
773 // b_turbo,a_turbo RLDU SEBA
\r
774 static bind_action_t ctrl_actions[] =
\r
777 { "DOWN ", 0x020 },
\r
778 { "LEFT ", 0x040 },
\r
779 { "RIGHT ", 0x080 },
\r
782 { "A TURBO", 0x100 },
\r
783 { "B TURBO", 0x200 },
\r
784 { "START ", 0x008 },
\r
785 { "SELECT ", 0x004 },
\r
788 static bind_action_t emuctrl_actions[] =
\r
790 { "Save State ", 1<<31 },
\r
791 { "Load State ", 1<<30 },
\r
794 static void kc_sel_loop(void)
\r
796 int menu_sel = 3, menu_sel_max = 3;
\r
797 unsigned long inp = 0;
\r
801 draw_kc_sel(menu_sel);
\r
802 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);
\r
803 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
\r
804 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
\r
806 switch (menu_sel) {
\r
807 case 0: key_config_loop(ctrl_actions, 10, 0); return;
\r
808 case 1: key_config_loop(ctrl_actions, 8, 1); return;
\r
809 case 2: key_config_loop(emuctrl_actions,
\r
810 sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]), -1); return;
\r
814 if(inp & GP2X_X) return;
\r
819 // --------- FCEU options ----------
\r
821 static void draw_fcemenu_options(int menu_sel)
\r
823 int tl_x = 25, tl_y = 60, y;
\r
826 gp2x_fceu_copy_bg();
\r
828 gp2x_text_out15(tl_x, y, " %s", "OFF"); // 0
\r
829 gp2x_text_out15(tl_x, (y+=10), "Done");
\r
832 gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");
\r
837 static void fcemenu_loop_options(void)
\r
839 int menu_sel = 0, menu_sel_max = 1;
\r
840 unsigned long inp = 0;
\r
844 draw_fcemenu_options(menu_sel);
\r
845 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);
\r
846 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
\r
847 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
\r
848 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options
\r
849 switch (menu_sel) {
\r
854 if(inp & (GP2X_X|GP2X_A)) return;
\r
855 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise
\r
856 switch (menu_sel) {
\r
864 // -------------- options --------------
\r
866 static void draw_menu_options(int menu_sel)
\r
868 int tl_x = 25, tl_y = 32, y;
\r
869 char strframeskip[8], *strscaling, *strssconfirm;
\r
870 char *mms = mmuhack_status ? "active) " : "inactive)";
\r
872 if (Settings.frameskip < 0)
\r
873 strcpy(strframeskip, "Auto");
\r
874 else sprintf(strframeskip, "%i", Settings.frameskip);
\r
875 switch (Settings.scaling) {
\r
876 default: strscaling = " OFF"; break;
\r
877 case 1: strscaling = "hw horizontal"; break;
\r
878 case 2: strscaling = "hw horiz. + vert."; break;
\r
879 case 3: strscaling = "sw horizontal"; break;
\r
881 switch (Settings.sstate_confirm) {
\r
882 default: strssconfirm = "OFF"; break;
\r
883 case 1: strssconfirm = "writes"; break;
\r
884 case 2: strssconfirm = "loads"; break;
\r
885 case 3: strssconfirm = "both"; break;
\r
889 gp2x_fceu_copy_bg();
\r
891 gp2x_text_out15(tl_x, y, "Scaling: %s", strscaling); // 0
\r
892 gp2x_text_out15(tl_x, (y+=10), "Show FPS %s", Settings.showfps?"ON":"OFF"); // 1
\r
893 gp2x_text_out15(tl_x, (y+=10), "Frameskip %s", strframeskip); // 2
\r
894 gp2x_text_out15(tl_x, (y+=10), "Enable sound %s", soundvol?"ON":"OFF");
\r
895 gp2x_text_out15(tl_x, (y+=10), "Sound Rate: %5iHz", Settings.sound_rate); // 4
\r
896 gp2x_text_out15(tl_x, (y+=10), "Force Region: %s",
\r
897 Settings.region_force == 2 ? "PAL" : Settings.region_force == 1 ? "NTSC" : "OFF"); // 5
\r
898 gp2x_text_out15(tl_x, (y+=10), "Use SRAM savestates %s", "OFF");
\r
899 gp2x_text_out15(tl_x, (y+=10), "Turbo rate %iHz", (Settings.turbo_rate_add*60/2) >> 24);
\r
900 gp2x_text_out15(tl_x, (y+=10), "Confirm savestate %s", strssconfirm); // 8
\r
901 gp2x_text_out15(tl_x, (y+=10), "Save slot %i", CurrentState);
\r
902 gp2x_text_out15(tl_x, (y+=10), "Faster RAM timings %s", Settings.ramtimings?"ON":"OFF");
\r
903 gp2x_text_out15(tl_x, (y+=10), "squidgehack (now %s %s", mms, Settings.mmuhack?"ON":"OFF"); // 11
\r
904 gp2x_text_out15(tl_x, (y+=10), "Gamma correction %i.%02i", Settings.gamma / 100, Settings.gamma%100);
\r
905 gp2x_text_out15(tl_x, (y+=10), "GP2X CPU clock %iMhz", Settings.cpuclock); // 13
\r
906 gp2x_text_out15(tl_x, (y+=10), "[FCE Ultra options]");
\r
907 gp2x_text_out15(tl_x, (y+=10), "Save cfg as default");
\r
909 gp2x_text_out15(tl_x, (y+=10), "Save cfg for current game only");
\r
912 gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");
\r
917 static int sndrate_prevnext(int rate, int dir)
\r
919 int i, rates[] = { 8000, 11025, 16000, 22050, 44100 };
\r
921 for (i = 0; i < 5; i++)
\r
922 if (rates[i] == rate) break;
\r
925 if (i > 4) return dir ? 44100 : 22050;
\r
926 if (i < 0) return dir ? 11025 : 8000;
\r
930 static void int_incdec(int *p, int inc, int min, int max)
\r
933 if (*p < min) *p = min;
\r
934 else if (*p > max) *p = max;
\r
937 static void config_commit(void)
\r
939 gp2x_cpuclock_gamma_update();
\r
940 if (Settings.region_force)
\r
941 FCEUI_SetVidSystem(Settings.region_force - 1);
\r
944 static int menu_loop_options(void)
\r
946 static int menu_sel = 0;
\r
947 int menu_sel_max = 15;
\r
948 unsigned long inp = 0;
\r
950 if (fceugi) menu_sel_max++;
\r
954 draw_menu_options(menu_sel);
\r
955 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);
\r
956 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
\r
957 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
\r
958 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options
\r
959 switch (menu_sel) {
\r
960 case 1: Settings.showfps = !Settings.showfps; break;
\r
961 case 3: soundvol = soundvol ? 0 : 100; break;
\r
962 case 10: Settings.ramtimings = !Settings.ramtimings; break;
\r
963 case 11: Settings.mmuhack = !Settings.mmuhack; break;
\r
964 case 14: fcemenu_loop_options(); break;
\r
965 case 15: // done (update and write)
\r
969 case 16: // done (update and write for current game)
\r
971 if (lastLoadedGameName[0])
\r
972 SaveConfig(lastLoadedGameName);
\r
976 if(inp & (GP2X_X|GP2X_A)) {
\r
978 return 0; // done (update, no write)
\r
980 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise
\r
981 switch (menu_sel) {
\r
982 case 0: int_incdec(&Settings.scaling, (inp & GP2X_LEFT) ? -1 : 1, 0, 3); break;
\r
983 case 2: int_incdec(&Settings.frameskip, (inp & GP2X_LEFT) ? -1 : 1, -1, 32); break;
\r
985 Settings.sound_rate = sndrate_prevnext(Settings.sound_rate, inp & GP2X_RIGHT);
\r
988 case 5: int_incdec(&Settings.region_force, (inp & GP2X_LEFT) ? -1 : 1, 0, 2); break;
\r
990 int hz = Settings.turbo_rate_add*60/2 >> 24;
\r
991 int_incdec(&hz, (inp & GP2X_LEFT) ? -1 : 1, 1, 30);
\r
992 Settings.turbo_rate_add = (hz*2 << 24) / 60 + 1;
\r
995 case 8: int_incdec(&Settings.sstate_confirm, (inp & GP2X_LEFT) ? -1 : 1, 0, 3); break;
\r
996 case 9: int_incdec(&CurrentState, (inp & GP2X_LEFT) ? -1 : 1, 0, 9); break;
\r
997 case 12: int_incdec(&Settings.gamma, (inp & GP2X_LEFT) ? -1 : 1, 0, 300); break;
\r
999 while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {
\r
1000 Settings.cpuclock += (inp & GP2X_LEFT) ? -1 : 1;
\r
1001 if (Settings.cpuclock < 0) Settings.cpuclock = 0; // 0 ~ do not change
\r
1002 draw_menu_options(menu_sel);
\r
1011 // -------------- credits --------------
\r
1013 static void draw_menu_credits(void)
\r
1015 //int tl_x = 15, tl_y = 70;
\r
1016 gp2x_fceu_copy_bg();
\r
1020 gp2x_video_flip();
\r
1024 // -------------- root menu --------------
\r
1026 static void draw_menu_root(int menu_sel)
\r
1028 int tl_x = 30, tl_y = 128, y;
\r
1029 gp2x_fceu_copy_bg();
\r
1033 gp2x_text_out15(tl_x, y, "Resume game");
\r
1034 gp2x_text_out15(tl_x, (y+=10), "Save State");
\r
1035 gp2x_text_out15(tl_x, (y+=10), "Load State");
\r
1036 gp2x_text_out15(tl_x, (y+=10), "Reset game");
\r
1040 gp2x_text_out15(tl_x, (y+=10), "Load new ROM");
\r
1041 gp2x_text_out15(tl_x, (y+=10), "Options");
\r
1042 gp2x_text_out15(tl_x, (y+=10), "Controls");
\r
1043 gp2x_text_out15(tl_x, (y+=10), "Credits");
\r
1044 gp2x_text_out15(tl_x, (y+=10), "Exit");
\r
1046 // if (PicoPatches)
\r
1047 // gp2x_text_out15(tl_x, (y+=10), "Patches / GameGenie");
\r
1050 gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");
\r
1052 if (menuErrorMsg[0]) gp2x_text_out15(1, 230, menuErrorMsg);
\r
1055 sprintf(vstr, "v" GP2X_PORT_VERSION " r%i", GP2X_PORT_REV);
\r
1056 gp2x_text_out15(320-strlen(vstr)*8-1, 230, vstr);
\r
1058 gp2x_video_flip();
\r
1062 static int menu_loop_root(void)
\r
1064 int ret, menu_sel_max = 8, menu_sel_min = 4;
\r
1065 static int menu_sel = 4;
\r
1066 unsigned long inp = 0;
\r
1067 char curr_path[PATH_MAX], *selfname;
\r
1070 if ( (tstf = fopen(lastLoadedGameName, "rb")) )
\r
1073 strncpy(curr_path, lastLoadedGameName, sizeof(curr_path));
\r
1074 curr_path[sizeof(curr_path)-1] = 0;
\r
1078 getcwd(curr_path, PATH_MAX);
\r
1081 if (fceugi) menu_sel_min = 0;
\r
1082 // TODO if (PicoPatches) menu_sel_max = 9;
\r
1083 if (menu_sel < menu_sel_min || menu_sel > menu_sel_max)
\r
1084 menu_sel = menu_sel_min;
\r
1086 /* make sure action buttons are not pressed on entering menu */
\r
1087 draw_menu_root(menu_sel);
\r
1088 while (gp2x_joystick_read(1) & (GP2X_B|GP2X_X|GP2X_SELECT)) usleep(50*1000);
\r
1092 draw_menu_root(menu_sel);
\r
1093 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);
\r
1094 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < menu_sel_min) menu_sel = menu_sel_max; }
\r
1095 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = menu_sel_min; }
\r
1096 if(inp &(GP2X_SELECT|GP2X_X)){
\r
1098 while (gp2x_joystick_read(1) & GP2X_X) usleep(50*1000); // wait until X is released
\r
1103 if(inp & GP2X_B ) {
\r
1104 switch (menu_sel) {
\r
1105 case 0: // resume game
\r
1107 while (gp2x_joystick_read(1) & GP2X_B) usleep(50*1000);
\r
1112 case 1: // save state
\r
1114 /*if(savestate_menu_loop(0))
\r
1116 FCEUI_SaveState();
\r
1120 case 2: // load state
\r
1122 /*if(savestate_menu_loop(1))
\r
1124 FCEUI_LoadState();
\r
1128 case 3: // reset game
\r
1130 FCEU_DoSimpleCommand(FCEUNPCMD_RESET);
\r
1135 case 4: // select rom
\r
1136 selfname = romsel_loop(curr_path);
\r
1138 printf("selected file: %s\n", selfname);
\r
1142 case 5: // options
\r
1143 ret = menu_loop_options();
\r
1144 if (ret == 1) continue; // status update
\r
1146 case 6: // controls
\r
1149 case 7: // credits
\r
1150 draw_menu_credits();
\r
1152 inp = wait_for_input(GP2X_B|GP2X_X);
\r
1156 case 9: // patches/gg
\r
1160 menuErrorMsg[0] = 0; // clear error msg
\r
1165 static void menu_prepare_bg(void)
\r
1170 static void menu_gfx_prepare(void)
\r
1172 menu_prepare_bg();
\r
1175 gp2x_video_changemode(16);
\r
1176 gp2x_video_set_offs(0);
\r
1177 gp2x_video_RGB_setscaling(0, 320, 240);
\r
1178 gp2x_video_flip();
\r
1182 int gp2x_menu_do(void)
\r
1186 menu_gfx_prepare();
\r
1188 ret = menu_loop_root();
\r
1190 menuErrorMsg[0] = 0;
\r