menu.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  1. // (c) Copyright 2006,2007 notaz, All rights reserved.
  2. // Free for non-commercial use.
  3. // For commercial use, separate licencing terms must be obtained.
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <stdarg.h>
  8. #include <unistd.h>
  9. #include <dirent.h>
  10. #include "gp2x.h"
  11. #include "emu.h"
  12. #include "menu.h"
  13. #include "usbjoy.h"
  14. #include "../common/emu.h"
  15. #include "../common/menu.h"
  16. #include "../common/arm_utils.h"
  17. #include "../common/readpng.h"
  18. #include "version.h"
  19. #include <Pico/PicoInt.h>
  20. #include <Pico/Patch.h>
  21. #include <zlib/zlib.h>
  22. #ifndef _DIRENT_HAVE_D_TYPE
  23. #error "need d_type for file browser
  24. #endif
  25. extern int mmuhack_status;
  26. static const char *gp2xKeyNames[] = {
  27. "UP", "???", "LEFT", "???", "DOWN", "???", "RIGHT", "???",
  28. "START", "SELECT", "L", "R", "A", "B", "X", "Y",
  29. "???", "???", "???", "???", "???", "???", "VOL DOWN", "VOL UP",
  30. "???", "???", "???", "PUSH", "???", "???", "???", "???"
  31. };
  32. static void menu_darken_bg(void *dst, int pixels, int darker);
  33. static void menu_prepare_bg(int use_game_bg);
  34. static unsigned long inp_prev = 0;
  35. static int inp_prevjoy = 0;
  36. static unsigned long wait_for_input(unsigned long interesting)
  37. {
  38. unsigned long ret;
  39. static int repeats = 0, wait = 50*1000;
  40. int release = 0, i;
  41. if (repeats == 2 || repeats == 4) wait /= 2;
  42. if (repeats == 6) wait = 15 * 1000;
  43. for (i = 0; i < 6 && inp_prev == gp2x_joystick_read(1); i++) {
  44. if (i == 0) repeats++;
  45. if (wait >= 30*1000) usleep(wait); // usleep sleeps for ~30ms minimum
  46. else spend_cycles(wait * currentConfig.CPUclock);
  47. }
  48. while ( !((ret = gp2x_joystick_read(1)) & interesting) ) {
  49. usleep(50000);
  50. release = 1;
  51. }
  52. if (release || ret != inp_prev) {
  53. repeats = 0;
  54. wait = 50*1000;
  55. }
  56. inp_prev = ret;
  57. inp_prevjoy = 0;
  58. // we don't need diagonals in menus
  59. if ((ret&GP2X_UP) && (ret&GP2X_LEFT)) ret &= ~GP2X_LEFT;
  60. if ((ret&GP2X_UP) && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;
  61. if ((ret&GP2X_DOWN) && (ret&GP2X_LEFT)) ret &= ~GP2X_LEFT;
  62. if ((ret&GP2X_DOWN) && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;
  63. return ret;
  64. }
  65. static unsigned long input2_read(unsigned long interesting, int *joy)
  66. {
  67. unsigned long ret;
  68. int i;
  69. do
  70. {
  71. *joy = 0;
  72. if ((ret = gp2x_joystick_read(0) & interesting)) break;
  73. gp2x_usbjoy_update();
  74. for (i = 0; i < num_of_joys; i++) {
  75. ret = gp2x_usbjoy_check2(i);
  76. if (ret) { *joy = i + 1; break; }
  77. }
  78. if (ret) break;
  79. }
  80. while(0);
  81. return ret;
  82. }
  83. // similar to wait_for_input(), but returns joy num
  84. static unsigned long wait_for_input_usbjoy(unsigned long interesting, int *joy)
  85. {
  86. unsigned long ret;
  87. const int wait = 300*1000;
  88. int i;
  89. if (inp_prevjoy == 0) inp_prev &= interesting;
  90. for (i = 0; i < 6; i++) {
  91. ret = input2_read(interesting, joy);
  92. if (*joy != inp_prevjoy || ret != inp_prev) break;
  93. usleep(wait/6);
  94. }
  95. while ( !(ret = input2_read(interesting, joy)) ) {
  96. usleep(50000);
  97. }
  98. inp_prev = ret;
  99. inp_prevjoy = *joy;
  100. return ret;
  101. }
  102. static void menu_flip(void)
  103. {
  104. gp2x_video_flush_cache();
  105. gp2x_video_flip2();
  106. }
  107. // --------- loading ROM screen ----------
  108. static int cdload_called = 0;
  109. static void load_progress_cb(int percent)
  110. {
  111. int ln, len = percent * 320 / 100;
  112. unsigned short *dst = (unsigned short *)gp2x_screen + 320*20;
  113. if (len > 320) len = 320;
  114. for (ln = 8; ln > 0; ln--, dst += 320)
  115. memset(dst, 0xff, len*2);
  116. menu_flip();
  117. }
  118. static void cdload_progress_cb(int percent)
  119. {
  120. int ln, len = percent * 320 / 100;
  121. unsigned short *dst = (unsigned short *)gp2x_screen + 320*20;
  122. memset(dst, 0xff, 320*2*8);
  123. smalltext_out16(1, 3*10, "Processing CD image / MP3s", 0xffff);
  124. smalltext_out16_lim(1, 4*10, romFileName, 0xffff, 80);
  125. dst += 30;
  126. if (len > 320) len = 320;
  127. for (ln = 8; ln > 0; ln--, dst += 320)
  128. memset(dst, 0xff, len*2);
  129. cdload_called = 1;
  130. }
  131. void menu_romload_prepare(const char *rom_name)
  132. {
  133. const char *p = rom_name + strlen(rom_name);
  134. while (p > rom_name && *p != '/') p--;
  135. if (rom_data) gp2x_pd_clone_buffer2();
  136. else memset(gp2x_screen, 0, 320*240*2);
  137. smalltext_out16(1, 1, "Loading", 0xffff);
  138. smalltext_out16_lim(1, 10, p, 0xffff, 53);
  139. gp2x_memcpy_buffers(3, gp2x_screen, 0, 320*240*2);
  140. menu_flip();
  141. PicoCartLoadProgressCB = load_progress_cb;
  142. PicoCDLoadProgressCB = cdload_progress_cb;
  143. cdload_called = 0;
  144. }
  145. void menu_romload_end(void)
  146. {
  147. PicoCartLoadProgressCB = NULL;
  148. smalltext_out16(1, cdload_called ? 60 : 30, "Starting emulation...", 0xffff);
  149. menu_flip();
  150. }
  151. // -------------- ROM selector --------------
  152. // rrrr rggg gggb bbbb
  153. static unsigned short file2color(const char *fname)
  154. {
  155. const char *ext = fname + strlen(fname) - 3;
  156. static const char *rom_exts[] = { "zip", "bin", "smd", "gen", "iso", "cso" };
  157. static const char *other_exts[] = { "gmv", "pat" };
  158. int i;
  159. if (ext < fname) ext = fname;
  160. for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
  161. if (strcasecmp(ext, rom_exts[i]) == 0) return 0xbdff;
  162. for (i = 0; i < sizeof(other_exts)/sizeof(other_exts[0]); i++)
  163. if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;
  164. return 0xffff;
  165. }
  166. static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
  167. {
  168. int start, i, pos;
  169. start = 12 - sel;
  170. n--; // exclude current dir (".")
  171. gp2x_pd_clone_buffer2();
  172. if (rom_data == NULL) {
  173. menu_darken_bg(gp2x_screen, 320*240, 0);
  174. }
  175. menu_darken_bg((char *)gp2x_screen + 320*120*2, 320*8, 0);
  176. if(start - 2 >= 0)
  177. smalltext_out16_lim(14, (start - 2)*10, curdir, 0xffff, 53-2);
  178. for (i = 0; i < n; i++) {
  179. pos = start + i;
  180. if (pos < 0) continue;
  181. if (pos > 23) break;
  182. if (namelist[i+1]->d_type == DT_DIR) {
  183. smalltext_out16_lim(14, pos*10, "/", 0xfff6, 1);
  184. smalltext_out16_lim(14+6, pos*10, namelist[i+1]->d_name, 0xfff6, 53-3);
  185. } else {
  186. unsigned short color = file2color(namelist[i+1]->d_name);
  187. smalltext_out16_lim(14, pos*10, namelist[i+1]->d_name, color, 53-2);
  188. }
  189. }
  190. text_out16(5, 120, ">");
  191. menu_flip();
  192. }
  193. static int scandir_cmp(const void *p1, const void *p2)
  194. {
  195. struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2;
  196. if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2);
  197. if ((*d1)->d_type == DT_DIR) return -1; // put before
  198. if ((*d2)->d_type == DT_DIR) return 1;
  199. return alphasort(d1, d2);
  200. }
  201. static char *filter_exts[] = {
  202. ".mp3", ".MP3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",
  203. ".jpg", ".gpe", ".cue"
  204. };
  205. static int scandir_filter(const struct dirent *ent)
  206. {
  207. const char *p;
  208. int i;
  209. if (ent == NULL || ent->d_name == NULL) return 0;
  210. if (strlen(ent->d_name) < 5) return 1;
  211. p = ent->d_name + strlen(ent->d_name) - 4;
  212. for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)
  213. {
  214. if (strcmp(p, filter_exts[i]) == 0) return 0;
  215. }
  216. return 1;
  217. }
  218. static char *romsel_loop(char *curr_path)
  219. {
  220. struct dirent **namelist;
  221. DIR *dir;
  222. int n, sel = 0;
  223. unsigned long inp = 0;
  224. char *ret = NULL, *fname = NULL;
  225. // is this a dir or a full path?
  226. if ((dir = opendir(curr_path))) {
  227. closedir(dir);
  228. } else {
  229. char *p;
  230. for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);
  231. *p = 0;
  232. fname = p+1;
  233. }
  234. n = scandir(curr_path, &namelist, scandir_filter, scandir_cmp);
  235. if (n < 0) {
  236. // try root
  237. n = scandir("/", &namelist, scandir_filter, scandir_cmp);
  238. if (n < 0) {
  239. // oops, we failed
  240. printf("dir: "); printf(curr_path); printf("\n");
  241. perror("scandir");
  242. return NULL;
  243. }
  244. }
  245. // try to find sel
  246. if (fname != NULL) {
  247. int i;
  248. for (i = 1; i < n; i++) {
  249. if (strcmp(namelist[i]->d_name, fname) == 0) {
  250. sel = i - 1;
  251. break;
  252. }
  253. }
  254. }
  255. for (;;)
  256. {
  257. draw_dirlist(curr_path, namelist, n, sel);
  258. inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);
  259. if(inp & GP2X_UP ) { sel--; if (sel < 0) sel = n-2; }
  260. if(inp & GP2X_DOWN) { sel++; if (sel > n-2) sel = 0; }
  261. if(inp & GP2X_LEFT) { sel-=10; if (sel < 0) sel = 0; }
  262. if(inp & GP2X_L) { sel-=24; if (sel < 0) sel = 0; }
  263. if(inp & GP2X_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
  264. if(inp & GP2X_R) { sel+=24; if (sel > n-2) sel = n-2; }
  265. if(inp & GP2X_B) { // enter dir/select
  266. again:
  267. if (namelist[sel+1]->d_type == DT_REG) {
  268. strcpy(romFileName, curr_path);
  269. strcat(romFileName, "/");
  270. strcat(romFileName, namelist[sel+1]->d_name);
  271. ret = romFileName;
  272. break;
  273. } else if (namelist[sel+1]->d_type == DT_DIR) {
  274. int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
  275. char *p, *newdir = malloc(newlen);
  276. if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
  277. char *start = curr_path;
  278. p = start + strlen(start) - 1;
  279. while (*p == '/' && p > start) p--;
  280. while (*p != '/' && p > start) p--;
  281. if (p <= start) strcpy(newdir, "/");
  282. else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
  283. } else {
  284. strcpy(newdir, curr_path);
  285. p = newdir + strlen(newdir) - 1;
  286. while (*p == '/' && p >= newdir) *p-- = 0;
  287. strcat(newdir, "/");
  288. strcat(newdir, namelist[sel+1]->d_name);
  289. }
  290. ret = romsel_loop(newdir);
  291. free(newdir);
  292. break;
  293. } else {
  294. // unknown file type, happens on NTFS mounts. Try to guess.
  295. FILE *tstf; int tmp;
  296. strcpy(romFileName, curr_path);
  297. strcat(romFileName, "/");
  298. strcat(romFileName, namelist[sel+1]->d_name);
  299. tstf = fopen(romFileName, "rb");
  300. if (tstf != NULL)
  301. {
  302. if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)
  303. namelist[sel+1]->d_type = DT_REG;
  304. else namelist[sel+1]->d_type = DT_DIR;
  305. fclose(tstf);
  306. goto again;
  307. }
  308. }
  309. }
  310. if(inp & GP2X_X) break; // cancel
  311. }
  312. if (n > 0) {
  313. while(n--) free(namelist[n]);
  314. free(namelist);
  315. }
  316. return ret;
  317. }
  318. // ------------ debug menu ------------
  319. char *debugString(void);
  320. static void draw_debug(void)
  321. {
  322. char *p, *str = debugString();
  323. int len, line;
  324. gp2x_pd_clone_buffer2();
  325. p = str;
  326. for (line = 0; line < 24; line++)
  327. {
  328. while (*p && *p != '\n') p++;
  329. len = p - str;
  330. if (len > 55) len = 55;
  331. smalltext_out16_lim(1, line*10, str, 0xffff, len);
  332. if (*p == 0) break;
  333. p++; str = p;
  334. }
  335. menu_flip();
  336. }
  337. static void debug_menu_loop(void)
  338. {
  339. draw_debug();
  340. wait_for_input(GP2X_B|GP2X_X);
  341. }
  342. // ------------ patch/gg menu ------------
  343. static void draw_patchlist(int sel)
  344. {
  345. int start, i, pos, active;
  346. start = 12 - sel;
  347. gp2x_pd_clone_buffer2();
  348. for (i = 0; i < PicoPatchCount; i++) {
  349. pos = start + i;
  350. if (pos < 0) continue;
  351. if (pos > 23) break;
  352. active = PicoPatches[i].active;
  353. smalltext_out16_lim(14, pos*10, active ? "ON " : "OFF", active ? 0xfff6 : 0xffff, 3);
  354. smalltext_out16_lim(14+6*4, pos*10, PicoPatches[i].name, active ? 0xfff6 : 0xffff, 53-6);
  355. }
  356. pos = start + i;
  357. if (pos < 24) smalltext_out16_lim(14, pos*10, "done", 0xffff, 4);
  358. text_out16(5, 120, ">");
  359. menu_flip();
  360. }
  361. static void patches_menu_loop(void)
  362. {
  363. int menu_sel = 0;
  364. unsigned long inp = 0;
  365. for(;;)
  366. {
  367. draw_patchlist(menu_sel);
  368. inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);
  369. if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }
  370. if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }
  371. if(inp &(GP2X_LEFT|GP2X_L)) { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }
  372. if(inp &(GP2X_RIGHT|GP2X_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }
  373. if(inp & GP2X_B) { // action
  374. if (menu_sel < PicoPatchCount)
  375. PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;
  376. else return;
  377. }
  378. if(inp & GP2X_X) return;
  379. }
  380. }
  381. // ------------ savestate loader ------------
  382. static int state_slot_flags = 0;
  383. static void state_check_slots(void)
  384. {
  385. int slot;
  386. state_slot_flags = 0;
  387. for (slot = 0; slot < 10; slot++)
  388. {
  389. if (emu_checkSaveFile(slot))
  390. {
  391. state_slot_flags |= 1 << slot;
  392. }
  393. }
  394. }
  395. static void draw_savestate_bg(int slot)
  396. {
  397. struct PicoVideo tmp_pv;
  398. unsigned short tmp_cram[0x40];
  399. unsigned short tmp_vsram[0x40];
  400. void *tmp_vram, *file;
  401. char *fname;
  402. fname = emu_GetSaveFName(1, 0, slot);
  403. if (!fname) return;
  404. tmp_vram = malloc(sizeof(Pico.vram));
  405. if (tmp_vram == NULL) return;
  406. memcpy(tmp_vram, Pico.vram, sizeof(Pico.vram));
  407. memcpy(tmp_cram, Pico.cram, sizeof(Pico.cram));
  408. memcpy(tmp_vsram, Pico.vsram, sizeof(Pico.vsram));
  409. memcpy(&tmp_pv, &Pico.video, sizeof(Pico.video));
  410. if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {
  411. file = gzopen(fname, "rb");
  412. emu_setSaveStateCbs(1);
  413. } else {
  414. file = fopen(fname, "rb");
  415. emu_setSaveStateCbs(0);
  416. }
  417. if (file) {
  418. if (PicoMCD & 1) {
  419. PicoCdLoadStateGfx(file);
  420. } else {
  421. areaSeek(file, 0x10020, SEEK_SET); // skip header and RAM in state file
  422. areaRead(Pico.vram, 1, sizeof(Pico.vram), file);
  423. areaSeek(file, 0x2000, SEEK_CUR);
  424. areaRead(Pico.cram, 1, sizeof(Pico.cram), file);
  425. areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);
  426. areaSeek(file, 0x221a0, SEEK_SET);
  427. areaRead(&Pico.video, 1, sizeof(Pico.video), file);
  428. }
  429. areaClose(file);
  430. }
  431. emu_forcedFrame();
  432. menu_prepare_bg(1);
  433. memcpy(Pico.vram, tmp_vram, sizeof(Pico.vram));
  434. memcpy(Pico.cram, tmp_cram, sizeof(Pico.cram));
  435. memcpy(Pico.vsram, tmp_vsram, sizeof(Pico.vsram));
  436. memcpy(&Pico.video, &tmp_pv, sizeof(Pico.video));
  437. free(tmp_vram);
  438. }
  439. static void draw_savestate_menu(int menu_sel, int is_loading)
  440. {
  441. int tl_x = 25, tl_y = 60, y, i;
  442. if (state_slot_flags & (1 << menu_sel))
  443. draw_savestate_bg(menu_sel);
  444. gp2x_pd_clone_buffer2();
  445. text_out16(tl_x, 30, is_loading ? "Load state" : "Save state");
  446. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 108);
  447. /* draw all 10 slots */
  448. y = tl_y;
  449. for (i = 0; i < 10; i++, y+=10)
  450. {
  451. text_out16(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");
  452. }
  453. text_out16(tl_x, y, "back");
  454. menu_flip();
  455. }
  456. static int savestate_menu_loop(int is_loading)
  457. {
  458. static int menu_sel = 10;
  459. int menu_sel_max = 10;
  460. unsigned long inp = 0;
  461. state_check_slots();
  462. for(;;)
  463. {
  464. draw_savestate_menu(menu_sel, is_loading);
  465. inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);
  466. if(inp & GP2X_UP ) {
  467. do {
  468. menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;
  469. } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
  470. }
  471. if(inp & GP2X_DOWN) {
  472. do {
  473. menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;
  474. } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
  475. }
  476. if(inp & GP2X_B) { // save/load
  477. if (menu_sel < 10) {
  478. state_slot = menu_sel;
  479. if (emu_SaveLoadGame(is_loading, 0)) {
  480. strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");
  481. return 1;
  482. }
  483. return 0;
  484. } else return 1;
  485. }
  486. if(inp & GP2X_X) return 1;
  487. }
  488. }
  489. // -------------- key config --------------
  490. static char *usb_joy_key_name(int joy, int num)
  491. {
  492. static char name[16];
  493. switch (num)
  494. {
  495. case 0: sprintf(name, "Joy%i UP", joy); break;
  496. case 1: sprintf(name, "Joy%i DOWN", joy); break;
  497. case 2: sprintf(name, "Joy%i LEFT", joy); break;
  498. case 3: sprintf(name, "Joy%i RIGHT", joy); break;
  499. default:sprintf(name, "Joy%i b%i", joy, num-3); break;
  500. }
  501. return name;
  502. }
  503. static char *action_binds(int player_idx, int action_mask)
  504. {
  505. static char strkeys[32*5];
  506. int joy, i;
  507. strkeys[0] = 0;
  508. for (i = 0; i < 32; i++) // i is key index
  509. {
  510. if (currentConfig.KeyBinds[i] & action_mask)
  511. {
  512. if (player_idx >= 0 && ((currentConfig.KeyBinds[i] >> 16) & 3) != player_idx) continue;
  513. if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, gp2xKeyNames[i]); break; }
  514. else strcpy(strkeys, gp2xKeyNames[i]);
  515. }
  516. }
  517. for (joy = 0; joy < num_of_joys; joy++)
  518. {
  519. for (i = 0; i < 32; i++)
  520. {
  521. if (currentConfig.JoyBinds[joy][i] & action_mask)
  522. {
  523. if (player_idx >= 0 && ((currentConfig.JoyBinds[joy][i] >> 16) & 3) != player_idx) continue;
  524. if (strkeys[0]) {
  525. strcat(strkeys, ", "); strcat(strkeys, usb_joy_key_name(joy + 1, i));
  526. break;
  527. }
  528. else strcpy(strkeys, usb_joy_key_name(joy + 1, i));
  529. }
  530. }
  531. }
  532. // limit..
  533. strkeys[20] = 0;
  534. return strkeys;
  535. }
  536. static void unbind_action(int action, int pl_idx, int joy)
  537. {
  538. int i, u;
  539. if (joy <= 0)
  540. {
  541. for (i = 0; i < 32; i++) {
  542. if (pl_idx >= 0 && (currentConfig.KeyBinds[i]&0x30000) != (pl_idx<<16)) continue;
  543. currentConfig.KeyBinds[i] &= ~action;
  544. }
  545. }
  546. if (joy < 0)
  547. {
  548. for (u = 0; u < 4; u++)
  549. for (i = 0; i < 32; i++) {
  550. if (pl_idx >= 0 && (currentConfig.JoyBinds[u][i]&0x30000) != (pl_idx<<16)) continue;
  551. currentConfig.JoyBinds[u][i] &= ~action;
  552. }
  553. }
  554. else if (joy > 0)
  555. {
  556. for (i = 0; i < 32; i++) {
  557. if (pl_idx >= 0 && (currentConfig.JoyBinds[joy-1][i]&0x30000) != (pl_idx<<16)) continue;
  558. currentConfig.JoyBinds[joy-1][i] &= ~action;
  559. }
  560. }
  561. }
  562. static int count_bound_keys(int action, int pl_idx, int joy)
  563. {
  564. int i, keys = 0;
  565. if (joy)
  566. {
  567. for (i = 0; i < 32; i++) {
  568. if (pl_idx >= 0 && (currentConfig.JoyBinds[joy-1][i]&0x30000) != (pl_idx<<16)) continue;
  569. if (currentConfig.JoyBinds[joy-1][i] & action) keys++;
  570. }
  571. }
  572. else
  573. {
  574. for (i = 0; i < 32; i++) {
  575. if (pl_idx >= 0 && (currentConfig.KeyBinds[i]&0x30000) != (pl_idx<<16)) continue;
  576. if (currentConfig.KeyBinds[i] & action) keys++;
  577. }
  578. }
  579. return keys;
  580. }
  581. typedef struct { char *name; int mask; } bind_action_t;
  582. static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_idx, int sel)
  583. {
  584. int x, y, tl_y = 40, i;
  585. gp2x_pd_clone_buffer2();
  586. if (player_idx >= 0) {
  587. text_out16(80, 20, "Player %i controls", player_idx + 1);
  588. x = 80;
  589. } else {
  590. text_out16(80, 20, "Emulator controls");
  591. x = 40;
  592. }
  593. menu_draw_selection(x - 16, tl_y + sel*10, (player_idx >= 0) ? 66 : 130);
  594. y = tl_y;
  595. for (i = 0; i < opt_cnt; i++, y+=10)
  596. text_out16(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));
  597. text_out16(x, y, "Done");
  598. if (sel < opt_cnt) {
  599. text_out16(30, 180, "Press a button to bind/unbind");
  600. text_out16(30, 190, "Use SELECT to clear");
  601. text_out16(30, 200, "To bind UP/DOWN, hold SELECT");
  602. text_out16(30, 210, "Select \"Done\" to exit");
  603. } else {
  604. text_out16(30, 190, "Use Options -> Save cfg");
  605. text_out16(30, 200, "to save controls");
  606. text_out16(30, 210, "Press B or X to exit");
  607. }
  608. menu_flip();
  609. }
  610. static void key_config_loop(const bind_action_t *opts, int opt_cnt, int player_idx)
  611. {
  612. int joy = 0, sel = 0, menu_sel_max = opt_cnt, prev_select = 0, i;
  613. unsigned long inp = 0;
  614. for (;;)
  615. {
  616. draw_key_config(opts, opt_cnt, player_idx, sel);
  617. inp = wait_for_input_usbjoy(CONFIGURABLE_KEYS, &joy);
  618. // printf("got %08lX from joy %i\n", inp, joy);
  619. if (joy == 0) {
  620. if (!(inp & GP2X_SELECT)) {
  621. prev_select = 0;
  622. if(inp & GP2X_UP ) { sel--; if (sel < 0) sel = menu_sel_max; continue; }
  623. if(inp & GP2X_DOWN) { sel++; if (sel > menu_sel_max) sel = 0; continue; }
  624. }
  625. if (sel >= opt_cnt) {
  626. if (inp & (GP2X_B|GP2X_X)) break;
  627. else continue;
  628. }
  629. // if we are here, we want to bind/unbind something
  630. if ((inp & GP2X_SELECT) && !prev_select)
  631. unbind_action(opts[sel].mask, player_idx, -1);
  632. prev_select = inp & GP2X_SELECT;
  633. inp &= CONFIGURABLE_KEYS;
  634. inp &= ~GP2X_SELECT;
  635. for (i = 0; i < 32; i++)
  636. if (inp & (1 << i)) {
  637. if (count_bound_keys(opts[sel].mask, player_idx, 0) >= 2)
  638. currentConfig.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only
  639. else currentConfig.KeyBinds[i] ^= opts[sel].mask;
  640. if (player_idx >= 0 && (currentConfig.KeyBinds[i] & opts[sel].mask)) {
  641. currentConfig.KeyBinds[i] &= ~(3 << 16);
  642. currentConfig.KeyBinds[i] |= player_idx << 16;
  643. }
  644. }
  645. }
  646. else if (sel < opt_cnt)
  647. {
  648. for (i = 0; i < 32; i++)
  649. if (inp & (1 << i)) {
  650. int *bind = &currentConfig.JoyBinds[joy-1][i];
  651. if ((*bind & opts[sel].mask) && (player_idx < 0 || player_idx == ((*bind>>16)&3)))
  652. currentConfig.JoyBinds[joy-1][i] &= ~opts[sel].mask;
  653. else {
  654. // override
  655. unbind_action(opts[sel].mask, player_idx, joy);
  656. *bind = opts[sel].mask;
  657. if (player_idx >= 0) *bind |= player_idx << 16;
  658. }
  659. }
  660. }
  661. }
  662. }
  663. static void draw_kc_sel(int menu_sel)
  664. {
  665. int tl_x = 25+40, tl_y = 60, y, i;
  666. char joyname[36];
  667. y = tl_y;
  668. gp2x_pd_clone_buffer2();
  669. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 138);
  670. text_out16(tl_x, y, "Player 1");
  671. text_out16(tl_x, (y+=10), "Player 2");
  672. text_out16(tl_x, (y+=10), "Emulator controls");
  673. text_out16(tl_x, (y+=10), "Done");
  674. tl_x = 25;
  675. text_out16(tl_x, (y=110), "USB joys detected:");
  676. if (num_of_joys > 0) {
  677. for (i = 0; i < num_of_joys; i++) {
  678. strncpy(joyname, joy_name(joys[i]), 33); joyname[33] = 0;
  679. text_out16(tl_x, (y+=10), "%i: %s", i+1, joyname);
  680. }
  681. } else {
  682. text_out16(tl_x, (y+=10), "none");
  683. }
  684. menu_flip();
  685. }
  686. // PicoPad[] format: MXYZ SACB RLDU
  687. static bind_action_t ctrl_actions[] =
  688. {
  689. { "UP ", 0x001 },
  690. { "DOWN ", 0x002 },
  691. { "LEFT ", 0x004 },
  692. { "RIGHT ", 0x008 },
  693. { "A ", 0x040 },
  694. { "B ", 0x010 },
  695. { "C ", 0x020 },
  696. { "START ", 0x080 },
  697. { "MODE ", 0x800 },
  698. { "X ", 0x400 },
  699. { "Y ", 0x200 },
  700. { "Z ", 0x100 },
  701. };
  702. // player2_flag, ?, ?, ?, ?, ?, ?, menu
  703. // "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",
  704. // "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"
  705. static bind_action_t emuctrl_actions[] =
  706. {
  707. { "Load State ", 1<<28 },
  708. { "Save State ", 1<<27 },
  709. { "Prev Save Slot ", 1<<25 },
  710. { "Next Save Slot ", 1<<24 },
  711. { "Switch Renderer", 1<<26 },
  712. { "Volume Down ", 1<<30 },
  713. { "Volume Up ", 1<<29 },
  714. { "Enter Menu ", 1<<23 },
  715. };
  716. static void kc_sel_loop(void)
  717. {
  718. int menu_sel = 3, menu_sel_max = 3;
  719. unsigned long inp = 0;
  720. int is_6button = currentConfig.PicoOpt & 0x020;
  721. while (1)
  722. {
  723. draw_kc_sel(menu_sel);
  724. inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);
  725. if (inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  726. if (inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  727. if (inp & GP2X_B) {
  728. switch (menu_sel) {
  729. case 0: key_config_loop(ctrl_actions, is_6button ? 12 : 8, 0); return;
  730. case 1: key_config_loop(ctrl_actions, is_6button ? 12 : 8, 1); return;
  731. case 2: key_config_loop(emuctrl_actions,
  732. sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]), -1); return;
  733. case 3: if (rom_data == NULL) emu_WriteConfig(0); return;
  734. default: return;
  735. }
  736. }
  737. if (inp & GP2X_X) return;
  738. }
  739. }
  740. // --------- sega/mega cd options ----------
  741. menu_entry cdopt_entries[] =
  742. {
  743. { NULL, MB_NONE, MA_CDOPT_TESTBIOS_USA, NULL, 0, 0, 0, 1 },
  744. { NULL, MB_NONE, MA_CDOPT_TESTBIOS_EUR, NULL, 0, 0, 0, 1 },
  745. { NULL, MB_NONE, MA_CDOPT_TESTBIOS_JAP, NULL, 0, 0, 0, 1 },
  746. { "CD LEDs", MB_ONOFF, MA_CDOPT_LEDS, &currentConfig.EmuOpt, 0x0400, 0, 0, 1 },
  747. { "CDDA audio (using mp3s)", MB_ONOFF, MA_CDOPT_CDDA, &currentConfig.PicoOpt, 0x0800, 0, 0, 1 },
  748. { "PCM audio", MB_ONOFF, MA_CDOPT_PCM, &currentConfig.PicoOpt, 0x0400, 0, 0, 1 },
  749. { NULL, MB_NONE, MA_CDOPT_READAHEAD, NULL, 0, 0, 0, 1 },
  750. { "SaveRAM cart", MB_ONOFF, MA_CDOPT_SAVERAM, &currentConfig.PicoOpt, 0x8000, 0, 0, 1 },
  751. { "Scale/Rot. fx (slow)", MB_ONOFF, MA_CDOPT_SCALEROT_CHIP,&currentConfig.PicoOpt, 0x1000, 0, 0, 1 },
  752. { "Better sync (slow)", MB_ONOFF, MA_CDOPT_BETTER_SYNC, &currentConfig.PicoOpt, 0x2000, 0, 0, 1 },
  753. { "done", MB_NONE, MA_CDOPT_DONE, NULL, 0, 0, 0, 1 },
  754. };
  755. #define CDOPT_ENTRY_COUNT (sizeof(cdopt_entries) / sizeof(cdopt_entries[0]))
  756. struct bios_names_t
  757. {
  758. char us[32], eu[32], jp[32];
  759. };
  760. static void menu_cdopt_cust_draw(const menu_entry *entry, int x, int y, void *param)
  761. {
  762. struct bios_names_t *bios_names = param;
  763. char ra_buff[16];
  764. switch (entry->id)
  765. {
  766. case MA_CDOPT_TESTBIOS_USA: text_out16(x, y, "USA BIOS: %s", bios_names->us); break;
  767. case MA_CDOPT_TESTBIOS_EUR: text_out16(x, y, "EUR BIOS: %s", bios_names->eu); break;
  768. case MA_CDOPT_TESTBIOS_JAP: text_out16(x, y, "JAP BIOS: %s", bios_names->jp); break;
  769. case MA_CDOPT_READAHEAD:
  770. if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
  771. else strcpy(ra_buff, " OFF");
  772. text_out16(x, y, "ReadAhead buffer %s", ra_buff);
  773. break;
  774. default:break;
  775. }
  776. }
  777. static void draw_cd_menu_options(int menu_sel, struct bios_names_t *bios_names)
  778. {
  779. int tl_x = 25, tl_y = 60;
  780. menu_id selected_id;
  781. char ra_buff[16];
  782. if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
  783. else strcpy(ra_buff, " OFF");
  784. gp2x_pd_clone_buffer2();
  785. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 246);
  786. me_draw(cdopt_entries, CDOPT_ENTRY_COUNT, tl_x, tl_y, menu_cdopt_cust_draw, bios_names);
  787. selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
  788. if ((selected_id == MA_CDOPT_TESTBIOS_USA && strcmp(bios_names->us, "NOT FOUND")) ||
  789. (selected_id == MA_CDOPT_TESTBIOS_EUR && strcmp(bios_names->eu, "NOT FOUND")) ||
  790. (selected_id == MA_CDOPT_TESTBIOS_JAP && strcmp(bios_names->jp, "NOT FOUND")))
  791. text_out16(tl_x, 210, "Press start to test selected BIOS");
  792. menu_flip();
  793. }
  794. static void cd_menu_loop_options(void)
  795. {
  796. static int menu_sel = 0;
  797. int menu_sel_max = 10;
  798. unsigned long inp = 0;
  799. struct bios_names_t bios_names;
  800. menu_id selected_id;
  801. char *bios, *p;
  802. if (emu_findBios(4, &bios)) { // US
  803. for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;
  804. strncpy(bios_names.us, p, sizeof(bios_names.us)); bios_names.us[sizeof(bios_names.us)-1] = 0;
  805. } else strcpy(bios_names.us, "NOT FOUND");
  806. if (emu_findBios(8, &bios)) { // EU
  807. for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;
  808. strncpy(bios_names.eu, p, sizeof(bios_names.eu)); bios_names.eu[sizeof(bios_names.eu)-1] = 0;
  809. } else strcpy(bios_names.eu, "NOT FOUND");
  810. if (emu_findBios(1, &bios)) { // JP
  811. for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;
  812. strncpy(bios_names.jp, p, sizeof(bios_names.jp)); bios_names.jp[sizeof(bios_names.jp)-1] = 0;
  813. } else strcpy(bios_names.jp, "NOT FOUND");
  814. for(;;)
  815. {
  816. draw_cd_menu_options(menu_sel, &bios_names);
  817. inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A|GP2X_START);
  818. if (inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  819. if (inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  820. selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
  821. if (inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise
  822. if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, (inp&GP2X_RIGHT) ? 1 : 0) &&
  823. selected_id == MA_CDOPT_READAHEAD) {
  824. if (inp & GP2X_LEFT) {
  825. PicoCDBuffers >>= 1;
  826. if (PicoCDBuffers < 64) PicoCDBuffers = 0;
  827. } else {
  828. if (PicoCDBuffers < 64) PicoCDBuffers = 64;
  829. else PicoCDBuffers <<= 1;
  830. if (PicoCDBuffers > 8*1024) PicoCDBuffers = 8*1024; // 16M
  831. }
  832. }
  833. }
  834. if (inp & GP2X_B) { // toggleable options
  835. if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, 1) &&
  836. selected_id == MA_CDOPT_DONE) {
  837. return;
  838. }
  839. }
  840. if (inp & GP2X_START) { // BIOS testers
  841. switch (selected_id) {
  842. case MA_CDOPT_TESTBIOS_USA:
  843. if (emu_findBios(4, &bios)) { // test US
  844. strcpy(romFileName, bios);
  845. engineState = PGS_ReloadRom;
  846. return;
  847. }
  848. break;
  849. case MA_CDOPT_TESTBIOS_EUR:
  850. if (emu_findBios(8, &bios)) { // test EU
  851. strcpy(romFileName, bios);
  852. engineState = PGS_ReloadRom;
  853. return;
  854. }
  855. break;
  856. case MA_CDOPT_TESTBIOS_JAP:
  857. if (emu_findBios(1, &bios)) { // test JP
  858. strcpy(romFileName, bios);
  859. engineState = PGS_ReloadRom;
  860. return;
  861. }
  862. break;
  863. default:
  864. break;
  865. }
  866. }
  867. if (inp & (GP2X_X|GP2X_A)) return;
  868. }
  869. }
  870. // --------- advanced options ----------
  871. menu_entry opt2_entries[] =
  872. {
  873. { NULL, MB_NONE, MA_OPT2_GAMMA, NULL, 0, 0, 0, 1 },
  874. { "A_SN's gamma curve", MB_ONOFF, MA_OPT2_A_SN_GAMMA, &currentConfig.EmuOpt, 0x1000, 0, 0, 1 },
  875. { "Perfect vsync", MB_ONOFF, MA_OPT2_VSYNC, &currentConfig.EmuOpt, 0x2000, 0, 0, 1 },
  876. { "Emulate Z80", MB_ONOFF, MA_OPT2_ENABLE_Z80, &currentConfig.PicoOpt,0x0004, 0, 0, 1 },
  877. { "Emulate YM2612 (FM)", MB_ONOFF, MA_OPT2_ENABLE_YM2612, &currentConfig.PicoOpt,0x0001, 0, 0, 1 },
  878. { "Emulate SN76496 (PSG)", MB_ONOFF, MA_OPT2_ENABLE_SN76496,&currentConfig.PicoOpt,0x0002, 0, 0, 1 },
  879. { "gzip savestates", MB_ONOFF, MA_OPT2_GZIP_STATES, &currentConfig.EmuOpt, 0x0008, 0, 0, 1 },
  880. { "Don't save last used ROM", MB_ONOFF, MA_OPT2_NO_LAST_ROM, &currentConfig.EmuOpt, 0x0020, 0, 0, 1 },
  881. { "needs restart:", MB_NONE, MA_NONE, NULL, 0, 0, 0, 1 },
  882. { "craigix's RAM timings", MB_ONOFF, MA_OPT2_RAMTIMINGS, &currentConfig.EmuOpt, 0x0100, 0, 0, 1 },
  883. { NULL, MB_ONOFF, MA_OPT2_SQUIDGEHACK, &currentConfig.EmuOpt, 0x0010, 0, 0, 1 },
  884. { "done", MB_NONE, MA_OPT2_DONE, NULL, 0, 0, 0, 1 },
  885. };
  886. #define OPT2_ENTRY_COUNT (sizeof(opt2_entries) / sizeof(opt2_entries[0]))
  887. static void menu_opt2_cust_draw(const menu_entry *entry, int x, int y, void *param)
  888. {
  889. if (entry->id == MA_OPT2_GAMMA)
  890. text_out16(x, y, "Gamma correction %i.%02i", currentConfig.gamma / 100, currentConfig.gamma%100);
  891. else if (entry->id == MA_OPT2_SQUIDGEHACK)
  892. text_out16(x, y, "squidgehack (now %s %s", mmuhack_status ? "active) " : "inactive)",
  893. (currentConfig.EmuOpt&0x0010)?"ON":"OFF");
  894. }
  895. static void draw_amenu_options(int menu_sel)
  896. {
  897. int tl_x = 25, tl_y = 50;
  898. gp2x_pd_clone_buffer2();
  899. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 252);
  900. me_draw(opt2_entries, OPT2_ENTRY_COUNT, tl_x, tl_y, menu_opt2_cust_draw, NULL);
  901. menu_flip();
  902. }
  903. static void amenu_loop_options(void)
  904. {
  905. static int menu_sel = 0;
  906. int menu_sel_max;
  907. unsigned long inp = 0;
  908. menu_id selected_id;
  909. menu_sel_max = me_count_enabled(opt2_entries, OPT2_ENTRY_COUNT) - 1;
  910. for(;;)
  911. {
  912. draw_amenu_options(menu_sel);
  913. inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);
  914. if (inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  915. if (inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  916. selected_id = me_index2id(opt2_entries, OPT2_ENTRY_COUNT, menu_sel);
  917. if (inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise
  918. if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, (inp&GP2X_RIGHT) ? 1 : 0) &&
  919. selected_id == MA_OPT2_GAMMA) {
  920. while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {
  921. currentConfig.gamma += (inp & GP2X_LEFT) ? -1 : 1;
  922. if (currentConfig.gamma < 1) currentConfig.gamma = 1;
  923. if (currentConfig.gamma > 300) currentConfig.gamma = 300;
  924. draw_amenu_options(menu_sel);
  925. usleep(18*1000);
  926. }
  927. }
  928. }
  929. if (inp & GP2X_B) { // toggleable options
  930. if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, 1) &&
  931. selected_id == MA_OPT2_DONE) {
  932. return;
  933. }
  934. }
  935. if (inp & (GP2X_X|GP2X_A)) return;
  936. }
  937. }
  938. // -------------- options --------------
  939. menu_entry opt_entries[] =
  940. {
  941. { NULL, MB_NONE, MA_OPT_RENDERER, NULL, 0, 0, 0, 1 },
  942. { NULL, MB_RANGE, MA_OPT_SCALING, &currentConfig.scaling, 0, 0, 3, 1 },
  943. { "Accurate timing (slower)", MB_ONOFF, MA_OPT_ACC_TIMING, &currentConfig.PicoOpt, 0x040, 0, 0, 1 },
  944. { "Accurate sprites (slower)", MB_ONOFF, MA_OPT_ACC_SPRITES, &currentConfig.PicoOpt, 0x080, 0, 0, 1 },
  945. { "Show FPS", MB_ONOFF, MA_OPT_SHOW_FPS, &currentConfig.EmuOpt, 0x002, 0, 0, 1 },
  946. { NULL, MB_RANGE, MA_OPT_FRAMESKIP, &currentConfig.Frameskip, 0, -1, 16, 1 },
  947. { "Enable sound", MB_ONOFF, MA_OPT_ENABLE_SOUND, &currentConfig.EmuOpt, 0x004, 0, 0, 1 },
  948. { NULL, MB_NONE, MA_OPT_SOUND_QUALITY, NULL, 0, 0, 0, 1 },
  949. { "Use ARM940 core for sound", MB_ONOFF, MA_OPT_ARM940_SOUND, &currentConfig.PicoOpt, 0x200, 0, 0, 1 },
  950. { "6 button pad", MB_ONOFF, MA_OPT_6BUTTON_PAD, &currentConfig.PicoOpt, 0x020, 0, 0, 1 },
  951. { NULL, MB_NONE, MA_OPT_REGION, NULL, 0, 0, 0, 1 },
  952. { "Use SRAM/BRAM savestates", MB_ONOFF, MA_OPT_SRAM_STATES, &currentConfig.EmuOpt, 0x001, 0, 0, 1 },
  953. { NULL, MB_NONE, MA_OPT_CONFIRM_STATES,NULL, 0, 0, 0, 1 },
  954. { "Save slot", MB_RANGE, MA_OPT_SAVE_SLOT, &state_slot, 0, 0, 9, 1 },
  955. { NULL, MB_NONE, MA_OPT_CPU_CLOCKS, NULL, 0, 0, 0, 1 },
  956. { "[Sega/Mega CD options]", MB_NONE, MA_OPT_SCD_OPTS, NULL, 0, 0, 0, 1 },
  957. { "[advanced options]", MB_NONE, MA_OPT_ADV_OPTS, NULL, 0, 0, 0, 1 },
  958. { NULL, MB_NONE, MA_OPT_SAVECFG, NULL, 0, 0, 0, 1 },
  959. { "Save cfg for current game only",MB_NONE,MA_OPT_SAVECFG_GAME,NULL, 0, 0, 0, 1 },
  960. { NULL, MB_NONE, MA_OPT_LOADCFG, NULL, 0, 0, 0, 1 },
  961. };
  962. #define OPT_ENTRY_COUNT (sizeof(opt_entries) / sizeof(opt_entries[0]))
  963. static const char *region_name(unsigned int code)
  964. {
  965. static const char *names[] = { "Auto", " Japan NTSC", " Japan PAL", " USA", " Europe" };
  966. static const char *names_short[] = { "", " JP", " JP", " US", " EU" };
  967. int u, i = 0;
  968. if (code) {
  969. code <<= 1;
  970. while((code >>= 1)) i++;
  971. if (i > 4) return "unknown";
  972. return names[i];
  973. } else {
  974. static char name[24];
  975. strcpy(name, "Auto:");
  976. for (u = 0; u < 3; u++) {
  977. i = 0; code = ((PicoAutoRgnOrder >> u*4) & 0xf) << 1;
  978. while((code >>= 1)) i++;
  979. strcat(name, names_short[i]);
  980. }
  981. return name;
  982. }
  983. }
  984. static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *param)
  985. {
  986. char *str, str24[24];
  987. switch (entry->id)
  988. {
  989. case MA_OPT_RENDERER:
  990. if (currentConfig.PicoOpt&0x10)
  991. str = " 8bit fast";
  992. else if (currentConfig.EmuOpt&0x80)
  993. str = "16bit accurate";
  994. else
  995. str = " 8bit accurate";
  996. text_out16(x, y, "Renderer: %s", str);
  997. break;
  998. case MA_OPT_SCALING:
  999. switch (currentConfig.scaling) {
  1000. default: str = " OFF"; break;
  1001. case 1: str = "hw horizontal"; break;
  1002. case 2: str = "hw horiz. + vert."; break;
  1003. case 3: str = "sw horizontal"; break;
  1004. }
  1005. text_out16(x, y, "Scaling: %s", str);
  1006. break;
  1007. case MA_OPT_FRAMESKIP:
  1008. if (currentConfig.Frameskip < 0)
  1009. strcpy(str24, "Auto");
  1010. else sprintf(str24, "%i", currentConfig.Frameskip);
  1011. text_out16(x, y, "Frameskip %s", str24);
  1012. break;
  1013. case MA_OPT_SOUND_QUALITY:
  1014. str = (currentConfig.PicoOpt&0x08)?"stereo":"mono";
  1015. text_out16(x, y, "Sound Quality: %5iHz %s", currentConfig.PsndRate, str);
  1016. break;
  1017. case MA_OPT_REGION:
  1018. text_out16(x, y, "Region: %s", region_name(currentConfig.PicoRegion));
  1019. break;
  1020. case MA_OPT_CONFIRM_STATES:
  1021. switch ((currentConfig.EmuOpt >> 9) & 5) {
  1022. default: str = "OFF"; break;
  1023. case 1: str = "writes"; break;
  1024. case 4: str = "loads"; break;
  1025. case 5: str = "both"; break;
  1026. }
  1027. text_out16(x, y, "Confirm savestate %s", str);
  1028. break;
  1029. case MA_OPT_CPU_CLOCKS:
  1030. text_out16(x, y, "GP2X CPU clocks %iMhz", currentConfig.CPUclock);
  1031. break;
  1032. case MA_OPT_SAVECFG:
  1033. str24[0] = 0;
  1034. if (config_slot != 0) sprintf(str24, " (profile: %i)", config_slot);
  1035. text_out16(x, y, "Save cfg as default%s", str24);
  1036. break;
  1037. case MA_OPT_LOADCFG:
  1038. text_out16(x, y, "Load cfg from profile %i", config_slot);
  1039. break;
  1040. default:
  1041. printf("%s: unimplemented (%i)\n", __FUNCTION__, entry->id);
  1042. break;
  1043. }
  1044. }
  1045. static void draw_menu_options(int menu_sel)
  1046. {
  1047. int tl_x = 25, tl_y = 24;
  1048. gp2x_pd_clone_buffer2();
  1049. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 284);
  1050. me_draw(opt_entries, OPT_ENTRY_COUNT, tl_x, tl_y, menu_opt_cust_draw, NULL);
  1051. menu_flip();
  1052. }
  1053. static int sndrate_prevnext(int rate, int dir)
  1054. {
  1055. int i, rates[] = { 8000, 11025, 16000, 22050, 44100 };
  1056. for (i = 0; i < 5; i++)
  1057. if (rates[i] == rate) break;
  1058. i += dir ? 1 : -1;
  1059. if (i > 4) return dir ? 44100 : 22050;
  1060. if (i < 0) return dir ? 11025 : 8000;
  1061. return rates[i];
  1062. }
  1063. static void region_prevnext(int right)
  1064. {
  1065. // jp_ntsc=1, jp_pal=2, usa=4, eu=8
  1066. static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };
  1067. int i;
  1068. if (right) {
  1069. if (!currentConfig.PicoRegion) {
  1070. for (i = 0; i < 6; i++)
  1071. if (rgn_orders[i] == PicoAutoRgnOrder) break;
  1072. if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];
  1073. else currentConfig.PicoRegion=1;
  1074. }
  1075. else currentConfig.PicoRegion<<=1;
  1076. if (currentConfig.PicoRegion > 8) currentConfig.PicoRegion = 8;
  1077. } else {
  1078. if (!currentConfig.PicoRegion) {
  1079. for (i = 0; i < 6; i++)
  1080. if (rgn_orders[i] == PicoAutoRgnOrder) break;
  1081. if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];
  1082. }
  1083. else currentConfig.PicoRegion>>=1;
  1084. }
  1085. }
  1086. static void menu_options_save(void)
  1087. {
  1088. PicoOpt = currentConfig.PicoOpt;
  1089. PsndRate = currentConfig.PsndRate;
  1090. PicoRegionOverride = currentConfig.PicoRegion;
  1091. if (PicoRegionOverride) {
  1092. // force setting possibly changed..
  1093. Pico.m.pal = (PicoRegionOverride == 2 || PicoRegionOverride == 8) ? 1 : 0;
  1094. }
  1095. if (!(PicoOpt & 0x20)) {
  1096. // unbind XYZ MODE, just in case
  1097. unbind_action(0xf00, -1, -1);
  1098. }
  1099. }
  1100. static int menu_loop_options(void)
  1101. {
  1102. static int menu_sel = 0;
  1103. int menu_sel_max, ret;
  1104. unsigned long inp = 0;
  1105. menu_id selected_id;
  1106. currentConfig.PicoOpt = PicoOpt;
  1107. currentConfig.PsndRate = PsndRate;
  1108. currentConfig.PicoRegion = PicoRegionOverride;
  1109. me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_SAVECFG_GAME, rom_data != NULL);
  1110. me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
  1111. menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
  1112. if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
  1113. while (1)
  1114. {
  1115. draw_menu_options(menu_sel);
  1116. inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);
  1117. if (inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  1118. if (inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  1119. selected_id = me_index2id(opt_entries, OPT_ENTRY_COUNT, menu_sel);
  1120. if (inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise
  1121. if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, (inp&GP2X_RIGHT) ? 1 : 0)) {
  1122. switch (selected_id) {
  1123. case MA_OPT_RENDERER:
  1124. if (inp & GP2X_LEFT) {
  1125. if ( currentConfig.PicoOpt&0x10) currentConfig.PicoOpt&= ~0x10;
  1126. else if (!(currentConfig.EmuOpt &0x80))currentConfig.EmuOpt |= 0x80;
  1127. else if ( currentConfig.EmuOpt &0x80) break;
  1128. } else {
  1129. if ( currentConfig.PicoOpt&0x10) break;
  1130. else if (!(currentConfig.EmuOpt &0x80))currentConfig.PicoOpt|= 0x10;
  1131. else if ( currentConfig.EmuOpt &0x80) currentConfig.EmuOpt &= ~0x80;
  1132. }
  1133. break;
  1134. case MA_OPT_SOUND_QUALITY:
  1135. if ((inp & GP2X_RIGHT) && currentConfig.PsndRate == 44100 && !(currentConfig.PicoOpt&0x08)) {
  1136. currentConfig.PsndRate = 8000; currentConfig.PicoOpt|= 0x08;
  1137. } else if ((inp & GP2X_LEFT) && currentConfig.PsndRate == 8000 && (currentConfig.PicoOpt&0x08)) {
  1138. currentConfig.PsndRate = 44100; currentConfig.PicoOpt&=~0x08;
  1139. } else currentConfig.PsndRate = sndrate_prevnext(currentConfig.PsndRate, inp & GP2X_RIGHT);
  1140. break;
  1141. case MA_OPT_REGION:
  1142. region_prevnext(inp & GP2X_RIGHT);
  1143. break;
  1144. case MA_OPT_CONFIRM_STATES: {
  1145. int n = ((currentConfig.EmuOpt>>9)&1) | ((currentConfig.EmuOpt>>10)&2);
  1146. n += (inp & GP2X_LEFT) ? -1 : 1;
  1147. if (n < 0) n = 0; else if (n > 3) n = 3;
  1148. n |= n << 1; n &= ~2;
  1149. currentConfig.EmuOpt &= ~0xa00;
  1150. currentConfig.EmuOpt |= n << 9;
  1151. break;
  1152. }
  1153. case MA_OPT_SAVE_SLOT:
  1154. if (inp & GP2X_RIGHT) {
  1155. state_slot++; if (state_slot > 9) state_slot = 0;
  1156. } else {state_slot--; if (state_slot < 0) state_slot = 9;
  1157. }
  1158. break;
  1159. case MA_OPT_CPU_CLOCKS:
  1160. while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {
  1161. currentConfig.CPUclock += (inp & GP2X_LEFT) ? -1 : 1;
  1162. if (currentConfig.CPUclock < 1) currentConfig.CPUclock = 1;
  1163. draw_menu_options(menu_sel);
  1164. usleep(50*1000);
  1165. }
  1166. break;
  1167. case MA_OPT_SAVECFG:
  1168. case MA_OPT_SAVECFG_GAME:
  1169. case MA_OPT_LOADCFG:
  1170. config_slot += (inp&GP2X_RIGHT) ? 1 : -1;
  1171. if (config_slot > 9) config_slot = 0;
  1172. if (config_slot < 0) config_slot = 9;
  1173. me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
  1174. menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
  1175. if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
  1176. break;
  1177. default:
  1178. //printf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
  1179. break;
  1180. }
  1181. }
  1182. }
  1183. if (inp & GP2X_B) {
  1184. if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, 1))
  1185. {
  1186. switch (selected_id)
  1187. {
  1188. case MA_OPT_SCD_OPTS:
  1189. cd_menu_loop_options();
  1190. if (engineState == PGS_ReloadRom)
  1191. return 0; // test BIOS
  1192. break;
  1193. case MA_OPT_ADV_OPTS:
  1194. amenu_loop_options();
  1195. break;
  1196. case MA_OPT_SAVECFG: // done (update and write)
  1197. menu_options_save();
  1198. if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");
  1199. else strcpy(menuErrorMsg, "failed to write config");
  1200. return 1;
  1201. case MA_OPT_SAVECFG_GAME: // done (update and write for current game)
  1202. menu_options_save();
  1203. if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");
  1204. else strcpy(menuErrorMsg, "failed to write config");
  1205. return 1;
  1206. case MA_OPT_LOADCFG:
  1207. ret = emu_ReadConfig(1, 1);
  1208. if (!ret) ret = emu_ReadConfig(0, 1);
  1209. if (ret) strcpy(menuErrorMsg, "config loaded");
  1210. else strcpy(menuErrorMsg, "failed to load config");
  1211. return 1;
  1212. default:
  1213. //printf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
  1214. break;
  1215. }
  1216. }
  1217. }
  1218. if(inp & (GP2X_X|GP2X_A)) {
  1219. menu_options_save();
  1220. return 0; // done (update, no write)
  1221. }
  1222. }
  1223. }
  1224. // -------------- credits --------------
  1225. static void draw_menu_credits(void)
  1226. {
  1227. int tl_x = 15, tl_y = 64, y;
  1228. gp2x_pd_clone_buffer2();
  1229. text_out16(tl_x, 20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");
  1230. y = tl_y;
  1231. text_out16(tl_x, y, "Credits:");
  1232. text_out16(tl_x, (y+=10), "fDave: Cyclone 68000 core,");
  1233. text_out16(tl_x, (y+=10), " base code of PicoDrive");
  1234. text_out16(tl_x, (y+=10), "Reesy & FluBBa: DrZ80 core");
  1235. text_out16(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");
  1236. text_out16(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");
  1237. text_out16(tl_x, (y+=10), "Stephane Dallongeville:");
  1238. text_out16(tl_x, (y+=10), " opensource Gens");
  1239. text_out16(tl_x, (y+=10), "Haze: Genesis hw info");
  1240. text_out16(tl_x, (y+=10), "rlyeh and others: minimal SDK");
  1241. text_out16(tl_x, (y+=10), "Squidge: squidgehack");
  1242. text_out16(tl_x, (y+=10), "Dzz: ARM940 sample");
  1243. text_out16(tl_x, (y+=10), "GnoStiC / Puck2099: USB joystick");
  1244. text_out16(tl_x, (y+=10), "craigix: GP2X hardware");
  1245. text_out16(tl_x, (y+=10), "ketchupgun: skin design");
  1246. menu_flip();
  1247. }
  1248. // -------------- root menu --------------
  1249. menu_entry main_entries[] =
  1250. {
  1251. { "Resume game", MB_NONE, MA_MAIN_RESUME_GAME, NULL, 0, 0, 0, 0 },
  1252. { "Save State", MB_NONE, MA_MAIN_SAVE_STATE, NULL, 0, 0, 0, 0 },
  1253. { "Load State", MB_NONE, MA_MAIN_LOAD_STATE, NULL, 0, 0, 0, 0 },
  1254. { "Reset game", MB_NONE, MA_MAIN_RESET_GAME, NULL, 0, 0, 0, 0 },
  1255. { "Load new ROM/ISO", MB_NONE, MA_MAIN_LOAD_ROM, NULL, 0, 0, 0, 1 },
  1256. { "Change options", MB_NONE, MA_MAIN_OPTIONS, NULL, 0, 0, 0, 1 },
  1257. { "Configure controls", MB_NONE, MA_MAIN_CONTROLS, NULL, 0, 0, 0, 1 },
  1258. { "Credits", MB_NONE, MA_MAIN_CREDITS, NULL, 0, 0, 0, 1 },
  1259. { "Patches / GameGenie",MB_NONE, MA_MAIN_PATCHES, NULL, 0, 0, 0, 0 },
  1260. { "Exit", MB_NONE, MA_MAIN_EXIT, NULL, 0, 0, 0, 1 }
  1261. };
  1262. #define MAIN_ENTRY_COUNT (sizeof(main_entries) / sizeof(main_entries[0]))
  1263. static void draw_menu_root(int menu_sel)
  1264. {
  1265. const int tl_x = 70, tl_y = 70;
  1266. gp2x_pd_clone_buffer2();
  1267. text_out16(tl_x, 20, "PicoDrive v" VERSION);
  1268. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 146);
  1269. me_draw(main_entries, MAIN_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
  1270. // error
  1271. if (menuErrorMsg[0]) {
  1272. memset((char *)gp2x_screen + 320*224*2, 0, 320*16*2);
  1273. text_out16(5, 226, menuErrorMsg);
  1274. }
  1275. menu_flip();
  1276. }
  1277. static void menu_loop_root(void)
  1278. {
  1279. static int menu_sel = 0;
  1280. int ret, menu_sel_max;
  1281. unsigned long inp = 0;
  1282. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESUME_GAME, rom_data != NULL);
  1283. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_SAVE_STATE, rom_data != NULL);
  1284. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_LOAD_STATE, rom_data != NULL);
  1285. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESET_GAME, rom_data != NULL);
  1286. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_PATCHES, PicoPatches != NULL);
  1287. menu_sel_max = me_count_enabled(main_entries, MAIN_ENTRY_COUNT) - 1;
  1288. if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
  1289. /* make sure action buttons are not pressed on entering menu */
  1290. draw_menu_root(menu_sel);
  1291. while (gp2x_joystick_read(1) & (GP2X_B|GP2X_X|GP2X_SELECT)) usleep(50*1000);
  1292. for (;;)
  1293. {
  1294. draw_menu_root(menu_sel);
  1295. inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X|GP2X_SELECT|GP2X_L|GP2X_R);
  1296. if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  1297. if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  1298. if((inp & (GP2X_L|GP2X_R)) == (GP2X_L|GP2X_R)) debug_menu_loop();
  1299. if(inp &(GP2X_SELECT|GP2X_X)){
  1300. if (rom_data) {
  1301. while (gp2x_joystick_read(1) & (GP2X_SELECT|GP2X_X)) usleep(50*1000); // wait until select is released
  1302. engineState = PGS_Running;
  1303. break;
  1304. }
  1305. }
  1306. if(inp & GP2X_B) {
  1307. switch (me_index2id(main_entries, MAIN_ENTRY_COUNT, menu_sel))
  1308. {
  1309. case MA_MAIN_RESUME_GAME:
  1310. if (rom_data) {
  1311. while (gp2x_joystick_read(1) & GP2X_B) usleep(50*1000);
  1312. engineState = PGS_Running;
  1313. return;
  1314. }
  1315. break;
  1316. case MA_MAIN_SAVE_STATE:
  1317. if (rom_data) {
  1318. if(savestate_menu_loop(0))
  1319. continue;
  1320. engineState = PGS_Running;
  1321. return;
  1322. }
  1323. break;
  1324. case MA_MAIN_LOAD_STATE:
  1325. if (rom_data) {
  1326. if(savestate_menu_loop(1))
  1327. continue;
  1328. engineState = PGS_Running;
  1329. return;
  1330. }
  1331. break;
  1332. case MA_MAIN_RESET_GAME:
  1333. if (rom_data) {
  1334. emu_ResetGame();
  1335. engineState = PGS_Running;
  1336. return;
  1337. }
  1338. break;
  1339. case MA_MAIN_LOAD_ROM:
  1340. {
  1341. char curr_path[PATH_MAX], *selfname;
  1342. FILE *tstf;
  1343. if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )
  1344. {
  1345. fclose(tstf);
  1346. strcpy(curr_path, currentConfig.lastRomFile);
  1347. }
  1348. else
  1349. getcwd(curr_path, PATH_MAX);
  1350. selfname = romsel_loop(curr_path);
  1351. if (selfname) {
  1352. printf("selected file: %s\n", selfname);
  1353. engineState = PGS_ReloadRom;
  1354. return;
  1355. }
  1356. break;
  1357. }
  1358. case MA_MAIN_OPTIONS:
  1359. ret = menu_loop_options();
  1360. if (ret == 1) continue; // status update
  1361. if (engineState == PGS_ReloadRom)
  1362. return; // BIOS test
  1363. break;
  1364. case MA_MAIN_CONTROLS:
  1365. kc_sel_loop();
  1366. break;
  1367. case MA_MAIN_CREDITS:
  1368. draw_menu_credits();
  1369. usleep(500*1000);
  1370. inp = wait_for_input(GP2X_B|GP2X_X);
  1371. break;
  1372. case MA_MAIN_EXIT:
  1373. engineState = PGS_Quit;
  1374. return;
  1375. case MA_MAIN_PATCHES:
  1376. if (rom_data && PicoPatches) {
  1377. patches_menu_loop();
  1378. PicoPatchApply();
  1379. strcpy(menuErrorMsg, "Patches applied");
  1380. continue;
  1381. }
  1382. break;
  1383. default:
  1384. printf("%s: something unknown selected\n", __FUNCTION__);
  1385. break;
  1386. }
  1387. }
  1388. menuErrorMsg[0] = 0; // clear error msg
  1389. }
  1390. }
  1391. static void menu_darken_bg(void *dst, int pixels, int darker)
  1392. {
  1393. unsigned int *screen = dst;
  1394. pixels /= 2;
  1395. if (darker)
  1396. {
  1397. while (pixels--)
  1398. {
  1399. unsigned int p = *screen;
  1400. *screen++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
  1401. }
  1402. }
  1403. else
  1404. {
  1405. while (pixels--)
  1406. {
  1407. unsigned int p = *screen;
  1408. *screen++ = (p&0xf79ef79e)>>1;
  1409. }
  1410. }
  1411. }
  1412. static void menu_prepare_bg(int use_game_bg)
  1413. {
  1414. if (use_game_bg)
  1415. {
  1416. // darken the active framebuffer
  1417. memset(gp2x_screen, 0, 320*8*2);
  1418. menu_darken_bg((char *)gp2x_screen + 320*8*2, 320*224, 1);
  1419. memset((char *)gp2x_screen + 320*232*2, 0, 320*8*2);
  1420. }
  1421. else
  1422. {
  1423. // should really only happen once, on startup..
  1424. readpng(gp2x_screen, "skin/background.png", READPNG_BG);
  1425. }
  1426. // copy to buffer2
  1427. gp2x_memcpy_buffers((1<<2), gp2x_screen, 0, 320*240*2);
  1428. }
  1429. static void menu_gfx_prepare(void)
  1430. {
  1431. menu_prepare_bg(rom_data != NULL);
  1432. // switch to 16bpp
  1433. gp2x_video_changemode2(16);
  1434. gp2x_video_RGB_setscaling(0, 320, 240);
  1435. menu_flip();
  1436. }
  1437. void menu_loop(void)
  1438. {
  1439. menu_gfx_prepare();
  1440. menu_loop_root();
  1441. menuErrorMsg[0] = 0;
  1442. }
  1443. // --------- CD tray close menu ----------
  1444. static void draw_menu_tray(int menu_sel)
  1445. {
  1446. int tl_x = 70, tl_y = 90, y;
  1447. memset(gp2x_screen, 0, 320*240*2);
  1448. text_out16(tl_x, 20, "The unit is about to");
  1449. text_out16(tl_x, 30, "close the CD tray.");
  1450. y = tl_y;
  1451. text_out16(tl_x, y, "Load new CD image");
  1452. text_out16(tl_x, (y+=10), "Insert nothing");
  1453. // draw cursor
  1454. text_out16(tl_x - 16, tl_y + menu_sel*10, ">");
  1455. // error
  1456. if (menuErrorMsg[0]) text_out16(5, 226, menuErrorMsg);
  1457. menu_flip();
  1458. }
  1459. int menu_loop_tray(void)
  1460. {
  1461. int menu_sel = 0, menu_sel_max = 1;
  1462. unsigned long inp = 0;
  1463. char curr_path[PATH_MAX], *selfname;
  1464. FILE *tstf;
  1465. gp2x_memset_all_buffers(0, 0, 320*240*2);
  1466. menu_gfx_prepare();
  1467. if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )
  1468. {
  1469. fclose(tstf);
  1470. strcpy(curr_path, currentConfig.lastRomFile);
  1471. }
  1472. else
  1473. {
  1474. getcwd(curr_path, PATH_MAX);
  1475. }
  1476. /* make sure action buttons are not pressed on entering menu */
  1477. draw_menu_tray(menu_sel);
  1478. while (gp2x_joystick_read(1) & GP2X_B) usleep(50*1000);
  1479. for (;;)
  1480. {
  1481. draw_menu_tray(menu_sel);
  1482. inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B);
  1483. if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  1484. if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  1485. if(inp & GP2X_B ) {
  1486. switch (menu_sel) {
  1487. case 0: // select image
  1488. selfname = romsel_loop(curr_path);
  1489. if (selfname) {
  1490. int ret = -1, cd_type;
  1491. cd_type = emu_cdCheck(NULL);
  1492. if (cd_type > 0)
  1493. ret = Insert_CD(romFileName, cd_type == 2);
  1494. if (ret != 0) {
  1495. sprintf(menuErrorMsg, "Load failed, invalid CD image?");
  1496. printf("%s\n", menuErrorMsg);
  1497. continue;
  1498. }
  1499. engineState = PGS_RestartRun;
  1500. return 1;
  1501. }
  1502. break;
  1503. case 1: // insert nothing
  1504. engineState = PGS_RestartRun;
  1505. return 0;
  1506. }
  1507. }
  1508. menuErrorMsg[0] = 0; // clear error msg
  1509. }
  1510. }