giz menu works
[picodrive.git] / platform / gp2x / menu.c
index 8de891b..a8d1f9e 100644 (file)
@@ -1,4 +1,4 @@
-// (c) Copyright 2006 notaz, All rights reserved.\r
+// (c) Copyright 2006,2007 notaz, All rights reserved.\r
 // Free for non-commercial use.\r
 \r
 // For commercial use, separate licencing terms must be obtained.\r
 #include "gp2x.h"\r
 #include "emu.h"\r
 #include "menu.h"\r
-#include "fonts.h"\r
 #include "usbjoy.h"\r
-#include "asmutils.h"\r
+#include "../common/arm_utils.h"\r
+#include "../common/menu.h"\r
+#include "../common/readpng.h"\r
 #include "version.h"\r
 \r
 #include <Pico/PicoInt.h>\r
 #error "need d_type for file browser\r
 #endif\r
 \r
-extern char *actionNames[];\r
 extern char romFileName[PATH_MAX];\r
 extern char *rom_data;\r
 extern int  mmuhack_status;\r
 extern int  state_slot;\r
 extern int  config_slot, config_slot_current;\r
 \r
-static char *gp2xKeyNames[] = {\r
+static const char *gp2xKeyNames[] = {\r
        "UP",    "???",    "LEFT", "???",  "DOWN", "???", "RIGHT",    "???",\r
        "START", "SELECT", "L",    "R",    "A",    "B",   "X",        "Y",\r
        "???",   "???",    "???",  "???",  "???",  "???", "VOL DOWN", "VOL UP",\r
@@ -42,298 +42,8 @@ static char *gp2xKeyNames[] = {
 \r
 char menuErrorMsg[40] = {0, };\r
 \r
-\r
-static void gp2x_text(unsigned char *screen, int x, int y, const char *text, int color)\r
-{\r
-       int i,l;\r
-\r
-       screen = screen + x + y*320;\r
-\r
-       for (i = 0; i < strlen(text); i++)\r
-       {\r
-               for (l=0;l<8;l++)\r
-               {\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x80) screen[l*320+0]=color;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x40) screen[l*320+1]=color;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x20) screen[l*320+2]=color;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x10) screen[l*320+3]=color;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x08) screen[l*320+4]=color;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x04) screen[l*320+5]=color;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x02) screen[l*320+6]=color;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x01) screen[l*320+7]=color;\r
-               }\r
-               screen += 8;\r
-       }\r
-}\r
-\r
-// draws white text to current bbp15 screen\r
-void gp2x_text_out15(int x, int y, const char *text)\r
-{\r
-       int i,l;\r
-       unsigned short *screen = gp2x_screen;\r
-\r
-       screen = screen + x + y*320;\r
-\r
-       for (i = 0; i < strlen(text); i++)\r
-       {\r
-               for (l=0;l<8;l++)\r
-               {\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x80) screen[l*320+0]=0xffff;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x40) screen[l*320+1]=0xffff;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x20) screen[l*320+2]=0xffff;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x10) screen[l*320+3]=0xffff;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x08) screen[l*320+4]=0xffff;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x04) screen[l*320+5]=0xffff;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x02) screen[l*320+6]=0xffff;\r
-                       if(fontdata8x8[((text[i])*8)+l]&0x01) screen[l*320+7]=0xffff;\r
-               }\r
-               screen += 8;\r
-       }\r
-}\r
-\r
-\r
-void gp2x_text_out8(int x, int y, const char *texto, ...)\r
-{\r
-       va_list args;\r
-       char    buffer[512];\r
-\r
-       va_start(args,texto);\r
-       vsprintf(buffer,texto,args);\r
-       va_end(args);\r
-\r
-       gp2x_text(gp2x_screen,x,y,buffer,0xf0);\r
-}\r
-\r
-\r
-void gp2x_text_out8_2(int x, int y, const char *texto, int color)\r
-{\r
-       gp2x_text(gp2x_screen, x, y, texto, color);\r
-}\r
-\r
-void gp2x_text_out8_lim(int x, int y, const char *texto, int max)\r
-{\r
-       char    buffer[320/8+1];\r
-\r
-       strncpy(buffer, texto, 320/8);\r
-       if (max > 320/8) max = 320/8;\r
-       if (max < 0) max = 0;\r
-       buffer[max] = 0;\r
-\r
-       gp2x_text(gp2x_screen,x,y,buffer,0xf0);\r
-}\r
-\r
-static void gp2x_smalltext8(int x, int y, const char *texto)\r
-{\r
-       int i;\r
-       unsigned char *src, *dst;\r
-\r
-       for (i = 0;; i++, x += 6)\r
-       {\r
-               unsigned char c = (unsigned char) texto[i];\r
-               int h = 8;\r
-\r
-               if (!c) break;\r
-\r
-               src = fontdata6x8[c];\r
-               dst = (unsigned char *)gp2x_screen + x + y*320;\r
-\r
-               while (h--)\r
-               {\r
-                       int w = 0x20;\r
-                       while (w)\r
-                       {\r
-                               if( *src & w ) *dst = 0xf0;\r
-                               dst++;\r
-                               w>>=1;\r
-                       }\r
-                       src++;\r
-\r
-                       dst += 320-6;\r
-               }\r
-       }\r
-}\r
-\r
-static void gp2x_smalltext8_lim(int x, int y, const char *texto, int max)\r
-{\r
-       char    buffer[320/6+1];\r
-\r
-       strncpy(buffer, texto, 320/6);\r
-       if (max > 320/6) max = 320/6;\r
-       if (max < 0) max = 0;\r
-       buffer[max] = 0;\r
-\r
-       gp2x_smalltext8(x, y, buffer);\r
-}\r
-\r
-\r
-typedef enum\r
-{\r
-       MB_NONE = 1,            /* no auto processing */\r
-       MB_ONOFF,               /* ON/OFF setting */\r
-       MB_RANGE,               /* [min-max] setting */\r
-} menu_behavior;\r
-\r
-typedef enum\r
-{\r
-       MA_NONE = 1,\r
-       MA_MAIN_RESUME_GAME,\r
-       MA_MAIN_SAVE_STATE,\r
-       MA_MAIN_LOAD_STATE,\r
-       MA_MAIN_RESET_GAME,\r
-       MA_MAIN_LOAD_ROM,\r
-       MA_MAIN_OPTIONS,\r
-       MA_MAIN_CONTROLS,\r
-       MA_MAIN_CREDITS,\r
-       MA_MAIN_PATCHES,\r
-       MA_MAIN_EXIT,\r
-       MA_OPT_RENDERER,\r
-       MA_OPT_SCALING,\r
-       MA_OPT_ACC_TIMING,\r
-       MA_OPT_ACC_SPRITES,\r
-       MA_OPT_SHOW_FPS,\r
-       MA_OPT_FRAMESKIP,\r
-       MA_OPT_ENABLE_SOUND,\r
-       MA_OPT_SOUND_QUALITY,\r
-       MA_OPT_ARM940_SOUND,\r
-       MA_OPT_6BUTTON_PAD,\r
-       MA_OPT_REGION,\r
-       MA_OPT_SRAM_STATES,\r
-       MA_OPT_CONFIRM_STATES,\r
-       MA_OPT_SAVE_SLOT,\r
-       MA_OPT_CPU_CLOCKS,\r
-       MA_OPT_SCD_OPTS,\r
-       MA_OPT_ADV_OPTS,\r
-       MA_OPT_SAVECFG,\r
-       MA_OPT_SAVECFG_GAME,\r
-       MA_OPT_LOADCFG,\r
-       MA_OPT2_GAMMA,\r
-       MA_OPT2_A_SN_GAMMA,\r
-       MA_OPT2_VSYNC,\r
-       MA_OPT2_ENABLE_Z80,\r
-       MA_OPT2_ENABLE_YM2612,\r
-       MA_OPT2_ENABLE_SN76496,\r
-       MA_OPT2_GZIP_STATES,\r
-       MA_OPT2_NO_LAST_ROM,\r
-       MA_OPT2_RAMTIMINGS,\r
-       MA_OPT2_SQUIDGEHACK,\r
-       MA_OPT2_DONE,\r
-       MA_CDOPT_TESTBIOS_USA,\r
-       MA_CDOPT_TESTBIOS_EUR,\r
-       MA_CDOPT_TESTBIOS_JAP,\r
-       MA_CDOPT_LEDS,\r
-       MA_CDOPT_CDDA,\r
-       MA_CDOPT_PCM,\r
-       MA_CDOPT_READAHEAD,\r
-       MA_CDOPT_SAVERAM,\r
-       MA_CDOPT_SCALEROT_CHIP,\r
-       MA_CDOPT_BETTER_SYNC,\r
-       MA_CDOPT_DONE,\r
-} menu_id;\r
-\r
-typedef struct\r
-{\r
-       char *name;\r
-       menu_behavior beh;\r
-       menu_id id;\r
-       void *var;              /* for on-off settings */\r
-       int mask;\r
-       signed char min;        /* for ranged integer settings, to be sign-extended */\r
-       signed char max;\r
-       char enabled;\r
-} menu_entry;\r
-\r
-static int me_id2offset(const menu_entry *entries, int count, menu_id id)\r
-{\r
-       int i;\r
-       for (i = 0; i < count; i++)\r
-       {\r
-               if (entries[i].id == id) return i;\r
-       }\r
-\r
-       printf("%s: id %i not found\n", __FUNCTION__, id);\r
-       return 0;\r
-}\r
-\r
-static void me_enable(menu_entry *entries, int count, menu_id id, int enable)\r
-{\r
-       int i = me_id2offset(entries, count, id);\r
-       entries[i].enabled = enable;\r
-}\r
-\r
-static int me_count_enabled(const menu_entry *entries, int count)\r
-{\r
-       int i, ret = 0;\r
-\r
-       for (i = 0; i < count; i++)\r
-       {\r
-               if (entries[i].enabled) ret++;\r
-       }\r
-\r
-       return ret;\r
-}\r
-\r
-static menu_id me_index2id(const menu_entry *entries, int count, int index)\r
-{\r
-       int i;\r
-\r
-       for (i = 0; i < count; i++)\r
-       {\r
-               if (entries[i].enabled)\r
-               {\r
-                       if (index == 0) break;\r
-                       index--;\r
-               }\r
-       }\r
-       if (i >= count) i = count - 1;\r
-       return entries[i].id;\r
-}\r
-\r
-typedef void (me_draw_custom_f)(const menu_entry *entry, int x, int y, void *param);\r
-\r
-static void me_draw(const menu_entry *entries, int count, int x, int y, me_draw_custom_f *cust_draw, void *param)\r
-{\r
-       int i, y1 = y;\r
-\r
-       for (i = 0; i < count; i++)\r
-       {\r
-               if (!entries[i].enabled) continue;\r
-               if (entries[i].name == NULL)\r
-               {\r
-                       if (cust_draw != NULL)\r
-                               cust_draw(&entries[i], x, y1, param);\r
-                       y1 += 10;\r
-                       continue;\r
-               }\r
-               gp2x_text_out8(x, y1, entries[i].name);\r
-               if (entries[i].beh == MB_ONOFF)\r
-                       gp2x_text_out8(x + 27*8, y1, (*(int *)entries[i].var & entries[i].mask) ? "ON" : "OFF");\r
-               else if (entries[i].beh == MB_RANGE)\r
-                       gp2x_text_out8(x + 27*8, y1, "%i", *(int *)entries[i].var);\r
-               y1 += 10;\r
-       }\r
-}\r
-\r
-static int me_process(menu_entry *entries, int count, menu_id id, int is_next)\r
-{\r
-       int i = me_id2offset(entries, count, id);\r
-       menu_entry *entry = &entries[i];\r
-       switch (entry->beh)\r
-       {\r
-               case MB_ONOFF:\r
-                       *(int *)entry->var ^= entry->mask;\r
-                       return 1;\r
-               case MB_RANGE:\r
-                       *(int *)entry->var += is_next ? 1 : -1;\r
-                       if (*(int *)entry->var < (int)entry->min) *(int *)entry->var = (int)entry->min;\r
-                       if (*(int *)entry->var > (int)entry->max) *(int *)entry->var = (int)entry->max;\r
-                       return 1;\r
-               default:\r
-                       return 0;\r
-       }\r
-}\r
-\r
-\r
-\r
+static void menu_darken_bg(void *dst, int pixels, int darker);\r
+static void menu_prepare_bg(int use_game_bg);\r
 \r
 static unsigned long inp_prev = 0;\r
 static int inp_prevjoy = 0;\r
@@ -419,10 +129,66 @@ static unsigned long wait_for_input_usbjoy(unsigned long interesting, int *joy)
        return ret;\r
 }\r
 \r
+static void menu_flip(void)\r
+{\r
+       gp2x_video_flush_cache();\r
+       gp2x_video_flip2();\r
+}\r
+\r
+\r
+// --------- loading ROM screen ----------\r
 \r
+static void load_progress_cb(int percent)\r
+{\r
+       int ln, len = percent * 320 / 100;\r
+       unsigned short *dst = (unsigned short *)gp2x_screen + 320*20;\r
+\r
+       if (len > 320) len = 320;\r
+       for (ln = 10; ln > 0; ln--, dst += 320)\r
+               memset(dst, 0xff, len*2);\r
+       menu_flip();\r
+}\r
+\r
+void menu_romload_prepare(const char *rom_name)\r
+{\r
+       const char *p = rom_name + strlen(rom_name);\r
+       while (p > rom_name && *p != '/') p--;\r
+\r
+       if (rom_data) gp2x_pd_clone_buffer2();\r
+       else memset(gp2x_screen, 0, 320*240*2);\r
+\r
+       smalltext_out16(1, 1, "Loading", 0xffff);\r
+       smalltext_out16_lim(1, 10, p, 0xffff, 53);\r
+       gp2x_memcpy_buffers(3, gp2x_screen, 0, 320*240*2);\r
+       menu_flip();\r
+       PicoCartLoadProgressCB = load_progress_cb;\r
+}\r
+\r
+void menu_romload_end(void)\r
+{\r
+       PicoCartLoadProgressCB = NULL;\r
+       smalltext_out16(1, 30, "Starting emulation...", 0xffff);\r
+       menu_flip();\r
+}\r
 \r
 // -------------- ROM selector --------------\r
 \r
+// rrrr rggg gggb bbbb\r
+static unsigned short file2color(const char *fname)\r
+{\r
+       const char *ext = fname + strlen(fname) - 3;\r
+       static const char *rom_exts[]   = { "zip", "bin", "smd", "gen", "iso" };\r
+       static const char *other_exts[] = { "gmv", "pat" };\r
+       int i;\r
+\r
+       if (ext < fname) ext = fname;\r
+       for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)\r
+               if (strcasecmp(ext, rom_exts[i]) == 0) return 0xbdff;\r
+       for (i = 0; i < sizeof(other_exts)/sizeof(other_exts[0]); i++)\r
+               if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;\r
+       return 0xffff;\r
+}\r
+\r
 static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)\r
 {\r
        int start, i, pos;\r
@@ -430,24 +196,30 @@ static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
        start = 12 - sel;\r
        n--; // exclude current dir (".")\r
 \r
-       //memset(gp2x_screen, 0, 320*240);\r
        gp2x_pd_clone_buffer2();\r
 \r
+       if (rom_data == NULL) {\r
+               menu_darken_bg(gp2x_screen, 320*240, 0);\r
+       }\r
+\r
+       menu_darken_bg((char *)gp2x_screen + 320*120*2, 320*8, 0);\r
+\r
        if(start - 2 >= 0)\r
-               gp2x_smalltext8_lim(14, (start - 2)*10, curdir, 53-2);\r
+               smalltext_out16_lim(14, (start - 2)*10, curdir, 0xffff, 53-2);\r
        for (i = 0; i < n; i++) {\r
                pos = start + i;\r
                if (pos < 0)  continue;\r
                if (pos > 23) break;\r
                if (namelist[i+1]->d_type == DT_DIR) {\r
-                       gp2x_smalltext8_lim(14,   pos*10, "/", 1);\r
-                       gp2x_smalltext8_lim(14+6, pos*10, namelist[i+1]->d_name, 53-3);\r
+                       smalltext_out16_lim(14,   pos*10, "/", 0xfff6, 1);\r
+                       smalltext_out16_lim(14+6, pos*10, namelist[i+1]->d_name, 0xfff6, 53-3);\r
                } else {\r
-                       gp2x_smalltext8_lim(14,   pos*10, namelist[i+1]->d_name, 53-2);\r
+                       unsigned short color = file2color(namelist[i+1]->d_name);\r
+                       smalltext_out16_lim(14,   pos*10, namelist[i+1]->d_name, color, 53-2);\r
                }\r
        }\r
-       gp2x_text_out8(5, 120, ">");\r
-       gp2x_video_flip2();\r
+       text_out16(5, 120, ">");\r
+       menu_flip();\r
 }\r
 \r
 static int scandir_cmp(const void *p1, const void *p2)\r
@@ -606,11 +378,11 @@ static void draw_debug(void)
                while (*p && *p != '\n') p++;\r
                len = p - str;\r
                if (len > 55) len = 55;\r
-               gp2x_smalltext8_lim(1, line*10, str, len);\r
+               smalltext_out16_lim(1, line*10, str, 0xffff, len);\r
                if (*p == 0) break;\r
                p++; str = p;\r
        }\r
-       gp2x_video_flip2();\r
+       menu_flip();\r
 }\r
 \r
 static void debug_menu_loop(void)\r
@@ -623,7 +395,7 @@ static void debug_menu_loop(void)
 \r
 static void draw_patchlist(int sel)\r
 {\r
-       int start, i, pos;\r
+       int start, i, pos, active;\r
 \r
        start = 12 - sel;\r
 \r
@@ -633,14 +405,15 @@ static void draw_patchlist(int sel)
                pos = start + i;\r
                if (pos < 0)  continue;\r
                if (pos > 23) break;\r
-               gp2x_smalltext8_lim(14,     pos*10, PicoPatches[i].active ? "ON " : "OFF", 3);\r
-               gp2x_smalltext8_lim(14+6*4, pos*10, PicoPatches[i].name, 53-6);\r
+               active = PicoPatches[i].active;\r
+               smalltext_out16_lim(14,     pos*10, active ? "ON " : "OFF", active ? 0xfff6 : 0xffff, 3);\r
+               smalltext_out16_lim(14+6*4, pos*10, PicoPatches[i].name, active ? 0xfff6 : 0xffff, 53-6);\r
        }\r
        pos = start + i;\r
-       if (pos < 24) gp2x_smalltext8_lim(14, pos*10, "done", 4);\r
+       if (pos < 24) smalltext_out16_lim(14, pos*10, "done", 0xffff, 4);\r
 \r
-       gp2x_text_out8(5, 120, ">");\r
-       gp2x_video_flip2();\r
+       text_out16(5, 120, ">");\r
+       menu_flip();\r
 }\r
 \r
 \r
@@ -669,8 +442,6 @@ static void patches_menu_loop(void)
 \r
 // ------------ savestate loader ------------\r
 \r
-static void menu_prepare_bg(void);\r
-\r
 static int state_slot_flags = 0;\r
 \r
 static void state_check_slots(void)\r
@@ -731,8 +502,7 @@ static void draw_savestate_bg(int slot)
        }\r
 \r
        emu_forced_frame();\r
-       gp2x_memcpy_buffers((1<<2), gp2x_screen, 0, 320*240*2);\r
-       menu_prepare_bg();\r
+       menu_prepare_bg(1);\r
 \r
        memcpy(Pico.vram, tmp_vram, sizeof(Pico.vram));\r
        memcpy(Pico.cram, tmp_cram, sizeof(Pico.cram));\r
@@ -749,20 +519,19 @@ static void draw_savestate_menu(int menu_sel, int is_loading)
                draw_savestate_bg(menu_sel);\r
        gp2x_pd_clone_buffer2();\r
 \r
-       gp2x_text_out8(tl_x, 30, is_loading ? "Load state" : "Save state");\r
+       text_out16(tl_x, 30, is_loading ? "Load state" : "Save state");\r
+\r
+       menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 108);\r
 \r
        /* draw all 10 slots */\r
        y = tl_y;\r
        for (i = 0; i < 10; i++, y+=10)\r
        {\r
-               gp2x_text_out8(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");\r
+               text_out16(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");\r
        }\r
-       gp2x_text_out8(tl_x, y, "back");\r
-\r
-       // draw cursor\r
-       gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
+       text_out16(tl_x, y, "back");\r
 \r
-       gp2x_video_flip2();\r
+       menu_flip();\r
 }\r
 \r
 static int savestate_menu_loop(int is_loading)\r
@@ -817,17 +586,17 @@ static char *usb_joy_key_name(int joy, int num)
        return name;\r
 }\r
 \r
-static void draw_key_config(int curr_act, int is_p2)\r
+static char *action_binds(int player_idx, int action_mask)\r
 {\r
-       char strkeys[32*5];\r
+       static char strkeys[32*5];\r
        int joy, i;\r
 \r
        strkeys[0] = 0;\r
-       for (i = 0; i < 32; i++)\r
+       for (i = 0; i < 32; i++) // i is key index\r
        {\r
-               if (currentConfig.KeyBinds[i] & (1 << curr_act))\r
+               if (currentConfig.KeyBinds[i] & action_mask)\r
                {\r
-                       if (curr_act < 16 && (currentConfig.KeyBinds[i] & (1 << 16)) != (is_p2 << 16)) continue;\r
+                       if (player_idx >= 0 && ((currentConfig.KeyBinds[i] >> 16) & 3) != player_idx) continue;\r
                        if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, gp2xKeyNames[i]); break; }\r
                        else strcpy(strkeys, gp2xKeyNames[i]);\r
                }\r
@@ -836,9 +605,9 @@ static void draw_key_config(int curr_act, int is_p2)
        {\r
                for (i = 0; i < 32; i++)\r
                {\r
-                       if (currentConfig.JoyBinds[joy][i] & (1 << curr_act))\r
+                       if (currentConfig.JoyBinds[joy][i] & action_mask)\r
                        {\r
-                               if (curr_act < 16 && (currentConfig.JoyBinds[joy][i] & (1 << 16)) != (is_p2 << 16)) continue;\r
+                               if (player_idx >= 0 && ((currentConfig.JoyBinds[joy][i] >> 16) & 3) != player_idx) continue;\r
                                if (strkeys[0]) {\r
                                        strcat(strkeys, ", "); strcat(strkeys, usb_joy_key_name(joy + 1, i));\r
                                        break;\r
@@ -848,51 +617,121 @@ static void draw_key_config(int curr_act, int is_p2)
                }\r
        }\r
 \r
-       //memset(gp2x_screen, 0, 320*240);\r
+       return strkeys;\r
+}\r
+\r
+static void unbind_action(int action)\r
+{\r
+       int i, u;\r
+\r
+       for (i = 0; i < 32; i++)\r
+               currentConfig.KeyBinds[i] &= ~action;\r
+       for (u = 0; u < 4; u++)\r
+               for (i = 0; i < 32; i++)\r
+                       currentConfig.JoyBinds[u][i] &= ~action;\r
+}\r
+\r
+static int count_bound_keys(int action, int joy)\r
+{\r
+       int i, keys = 0;\r
+\r
+       if (joy)\r
+       {\r
+               for (i = 0; i < 32; i++)\r
+                       if (currentConfig.JoyBinds[joy-1][i] & action) keys++;\r
+       }\r
+       else\r
+       {\r
+               for (i = 0; i < 32; i++)\r
+                       if (currentConfig.KeyBinds[i] & action) keys++;\r
+       }\r
+       return keys;\r
+}\r
+\r
+typedef struct { char *name; int mask; } bind_action_t;\r
+\r
+static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_idx, int sel)\r
+{\r
+       int x, y, tl_y = 40, i;\r
+\r
        gp2x_pd_clone_buffer2();\r
-       gp2x_text_out8(60, 40, "Action: %s", actionNames[curr_act]);\r
-       gp2x_text_out8(60, 60, "Keys: %s", strkeys);\r
+       if (player_idx >= 0) {\r
+               text_out16(80, 20, "Player %i controls", player_idx + 1);\r
+               x = 100;\r
+       } else {\r
+               text_out16(80, 20, "Emulator controls");\r
+               x = 40;\r
+       }\r
 \r
-       gp2x_text_out8(30, 180, "Use SELECT to change action");\r
-       gp2x_text_out8(30, 190, "Press a key to bind/unbind");\r
-       gp2x_text_out8(30, 200, "Select \"Done\" action and");\r
-       gp2x_text_out8(30, 210, "  press any key to finish");\r
-       gp2x_video_flip2();\r
+       menu_draw_selection(x - 16, tl_y + sel*10, (player_idx >= 0) ? 66 : 130);\r
+\r
+       y = tl_y;\r
+       for (i = 0; i < opt_cnt; i++, y+=10)\r
+               text_out16(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));\r
+\r
+       text_out16(x, y, "Done");\r
+\r
+       if (sel < opt_cnt) {\r
+               text_out16(30, 180, "Press a button to bind/unbind");\r
+               text_out16(30, 190, "Use SELECT to clear");\r
+               text_out16(30, 200, "To bind UP/DOWN, hold SELECT");\r
+               text_out16(30, 210, "Select \"Done\" to exit");\r
+       } else {\r
+               text_out16(30, 190, "Use Options -> Save cfg");\r
+               text_out16(30, 200, "to save controls");\r
+               text_out16(30, 210, "Press B or X to exit");\r
+       }\r
+       menu_flip();\r
 }\r
 \r
-static void key_config_loop(int is_p2)\r
+static void key_config_loop(const bind_action_t *opts, int opt_cnt, int player_idx)\r
 {\r
-       int curr_act = 0, joy = 0, i;\r
+       int joy = 0, sel = 0, menu_sel_max = opt_cnt, prev_select = 0, i;\r
        unsigned long inp = 0;\r
 \r
        for (;;)\r
        {\r
-               draw_key_config(curr_act, is_p2);\r
+               draw_key_config(opts, opt_cnt, player_idx, sel);\r
                inp = wait_for_input_usbjoy(CONFIGURABLE_KEYS, &joy);\r
                // printf("got %08lX from joy %i\n", inp, joy);\r
                if (joy == 0) {\r
-                       if (inp & GP2X_SELECT) {\r
-                               curr_act++;\r
-                               while (!actionNames[curr_act] && curr_act < 32) curr_act++;\r
-                               if (curr_act > 31) curr_act = 0;\r
+                       if (!(inp & GP2X_SELECT)) {\r
+                               prev_select = 0;\r
+                               if(inp & GP2X_UP  ) { sel--; if (sel < 0) sel = menu_sel_max; continue; }\r
+                               if(inp & GP2X_DOWN) { sel++; if (sel > menu_sel_max) sel = 0; continue; }\r
                        }\r
+                       if (sel >= opt_cnt) {\r
+                               if (inp & (GP2X_B|GP2X_X)) break;\r
+                               else continue;\r
+                       }\r
+                       // if we are here, we want to bind/unbind something\r
+                       if ((inp & GP2X_SELECT) && !prev_select)\r
+                               unbind_action(opts[sel].mask);\r
+                       prev_select = inp & GP2X_SELECT;\r
                        inp &= CONFIGURABLE_KEYS;\r
                        inp &= ~GP2X_SELECT;\r
-               }\r
-               if (curr_act == 31 && inp) break;\r
-               if (joy == 0) {\r
                        for (i = 0; i < 32; i++)\r
                                if (inp & (1 << i)) {\r
-                                       currentConfig.KeyBinds[i] ^= (1 << curr_act);\r
-                                       if (is_p2) currentConfig.KeyBinds[i] |=  (1 << 16); // player 2 flag\r
-                                       else       currentConfig.KeyBinds[i] &= ~(1 << 16);\r
+                                       if (count_bound_keys(opts[sel].mask, 0) >= 2)\r
+                                            currentConfig.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only\r
+                                       else currentConfig.KeyBinds[i] ^=  opts[sel].mask;\r
+                                       if (player_idx >= 0) {\r
+                                               currentConfig.KeyBinds[i] &= ~(3 << 16);\r
+                                               currentConfig.KeyBinds[i] |= player_idx << 16;\r
+                                       }\r
                                }\r
-               } else {\r
+               }\r
+               else if (sel < opt_cnt)\r
+               {\r
                        for (i = 0; i < 32; i++)\r
                                if (inp & (1 << i)) {\r
-                                       currentConfig.JoyBinds[joy-1][i] ^= (1 << curr_act);\r
-                                       if (is_p2) currentConfig.JoyBinds[joy-1][i] |=  (1 << 16);\r
-                                       else       currentConfig.JoyBinds[joy-1][i] &= ~(1 << 16);\r
+                                       if (count_bound_keys(opts[sel].mask, joy) >= 1) // disallow combos for usbjoy\r
+                                            currentConfig.JoyBinds[joy-1][i] &= ~opts[sel].mask;\r
+                                       else currentConfig.JoyBinds[joy-1][i] ^=  opts[sel].mask;\r
+                                       if (player_idx >= 0) {\r
+                                               currentConfig.JoyBinds[joy-1][i] &= ~(3 << 16);\r
+                                               currentConfig.JoyBinds[joy-1][i] |= player_idx << 16;\r
+                                       }\r
                                }\r
                }\r
        }\r
@@ -904,54 +743,88 @@ static void draw_kc_sel(int menu_sel)
        char joyname[36];\r
 \r
        y = tl_y;\r
-       //memset(gp2x_screen, 0, 320*240);\r
        gp2x_pd_clone_buffer2();\r
-       gp2x_text_out8(tl_x, y,       "Player 1");\r
-       gp2x_text_out8(tl_x, (y+=10), "Player 2");\r
-       gp2x_text_out8(tl_x, (y+=10), "Done");\r
+       menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 138);\r
 \r
-       // draw cursor\r
-       gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
+       text_out16(tl_x, y,       "Player 1");\r
+       text_out16(tl_x, (y+=10), "Player 2");\r
+       text_out16(tl_x, (y+=10), "Emulator controls");\r
+       text_out16(tl_x, (y+=10), "Done");\r
 \r
        tl_x = 25;\r
-       gp2x_text_out8(tl_x, (y=110), "USB joys detected:");\r
+       text_out16(tl_x, (y=110), "USB joys detected:");\r
        if (num_of_joys > 0) {\r
                for (i = 0; i < num_of_joys; i++) {\r
                        strncpy(joyname, joy_name(joys[i]), 33); joyname[33] = 0;\r
-                       gp2x_text_out8(tl_x, (y+=10), "%i: %s", i+1, joyname);\r
+                       text_out16(tl_x, (y+=10), "%i: %s", i+1, joyname);\r
                }\r
        } else {\r
-               gp2x_text_out8(tl_x, (y+=10), "none");\r
+               text_out16(tl_x, (y+=10), "none");\r
        }\r
 \r
-\r
-       gp2x_video_flip2();\r
+       menu_flip();\r
 }\r
 \r
+\r
+// PicoPad[] format: MXYZ SACB RLDU\r
+static bind_action_t ctrl_actions[] =\r
+{\r
+       { "UP     ", 0x001 },\r
+       { "DOWN   ", 0x002 },\r
+       { "LEFT   ", 0x004 },\r
+       { "RIGHT  ", 0x008 },\r
+       { "A      ", 0x040 },\r
+       { "B      ", 0x010 },\r
+       { "C      ", 0x020 },\r
+       { "START  ", 0x080 },\r
+       { "MODE   ", 0x800 },\r
+       { "X      ", 0x400 },\r
+       { "Y      ", 0x200 },\r
+       { "Z      ", 0x100 },\r
+};\r
+\r
+// player2_flag, ?, ?, ?, ?, ?, ?, menu\r
+// "NEXT SAVE SLOT", "PREV SAVE SLOT", "SWITCH RENDERER", "SAVE STATE",\r
+// "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"\r
+static bind_action_t emuctrl_actions[] =\r
+{\r
+       { "Load State     ", 1<<28 },\r
+       { "Save State     ", 1<<27 },\r
+       { "Prev Save Slot ", 1<<25 },\r
+       { "Next Save Slot ", 1<<24 },\r
+       { "Switch Renderer", 1<<26 },\r
+       { "Volume Down    ", 1<<30 },\r
+       { "Volume Up      ", 1<<29 },\r
+       { "Enter Menu     ", 1<<23 },\r
+};\r
+\r
 static void kc_sel_loop(void)\r
 {\r
-       int menu_sel = 2, menu_sel_max = 2;\r
+       int menu_sel = 3, menu_sel_max = 3;\r
        unsigned long inp = 0;\r
+       int is_6button = currentConfig.PicoOpt & 0x020;\r
 \r
-       for(;;)\r
+       while (1)\r
        {\r
                draw_kc_sel(menu_sel);\r
                inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
-               if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
-               if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
-               if(inp & GP2X_B) {\r
+               if (inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
+               if (inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
+               if (inp & GP2X_B) {\r
                        switch (menu_sel) {\r
-                               case 0: key_config_loop(0); return;\r
-                               case 1: key_config_loop(1); return;\r
+                               case 0: key_config_loop(ctrl_actions, is_6button ? 12 : 8, 0); return;\r
+                               case 1: key_config_loop(ctrl_actions, is_6button ? 12 : 8, 1); return;\r
+                               case 2: key_config_loop(emuctrl_actions,\r
+                                               sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]), -1); return;\r
+                               case 3: if (rom_data == NULL) emu_WriteConfig(0); return;\r
                                default: return;\r
                        }\r
                }\r
-               if(inp & GP2X_X) return;\r
+               if (inp & GP2X_X) return;\r
        }\r
 }\r
 \r
 \r
-\r
 // --------- sega/mega cd options ----------\r
 \r
 menu_entry cdopt_entries[] =\r
@@ -984,13 +857,13 @@ static void menu_cdopt_cust_draw(const menu_entry *entry, int x, int y, void *pa
 \r
        switch (entry->id)\r
        {\r
-               case MA_CDOPT_TESTBIOS_USA: gp2x_text_out8(x, y, "USA BIOS:     %s", bios_names->us); break;\r
-               case MA_CDOPT_TESTBIOS_EUR: gp2x_text_out8(x, y, "EUR BIOS:     %s", bios_names->eu); break;\r
-               case MA_CDOPT_TESTBIOS_JAP: gp2x_text_out8(x, y, "JAP BIOS:     %s", bios_names->jp); break;\r
+               case MA_CDOPT_TESTBIOS_USA: text_out16(x, y, "USA BIOS:     %s", bios_names->us); break;\r
+               case MA_CDOPT_TESTBIOS_EUR: text_out16(x, y, "EUR BIOS:     %s", bios_names->eu); break;\r
+               case MA_CDOPT_TESTBIOS_JAP: text_out16(x, y, "JAP BIOS:     %s", bios_names->jp); break;\r
                case MA_CDOPT_READAHEAD:\r
                        if (PicoCDBuffers > 1) sprintf(ra_buff, "%5iK", PicoCDBuffers * 2);\r
                        else strcpy(ra_buff, "     OFF");\r
-                       gp2x_text_out8(x, y, "ReadAhead buffer      %s", ra_buff);\r
+                       text_out16(x, y, "ReadAhead buffer      %s", ra_buff);\r
                        break;\r
                default:break;\r
        }\r
@@ -1007,18 +880,17 @@ static void draw_cd_menu_options(int menu_sel, struct bios_names_t *bios_names)
 \r
        gp2x_pd_clone_buffer2();\r
 \r
-       me_draw(cdopt_entries, CDOPT_ENTRY_COUNT, tl_x, tl_y, menu_cdopt_cust_draw, bios_names);\r
+       menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 246);\r
 \r
-       // draw cursor\r
-       gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
+       me_draw(cdopt_entries, CDOPT_ENTRY_COUNT, tl_x, tl_y, menu_cdopt_cust_draw, bios_names);\r
 \r
        selected_id = me_index2id(cdopt_entries, CDOPT_ENTRY_COUNT, menu_sel);\r
        if ((selected_id == MA_CDOPT_TESTBIOS_USA && strcmp(bios_names->us, "NOT FOUND")) ||\r
                (selected_id == MA_CDOPT_TESTBIOS_EUR && strcmp(bios_names->eu, "NOT FOUND")) ||\r
                (selected_id == MA_CDOPT_TESTBIOS_JAP && strcmp(bios_names->jp, "NOT FOUND")))\r
-                       gp2x_text_out8(tl_x, 220, "Press start to test selected BIOS");\r
+                       text_out16(tl_x, 210, "Press start to test selected BIOS");\r
 \r
-       gp2x_video_flip2();\r
+       menu_flip();\r
 }\r
 \r
 static void cd_menu_loop_options(void)\r
@@ -1109,7 +981,7 @@ menu_entry opt2_entries[] =
 {\r
        { NULL,                        MB_NONE,  MA_OPT2_GAMMA,         NULL, 0, 0, 0, 1 },\r
        { "A_SN's gamma curve",        MB_ONOFF, MA_OPT2_A_SN_GAMMA,    &currentConfig.EmuOpt, 0x1000, 0, 0, 1 },\r
-       { "Perfecf vsync",             MB_ONOFF, MA_OPT2_VSYNC,         &currentConfig.EmuOpt, 0x2000, 0, 0, 1 },\r
+       { "Perfect vsync",             MB_ONOFF, MA_OPT2_VSYNC,         &currentConfig.EmuOpt, 0x2000, 0, 0, 1 },\r
        { "Emulate Z80",               MB_ONOFF, MA_OPT2_ENABLE_Z80,    &currentConfig.PicoOpt,0x0004, 0, 0, 1 },\r
        { "Emulate YM2612 (FM)",       MB_ONOFF, MA_OPT2_ENABLE_YM2612, &currentConfig.PicoOpt,0x0001, 0, 0, 1 },\r
        { "Emulate SN76496 (PSG)",     MB_ONOFF, MA_OPT2_ENABLE_SN76496,&currentConfig.PicoOpt,0x0002, 0, 0, 1 },\r
@@ -1126,9 +998,9 @@ menu_entry opt2_entries[] =
 static void menu_opt2_cust_draw(const menu_entry *entry, int x, int y, void *param)\r
 {\r
        if (entry->id == MA_OPT2_GAMMA)\r
-               gp2x_text_out8(x, y, "Gamma correction           %i.%02i", currentConfig.gamma / 100, currentConfig.gamma%100);\r
+               text_out16(x, y, "Gamma correction           %i.%02i", currentConfig.gamma / 100, currentConfig.gamma%100);\r
        else if (entry->id == MA_OPT2_SQUIDGEHACK)\r
-               gp2x_text_out8(x, y, "squidgehack (now %s %s", mmuhack_status ? "active)  " : "inactive)",\r
+               text_out16(x, y, "squidgehack (now %s %s", mmuhack_status ? "active)  " : "inactive)",\r
                        (currentConfig.EmuOpt&0x0010)?"ON":"OFF");\r
 }\r
 \r
@@ -1139,12 +1011,11 @@ static void draw_amenu_options(int menu_sel)
 \r
        gp2x_pd_clone_buffer2();\r
 \r
-       me_draw(opt2_entries, OPT2_ENTRY_COUNT, tl_x, tl_y, menu_opt2_cust_draw, NULL);\r
+       menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 252);\r
 \r
-       // draw cursor\r
-       gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
+       me_draw(opt2_entries, OPT2_ENTRY_COUNT, tl_x, tl_y, menu_opt2_cust_draw, NULL);\r
 \r
-       gp2x_video_flip2();\r
+       menu_flip();\r
 }\r
 \r
 static void amenu_loop_options(void)\r
@@ -1251,7 +1122,7 @@ static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *para
                                str = "16bit accurate";\r
                        else\r
                                str = " 8bit accurate";\r
-                       gp2x_text_out8(x, y, "Renderer:            %s", str);\r
+                       text_out16(x, y, "Renderer:            %s", str);\r
                        break;\r
                case MA_OPT_SCALING:\r
                        switch (currentConfig.scaling) {\r
@@ -1260,20 +1131,20 @@ static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *para
                                case 2:  str = "hw horiz. + vert."; break;\r
                                case 3:  str = "sw horizontal";     break;\r
                        }\r
-                       gp2x_text_out8(x, y, "Scaling:       %s", str);\r
+                       text_out16(x, y, "Scaling:       %s", str);\r
                        break;\r
                case MA_OPT_FRAMESKIP:\r
                        if (currentConfig.Frameskip < 0)\r
                             strcpy(str24, "Auto");\r
                        else sprintf(str24, "%i", currentConfig.Frameskip);\r
-                       gp2x_text_out8(x, y, "Frameskip                  %s", str24);\r
+                       text_out16(x, y, "Frameskip                  %s", str24);\r
                        break;\r
                case MA_OPT_SOUND_QUALITY:\r
                        str = (currentConfig.PicoOpt&0x08)?"stereo":"mono";\r
-                       gp2x_text_out8(x, y, "Sound Quality:     %5iHz %s", currentConfig.PsndRate, str);\r
+                       text_out16(x, y, "Sound Quality:     %5iHz %s", currentConfig.PsndRate, str);\r
                        break;\r
                case MA_OPT_REGION:\r
-                       gp2x_text_out8(x, y, "Region:              %s", region_name(currentConfig.PicoRegion));\r
+                       text_out16(x, y, "Region:              %s", region_name(currentConfig.PicoRegion));\r
                        break;\r
                case MA_OPT_CONFIRM_STATES:\r
                        switch ((currentConfig.EmuOpt >> 9) & 5) {\r
@@ -1282,18 +1153,18 @@ static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *para
                                case 4:  str = "loads";  break;\r
                                case 5:  str = "both";   break;\r
                        }\r
-                       gp2x_text_out8(x, y, "Confirm savestate          %s", str);\r
+                       text_out16(x, y, "Confirm savestate          %s", str);\r
                        break;\r
                case MA_OPT_CPU_CLOCKS:\r
-                       gp2x_text_out8(x, y, "GP2X CPU clocks            %iMhz", currentConfig.CPUclock);\r
+                       text_out16(x, y, "GP2X CPU clocks            %iMhz", currentConfig.CPUclock);\r
                        break;\r
                case MA_OPT_SAVECFG:\r
                        str24[0] = 0;\r
                        if (config_slot != 0) sprintf(str24, " (profile: %i)", config_slot);\r
-                       gp2x_text_out8(x, y, "Save cfg as default%s", str24);\r
+                       text_out16(x, y, "Save cfg as default%s", str24);\r
                        break;\r
                case MA_OPT_LOADCFG:\r
-                       gp2x_text_out8(x, y, "Load cfg from profile %i", config_slot);\r
+                       text_out16(x, y, "Load cfg from profile %i", config_slot);\r
                        break;\r
                default:\r
                        printf("%s: unimplemented (%i)\n", __FUNCTION__, entry->id);\r
@@ -1305,16 +1176,15 @@ static void menu_opt_cust_draw(const menu_entry *entry, int x, int y, void *para
 \r
 static void draw_menu_options(int menu_sel)\r
 {\r
-       int tl_x = 25, tl_y = 32;\r
+       int tl_x = 25, tl_y = 24;\r
 \r
        gp2x_pd_clone_buffer2();\r
 \r
-       me_draw(opt_entries, OPT_ENTRY_COUNT, tl_x, tl_y, menu_opt_cust_draw, NULL);\r
+       menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 284);\r
 \r
-       // draw cursor\r
-       gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
+       me_draw(opt_entries, OPT_ENTRY_COUNT, tl_x, tl_y, menu_opt_cust_draw, NULL);\r
 \r
-       gp2x_video_flip2();\r
+       menu_flip();\r
 }\r
 \r
 static int sndrate_prevnext(int rate, int dir)\r
@@ -1359,11 +1229,9 @@ static void menu_options_save(void)
        PicoOpt = currentConfig.PicoOpt;\r
        PsndRate = currentConfig.PsndRate;\r
        PicoRegionOverride = currentConfig.PicoRegion;\r
-       if (PicoOpt & 0x20) {\r
-               actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
-               actionNames[10] = "X"; actionNames[11] = "MODE";\r
-       } else {\r
-               actionNames[8] = actionNames[9] = actionNames[10] = actionNames[11] = 0;\r
+       if (!(PicoOpt & 0x20)) {\r
+               // unbind XYZ MODE, just in case\r
+               unbind_action(0xf00);\r
        }\r
        scaling_update();\r
 }\r
@@ -1382,6 +1250,7 @@ static int menu_loop_options(void)
        me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_SAVECFG_GAME, rom_data != NULL);\r
        me_enable(opt_entries, OPT_ENTRY_COUNT, MA_OPT_LOADCFG, config_slot != config_slot_current);\r
        menu_sel_max = me_count_enabled(opt_entries, OPT_ENTRY_COUNT) - 1;\r
+       if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;\r
 \r
        while (1)\r
        {\r
@@ -1477,9 +1346,9 @@ static int menu_loop_options(void)
                                                else strcpy(menuErrorMsg, "failed to write config");\r
                                                return 1;\r
                                        case MA_OPT_LOADCFG:\r
-                                               ret = emu_ReadConfig(1);\r
-                                               if (!ret) ret = emu_ReadConfig(0);\r
-                                               if (!ret) strcpy(menuErrorMsg, "config loaded");\r
+                                               ret = emu_ReadConfig(1, 1);\r
+                                               if (!ret) ret = emu_ReadConfig(0, 1);\r
+                                               if (ret)  strcpy(menuErrorMsg, "config loaded");\r
                                                else      strcpy(menuErrorMsg, "failed to load config");\r
                                                return 1;\r
                                        default:\r
@@ -1499,28 +1368,28 @@ static int menu_loop_options(void)
 \r
 static void draw_menu_credits(void)\r
 {\r
-       int tl_x = 15, tl_y = 70, y;\r
-       //memset(gp2x_screen, 0, 320*240);\r
+       int tl_x = 15, tl_y = 64, y;\r
        gp2x_pd_clone_buffer2();\r
 \r
-       gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");\r
+       text_out16(tl_x, 20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");\r
        y = tl_y;\r
-       gp2x_text_out8(tl_x, y, "Credits:");\r
-       gp2x_text_out8(tl_x, (y+=10), "Dave: Cyclone 68000 core,");\r
-       gp2x_text_out8(tl_x, (y+=10), "      base code of PicoDrive");\r
-       gp2x_text_out8(tl_x, (y+=10), "Reesy & FluBBa: DrZ80 core");\r
-       gp2x_text_out8(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");\r
-       gp2x_text_out8(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");\r
-       gp2x_text_out8(tl_x, (y+=10), "Stephane Dallongeville:");\r
-       gp2x_text_out8(tl_x, (y+=10), "      opensource Gens");\r
-       gp2x_text_out8(tl_x, (y+=10), "Haze: Genesis hw info");\r
-       gp2x_text_out8(tl_x, (y+=10), "rlyeh and others: minimal SDK");\r
-       gp2x_text_out8(tl_x, (y+=10), "Squidge: squidgehack");\r
-       gp2x_text_out8(tl_x, (y+=10), "Dzz: ARM940 sample");\r
-       gp2x_text_out8(tl_x, (y+=10), "GnoStiC / Puck2099: USB joystick");\r
-       gp2x_text_out8(tl_x, (y+=10), "craigix: GP2X hardware");\r
-\r
-       gp2x_video_flip2();\r
+       text_out16(tl_x, y, "Credits:");\r
+       text_out16(tl_x, (y+=10), "Dave: Cyclone 68000 core,");\r
+       text_out16(tl_x, (y+=10), "      base code of PicoDrive");\r
+       text_out16(tl_x, (y+=10), "Reesy & FluBBa: DrZ80 core");\r
+       text_out16(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");\r
+       text_out16(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");\r
+       text_out16(tl_x, (y+=10), "Stephane Dallongeville:");\r
+       text_out16(tl_x, (y+=10), "      opensource Gens");\r
+       text_out16(tl_x, (y+=10), "Haze: Genesis hw info");\r
+       text_out16(tl_x, (y+=10), "rlyeh and others: minimal SDK");\r
+       text_out16(tl_x, (y+=10), "Squidge: squidgehack");\r
+       text_out16(tl_x, (y+=10), "Dzz: ARM940 sample");\r
+       text_out16(tl_x, (y+=10), "GnoStiC / Puck2099: USB joystick");\r
+       text_out16(tl_x, (y+=10), "craigix: GP2X hardware");\r
+       text_out16(tl_x, (y+=10), "ketchupgun: skin design");\r
+\r
+       menu_flip();\r
 }\r
 \r
 \r
@@ -1548,15 +1417,18 @@ static void draw_menu_root(int menu_sel)
 \r
        gp2x_pd_clone_buffer2();\r
 \r
-       gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION);\r
+       text_out16(tl_x, 20, "PicoDrive v" VERSION);\r
+\r
+       menu_draw_selection(tl_x - 16, tl_y + menu_sel*10, 146);\r
 \r
        me_draw(main_entries, MAIN_ENTRY_COUNT, tl_x, tl_y, NULL, NULL);\r
 \r
-       // draw cursor\r
-       gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
        // error\r
-       if (menuErrorMsg[0]) gp2x_text_out8(5, 226, menuErrorMsg);\r
-       gp2x_video_flip2();\r
+       if (menuErrorMsg[0]) {\r
+               memset((char *)gp2x_screen + 320*224*2, 0, 320*16*2);\r
+               text_out16(5, 226, menuErrorMsg);\r
+       }\r
+       menu_flip();\r
 }\r
 \r
 \r
@@ -1565,18 +1437,6 @@ static void menu_loop_root(void)
        static int menu_sel = 0;\r
        int ret, menu_sel_max;\r
        unsigned long inp = 0;\r
-       char curr_path[PATH_MAX], *selfname;\r
-       FILE *tstf;\r
-\r
-       if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )\r
-       {\r
-               fclose(tstf);\r
-               strcpy(curr_path, currentConfig.lastRomFile);\r
-       }\r
-       else\r
-       {\r
-               getcwd(curr_path, PATH_MAX);\r
-       }\r
 \r
        me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_RESUME_GAME, rom_data != NULL);\r
        me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_SAVE_STATE,  rom_data != NULL);\r
@@ -1585,6 +1445,7 @@ static void menu_loop_root(void)
        me_enable(main_entries, MAIN_ENTRY_COUNT, MA_MAIN_PATCHES,     PicoPatches != NULL);\r
 \r
        menu_sel_max = me_count_enabled(main_entries, MAIN_ENTRY_COUNT) - 1;\r
+       if (menu_sel > menu_sel_max) menu_sel = menu_sel_max;\r
 \r
        /* make sure action buttons are not pressed on entering menu */\r
        draw_menu_root(menu_sel);\r
@@ -1638,12 +1499,24 @@ static void menu_loop_root(void)
                                        }\r
                                        break;\r
                                case MA_MAIN_LOAD_ROM:\r
+                               {\r
+                                       char curr_path[PATH_MAX], *selfname;\r
+                                       FILE *tstf;\r
+                                       if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )\r
+                                       {\r
+                                               fclose(tstf);\r
+                                               strcpy(curr_path, currentConfig.lastRomFile);\r
+                                       }\r
+                                       else\r
+                                               getcwd(curr_path, PATH_MAX);\r
                                        selfname = romsel_loop(curr_path);\r
                                        if (selfname) {\r
                                                printf("selected file: %s\n", selfname);\r
                                                engineState = PGS_ReloadRom;\r
+                                               return;\r
                                        }\r
-                                       return;\r
+                                       break;\r
+                               }\r
                                case MA_MAIN_OPTIONS:\r
                                        ret = menu_loop_options();\r
                                        if (ret == 1) continue; // status update\r
@@ -1678,31 +1551,55 @@ static void menu_loop_root(void)
        }\r
 }\r
 \r
-\r
-static void menu_prepare_bg(void)\r
+static void menu_darken_bg(void *dst, int pixels, int darker)\r
 {\r
-       extern int localPal[0x100];\r
-       int c, i;\r
+       unsigned int *screen = dst;\r
+       pixels /= 2;\r
+       if (darker)\r
+       {\r
+               while (pixels--)\r
+               {\r
+                       unsigned int p = *screen;\r
+                       *screen++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);\r
+               }\r
+       }\r
+       else\r
+       {\r
+               while (pixels--)\r
+               {\r
+                       unsigned int p = *screen;\r
+                       *screen++ = (p&0xf79ef79e)>>1;\r
+               }\r
+       }\r
+}\r
 \r
-       // don't clear old palette just for fun (but make it dark)\r
-       for (i = 0x100-1; i >= 0; i--) {\r
-               c = localPal[i];\r
-               localPal[i] = ((c >> 1) & 0x007f7f7f) - ((c >> 3) & 0x001f1f1f);\r
+static void menu_prepare_bg(int use_game_bg)\r
+{\r
+       if (use_game_bg)\r
+       {\r
+               // darken the active framebuffer\r
+               memset(gp2x_screen, 0, 320*8*2);\r
+               menu_darken_bg((char *)gp2x_screen + 320*8*2, 320*224, 1);\r
+               memset((char *)gp2x_screen + 320*232*2, 0, 320*8*2);\r
+       }\r
+       else\r
+       {\r
+               // should really only happen once, on startup..\r
+               readpng(gp2x_screen, "skin/background.png", READPNG_BG);\r
        }\r
-       localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
-       localPal[0xf0] = 0x00ffffff;\r
 \r
-       gp2x_video_setpalette(localPal, 0x100);\r
+       // copy to buffer2\r
+       gp2x_memcpy_buffers((1<<2), gp2x_screen, 0, 320*240*2);\r
 }\r
 \r
 static void menu_gfx_prepare(void)\r
 {\r
-       menu_prepare_bg();\r
+       menu_prepare_bg(rom_data != NULL);\r
 \r
-       // switch to 8bpp\r
-       gp2x_video_changemode2(8);\r
+       // switch to 16bpp\r
+       gp2x_video_changemode2(16);\r
        gp2x_video_RGB_setscaling(0, 320, 240);\r
-       gp2x_video_flip2();\r
+       menu_flip();\r
 }\r
 \r
 \r
@@ -1721,20 +1618,20 @@ void menu_loop(void)
 static void draw_menu_tray(int menu_sel)\r
 {\r
        int tl_x = 70, tl_y = 90, y;\r
-       memset(gp2x_screen, 0xe0, 320*240);\r
+       memset(gp2x_screen, 0, 320*240*2);\r
 \r
-       gp2x_text_out8(tl_x, 20, "The unit is about to");\r
-       gp2x_text_out8(tl_x, 30, "close the CD tray.");\r
+       text_out16(tl_x, 20, "The unit is about to");\r
+       text_out16(tl_x, 30, "close the CD tray.");\r
 \r
        y = tl_y;\r
-       gp2x_text_out8(tl_x, y,       "Load new CD image");\r
-       gp2x_text_out8(tl_x, (y+=10), "Insert nothing");\r
+       text_out16(tl_x, y,       "Load new CD image");\r
+       text_out16(tl_x, (y+=10), "Insert nothing");\r
 \r
        // draw cursor\r
-       gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
+       text_out16(tl_x - 16, tl_y + menu_sel*10, ">");\r
        // error\r
-       if (menuErrorMsg[0]) gp2x_text_out8(5, 226, menuErrorMsg);\r
-       gp2x_video_flip2();\r
+       if (menuErrorMsg[0]) text_out16(5, 226, menuErrorMsg);\r
+       menu_flip();\r
 }\r
 \r
 \r
@@ -1745,7 +1642,7 @@ int menu_loop_tray(void)
        char curr_path[PATH_MAX], *selfname;\r
        FILE *tstf;\r
 \r
-       gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
+       gp2x_memset_all_buffers(0, 0, 320*240*2);\r
        menu_gfx_prepare();\r
 \r
        if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )\r