menu.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2006-2008
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. // don't like to use loads of #ifdefs, so duplicating GP2X code
  9. // horribly instead
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <wchar.h>
  14. #include <unistd.h> // for getcwd cegcc implementation
  15. #include <dirent.h> // for opendir
  16. #include <windows.h>
  17. #include "kgsdk/Framework.h"
  18. #include "kgsdk/Framework2D.h"
  19. #include "giz.h"
  20. #include "emu.h"
  21. #include "menu.h"
  22. #include "../common/arm_utils.h"
  23. #include "../common/menu.h"
  24. #include "../common/emu.h"
  25. #include "../common/readpng.h"
  26. #include "../common/input.h"
  27. #include "version.h"
  28. #include <pico/pico_int.h>
  29. #include <pico/patch.h>
  30. #include <zlib.h>
  31. #define gizKeyUnkn "???"
  32. const char * const keyNames[] = {
  33. "LEFT", "RIGHT", "UP", "DOWN", "STOP", "PLAY", "FORWARD", "REWIND",
  34. "L", "R", "HOME", "VOLUME", "BRIGHTNESS", "ALARM", "POWER", gizKeyUnkn,
  35. gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn,
  36. gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn, gizKeyUnkn
  37. };
  38. static unsigned char *bg_buffer = gfx_buffer + 321*240*2;
  39. unsigned char *menu_screen = gfx_buffer; /* draw here and blit later, to avoid flicker */
  40. void menu_darken_bg(void *dst, const void *src, int pixels, int darker);
  41. static void menu_prepare_bg(int use_game_bg);
  42. static unsigned int inp_prev = 0;
  43. void menu_draw_begin(int use_bgbuff)
  44. {
  45. if (use_bgbuff)
  46. memcpy32((int *)menu_screen, (int *)bg_buffer, 321*240*2/4);
  47. }
  48. void menu_draw_end(void)
  49. {
  50. giz_screen = fb_lock(0);
  51. if (giz_screen == NULL)
  52. {
  53. lprintf("%s: Framework2D_LockBuffer() returned NULL\n", __FUNCTION__);
  54. return;
  55. }
  56. memcpy32(giz_screen, (int *)menu_screen, 321*240*2/4);
  57. fb_unlock();
  58. giz_screen = NULL;
  59. fb_flip();
  60. }
  61. // --------- loading ROM screen ----------
  62. static void load_progress_cb(int percent)
  63. {
  64. int ln, len = percent * 320 / 100;
  65. unsigned short *dst;
  66. menu_draw_begin(0);
  67. dst = (unsigned short *)menu_screen + 321*20;
  68. if (len > 320) len = 320;
  69. for (ln = 10; ln > 0; ln--, dst += 321)
  70. memset(dst, 0xff, len*2);
  71. menu_draw_end();
  72. }
  73. void menu_romload_prepare(const char *rom_name)
  74. {
  75. const char *p = rom_name + strlen(rom_name);
  76. while (p > rom_name && *p != '/') p--;
  77. if (!rom_loaded)
  78. memset(menu_screen, 0, 321*240*2);
  79. menu_draw_begin(1);
  80. smalltext_out16(1, 1, "Loading", 0xffff);
  81. smalltext_out16_lim(1, 10, p, 0xffff, 53);
  82. menu_draw_end();
  83. PicoCartLoadProgressCB = load_progress_cb;
  84. }
  85. void menu_romload_end(void)
  86. {
  87. PicoCartLoadProgressCB = NULL;
  88. menu_draw_begin(0);
  89. smalltext_out16(1, 30, "Starting emulation...", 0xffff);
  90. menu_draw_end();
  91. }
  92. // -------------- ROM selector --------------
  93. #define DT_DIR 1
  94. #define DT_REG 2
  95. struct my_dirent
  96. {
  97. unsigned char d_type;
  98. char d_name[0];
  99. };
  100. // rrrr rggg gggb bbbb
  101. #if 1
  102. static unsigned short file2color(const char *fname)
  103. {
  104. const char *ext = fname + strlen(fname) - 3;
  105. static const char *rom_exts[] = { "zip", "bin", "smd", "gen", "iso", "cso" };
  106. static const char *other_exts[] = { "gmv", "pat" };
  107. int i;
  108. if (ext < fname) ext = fname;
  109. for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
  110. if (strcasecmp(ext, rom_exts[i]) == 0) return 0xbdff;
  111. for (i = 0; i < sizeof(other_exts)/sizeof(other_exts[0]); i++)
  112. if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;
  113. return 0xffff;
  114. }
  115. static void draw_dirlist(char *curdir, struct my_dirent **namelist, int n, int sel)
  116. {
  117. int start, i, pos;
  118. start = 12 - sel;
  119. n--; // exclude current dir (".")
  120. menu_draw_begin(1);
  121. if (!rom_loaded) {
  122. menu_darken_bg(menu_screen, menu_screen, 321*240, 0);
  123. }
  124. menu_darken_bg((char *)menu_screen + 321*120*2, (char *)menu_screen + 321*120*2, 321*8, 0);
  125. if(start - 2 >= 0)
  126. smalltext_out16_lim(14, (start - 2)*10, curdir, 0xffff, 53-2);
  127. for (i = 0; i < n; i++) {
  128. pos = start + i;
  129. if (pos < 0) continue;
  130. if (pos > 23) break;
  131. if (namelist[i+1]->d_type == DT_DIR) {
  132. smalltext_out16_lim(14, pos*10, "/", 0xfff6, 1);
  133. smalltext_out16_lim(14+6, pos*10, namelist[i+1]->d_name, 0xfff6, 53-3);
  134. } else {
  135. unsigned short color = file2color(namelist[i+1]->d_name);
  136. smalltext_out16_lim(14, pos*10, namelist[i+1]->d_name, color, 53-2);
  137. }
  138. }
  139. text_out16(5, 120, ">");
  140. menu_draw_end();
  141. }
  142. static int scandir_cmp(const void *p1, const void *p2)
  143. {
  144. struct my_dirent **d1 = (struct my_dirent **)p1, **d2 = (struct my_dirent **)p2;
  145. if ((*d1)->d_type == (*d2)->d_type) return strcasecmp((*d1)->d_name, (*d2)->d_name);
  146. if ((*d1)->d_type == DT_DIR) return -1; // put before
  147. if ((*d2)->d_type == DT_DIR) return 1;
  148. return strcasecmp((*d1)->d_name, (*d2)->d_name);
  149. }
  150. static char *filter_exts[] = {
  151. ".mp3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",
  152. ".jpg", ".cue", ".exe", ".dll"
  153. };
  154. static int scandir_filter(const struct my_dirent *ent)
  155. {
  156. const char *p;
  157. int i;
  158. if (ent == NULL || ent->d_name == NULL) return 0;
  159. if (strlen(ent->d_name) < 5) return 1;
  160. p = ent->d_name + strlen(ent->d_name) - 4;
  161. for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)
  162. {
  163. if (strcasecmp(p, filter_exts[i]) == 0) return 0;
  164. }
  165. return 1;
  166. }
  167. static int cstr2wstr(wchar_t *dst, const char *src)
  168. {
  169. int i, len;
  170. for (i = len = strlen(src); i >= 0; i--)
  171. dst[i] = src[i];
  172. return len;
  173. }
  174. static void wstr2cstr(char *dst, const wchar_t *src)
  175. {
  176. int i;
  177. for (i = wcslen(src); i >= 0; i--)
  178. dst[i] = src[i];
  179. }
  180. static int my_scandir(const char *dir, struct my_dirent ***namelist_out,
  181. int(*filter)(const struct my_dirent *),
  182. int(*compar)(const void *, const void *))
  183. {
  184. wchar_t *wdir;
  185. int i, len, ret = -1, name_alloc = 4, name_count = 0;
  186. struct my_dirent **namelist = NULL, *ent;
  187. WIN32_FIND_DATA finddata;
  188. HANDLE fh = INVALID_HANDLE_VALUE;
  189. BOOL bRet;
  190. wdir = malloc(sizeof(wdir[0]) * MAX_PATH);
  191. if (wdir == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
  192. namelist = malloc(sizeof(*namelist) * name_alloc);
  193. if (namelist == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
  194. // try to read first..
  195. len = cstr2wstr(wdir, dir);
  196. for (i = 0; i < len; i++)
  197. if (wdir[i] == '/') wdir[i] = '\\';
  198. if (wdir[len-1] != '\\') wdir[len++] = '\\';
  199. wdir[len++] = '*';
  200. wdir[len++] = 0;
  201. /*if (wdir[len-1] == '\\') wdir[len-1] = 0;*/
  202. bRet = 1;
  203. fh = FindFirstFile(wdir, &finddata);
  204. if (fh == INVALID_HANDLE_VALUE)
  205. {
  206. if (GetLastError() != ERROR_NO_MORE_FILES)
  207. {
  208. lprintf("FindFirstFile(\"%s\") failed with %lu\n", dir, GetLastError());
  209. goto fail;
  210. }
  211. bRet = 0;
  212. }
  213. // add "." and ".."
  214. ent = malloc(sizeof(*ent) + 3);
  215. ent->d_type = DT_DIR; strcpy(ent->d_name, ".");
  216. namelist[name_count++] = ent;
  217. ent = malloc(sizeof(*ent) + 3);
  218. ent->d_type = DT_DIR; strcpy(ent->d_name, "..");
  219. namelist[name_count++] = ent;
  220. while (bRet)
  221. {
  222. ent = malloc(sizeof(*ent) + wcslen(finddata.cFileName) + 1);
  223. ent->d_type = (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? DT_DIR : DT_REG;
  224. wstr2cstr(ent->d_name, finddata.cFileName);
  225. if (filter == NULL || filter(ent))
  226. namelist[name_count++] = ent;
  227. else free(ent);
  228. if (name_count >= name_alloc)
  229. {
  230. void *tmp;
  231. name_alloc *= 2;
  232. tmp = realloc(namelist, sizeof(*namelist) * name_alloc);
  233. if (tmp == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
  234. namelist = tmp;
  235. }
  236. bRet = FindNextFile(fh, &finddata);
  237. }
  238. // sort
  239. if (compar != NULL && name_count > 3) qsort(&namelist[2], name_count - 2, sizeof(namelist[0]), compar);
  240. // all done.
  241. ret = name_count;
  242. *namelist_out = namelist;
  243. goto end;
  244. fail:
  245. if (namelist != NULL)
  246. {
  247. while (name_count--)
  248. free(namelist[name_count]);
  249. free(namelist);
  250. }
  251. end:
  252. if (fh != INVALID_HANDLE_VALUE) FindClose(fh);
  253. if (wdir != NULL) free(wdir);
  254. return ret;
  255. }
  256. static char *romsel_loop(char *curr_path)
  257. {
  258. struct my_dirent **namelist;
  259. DIR *dir;
  260. int n, sel = 0;
  261. unsigned long inp = 0;
  262. char *ret = NULL, *fname = NULL;
  263. // is this a dir or a full path?
  264. if ((dir = opendir(curr_path))) {
  265. closedir(dir);
  266. } else {
  267. char *p;
  268. for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);
  269. *p = 0;
  270. fname = p+1;
  271. }
  272. n = my_scandir(curr_path, &namelist, scandir_filter, scandir_cmp);
  273. if (n < 0) {
  274. // try root..
  275. n = my_scandir("/", &namelist, scandir_filter, scandir_cmp);
  276. if (n < 0) {
  277. // oops, we failed
  278. lprintf("scandir failed, dir: "); lprintf(curr_path); lprintf("\n");
  279. return NULL;
  280. }
  281. }
  282. // try to find sel
  283. if (fname != NULL) {
  284. int i;
  285. for (i = 1; i < n; i++) {
  286. if (strcmp(namelist[i]->d_name, fname) == 0) {
  287. sel = i - 1;
  288. break;
  289. }
  290. }
  291. }
  292. for (;;)
  293. {
  294. draw_dirlist(curr_path, namelist, n, sel);
  295. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R|PBTN_PLAY|PBTN_STOP);
  296. if(inp & PBTN_UP ) { sel--; if (sel < 0) sel = n-2; }
  297. if(inp & PBTN_DOWN) { sel++; if (sel > n-2) sel = 0; }
  298. if(inp & PBTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }
  299. if(inp & PBTN_L) { sel-=24; if (sel < 0) sel = 0; }
  300. if(inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
  301. if(inp & PBTN_R) { sel+=24; if (sel > n-2) sel = n-2; }
  302. if(inp & PBTN_PLAY) { // enter dir/select
  303. if (namelist[sel+1]->d_type == DT_REG) {
  304. strcpy(romFileName, curr_path);
  305. strcat(romFileName, "/");
  306. strcat(romFileName, namelist[sel+1]->d_name);
  307. ret = romFileName;
  308. break;
  309. } else if (namelist[sel+1]->d_type == DT_DIR) {
  310. int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
  311. char *p, *newdir = malloc(newlen);
  312. if (strcmp(namelist[sel+1]->d_name, "..") == 0) {
  313. char *start = curr_path;
  314. p = start + strlen(start) - 1;
  315. while (*p == '/' && p > start) p--;
  316. while (*p != '/' && p > start) p--;
  317. if (p <= start) strcpy(newdir, "/");
  318. else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
  319. } else {
  320. strcpy(newdir, curr_path);
  321. p = newdir + strlen(newdir) - 1;
  322. while (*p == '/' && p >= newdir) *p-- = 0;
  323. strcat(newdir, "/");
  324. strcat(newdir, namelist[sel+1]->d_name);
  325. }
  326. ret = romsel_loop(newdir);
  327. free(newdir);
  328. break;
  329. }
  330. }
  331. if(inp & PBTN_STOP) break; // cancel
  332. }
  333. if (n > 0) {
  334. while(n--) free(namelist[n]);
  335. free(namelist);
  336. }
  337. return ret;
  338. }
  339. #else
  340. static char *romsel_loop(char *curr_path)
  341. {
  342. return NULL;
  343. }
  344. #endif
  345. // ------------ patch/gg menu ------------
  346. static void draw_patchlist(int sel)
  347. {
  348. int start, i, pos, active;
  349. start = 12 - sel;
  350. menu_draw_begin(1);
  351. for (i = 0; i < PicoPatchCount; i++) {
  352. pos = start + i;
  353. if (pos < 0) continue;
  354. if (pos > 23) break;
  355. active = PicoPatches[i].active;
  356. smalltext_out16_lim(14, pos*10, active ? "ON " : "OFF", active ? 0xfff6 : 0xffff, 3);
  357. smalltext_out16_lim(14+6*4, pos*10, PicoPatches[i].name, active ? 0xfff6 : 0xffff, 53-6);
  358. }
  359. pos = start + i;
  360. if (pos < 24) smalltext_out16_lim(14, pos*10, "done", 0xffff, 4);
  361. text_out16(5, 120, ">");
  362. menu_draw_end();
  363. }
  364. static void patches_menu_loop(void)
  365. {
  366. int menu_sel = 0;
  367. unsigned long inp = 0;
  368. for(;;)
  369. {
  370. draw_patchlist(menu_sel);
  371. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R|PBTN_PLAY|PBTN_STOP);
  372. if(inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }
  373. if(inp & PBTN_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }
  374. if(inp &(PBTN_LEFT|PBTN_L)) { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }
  375. if(inp &(PBTN_RIGHT|PBTN_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }
  376. if(inp & PBTN_PLAY) { // action
  377. if (menu_sel < PicoPatchCount)
  378. PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;
  379. else return;
  380. }
  381. if(inp & PBTN_STOP) return;
  382. }
  383. }
  384. // ------------ savestate loader ------------
  385. static int state_slot_flags = 0;
  386. static void state_check_slots(void)
  387. {
  388. int slot;
  389. state_slot_flags = 0;
  390. for (slot = 0; slot < 10; slot++)
  391. {
  392. if (emu_checkSaveFile(slot))
  393. {
  394. state_slot_flags |= 1 << slot;
  395. }
  396. }
  397. }
  398. static void draw_savestate_bg(int slot)
  399. {
  400. struct PicoVideo tmp_pv;
  401. unsigned short tmp_cram[0x40];
  402. unsigned short tmp_vsram[0x40];
  403. void *tmp_vram, *file;
  404. char *fname;
  405. fname = emu_GetSaveFName(1, 0, slot);
  406. if (!fname) return;
  407. tmp_vram = malloc(sizeof(Pico.vram));
  408. if (tmp_vram == NULL) return;
  409. memcpy(tmp_vram, Pico.vram, sizeof(Pico.vram));
  410. memcpy(tmp_cram, Pico.cram, sizeof(Pico.cram));
  411. memcpy(tmp_vsram, Pico.vsram, sizeof(Pico.vsram));
  412. memcpy(&tmp_pv, &Pico.video, sizeof(Pico.video));
  413. if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {
  414. file = gzopen(fname, "rb");
  415. emu_setSaveStateCbs(1);
  416. } else {
  417. file = fopen(fname, "rb");
  418. emu_setSaveStateCbs(0);
  419. }
  420. if (file) {
  421. if (PicoAHW & 1) {
  422. PicoCdLoadStateGfx(file);
  423. } else {
  424. areaSeek(file, 0x10020, SEEK_SET); // skip header and RAM in state file
  425. areaRead(Pico.vram, 1, sizeof(Pico.vram), file);
  426. areaSeek(file, 0x2000, SEEK_CUR);
  427. areaRead(Pico.cram, 1, sizeof(Pico.cram), file);
  428. areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);
  429. areaSeek(file, 0x221a0, SEEK_SET);
  430. areaRead(&Pico.video, 1, sizeof(Pico.video), file);
  431. }
  432. areaClose(file);
  433. }
  434. emu_forcedFrame(POPT_EN_SOFTSCALE);
  435. menu_prepare_bg(1);
  436. memcpy(Pico.vram, tmp_vram, sizeof(Pico.vram));
  437. memcpy(Pico.cram, tmp_cram, sizeof(Pico.cram));
  438. memcpy(Pico.vsram, tmp_vsram, sizeof(Pico.vsram));
  439. memcpy(&Pico.video, &tmp_pv, sizeof(Pico.video));
  440. free(tmp_vram);
  441. }
  442. static void draw_savestate_menu(int menu_sel, int is_loading)
  443. {
  444. int tl_x = 25, tl_y = 60, y, i;
  445. if (state_slot_flags & (1 << menu_sel))
  446. draw_savestate_bg(menu_sel);
  447. menu_draw_begin(1);
  448. text_out16(tl_x, 30, is_loading ? "Load state" : "Save state");
  449. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 108);
  450. /* draw all 10 slots */
  451. y = tl_y;
  452. for (i = 0; i < 10; i++, y+=10)
  453. {
  454. text_out16(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");
  455. }
  456. text_out16(tl_x, y, "back");
  457. menu_draw_end();
  458. }
  459. static int savestate_menu_loop(int is_loading)
  460. {
  461. static int menu_sel = 10;
  462. int menu_sel_max = 10;
  463. unsigned long inp = 0;
  464. state_check_slots();
  465. for(;;)
  466. {
  467. draw_savestate_menu(menu_sel, is_loading);
  468. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_PLAY|PBTN_STOP);
  469. if(inp & PBTN_UP ) {
  470. do {
  471. menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;
  472. } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
  473. }
  474. if(inp & PBTN_DOWN) {
  475. do {
  476. menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;
  477. } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);
  478. }
  479. if(inp & PBTN_PLAY) { // save/load
  480. if (menu_sel < 10) {
  481. state_slot = menu_sel;
  482. PicoStateProgressCB = emu_stateCb; /* also suitable for menu */
  483. if (emu_SaveLoadGame(is_loading, 0)) {
  484. strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");
  485. return 1;
  486. }
  487. return 0;
  488. } else return 1;
  489. }
  490. if(inp & PBTN_STOP) return 1;
  491. }
  492. }
  493. // -------------- key config --------------
  494. static char *action_binds(int player_idx, int action_mask)
  495. {
  496. static char strkeys[32*5];
  497. int i;
  498. strkeys[0] = 0;
  499. for (i = 0; i < 32; i++) // i is key index
  500. {
  501. if (currentConfig.KeyBinds[i] & action_mask)
  502. {
  503. if (player_idx >= 0 && ((currentConfig.KeyBinds[i] >> 16) & 3) != player_idx) continue;
  504. if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, keyNames[i]); break; }
  505. else strcpy(strkeys, keyNames[i]);
  506. }
  507. }
  508. return strkeys;
  509. }
  510. static void unbind_action(int action)
  511. {
  512. int i;
  513. for (i = 0; i < 32; i++)
  514. currentConfig.KeyBinds[i] &= ~action;
  515. }
  516. static int count_bound_keys(int action, int pl_idx)
  517. {
  518. int i, keys = 0;
  519. for (i = 0; i < 32; i++)
  520. {
  521. if (pl_idx >= 0 && (currentConfig.KeyBinds[i]&0x30000) != (pl_idx<<16)) continue;
  522. if (currentConfig.KeyBinds[i] & action) keys++;
  523. }
  524. return keys;
  525. }
  526. static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx, int sel)
  527. {
  528. int x, y, tl_y = 30, i;
  529. menu_draw_begin(1);
  530. if (player_idx >= 0) {
  531. text_out16(80, 10, "Player %i controls", player_idx + 1);
  532. x = 80;
  533. } else {
  534. text_out16(80, 10, "Emulator controls");
  535. x = 40;
  536. }
  537. menu_draw_selection(x - 16, tl_y + sel*10, (player_idx >= 0) ? 66 : 130);
  538. y = tl_y;
  539. for (i = 0; i < opt_cnt; i++, y+=10)
  540. text_out16(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));
  541. text_out16(x, y, "Done");
  542. if (sel < opt_cnt) {
  543. text_out16(30, 195, "Press a button to bind/unbind");
  544. text_out16(30, 205, "Use HOME to clear");
  545. text_out16(30, 215, "To bind UP/DOWN, hold HOME");
  546. text_out16(30, 225, "Select \"Done\" to exit");
  547. } else {
  548. text_out16(30, 205, "Use Options -> Save cfg");
  549. text_out16(30, 215, "to save controls");
  550. text_out16(30, 225, "Press PLAY or STOP to exit");
  551. }
  552. menu_draw_end();
  553. }
  554. static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)
  555. {
  556. int sel = 0, menu_sel_max = opt_cnt, prev_select = 0, i;
  557. unsigned long inp = 0;
  558. for (;;)
  559. {
  560. draw_key_config(opts, opt_cnt, player_idx, sel);
  561. inp = in_menu_wait(CONFIGURABLE_KEYS|PBTN_HOME);
  562. if (!(inp & PBTN_HOME)) {
  563. prev_select = 0;
  564. if(inp & PBTN_UP ) { sel--; if (sel < 0) sel = menu_sel_max; continue; }
  565. if(inp & PBTN_DOWN) { sel++; if (sel > menu_sel_max) sel = 0; continue; }
  566. }
  567. if (sel >= opt_cnt) {
  568. if (inp & (PBTN_PLAY|PBTN_STOP)) break;
  569. else continue;
  570. }
  571. // if we are here, we want to bind/unbind something
  572. if ((inp & PBTN_HOME) && !prev_select)
  573. unbind_action(opts[sel].mask);
  574. prev_select = inp & PBTN_HOME;
  575. inp &= CONFIGURABLE_KEYS;
  576. inp &= ~PBTN_HOME;
  577. for (i = 0; i < 32; i++)
  578. if (inp & (1 << i)) {
  579. if (count_bound_keys(opts[sel].mask, player_idx) >= 2)
  580. currentConfig.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only
  581. else currentConfig.KeyBinds[i] ^= opts[sel].mask;
  582. if (player_idx >= 0 && (currentConfig.KeyBinds[i] & opts[sel].mask)) {
  583. currentConfig.KeyBinds[i] &= ~(3 << 16);
  584. currentConfig.KeyBinds[i] |= player_idx << 16;
  585. }
  586. }
  587. }
  588. }
  589. menu_entry ctrlopt_entries[] =
  590. {
  591. { "Player 1", MB_NONE, MA_CTRL_PLAYER1, NULL, 0, 0, 0, 1, 0 },
  592. { "Player 2", MB_NONE, MA_CTRL_PLAYER2, NULL, 0, 0, 0, 1, 0 },
  593. { "Emulator controls", MB_NONE, MA_CTRL_EMU, NULL, 0, 0, 0, 1, 0 },
  594. { "6 button pad", MB_ONOFF, MA_OPT_6BUTTON_PAD, &PicoOpt, 0x020, 0, 0, 1, 1 },
  595. { "Turbo rate", MB_RANGE, MA_CTRL_TURBO_RATE, &currentConfig.turbo_rate, 0, 1, 30, 1, 1 },
  596. { "Done", MB_NONE, MA_CTRL_DONE, NULL, 0, 0, 0, 1, 0 },
  597. };
  598. #define CTRLOPT_ENTRY_COUNT (sizeof(ctrlopt_entries) / sizeof(ctrlopt_entries[0]))
  599. const int ctrlopt_entry_count = CTRLOPT_ENTRY_COUNT;
  600. static void draw_kc_sel(int menu_sel)
  601. {
  602. int tl_x = 25+40, tl_y = 60, y;
  603. y = tl_y;
  604. menu_draw_begin(1);
  605. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 138);
  606. me_draw(ctrlopt_entries, ctrlopt_entry_count, tl_x, tl_y, NULL, NULL);
  607. menu_draw_end();
  608. }
  609. // player2_flag, ?, ?, ?, ?, ?, ?, menu
  610. // "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",
  611. // "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"
  612. me_bind_action emuctrl_actions[] =
  613. {
  614. { "Load State ", 1<<28 },
  615. { "Save State ", 1<<27 },
  616. { "Prev Save Slot ", 1<<25 },
  617. { "Next Save Slot ", 1<<24 },
  618. { "Switch Renderer", 1<<26 },
  619. { "Volume Down ", 1<<30 },
  620. { "Volume Up ", 1<<29 },
  621. { NULL, 0 }
  622. };
  623. static void kc_sel_loop(void)
  624. {
  625. int menu_sel = 5, menu_sel_max = 5;
  626. unsigned long inp = 0;
  627. menu_id selected_id;
  628. while (1)
  629. {
  630. draw_kc_sel(menu_sel);
  631. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_PLAY|PBTN_STOP);
  632. selected_id = me_index2id(ctrlopt_entries, CTRLOPT_ENTRY_COUNT, menu_sel);
  633. if (inp & (PBTN_LEFT|PBTN_RIGHT)) // multi choise
  634. me_process(ctrlopt_entries, CTRLOPT_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0);
  635. if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  636. if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  637. if (inp & PBTN_PLAY) {
  638. int is_6button = PicoOpt & 0x020;
  639. switch (selected_id) {
  640. case MA_CTRL_PLAYER1: key_config_loop(me_ctrl_actions, is_6button ? 15 : 11, 0); return;
  641. case MA_CTRL_PLAYER2: key_config_loop(me_ctrl_actions, is_6button ? 15 : 11, 1); return;
  642. case MA_CTRL_EMU: key_config_loop(emuctrl_actions,
  643. sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]) - 1, -1); return;
  644. case MA_CTRL_DONE: if (!rom_loaded) emu_WriteConfig(0); return;
  645. default: return;
  646. }
  647. }
  648. if (inp & PBTN_STOP) return;
  649. }
  650. }
  651. // --------- sega/mega cd options ----------
  652. menu_entry cdopt_entries[] =
  653. {
  654. { NULL, MB_NONE, MA_CDOPT_TESTBIOS_USA, NULL, 0, 0, 0, 1, 0 },
  655. { NULL, MB_NONE, MA_CDOPT_TESTBIOS_EUR, NULL, 0, 0, 0, 1, 0 },
  656. { NULL, MB_NONE, MA_CDOPT_TESTBIOS_JAP, NULL, 0, 0, 0, 1, 0 },
  657. { "CD LEDs", MB_ONOFF, MA_CDOPT_LEDS, &currentConfig.EmuOpt, 0x0400, 0, 0, 1, 1 },
  658. { "CDDA audio", MB_ONOFF, MA_CDOPT_CDDA, &PicoOpt, 0x0800, 0, 0, 1, 1 },
  659. { "PCM audio", MB_ONOFF, MA_CDOPT_PCM, &PicoOpt, 0x0400, 0, 0, 1, 1 },
  660. { NULL, MB_NONE, MA_CDOPT_READAHEAD, NULL, 0, 0, 0, 1, 1 },
  661. { "SaveRAM cart", MB_ONOFF, MA_CDOPT_SAVERAM, &PicoOpt, 0x8000, 0, 0, 1, 1 },
  662. { "Scale/Rot. fx (slow)", MB_ONOFF, MA_CDOPT_SCALEROT_CHIP,&PicoOpt, 0x1000, 0, 0, 1, 1 },
  663. { "Better sync (slow)", MB_ONOFF, MA_CDOPT_BETTER_SYNC, &PicoOpt, 0x2000, 0, 0, 1, 1 },
  664. { "done", MB_NONE, MA_CDOPT_DONE, NULL, 0, 0, 0, 1, 0 },
  665. };
  666. #define CDOPT_ENTRY_COUNT (sizeof(cdopt_entries) / sizeof(cdopt_entries[0]))
  667. const int cdopt_entry_count = CDOPT_ENTRY_COUNT;
  668. struct bios_names_t
  669. {
  670. char us[32], eu[32], jp[32];
  671. };
  672. static void menu_cdopt_cust_draw(const menu_entry *entry, int x, int y, void *param)
  673. {
  674. struct bios_names_t *bios_names = param;
  675. char ra_buff[16];
  676. switch (entry->id)
  677. {
  678. case MA_CDOPT_TESTBIOS_USA: text_out16(x, y, "USA BIOS: %s", bios_names->us); break;
  679. case MA_CDOPT_TESTBIOS_EUR: text_out16(x, y, "EUR BIOS: %s", bios_names->eu); break;
  680. case MA_CDOPT_TESTBIOS_JAP: text_out16(x, y, "JAP BIOS: %s", bios_names->jp); break;
  681. case MA_CDOPT_READAHEAD:
  682. if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
  683. else strcpy(ra_buff, " OFF");
  684. text_out16(x, y, "ReadAhead buffer %s", ra_buff);
  685. break;
  686. default:break;
  687. }
  688. }
  689. static void draw_cd_menu_options(int menu_sel, struct bios_names_t *bios_names)
  690. {
  691. int tl_x = 25, tl_y = 60;
  692. menu_id selected_id;
  693. char ra_buff[16];
  694. if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);
  695. else strcpy(ra_buff, " OFF");
  696. menu_draw_begin(1);
  697. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 246);
  698. me_draw(cdopt_entries, CDOPT_ENTRY_COUNT, tl_x, tl_y, menu_cdopt_cust_draw, bios_names);
  699. selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
  700. if ((selected_id == MA_CDOPT_TESTBIOS_USA && strcmp(bios_names->us, "NOT FOUND")) ||
  701. (selected_id == MA_CDOPT_TESTBIOS_EUR && strcmp(bios_names->eu, "NOT FOUND")) ||
  702. (selected_id == MA_CDOPT_TESTBIOS_JAP && strcmp(bios_names->jp, "NOT FOUND")))
  703. text_out16(tl_x, 210, "Press start to test selected BIOS");
  704. menu_draw_end();
  705. }
  706. static void cd_menu_loop_options(void)
  707. {
  708. static int menu_sel = 0;
  709. int menu_sel_max = 10;
  710. unsigned long inp = 0;
  711. struct bios_names_t bios_names;
  712. menu_id selected_id;
  713. char *bios, *p;
  714. if (emu_findBios(4, &bios)) { // US
  715. for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
  716. if (*p == '/') p++;
  717. strncpy(bios_names.us, p, sizeof(bios_names.us)); bios_names.us[sizeof(bios_names.us)-1] = 0;
  718. } else strcpy(bios_names.us, "NOT FOUND");
  719. if (emu_findBios(8, &bios)) { // EU
  720. for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
  721. if (*p == '/') p++;
  722. strncpy(bios_names.eu, p, sizeof(bios_names.eu)); bios_names.eu[sizeof(bios_names.eu)-1] = 0;
  723. } else strcpy(bios_names.eu, "NOT FOUND");
  724. if (emu_findBios(1, &bios)) { // JP
  725. for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--);
  726. if (*p == '/') p++;
  727. strncpy(bios_names.jp, p, sizeof(bios_names.jp)); bios_names.jp[sizeof(bios_names.jp)-1] = 0;
  728. } else strcpy(bios_names.jp, "NOT FOUND");
  729. for(;;)
  730. {
  731. draw_cd_menu_options(menu_sel, &bios_names);
  732. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_PLAY|PBTN_STOP|PBTN_REW);
  733. if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  734. if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  735. selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);
  736. if (inp & (PBTN_LEFT|PBTN_RIGHT)) { // multi choise
  737. if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0) &&
  738. selected_id == MA_CDOPT_READAHEAD) {
  739. if (inp & PBTN_LEFT) {
  740. PicoCDBuffers >>= 1;
  741. if (PicoCDBuffers < 2) PicoCDBuffers = 0;
  742. } else {
  743. if (PicoCDBuffers < 2) PicoCDBuffers = 2;
  744. else PicoCDBuffers <<= 1;
  745. if (PicoCDBuffers > 8*1024) PicoCDBuffers = 8*1024; // 16M
  746. }
  747. }
  748. }
  749. if (inp & PBTN_PLAY) { // toggleable options
  750. if (!me_process(cdopt_entries, CDOPT_ENTRY_COUNT, selected_id, 1) &&
  751. selected_id == MA_CDOPT_DONE) {
  752. return;
  753. }
  754. switch (selected_id) { // BIOS testers
  755. case MA_CDOPT_TESTBIOS_USA:
  756. if (emu_findBios(4, &bios)) { // test US
  757. strcpy(romFileName, bios);
  758. engineState = PGS_ReloadRom;
  759. return;
  760. }
  761. break;
  762. case MA_CDOPT_TESTBIOS_EUR:
  763. if (emu_findBios(8, &bios)) { // test EU
  764. strcpy(romFileName, bios);
  765. engineState = PGS_ReloadRom;
  766. return;
  767. }
  768. break;
  769. case MA_CDOPT_TESTBIOS_JAP:
  770. if (emu_findBios(1, &bios)) { // test JP
  771. strcpy(romFileName, bios);
  772. engineState = PGS_ReloadRom;
  773. return;
  774. }
  775. break;
  776. default:
  777. break;
  778. }
  779. }
  780. if (inp & (PBTN_STOP|PBTN_REW)) return;
  781. }
  782. }
  783. // --------- advanced options ----------
  784. menu_entry opt2_entries[] =
  785. {
  786. { "Disable sprite limit", MB_ONOFF, MA_OPT2_NO_SPRITE_LIM, &PicoOpt, 0x40000, 0, 0, 1, 1 },
  787. { "Emulate Z80", MB_ONOFF, MA_OPT2_ENABLE_Z80, &PicoOpt, 0x00004, 0, 0, 1, 1 },
  788. { "Emulate YM2612 (FM)", MB_ONOFF, MA_OPT2_ENABLE_YM2612, &PicoOpt, 0x00001, 0, 0, 1, 1 },
  789. { "Emulate SN76496 (PSG)", MB_ONOFF, MA_OPT2_ENABLE_SN76496,&PicoOpt, 0x00002, 0, 0, 1, 1 },
  790. { "Double buffering", MB_ONOFF, MA_OPT2_DBLBUFF, &currentConfig.EmuOpt, 0x8000, 0, 0, 1, 1 },
  791. { "Wait for V-sync (slow)", MB_ONOFF, MA_OPT2_VSYNC, &currentConfig.EmuOpt, 0x2000, 0, 0, 1, 1 },
  792. { "gzip savestates", MB_ONOFF, MA_OPT2_GZIP_STATES, &currentConfig.EmuOpt, 0x0008, 0, 0, 1, 1 },
  793. { "Don't save last used ROM", MB_ONOFF, MA_OPT2_NO_LAST_ROM, &currentConfig.EmuOpt, 0x0020, 0, 0, 1, 1 },
  794. { "SVP dynarec", MB_ONOFF, MA_OPT2_SVP_DYNAREC, &PicoOpt, 0x20000, 0, 0, 1, 1 },
  795. { "Disable idle loop patching",MB_ONOFF, MA_OPT2_NO_IDLE_LOOPS, &PicoOpt, 0x80000, 0, 0, 1, 1 },
  796. { "done", MB_NONE, MA_OPT2_DONE, NULL, 0, 0, 0, 1, 0 },
  797. };
  798. #define OPT2_ENTRY_COUNT (sizeof(opt2_entries) / sizeof(opt2_entries[0]))
  799. const int opt2_entry_count = OPT2_ENTRY_COUNT;
  800. static void draw_amenu_options(int menu_sel)
  801. {
  802. int tl_x = 25, tl_y = 50;
  803. menu_draw_begin(1);
  804. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 252);
  805. me_draw(opt2_entries, OPT2_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
  806. menu_draw_end();
  807. }
  808. static void amenu_loop_options(void)
  809. {
  810. static int menu_sel = 0;
  811. int menu_sel_max;
  812. unsigned long inp = 0;
  813. menu_id selected_id;
  814. menu_sel_max = me_count_enabled(opt2_entries, OPT2_ENTRY_COUNT) - 1;
  815. for(;;)
  816. {
  817. draw_amenu_options(menu_sel);
  818. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_PLAY|PBTN_STOP|PBTN_REW);
  819. if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  820. if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  821. selected_id = me_index2id(opt2_entries, OPT2_ENTRY_COUNT, menu_sel);
  822. if (inp & (PBTN_LEFT|PBTN_RIGHT)) { // multi choise
  823. if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0) &&
  824. selected_id == MA_OPT2_GAMMA) {
  825. while ((inp = Framework_PollGetButtons()) & (PBTN_LEFT|PBTN_RIGHT)) {
  826. currentConfig.gamma += (inp & PBTN_LEFT) ? -1 : 1;
  827. if (currentConfig.gamma < 1) currentConfig.gamma = 1;
  828. if (currentConfig.gamma > 300) currentConfig.gamma = 300;
  829. draw_amenu_options(menu_sel);
  830. Sleep(18);
  831. }
  832. }
  833. }
  834. if (inp & PBTN_PLAY) { // toggleable options
  835. if (!me_process(opt2_entries, OPT2_ENTRY_COUNT, selected_id, 1) &&
  836. selected_id == MA_OPT2_DONE) {
  837. return;
  838. }
  839. }
  840. if (inp & (PBTN_STOP|PBTN_REW)) return;
  841. }
  842. }
  843. // -------------- options --------------
  844. menu_entry opt_entries[] =
  845. {
  846. { NULL, MB_NONE, MA_OPT_RENDERER, NULL, 0, 0, 0, 1, 1 },
  847. { "Accurate sprites", MB_ONOFF, MA_OPT_ACC_SPRITES, &PicoOpt, 0x080, 0, 0, 0, 1 },
  848. { "Scanline mode (faster)", MB_ONOFF, MA_OPT_INTERLACED, &currentConfig.EmuOpt, 0x4000, 0, 0, 1, 1 },
  849. { "Scale low res mode", MB_ONOFF, MA_OPT_SCALING, &currentConfig.scaling, 0x0001, 0, 3, 1, 1 },
  850. { "Show FPS", MB_ONOFF, MA_OPT_SHOW_FPS, &currentConfig.EmuOpt, 0x0002, 0, 0, 1, 1 },
  851. { NULL, MB_RANGE, MA_OPT_FRAMESKIP, &currentConfig.Frameskip, 0, -1, 16, 1, 1 },
  852. { "Enable sound", MB_ONOFF, MA_OPT_ENABLE_SOUND, &currentConfig.EmuOpt, 0x0004, 0, 0, 1, 1 },
  853. { NULL, MB_NONE, MA_OPT_SOUND_QUALITY, NULL, 0, 0, 0, 1, 1 },
  854. { NULL, MB_NONE, MA_OPT_REGION, NULL, 0, 0, 0, 1, 1 },
  855. { "Use SRAM/BRAM savestates", MB_ONOFF, MA_OPT_SRAM_STATES, &currentConfig.EmuOpt, 0x0001, 0, 0, 1, 1 },
  856. { NULL, MB_NONE, MA_OPT_CONFIRM_STATES,NULL, 0, 0, 0, 1, 1 },
  857. { "Save slot", MB_RANGE, MA_OPT_SAVE_SLOT, &state_slot, 0, 0, 9, 1, 1 },
  858. { "[Sega/Mega CD options]", MB_NONE, MA_OPT_SCD_OPTS, NULL, 0, 0, 0, 1, 0 },
  859. { "[advanced options]", MB_NONE, MA_OPT_ADV_OPTS, NULL, 0, 0, 0, 1, 0 },
  860. { NULL, MB_NONE, MA_OPT_SAVECFG, NULL, 0, 0, 0, 1, 0 },
  861. { "Save cfg for current game only",MB_NONE,MA_OPT_SAVECFG_GAME,NULL, 0, 0, 0, 1, 0 },
  862. { NULL, MB_NONE, MA_OPT_LOADCFG, NULL, 0, 0, 0, 1, 0 },
  863. };
  864. #define OPT_ENTRY_COUNT (sizeof(opt_entries) / sizeof(opt_entries[0]))
  865. const int opt_entry_count = OPT_ENTRY_COUNT;
  866. static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *param)
  867. {
  868. char *str, str24[24];
  869. switch (entry->id)
  870. {
  871. case MA_OPT_RENDERER:
  872. if (PicoOpt&0x10)
  873. str = " 8bit fast";
  874. else if (currentConfig.EmuOpt&0x80)
  875. str = "16bit accurate";
  876. else
  877. str = " 8bit accurate";
  878. text_out16(x, y, "Renderer: %s", str);
  879. break;
  880. case MA_OPT_FRAMESKIP:
  881. if (currentConfig.Frameskip < 0)
  882. strcpy(str24, "Auto");
  883. else sprintf(str24, "%i", currentConfig.Frameskip);
  884. text_out16(x, y, "Frameskip %s", str24);
  885. break;
  886. case MA_OPT_SOUND_QUALITY:
  887. str = (PicoOpt&0x08)?"stereo":"mono";
  888. text_out16(x, y, "Sound Quality: %5iHz %s", PsndRate, str);
  889. break;
  890. case MA_OPT_REGION:
  891. text_out16(x, y, "Region: %s", me_region_name(PicoRegionOverride, PicoAutoRgnOrder));
  892. break;
  893. case MA_OPT_CONFIRM_STATES:
  894. switch ((currentConfig.EmuOpt >> 9) & 5) {
  895. default: str = "OFF"; break;
  896. case 1: str = "writes"; break;
  897. case 4: str = "loads"; break;
  898. case 5: str = "both"; break;
  899. }
  900. text_out16(x, y, "Confirm savestate %s", str);
  901. break;
  902. case MA_OPT_SAVECFG:
  903. str24[0] = 0;
  904. if (config_slot != 0) sprintf(str24, " (profile: %i)", config_slot);
  905. text_out16(x, y, "Save cfg as default%s", str24);
  906. break;
  907. case MA_OPT_LOADCFG:
  908. text_out16(x, y, "Load cfg from profile %i", config_slot);
  909. break;
  910. default:
  911. lprintf("%s: unimplemented (%i)\n", __FUNCTION__, entry->id);
  912. break;
  913. }
  914. }
  915. static void draw_menu_options(int menu_sel)
  916. {
  917. int tl_x = 25, tl_y = 24;
  918. menu_draw_begin(1);
  919. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 284);
  920. me_draw(opt_entries, OPT_ENTRY_COUNT, tl_x, tl_y, menu_opt_cust_draw, NULL);
  921. menu_draw_end();
  922. }
  923. static int sndrate_prevnext(int rate, int dir)
  924. {
  925. int i, rates[] = { 11025, 22050, 44100 };
  926. for (i = 0; i < 5; i++)
  927. if (rates[i] == rate) break;
  928. i += dir ? 1 : -1;
  929. if (i > 2) return dir ? 44100 : 22050;
  930. if (i < 0) return dir ? 22050 : 11025;
  931. return rates[i];
  932. }
  933. static void region_prevnext(int right)
  934. {
  935. // jp_ntsc=1, jp_pal=2, usa=4, eu=8
  936. static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };
  937. int i;
  938. if (right) {
  939. if (!PicoRegionOverride) {
  940. for (i = 0; i < 6; i++)
  941. if (rgn_orders[i] == PicoAutoRgnOrder) break;
  942. if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];
  943. else PicoRegionOverride=1;
  944. }
  945. else PicoRegionOverride<<=1;
  946. if (PicoRegionOverride > 8) PicoRegionOverride = 8;
  947. } else {
  948. if (!PicoRegionOverride) {
  949. for (i = 0; i < 6; i++)
  950. if (rgn_orders[i] == PicoAutoRgnOrder) break;
  951. if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];
  952. }
  953. else PicoRegionOverride>>=1;
  954. }
  955. }
  956. static void menu_options_save(void)
  957. {
  958. if (PicoRegionOverride) {
  959. // force setting possibly changed..
  960. Pico.m.pal = (PicoRegionOverride == 2 || PicoRegionOverride == 8) ? 1 : 0;
  961. }
  962. if (!(PicoOpt & 0x20)) {
  963. // unbind XYZ MODE, just in case
  964. unbind_action(0xf00);
  965. }
  966. }
  967. static int menu_loop_options(void)
  968. {
  969. static int menu_sel = 0;
  970. int menu_sel_max, ret;
  971. unsigned long inp = 0;
  972. menu_id selected_id;
  973. me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_SAVECFG_GAME, rom_loaded);
  974. me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
  975. menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
  976. if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
  977. while (1)
  978. {
  979. draw_menu_options(menu_sel);
  980. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_PLAY|PBTN_STOP|PBTN_REW);
  981. if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  982. if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  983. selected_id = me_index2id(opt_entries, OPT_ENTRY_COUNT, menu_sel);
  984. if (inp & (PBTN_LEFT|PBTN_RIGHT)) { // multi choise
  985. if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, (inp&PBTN_RIGHT) ? 1 : 0)) {
  986. switch (selected_id) {
  987. case MA_OPT_RENDERER:
  988. if (inp & PBTN_LEFT) {
  989. if ((PicoOpt&0x10) || !(currentConfig.EmuOpt &0x80)) {
  990. PicoOpt&= ~0x10;
  991. currentConfig.EmuOpt |= 0x80;
  992. }
  993. } else {
  994. if (!(PicoOpt&0x10) || (currentConfig.EmuOpt &0x80)) {
  995. PicoOpt|= 0x10;
  996. currentConfig.EmuOpt &= ~0x80;
  997. }
  998. }
  999. break;
  1000. case MA_OPT_SOUND_QUALITY:
  1001. if ((inp & PBTN_RIGHT) && PsndRate == 44100 &&
  1002. !(PicoOpt&0x08))
  1003. {
  1004. PsndRate = 11025;
  1005. PicoOpt |= 8;
  1006. } else if ((inp & PBTN_LEFT) && PsndRate == 11025 &&
  1007. (PicoOpt&0x08) && !(PicoAHW&1))
  1008. {
  1009. PsndRate = 44100;
  1010. PicoOpt &= ~8;
  1011. } else
  1012. PsndRate = sndrate_prevnext(PsndRate, inp & PBTN_RIGHT);
  1013. break;
  1014. case MA_OPT_REGION:
  1015. region_prevnext(inp & PBTN_RIGHT);
  1016. break;
  1017. case MA_OPT_CONFIRM_STATES: {
  1018. int n = ((currentConfig.EmuOpt>>9)&1) | ((currentConfig.EmuOpt>>10)&2);
  1019. n += (inp & PBTN_LEFT) ? -1 : 1;
  1020. if (n < 0) n = 0; else if (n > 3) n = 3;
  1021. n |= n << 1; n &= ~2;
  1022. currentConfig.EmuOpt &= ~0xa00;
  1023. currentConfig.EmuOpt |= n << 9;
  1024. break;
  1025. }
  1026. case MA_OPT_SAVE_SLOT:
  1027. if (inp & PBTN_RIGHT) {
  1028. state_slot++; if (state_slot > 9) state_slot = 0;
  1029. } else {state_slot--; if (state_slot < 0) state_slot = 9;
  1030. }
  1031. break;
  1032. case MA_OPT_SAVECFG:
  1033. case MA_OPT_SAVECFG_GAME:
  1034. case MA_OPT_LOADCFG:
  1035. config_slot += (inp&PBTN_RIGHT) ? 1 : -1;
  1036. if (config_slot > 9) config_slot = 0;
  1037. if (config_slot < 0) config_slot = 9;
  1038. me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);
  1039. menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;
  1040. if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
  1041. break;
  1042. default:
  1043. //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
  1044. break;
  1045. }
  1046. }
  1047. }
  1048. if (inp & PBTN_PLAY) {
  1049. if (!me_process(opt_entries, OPT_ENTRY_COUNT, selected_id, 1))
  1050. {
  1051. switch (selected_id)
  1052. {
  1053. case MA_OPT_SCD_OPTS:
  1054. cd_menu_loop_options();
  1055. if (engineState == PGS_ReloadRom)
  1056. return 0; // test BIOS
  1057. break;
  1058. case MA_OPT_ADV_OPTS:
  1059. amenu_loop_options();
  1060. break;
  1061. case MA_OPT_SAVECFG: // done (update and write)
  1062. menu_options_save();
  1063. if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");
  1064. else strcpy(menuErrorMsg, "failed to write config");
  1065. return 1;
  1066. case MA_OPT_SAVECFG_GAME: // done (update and write for current game)
  1067. menu_options_save();
  1068. if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");
  1069. else strcpy(menuErrorMsg, "failed to write config");
  1070. return 1;
  1071. case MA_OPT_LOADCFG:
  1072. ret = emu_ReadConfig(1, 1);
  1073. if (!ret) ret = emu_ReadConfig(0, 1);
  1074. if (ret) strcpy(menuErrorMsg, "config loaded");
  1075. else strcpy(menuErrorMsg, "failed to load config");
  1076. return 1;
  1077. default:
  1078. //lprintf("%s: something unknown selected (%i)\n", __FUNCTION__, selected_id);
  1079. break;
  1080. }
  1081. }
  1082. }
  1083. if(inp & (PBTN_STOP|PBTN_REW)) {
  1084. menu_options_save();
  1085. return 0; // done (update, no write)
  1086. }
  1087. }
  1088. }
  1089. // -------------- credits --------------
  1090. static void draw_menu_credits(void)
  1091. {
  1092. int tl_x = 15, tl_y = 56, y;
  1093. menu_draw_begin(1);
  1094. text_out16(tl_x, 20, "PicoDrive v" VERSION " (c) notaz, 2006-2008");
  1095. y = tl_y;
  1096. text_out16(tl_x, y, "Credits:");
  1097. text_out16(tl_x, (y+=10), "fDave: Cyclone 68000 core,");
  1098. text_out16(tl_x, (y+=10), " base code of PicoDrive");
  1099. text_out16(tl_x, (y+=10), "Reesy & FluBBa: DrZ80 core");
  1100. text_out16(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");
  1101. text_out16(tl_x, (y+=10), "Reesy: kgsdk wrapper, sound code");
  1102. text_out16(tl_x, (y+=10), "jens.l: gizmondo hardware");
  1103. text_out16(tl_x, (y+=10), "ketchupgun: skin design");
  1104. text_out16(tl_x, (y+=20), "special thanks (for docs, ideas)");
  1105. text_out16(tl_x, (y+=10), " Charles MacDonald, Haze,");
  1106. text_out16(tl_x, (y+=10), " Stephane Dallongeville,");
  1107. text_out16(tl_x, (y+=10), " Lordus, Exophase, Rokas,");
  1108. text_out16(tl_x, (y+=10), " Nemesis, Tasco Deluxe");
  1109. menu_draw_end();
  1110. }
  1111. // -------------- root menu --------------
  1112. menu_entry main_entries[] =
  1113. {
  1114. { "Resume game", MB_NONE, MA_MAIN_RESUME_GAME, NULL, 0, 0, 0, 0, 0 },
  1115. { "Save State", MB_NONE, MA_MAIN_SAVE_STATE, NULL, 0, 0, 0, 0, 0 },
  1116. { "Load State", MB_NONE, MA_MAIN_LOAD_STATE, NULL, 0, 0, 0, 0, 0 },
  1117. { "Reset game", MB_NONE, MA_MAIN_RESET_GAME, NULL, 0, 0, 0, 0, 0 },
  1118. { "Load new ROM/ISO", MB_NONE, MA_MAIN_LOAD_ROM, NULL, 0, 0, 0, 1, 0 },
  1119. { "Change options", MB_NONE, MA_MAIN_OPTIONS, NULL, 0, 0, 0, 1, 0 },
  1120. { "Configure controls", MB_NONE, MA_MAIN_CONTROLS, NULL, 0, 0, 0, 1, 0 },
  1121. { "Credits", MB_NONE, MA_MAIN_CREDITS, NULL, 0, 0, 0, 1, 0 },
  1122. { "Patches / GameGenie",MB_NONE, MA_MAIN_PATCHES, NULL, 0, 0, 0, 0, 0 },
  1123. { "Exit", MB_NONE, MA_MAIN_EXIT, NULL, 0, 0, 0, 1, 0 }
  1124. };
  1125. #define MAIN_ENTRY_COUNT (sizeof(main_entries) / sizeof(main_entries[0]))
  1126. static void draw_menu_root(int menu_sel)
  1127. {
  1128. const int tl_x = 70, tl_y = 70;
  1129. menu_draw_begin(1);
  1130. text_out16(tl_x, 20, "PicoDrive v" VERSION);
  1131. menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 146);
  1132. me_draw(main_entries, MAIN_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);
  1133. // error
  1134. if (menuErrorMsg[0]) {
  1135. memset((char *)menu_screen + 321*224*2, 0, 321*16*2);
  1136. text_out16(5, 226, menuErrorMsg);
  1137. }
  1138. menu_draw_end();
  1139. }
  1140. static void menu_loop_root(void)
  1141. {
  1142. static int menu_sel = 0;
  1143. int ret, menu_sel_max;
  1144. unsigned long inp = 0;
  1145. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESUME_GAME, rom_loaded);
  1146. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_SAVE_STATE, rom_loaded);
  1147. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_LOAD_STATE, rom_loaded);
  1148. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESET_GAME, rom_loaded);
  1149. me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_PATCHES, PicoPatches != NULL);
  1150. menu_sel_max = me_count_enabled(main_entries, MAIN_ENTRY_COUNT) - 1;
  1151. if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;
  1152. /* make sure action buttons are not pressed on entering menu */
  1153. draw_menu_root(menu_sel);
  1154. while (Framework_PollGetButtons() & (PBTN_PLAY|PBTN_STOP|PBTN_HOME)) Sleep(50);
  1155. for (;;)
  1156. {
  1157. draw_menu_root(menu_sel);
  1158. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_PLAY|PBTN_STOP|PBTN_HOME|PBTN_L|PBTN_R);
  1159. if(inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  1160. if(inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  1161. if((inp & (PBTN_L|PBTN_R)) == (PBTN_L|PBTN_R)) debug_menu_loop();
  1162. if( inp & (PBTN_HOME|PBTN_STOP)) {
  1163. if (rom_loaded) {
  1164. while (Framework_PollGetButtons() & (PBTN_HOME|PBTN_STOP)) Sleep(50); // wait until released
  1165. engineState = PGS_Running;
  1166. break;
  1167. }
  1168. }
  1169. if(inp & PBTN_PLAY) {
  1170. switch (me_index2id(main_entries, MAIN_ENTRY_COUNT, menu_sel))
  1171. {
  1172. case MA_MAIN_RESUME_GAME:
  1173. if (rom_loaded) {
  1174. while (Framework_PollGetButtons() & PBTN_PLAY) Sleep(50);
  1175. engineState = PGS_Running;
  1176. return;
  1177. }
  1178. break;
  1179. case MA_MAIN_SAVE_STATE:
  1180. if (rom_loaded) {
  1181. if(savestate_menu_loop(0))
  1182. continue;
  1183. engineState = PGS_Running;
  1184. return;
  1185. }
  1186. break;
  1187. case MA_MAIN_LOAD_STATE:
  1188. if (rom_loaded) {
  1189. if(savestate_menu_loop(1))
  1190. continue;
  1191. while (Framework_PollGetButtons() & PBTN_PLAY) Sleep(50);
  1192. engineState = PGS_Running;
  1193. return;
  1194. }
  1195. break;
  1196. case MA_MAIN_RESET_GAME:
  1197. if (rom_loaded) {
  1198. emu_ResetGame();
  1199. while (Framework_PollGetButtons() & PBTN_PLAY) Sleep(50);
  1200. engineState = PGS_Running;
  1201. return;
  1202. }
  1203. break;
  1204. case MA_MAIN_LOAD_ROM:
  1205. {
  1206. char curr_path[MAX_PATH], *selfname;
  1207. FILE *tstf;
  1208. if ( (tstf = fopen(loadedRomFName, "rb")) )
  1209. {
  1210. fclose(tstf);
  1211. strcpy(curr_path, loadedRomFName);
  1212. }
  1213. else
  1214. getcwd(curr_path, MAX_PATH);
  1215. selfname = romsel_loop(curr_path);
  1216. if (selfname) {
  1217. lprintf("selected file: %s\n", selfname);
  1218. engineState = PGS_ReloadRom;
  1219. return;
  1220. }
  1221. break;
  1222. }
  1223. case MA_MAIN_OPTIONS:
  1224. ret = menu_loop_options();
  1225. if (ret == 1) continue; // status update
  1226. if (engineState == PGS_ReloadRom)
  1227. return; // BIOS test
  1228. break;
  1229. case MA_MAIN_CONTROLS:
  1230. kc_sel_loop();
  1231. break;
  1232. case MA_MAIN_CREDITS:
  1233. draw_menu_credits();
  1234. Sleep(500);
  1235. inp = in_menu_wait(PBTN_PLAY|PBTN_STOP);
  1236. break;
  1237. case MA_MAIN_EXIT:
  1238. engineState = PGS_Quit;
  1239. return;
  1240. case MA_MAIN_PATCHES:
  1241. if (rom_loaded && PicoPatches) {
  1242. patches_menu_loop();
  1243. PicoPatchApply();
  1244. strcpy(menuErrorMsg, "Patches applied");
  1245. continue;
  1246. }
  1247. break;
  1248. default:
  1249. lprintf("%s: something unknown selected\n", __FUNCTION__);
  1250. break;
  1251. }
  1252. }
  1253. menuErrorMsg[0] = 0; // clear error msg
  1254. }
  1255. }
  1256. // warning: alignment
  1257. void menu_darken_bg(void *dst, const void *src, int pixels, int darker)
  1258. {
  1259. unsigned int *dest = dst;
  1260. const unsigned int *srce = src;
  1261. pixels /= 2;
  1262. if (darker)
  1263. {
  1264. while (pixels--)
  1265. {
  1266. unsigned int p = *srce++;
  1267. *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
  1268. }
  1269. }
  1270. else
  1271. {
  1272. while (pixels--)
  1273. {
  1274. unsigned int p = *srce++;
  1275. *dest++ = (p&0xf79ef79e)>>1;
  1276. }
  1277. }
  1278. }
  1279. static void menu_prepare_bg(int use_game_bg)
  1280. {
  1281. if (use_game_bg)
  1282. {
  1283. // darken the active framebuffer
  1284. if (giz_screen == NULL)
  1285. giz_screen = fb_lock(1);
  1286. memset(bg_buffer, 0, 321*8*2);
  1287. menu_darken_bg(bg_buffer + 321*8*2, (char *)giz_screen + 321*8*2, 321*224, 1);
  1288. memset(bg_buffer + 321*232*2, 0, 321*8*2);
  1289. fb_unlock();
  1290. giz_screen = NULL;
  1291. }
  1292. else
  1293. {
  1294. int i;
  1295. // should really only happen once, on startup..
  1296. readpng(bg_buffer, "skin/background.png", READPNG_BG);
  1297. // adjust 320 width to 321
  1298. for (i = 239; i > 0; i--)
  1299. memmove(bg_buffer + 321*2*i, bg_buffer + 320*2*i, 320*2);
  1300. }
  1301. }
  1302. static void menu_gfx_prepare(void)
  1303. {
  1304. menu_prepare_bg(rom_loaded);
  1305. menu_draw_begin(1);
  1306. menu_draw_end();
  1307. }
  1308. void menu_loop(void)
  1309. {
  1310. menu_gfx_prepare();
  1311. menu_loop_root();
  1312. menuErrorMsg[0] = 0;
  1313. }
  1314. // --------- CD tray close menu ----------
  1315. static void draw_menu_tray(int menu_sel)
  1316. {
  1317. int tl_x = 70, tl_y = 90, y;
  1318. menu_draw_begin(1);
  1319. text_out16(tl_x, 20, "The unit is about to");
  1320. text_out16(tl_x, 30, "close the CD tray.");
  1321. y = tl_y;
  1322. text_out16(tl_x, y, "Load new CD image");
  1323. text_out16(tl_x, (y+=10), "Insert nothing");
  1324. // draw cursor
  1325. text_out16(tl_x - 16, tl_y + menu_sel*10, ">");
  1326. // error
  1327. if (menuErrorMsg[0]) text_out16(5, 226, menuErrorMsg);
  1328. menu_draw_end();
  1329. }
  1330. int menu_loop_tray(void)
  1331. {
  1332. int menu_sel = 0, menu_sel_max = 1;
  1333. unsigned long inp = 0;
  1334. char curr_path[MAX_PATH], *selfname;
  1335. FILE *tstf;
  1336. menu_gfx_prepare();
  1337. if ( (tstf = fopen(loadedRomFName, "rb")) )
  1338. {
  1339. fclose(tstf);
  1340. strcpy(curr_path, loadedRomFName);
  1341. }
  1342. else
  1343. {
  1344. getcwd(curr_path, MAX_PATH);
  1345. }
  1346. /* make sure action buttons are not pressed on entering menu */
  1347. draw_menu_tray(menu_sel);
  1348. while (Framework_PollGetButtons() & PBTN_PLAY) Sleep(50);
  1349. for (;;)
  1350. {
  1351. draw_menu_tray(menu_sel);
  1352. inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_PLAY);
  1353. if(inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
  1354. if(inp & PBTN_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
  1355. if(inp & PBTN_PLAY) {
  1356. switch (menu_sel) {
  1357. case 0: // select image
  1358. selfname = romsel_loop(curr_path);
  1359. if (selfname) {
  1360. int ret = -1;
  1361. cd_img_type cd_type;
  1362. cd_type = emu_cdCheck(NULL, romFileName);
  1363. if (cd_type != CIT_NOT_CD)
  1364. ret = Insert_CD(romFileName, cd_type);
  1365. if (ret != 0) {
  1366. sprintf(menuErrorMsg, "Load failed, invalid CD image?");
  1367. lprintf("%s\n", menuErrorMsg);
  1368. continue;
  1369. }
  1370. engineState = PGS_RestartRun;
  1371. return 1;
  1372. }
  1373. break;
  1374. case 1: // insert nothing
  1375. engineState = PGS_RestartRun;
  1376. return 0;
  1377. }
  1378. }
  1379. menuErrorMsg[0] = 0; // clear error msg
  1380. }
  1381. }