some skinning capabilities
[picodrive.git] / platform / gp2x / menu.c
index 06a3e3d..b45b295 100644 (file)
@@ -16,6 +16,7 @@
 #include "fonts.h"\r
 #include "usbjoy.h"\r
 #include "asmutils.h"\r
+#include "readpng.h"\r
 #include "version.h"\r
 \r
 #include <Pico/PicoInt.h>\r
@@ -32,66 +33,71 @@ extern int  mmuhack_status;
 extern int  state_slot;\r
 extern int  config_slot, config_slot_current;\r
 \r
+static unsigned char menu_font_data[10240];\r
 static char *gp2xKeyNames[] = {\r
        "UP",    "???",    "LEFT", "???",  "DOWN", "???", "RIGHT",    "???",\r
        "START", "SELECT", "L",    "R",    "A",    "B",   "X",        "Y",\r
        "???",   "???",    "???",  "???",  "???",  "???", "VOL DOWN", "VOL UP",\r
        "???",   "???",    "???",  "PUSH", "???",  "???", "???",      "???"\r
 };\r
+static int menu_text_color = 0xffff; // default to white\r
+static int menu_sel_color = -1; // disabled\r
 \r
 char menuErrorMsg[40] = {0, };\r
 \r
+static void menu_darken_bg(void *dst, int pixels);\r
+static void menu_prepare_bg(int use_game_bg);\r
 \r
-static void gp2x_text(unsigned char *screen, int x, int y, const char *text, int color)\r
+// draws text to current bbp16 screen\r
+static void text_out16_(int x, int y, const char *text, int color)\r
 {\r
-       int i,l;\r
+       int i, l, u, tr, tg, tb, len;\r
+       unsigned short *dest = (unsigned short *)gp2x_screen + x + y*320;\r
+       tr = (color & 0xf800) >> 8;\r
+       tg = (color & 0x07e0) >> 3;\r
+       tb = (color & 0x001f) << 3;\r
 \r
-       screen = screen + x + y*320;\r
-\r
-       for (i = 0; i < strlen(text); i++)\r
+       if (text == (void *)1)\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
+               // selector symbol\r
+               text = "";\r
+               len = 1;\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
+       else\r
+               len = strlen(text);\r
 \r
-       for (i = 0; i < strlen(text); i++)\r
+       for (i = 0; i < len; i++)\r
        {\r
-               for (l=0;l<8;l++)\r
+               unsigned char  *src = menu_font_data + (unsigned int)text[i]*4*10;\r
+               unsigned short *dst = dest;\r
+               for (l = 0; l < 10; l++, dst += 320-8)\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
+                       for (u = 8/2; u > 0; u--, src++)\r
+                       {\r
+                               int c, r, g, b;\r
+                               c = *src >> 4;\r
+                               r = (*dst & 0xf800) >> 8;\r
+                               g = (*dst & 0x07e0) >> 3;\r
+                               b = (*dst & 0x001f) << 3;\r
+                               r = (c^0xf)*r/15 + c*tr/15;\r
+                               g = (c^0xf)*g/15 + c*tg/15;\r
+                               b = (c^0xf)*b/15 + c*tb/15;\r
+                               *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);\r
+                               c = *src & 0xf;\r
+                               r = (*dst & 0xf800) >> 8;\r
+                               g = (*dst & 0x07e0) >> 3;\r
+                               b = (*dst & 0x001f) << 3;\r
+                               r = (c^0xf)*r/15 + c*tr/15;\r
+                               g = (c^0xf)*g/15 + c*tg/15;\r
+                               b = (c^0xf)*b/15 + c*tb/15;\r
+                               *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);\r
+                       }\r
                }\r
-               screen += 8;\r
+               dest += 8;\r
        }\r
 }\r
 \r
-\r
-void gp2x_text_out8(int x, int y, const char *texto, ...)\r
+void text_out16(int x, int y, const char *texto, ...)\r
 {\r
        va_list args;\r
        char    buffer[512];\r
@@ -100,31 +106,15 @@ void gp2x_text_out8(int x, int y, const char *texto, ...)
        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
+       text_out16_(x,y,buffer,menu_text_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
+static void smalltext_out16(int x, int y, const char *texto, int color)\r
 {\r
        int i;\r
-       unsigned char *src, *dst;\r
+       unsigned char  *src;\r
+       unsigned short *dst;\r
 \r
        for (i = 0;; i++, x += 6)\r
        {\r
@@ -134,14 +124,14 @@ static void gp2x_smalltext8(int x, int y, const char *texto)
                if (!c) break;\r
 \r
                src = fontdata6x8[c];\r
-               dst = (unsigned char *)gp2x_screen + x + y*320;\r
+               dst = (unsigned short *)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
+                               if( *src & w ) *dst = color;\r
                                dst++;\r
                                w>>=1;\r
                        }\r
@@ -152,7 +142,7 @@ static void gp2x_smalltext8(int x, int y, const char *texto)
        }\r
 }\r
 \r
-static void gp2x_smalltext8_lim(int x, int y, const char *texto, int max)\r
+static void smalltext_out16_lim(int x, int y, const char *texto, int color, int max)\r
 {\r
        char    buffer[320/6+1];\r
 \r
@@ -161,7 +151,33 @@ static void gp2x_smalltext8_lim(int x, int y, const char *texto, int max)
        if (max < 0) max = 0;\r
        buffer[max] = 0;\r
 \r
-       gp2x_smalltext8(x, y, buffer);\r
+       smalltext_out16(x, y, buffer, color);\r
+}\r
+\r
+static void draw_selection(int x, int y, int w)\r
+{\r
+       int i, h;\r
+       unsigned short *dst, *dest;\r
+\r
+       text_out16_(x, y, (void *)1, (menu_sel_color < 0) ? menu_text_color : menu_sel_color);\r
+\r
+       if (menu_sel_color < 0) return; // no selection hilight\r
+\r
+       if (y > 0) y--;\r
+       dest = (unsigned short *)gp2x_screen + x + y*320 + 14;\r
+       for (h = 11; h > 0; h--)\r
+       {\r
+               dst = dest;\r
+               for (i = w; i > 0; i--)\r
+                       *dst++ = menu_sel_color;\r
+               dest += 320;\r
+       }\r
+}\r
+\r
+static void menu_flip(void)\r
+{\r
+       gp2x_video_flush_cache();\r
+       gp2x_video_flip2();\r
 }\r
 \r
 \r
@@ -303,11 +319,11 @@ static void me_draw(const menu_entry *entries, int count, int x, int y, me_draw_
                        y1 += 10;\r
                        continue;\r
                }\r
-               gp2x_text_out8(x, y1, entries[i].name);\r
+               text_out16(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
+                       text_out16(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
+                       text_out16(x + 27*8, y1, "%i", *(int *)entries[i].var);\r
                y1 += 10;\r
        }\r
 }\r
@@ -332,7 +348,73 @@ static int me_process(menu_entry *entries, int count, menu_id id, int is_next)
 }\r
 \r
 \r
+static int parse_hex_color(char *buff)\r
+{\r
+       char *endp = buff;\r
+       int t = (int) strtoul(buff, &endp, 16);\r
+       if (endp != buff) return ((t>>8)&0xf800) | ((t>>5)&0x07e0) | ((t>>3)&0x1f);\r
+       return -1;\r
+}\r
+\r
+void menu_init(void)\r
+{\r
+       int c, l;\r
+       unsigned char *fd = menu_font_data;\r
+       char buff[256];\r
+       FILE *f;\r
+\r
+       // generate default font from fontdata8x8\r
+       memset(menu_font_data, 0, sizeof(menu_font_data));\r
+       for (c = 0; c < 256; c++)\r
+       {\r
+               for (l = 0; l < 8; l++)\r
+               {\r
+                       unsigned char fd8x8 = fontdata8x8[c*8+l];\r
+                       if (fd8x8&0x80) *fd |= 0xf0;\r
+                       if (fd8x8&0x40) *fd |= 0x0f; fd++;\r
+                       if (fd8x8&0x20) *fd |= 0xf0;\r
+                       if (fd8x8&0x10) *fd |= 0x0f; fd++;\r
+                       if (fd8x8&0x08) *fd |= 0xf0;\r
+                       if (fd8x8&0x04) *fd |= 0x0f; fd++;\r
+                       if (fd8x8&0x02) *fd |= 0xf0;\r
+                       if (fd8x8&0x01) *fd |= 0x0f; fd++;\r
+               }\r
+               fd += 8*2/2; // 2 empty lines\r
+       }\r
+\r
+       // load custom font and selector (stored as 1st symbol in font table)\r
+       readpng(menu_font_data, "skin/font.png", READPNG_FONT);\r
+       memcpy(menu_font_data, menu_font_data + ((int)'>')*4*10, 4*10); // default selector symbol is '>'\r
+       readpng(menu_font_data, "skin/selector.png", READPNG_SELECTOR);\r
 \r
+       // load custom colors\r
+       f = fopen("skin/skin.txt", "r");\r
+       if (f != NULL)\r
+       {\r
+               printf("found skin.txt\n");\r
+               while (!feof(f))\r
+               {\r
+                       fgets(buff, sizeof(buff), f);\r
+                       if (buff[0] == '#'  || buff[0] == '/')  continue; // comment\r
+                       if (buff[0] == '\r' || buff[0] == '\n') continue; // empty line\r
+                       if (strncmp(buff, "text_color=", 11) == 0)\r
+                       {\r
+                               int tmp = parse_hex_color(buff+11);\r
+                               if (tmp >= 0) menu_text_color = tmp;\r
+                               else printf("skin.txt: parse error for text_color\n");\r
+                       }\r
+                       else if (strncmp(buff, "selection_color=", 16) == 0)\r
+                       {\r
+                               int tmp = parse_hex_color(buff+16);\r
+                               if (tmp >= 0) menu_sel_color = tmp;\r
+                               else printf("skin.txt: parse error for selection_color\n");\r
+                       }\r
+                       else\r
+                               printf("skin.txt: parse error: %s\n", buff);\r
+               }\r
+               fclose(f);\r
+       }\r
+}\r
 \r
 static unsigned long inp_prev = 0;\r
 static int inp_prevjoy = 0;\r
@@ -425,12 +507,12 @@ static unsigned long wait_for_input_usbjoy(unsigned long interesting, int *joy)
 static void load_progress_cb(int percent)\r
 {\r
        int ln, len = percent * 320 / 100;\r
-       unsigned char *dst = (unsigned char *)gp2x_screen + 320*20;\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, 0xf0, len);\r
-       gp2x_video_flip2();\r
+               memset(dst, 0xff, len*2);\r
+       menu_flip();\r
 }\r
 \r
 void menu_romload_prepare(const char *rom_name)\r
@@ -438,23 +520,41 @@ void menu_romload_prepare(const char *rom_name)
        const char *p = rom_name + strlen(rom_name);\r
        while (p > rom_name && *p != '/') p--;\r
 \r
-       gp2x_pd_clone_buffer2();\r
-       gp2x_smalltext8(1, 1, "Loading");\r
-       gp2x_smalltext8_lim(1, 10, p, 53);\r
-       gp2x_memcpy_buffers(3, gp2x_screen, 0, 320*240);\r
-       gp2x_video_flip2();\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
-       gp2x_smalltext8(1, 30, "Starting emulation...");\r
-       gp2x_video_flip2();\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
@@ -462,23 +562,31 @@ static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)
        start = 12 - sel;\r
        n--; // exclude current dir (".")\r
 \r
-       gp2x_pd_clone_buffer2();\r
+       if (rom_data)\r
+               gp2x_pd_clone_buffer2();\r
+       else {\r
+               memset(gp2x_screen, 0, 320*240*2);\r
+               memset((char *)gp2x_screen + 320*120*2, 0xff, 320*8*2);\r
+       }\r
+\r
+       menu_darken_bg((char *)gp2x_screen + 320*120*2, 320*8);\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
@@ -637,11 +745,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
@@ -654,7 +762,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
@@ -664,14 +772,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
@@ -700,8 +809,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
@@ -762,8 +869,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
@@ -780,20 +886,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
+       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
+       text_out16(tl_x, y, "back");\r
 \r
-       // draw cursor\r
-       gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
-\r
-       gp2x_video_flip2();\r
+       menu_flip();\r
 }\r
 \r
 static int savestate_menu_loop(int is_loading)\r
@@ -918,33 +1023,32 @@ static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_i
 \r
        gp2x_pd_clone_buffer2();\r
        if (player_idx >= 0) {\r
-               gp2x_text_out8(80, 20, "Player %i controls", player_idx + 1);\r
+               text_out16(80, 20, "Player %i controls", player_idx + 1);\r
                x = 100;\r
        } else {\r
-               gp2x_text_out8(80, 20, "Emulator controls");\r
+               text_out16(80, 20, "Emulator controls");\r
                x = 40;\r
        }\r
 \r
+       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
-               gp2x_text_out8(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));\r
+               text_out16(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));\r
 \r
-       gp2x_text_out8(x, y, "Done");\r
-\r
-       // draw cursor\r
-       gp2x_text_out8(x - 16, tl_y + sel*10, ">");\r
+       text_out16(x, y, "Done");\r
 \r
        if (sel < opt_cnt) {\r
-               gp2x_text_out8(30, 190, "Press a button to bind/unbind");\r
-               gp2x_text_out8(30, 200, "Use SELECT to clear");\r
-               gp2x_text_out8(30, 210, "To bind UP/DOWN, hold SELECT");\r
-               gp2x_text_out8(30, 220, "Select \"Done\" to exit");\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
-               gp2x_text_out8(30, 200, "Use Options -> Save cfg");\r
-               gp2x_text_out8(30, 210, "to save controls");\r
-               gp2x_text_out8(30, 220, "Press B or X to exit");\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
-       gp2x_video_flip2();\r
+       menu_flip();\r
 }\r
 \r
 static void key_config_loop(const bind_action_t *opts, int opt_cnt, int player_idx)\r
@@ -1007,26 +1111,25 @@ static void draw_kc_sel(int menu_sel)
 \r
        y = tl_y;\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), "Emulator controls");\r
-       gp2x_text_out8(tl_x, (y+=10), "Done");\r
+       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
-       gp2x_video_flip2();\r
+       menu_flip();\r
 }\r
 \r
 \r
@@ -1052,13 +1155,13 @@ static bind_action_t ctrl_actions[] =
 // "LOAD STATE", "VOLUME UP", "VOLUME DOWN", "DONE"\r
 static bind_action_t emuctrl_actions[] =\r
 {\r
-       { "Volume Down    ", 1<<30 },\r
-       { "Volume Up      ", 1<<29 },\r
        { "Load State     ", 1<<28 },\r
        { "Save State     ", 1<<27 },\r
-       { "Switch Renderer", 1<<26 },\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
@@ -1121,13 +1224,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
@@ -1144,18 +1247,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
+       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
@@ -1263,9 +1365,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
@@ -1276,12 +1378,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
+       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
@@ -1388,7 +1489,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
@@ -1397,20 +1498,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
@@ -1419,18 +1520,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
@@ -1442,16 +1543,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
+       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
@@ -1635,28 +1735,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), "ketch: skin design");\r
+\r
+       menu_flip();\r
 }\r
 \r
 \r
@@ -1684,15 +1784,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
+       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
@@ -1701,18 +1804,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
@@ -1775,12 +1866,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
@@ -1815,31 +1918,45 @@ 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)\r
 {\r
-       extern int localPal[0x100];\r
-       int c, i;\r
+       unsigned int *screen = dst;\r
+       pixels /= 2;\r
+       while (pixels--)\r
+       {\r
+               unsigned int p = *screen;\r
+               *screen = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);\r
+               screen++;\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);\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
@@ -1858,20 +1975,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