starting SDL port, refactoring
authornotaz <notasas@gmail.com>
Sat, 22 Jun 2013 23:28:52 +0000 (02:28 +0300)
committernotaz <notasas@gmail.com>
Wed, 26 Jun 2013 00:07:08 +0000 (03:07 +0300)
12 files changed:
.gitignore
Makefile
platform/common/emu.c
platform/common/emu.h
platform/common/input_pico.h
platform/common/main.c
platform/common/menu_pico.c
platform/common/menu_pico.h
platform/common/mp3_dummy.c [new file with mode: 0644]
platform/common/plat_sdl.c [new file with mode: 0644]
platform/libpicofe
platform/linux/emu.c

index 3c582d0..8190719 100644 (file)
@@ -8,3 +8,6 @@ config.log
 cpu/musashi/m68kmake
 cpu/musashi/m68kops.c
 cpu/musashi/m68kops.h
+skin
+config.cfg
+srm/
index a503980..4cd832f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -26,20 +26,26 @@ CFLAGS += -I.
 CFLAGS += -Iplatform/linux/
 LDLIBS += -lm -lpng
 
+# tmp
+CFLAGS += `sdl-config --cflags`
+LDLIBS += `sdl-config --libs`
+
 all: PicoDrive
 
 # frontend
-OBJS += platform/linux/io.o platform/linux/emu.o platform/linux/blit.o \
+OBJS += platform/linux/emu.o platform/linux/blit.o \
        platform/linux/log_io.o
 
 # common
 OBJS += platform/common/main.o platform/common/emu.o platform/common/menu_pico.o \
-       platform/common/config.o
+       platform/common/config.o platform/common/plat_sdl.o \
+       platform/common/mp3_dummy.o
 
 # libpicofe
 OBJS += platform/libpicofe/input.o platform/libpicofe/readpng.o \
        platform/libpicofe/fonts.o platform/libpicofe/linux/in_evdev.o \
-       platform/libpicofe/linux/plat.o platform/libpicofe/linux/sndout_oss.o
+       platform/libpicofe/linux/plat.o platform/libpicofe/linux/sndout_oss.o \
+       platform/libpicofe/plat_sdl.o platform/libpicofe/in_sdl.o
 
 OBJS += platform/libpicofe/plat_dummy.o
 
index 8066796..76765a4 100644 (file)
@@ -47,8 +47,7 @@ int engineState = PGS_Menu;
 \r
 /* tmp buff to reduce stack usage for plats with small stack */\r
 static char static_buff[512];\r
-/* TODO: len checking */\r
-char rom_fname_reload[512];\r
+const char *rom_fname_reload;\r
 char rom_fname_loaded[512];\r
 int rom_loaded = 0;\r
 int reset_timing = 0;\r
@@ -147,7 +146,7 @@ static const char * const biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "Se
 static const char * const biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303"   };\r
 static const char * const biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };\r
 \r
-static int find_bios(int region, char **bios_file)\r
+static int find_bios(int region, const char **bios_file)\r
 {\r
        int i, count;\r
        const char * const *files;\r
@@ -484,21 +483,28 @@ static void system_announce(void)
        emu_status_msg("%s %s / %dFPS%s", tv_standard, sys_name, fps, extra);\r
 }\r
 \r
-// note: this function might mangle rom_fname\r
 // XXX: portions of this code should move to pico/\r
-int emu_reload_rom(char *rom_fname)\r
+int emu_reload_rom(const char *rom_fname_in)\r
 {\r
        unsigned int rom_size = 0;\r
-       char *used_rom_name = rom_fname;\r
+       const char *used_rom_name = NULL;\r
+       char *rom_fname = NULL;\r
        unsigned char *rom_data = NULL;\r
        char ext[5];\r
        pm_file *rom = NULL;\r
        int cd_state = CIT_NOT_CD;\r
        int ret, media_type, cd_region;\r
        int cfg_loaded = 0, bad_rom = 0;\r
+       int menu_romload_started = 0;\r
+       int retval = 0;\r
 \r
-       lprintf("emu_ReloadRom(%s)\n", rom_fname);\r
+       lprintf("emu_ReloadRom(%s)\n", rom_fname_in);\r
 \r
+       rom_fname = strdup(rom_fname_in);\r
+       if (rom_fname == NULL)\r
+               return 0;\r
+\r
+       used_rom_name = rom_fname;\r
        get_ext(rom_fname, ext);\r
 \r
        // early cleanup\r
@@ -515,7 +521,7 @@ int emu_reload_rom(char *rom_fname)
                FILE *movie_file = fopen(rom_fname, "rb");\r
                if (!movie_file) {\r
                        menu_update_msg("Failed to open movie.");\r
-                       return 0;\r
+                       goto out;\r
                }\r
                fseek(movie_file, 0, SEEK_END);\r
                movie_size = ftell(movie_file);\r
@@ -523,24 +529,24 @@ int emu_reload_rom(char *rom_fname)
                if (movie_size < 64+3) {\r
                        menu_update_msg("Invalid GMV file.");\r
                        fclose(movie_file);\r
-                       return 0;\r
+                       goto out;\r
                }\r
                movie_data = malloc(movie_size);\r
                if (movie_data == NULL) {\r
                        menu_update_msg("low memory.");\r
                        fclose(movie_file);\r
-                       return 0;\r
+                       goto out;\r
                }\r
                dummy = fread(movie_data, 1, movie_size, movie_file);\r
                fclose(movie_file);\r
                if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {\r
                        menu_update_msg("Invalid GMV file.");\r
-                       return 0;\r
+                       goto out;\r
                }\r
                dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);\r
                if (!dummy) {\r
                        menu_update_msg("Could't find a ROM for movie.");\r
-                       return 0;\r
+                       goto out;\r
                }\r
                get_ext(rom_fname, ext);\r
                lprintf("gmv loaded for %s\n", rom_fname);\r
@@ -552,7 +558,7 @@ int emu_reload_rom(char *rom_fname)
                dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);\r
                if (!dummy) {\r
                        menu_update_msg("Could't find a ROM to patch.");\r
-                       return 0;\r
+                       goto out;\r
                }\r
                get_ext(rom_fname, ext);\r
        }\r
@@ -560,7 +566,7 @@ int emu_reload_rom(char *rom_fname)
        media_type = detect_media(rom_fname);\r
        if (media_type == PM_BAD) {\r
                menu_update_msg("Not a ROM/CD img selected.");\r
-               return 0;\r
+               goto out;\r
        }\r
 \r
        shutdown_MCD();\r
@@ -588,14 +594,14 @@ int emu_reload_rom(char *rom_fname)
                                        (cd_region == 8 ? "EU" : "JAP") : "USA");\r
                        }\r
                        if (!find_bios(cd_region, &used_rom_name))\r
-                               return 0;\r
+                               goto out;\r
 \r
                        get_ext(used_rom_name, ext);\r
                        PicoAHW |= PAHW_MCD;\r
                }\r
                else {\r
                        menu_update_msg("Invalid CD image");\r
-                       return 0;\r
+                       goto out;\r
                }\r
        }\r
        else if (media_type == PM_MARK3) {\r
@@ -606,10 +612,11 @@ int emu_reload_rom(char *rom_fname)
        rom = pm_open(used_rom_name);\r
        if (rom == NULL) {\r
                menu_update_msg("Failed to open ROM");\r
-               return 0;\r
+               goto out;\r
        }\r
 \r
        menu_romload_prepare(used_rom_name); // also CD load\r
+       menu_romload_started = 1;\r
        used_rom_name = NULL; // uses static_buff\r
 \r
        ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoAHW & PAHW_SMS) ? 1 : 0);\r
@@ -618,7 +625,7 @@ int emu_reload_rom(char *rom_fname)
                if      (ret == 2) menu_update_msg("Out of memory");\r
                else if (ret == 3) menu_update_msg("Read failed");\r
                else               menu_update_msg("PicoCartLoad() failed.");\r
-               goto fail;\r
+               goto out;\r
        }\r
 \r
        // detect wrong files\r
@@ -634,7 +641,7 @@ int emu_reload_rom(char *rom_fname)
 \r
        if (bad_rom) {\r
                menu_update_msg("Bad ROM detected.");\r
-               goto fail;\r
+               goto out;\r
        }\r
 \r
        // load config for this ROM (do this before insert to get correct region)\r
@@ -648,7 +655,7 @@ int emu_reload_rom(char *rom_fname)
        emu_make_path(static_buff, "carthw.cfg", sizeof(static_buff));\r
        if (PicoCartInsert(rom_data, rom_size, static_buff)) {\r
                menu_update_msg("Failed to load ROM.");\r
-               goto fail;\r
+               goto out;\r
        }\r
 \r
        // insert CD if it was detected\r
@@ -658,11 +665,12 @@ int emu_reload_rom(char *rom_fname)
                        PicoCartUnload();\r
                        rom_data = NULL; // freed by unload\r
                        menu_update_msg("Insert_CD() failed, invalid CD image?");\r
-                       goto fail;\r
+                       goto out;\r
                }\r
        }\r
 \r
        menu_romload_end();\r
+       menu_romload_started = 0;\r
 \r
        if (PicoPatches) {\r
                PicoPatchPrepare();\r
@@ -702,13 +710,14 @@ int emu_reload_rom(char *rom_fname)
        if (currentConfig.EmuOpt & EOPT_EN_SRAM)\r
                emu_save_load_game(1, 1);\r
 \r
-       return 1;\r
-\r
-fail:\r
-       if (rom_data)\r
+       retval = 1;\r
+out:\r
+       if (retval == 0 && rom_data)\r
                free(rom_data);\r
-       menu_romload_end();\r
-       return 0;\r
+       if (menu_romload_started)\r
+               menu_romload_end();\r
+       free(rom_fname);\r
+       return retval;\r
 }\r
 \r
 int emu_swap_cd(const char *fname)\r
@@ -1466,6 +1475,7 @@ void emu_loop(void)
        if (PicoAHW & PAHW_MCD)\r
                PicoCDBufferInit();\r
 \r
+       plat_video_loop_prepare();\r
        pemu_loop_prep();\r
 \r
        /* number of ticks per frame */\r
index ae8e4d7..faa4868 100644 (file)
@@ -95,7 +95,7 @@ extern int reset_timing;
 extern int pico_pen_x, pico_pen_y;
 extern int pico_inp_mode;
 
-extern char rom_fname_reload[512];             // ROM to try loading on next PGS_ReloadRom
+extern const char *rom_fname_reload;           // ROM to try loading on next PGS_ReloadRom
 extern char rom_fname_loaded[512];             // currently loaded ROM filename
 
 // engine states
@@ -125,7 +125,7 @@ void  emu_init(void);
 void  emu_finish(void);
 void  emu_loop(void);
 
-int   emu_reload_rom(char *rom_fname);
+int   emu_reload_rom(const char *rom_fname_in);
 int   emu_swap_cd(const char *fname);
 int   emu_save_load_game(int load, int sram);
 void  emu_reset_game(void);
@@ -177,6 +177,7 @@ void plat_status_msg_busy_next(const char *msg);
 void plat_status_msg_clear(void);
 
 void plat_video_toggle_renderer(int change, int menu_call);
+void plat_video_loop_prepare(void);
 
 void plat_update_volume(int has_changed, int is_up);
 
index fbf328a..c2730b5 100644 (file)
@@ -1,6 +1,20 @@
 #ifndef INCLUDE_c48097f3ff2a6a9af1cce8fd7a9b3f0c
 #define INCLUDE_c48097f3ff2a6a9af1cce8fd7a9b3f0c 1
 
+/* gamepad - MXYZ SACB RLDU */
+#define GBTN_UP         0
+#define GBTN_DOWN       1
+#define GBTN_LEFT       2
+#define GBTN_RIGHT      3
+#define GBTN_B          4
+#define GBTN_C          5
+#define GBTN_A          6
+#define GBTN_START      7
+#define GBTN_Z          8
+#define GBTN_Y          9
+#define GBTN_X          10
+#define GBTN_MODE       11
+
 /* ui events */
 #define PEVB_VOL_DOWN   30
 #define PEVB_VOL_UP     29
index 5a0287e..968da0d 100644 (file)
@@ -49,24 +49,25 @@ void parse_cmd_line(int argc, char *argv[])
                                break;\r
                        }\r
                } else {\r
-                       /* External Frontend: ROM Name */\r
-                       FILE *f;\r
-                       strncpy(rom_fname_reload, argv[x], sizeof(rom_fname_reload));\r
-                       rom_fname_reload[sizeof(rom_fname_reload) - 1] = 0;\r
-                       f = fopen(rom_fname_reload, "rb");\r
-                       if (f) fclose(f);\r
-                       else unrecognized = 1;\r
-                       engineState = PGS_ReloadRom;\r
+                       FILE *f = fopen(argv[x], "rb");\r
+                       if (f) {\r
+                               fclose(f);\r
+                               rom_fname_reload = argv[x];\r
+                               engineState = PGS_ReloadRom;\r
+                       }\r
+                       else\r
+                               unrecognized = 1;\r
                        break;\r
                }\r
        }\r
 \r
        if (unrecognized) {\r
-               printf("\n\n\nPicoDrive v" VERSION " (c) notaz, 2006-2009\n");\r
+               printf("\n\n\nPicoDrive v" VERSION " (c) notaz, 2006-2009,2013\n");\r
                printf("usage: %s [options] [romfile]\n", argv[0]);\r
                printf("options:\n"\r
                        " -config <file>    use specified config file instead of default 'config.cfg'\n"\r
-                       " -loadstate <num>  if ROM is specified, try loading slot <num>\n");\r
+                       " -loadstate <num>  if ROM is specified, try loading savestate slot <num>\n");\r
+               exit(1);\r
        }\r
 }\r
 \r
@@ -75,12 +76,13 @@ int main(int argc, char *argv[])
 {\r
        g_argv = argv;\r
 \r
-       //plat_early_init();\r
+       plat_early_init();\r
 \r
        in_init();\r
-       in_probe();\r
+       //in_probe();\r
 \r
        plat_target_init();\r
+       plat_init();\r
 \r
        emu_prep_defconfig(); // depends on input\r
        emu_read_config(NULL, 0);\r
@@ -145,6 +147,7 @@ int main(int argc, char *argv[])
        endloop:\r
 \r
        emu_finish();\r
+       plat_finish();\r
        plat_target_finish();\r
 \r
        return 0;\r
index 47f0023..62b6b80 100644 (file)
 #define REVISION "0"
 
 static const char *rom_exts[] = {
-       "zip", "bin", "smd", "gen",
-       "iso", "cso", "cue", NULL
+       "zip",
+       "bin", "smd", "gen",
+       "iso", "cso", "cue",
+       "32x",
+       "sms",
+       NULL
 };
 
 // rrrr rggg gggb bbbb
@@ -56,8 +60,21 @@ static void menu_enter(int is_rom_loaded)
 {
        if (is_rom_loaded)
        {
+               int w = g_screen_width, h = g_screen_height;
+               short *src, *dst;
+
+               if (w > g_menuscreen_w)
+                       w = g_menuscreen_w;
+               if (h > g_menuscreen_h)
+                       h = g_menuscreen_h;
+               src = (short *)g_menubg_src_ptr;
+               dst = (short *)g_menubg_ptr +
+                       (g_menuscreen_h / 2 - h / 2) * g_menuscreen_w +
+                       (g_menuscreen_w / 2 - w / 2);
+
                // darken the active framebuffer
-               menu_darken_bg(g_menubg_ptr, g_menubg_src_ptr, g_menuscreen_w * g_menuscreen_h, 1);
+               for (; h > 0; dst += g_menuscreen_w, src += g_screen_width, h--)
+                       menu_darken_bg(dst, src, w, 1);
        }
        else
        {
@@ -468,9 +485,12 @@ static int menu_loop_adv_options(int id, int keys)
 
 // ------------ gfx options menu ------------
 
+static const char *men_dummy[] = { NULL };
+
 static menu_entry e_menu_gfx_options[] =
 {
-       mee_enum("Renderer", MA_OPT_RENDERER, currentConfig.renderer, renderer_names),
+       mee_enum("Video output mode", MA_OPT_VOUT_MODE, plat_target.vout_method, men_dummy),
+       mee_enum("Renderer",          MA_OPT_RENDERER, currentConfig.renderer, renderer_names),
        MENU_OPTIONS_GFX
        mee_end,
 };
@@ -866,21 +886,6 @@ static const char credits[] =
        " Lordus, Exophase, Rokas,\n"
        " Nemesis, Tasco Deluxe";
 
-static const char *romsel_run(void)
-{
-       const char *ret;
-       char *sel_name;
-
-       sel_name = malloc(sizeof(rom_fname_loaded));
-       if (sel_name == NULL)
-               return NULL;
-       strcpy(sel_name, rom_fname_loaded);
-
-       ret = menu_loop_romsel(sel_name, sizeof(rom_fname_loaded), rom_exts, NULL);
-       free(sel_name);
-       return ret;
-}
-
 static int main_menu_handler(int id, int keys)
 {
        const char *ret_name;
@@ -906,9 +911,12 @@ static int main_menu_handler(int id, int keys)
                }
                break;
        case MA_MAIN_LOAD_ROM:
-               ret_name = romsel_run();
+               rom_fname_reload = NULL;
+               ret_name = menu_loop_romsel(rom_fname_loaded,
+                       sizeof(rom_fname_loaded), rom_exts, NULL);
                if (ret_name != NULL) {
                        lprintf("selected file: %s\n", ret_name);
+                       rom_fname_reload = ret_name;
                        engineState = PGS_ReloadRom;
                        return 1;
                }
@@ -977,6 +985,7 @@ void menu_loop(void)
        }
 
        in_set_config_int(0, IN_CFG_BLOCKING, 0);
+       plat_video_menu_leave();
 }
 
 // --------- CD tray close menu ----------
@@ -985,10 +994,13 @@ static int mh_tray_load_cd(int id, int keys)
 {
        const char *ret_name;
 
-       ret_name = romsel_run();
+       rom_fname_reload = NULL;
+       ret_name = menu_loop_romsel(rom_fname_loaded,
+                       sizeof(rom_fname_loaded), rom_exts, NULL);
        if (ret_name == NULL)
                return 0;
 
+       rom_fname_reload = ret_name;
        engineState = PGS_RestartRun;
        return emu_swap_cd(ret_name);
 }
@@ -1024,6 +1036,7 @@ int menu_loop_tray(void)
 
        while (in_menu_wait_any(NULL, 50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK));
        in_set_config_int(0, IN_CFG_BLOCKING, 0);
+       plat_video_menu_leave();
 
        return ret;
 }
@@ -1090,5 +1103,12 @@ menu_entry *me_list_get_next(void)
 
 void menu_init(void)
 {
+       int i;
+
        menu_init_base();
+
+       i = me_id2offset(e_menu_gfx_options, MA_OPT_VOUT_MODE);
+       e_menu_gfx_options[i].data = plat_target.vout_methods;
+       me_enable(e_menu_gfx_options, MA_OPT_VOUT_MODE,
+               plat_target.vout_methods != NULL);
 }
index 31cee72..4302ded 100644 (file)
@@ -39,6 +39,7 @@ typedef enum
        MA_OPT_INTERLACED,      /* giz */
        MA_OPT_ROTATION,        /* uiq */
        MA_OPT_TEARING_FIX,     /* wiz */
+       MA_OPT_VOUT_MODE,
        MA_OPT2_GAMMA,
        MA_OPT2_A_SN_GAMMA,
        MA_OPT2_DBLBUFF,        /* giz */
diff --git a/platform/common/mp3_dummy.c b/platform/common/mp3_dummy.c
new file mode 100644 (file)
index 0000000..a76caf9
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * dummy/none mp3 code
+ * (C) notaz, 2013
+ *
+ * This work is licensed under the terms of MAME license.
+ * See COPYING file in the top-level directory.
+ */
+
+#include "mp3.h"
+#include <pico/pico.h>
+
+int mp3_get_bitrate(void *f_, int len)
+{
+       return -1;
+}
+
+void mp3_start_play(void *f_, int pos)
+{
+}
+
+void mp3_update(int *buffer, int length, int stereo)
+{
+}
diff --git a/platform/common/plat_sdl.c b/platform/common/plat_sdl.c
new file mode 100644 (file)
index 0000000..28b412d
--- /dev/null
@@ -0,0 +1,246 @@
+/*
+ * PicoDrive
+ * (C) notaz, 2013
+ *
+ * This work is licensed under the terms of MAME license.
+ * See COPYING file in the top-level directory.
+ */
+
+#include <stdio.h>
+
+#include "../libpicofe/input.h"
+#include "../libpicofe/plat_sdl.h"
+#include "../libpicofe/in_sdl.h"
+#include "../libpicofe/gl.h"
+#include "emu.h"
+#include "menu_pico.h"
+#include "input_pico.h"
+#include "version.h"
+
+// FIXME: these 2 shouldn't be here
+static unsigned char PicoDraw2FB_[(8+320) * (8+240+8)];
+unsigned char *PicoDraw2FB = PicoDraw2FB_;
+
+static void *shadow_fb;
+
+static const struct in_default_bind in_sdl_defbinds[] = {
+       { SDLK_UP,     IN_BINDTYPE_PLAYER12, GBTN_UP },
+       { SDLK_DOWN,   IN_BINDTYPE_PLAYER12, GBTN_DOWN },
+       { SDLK_LEFT,   IN_BINDTYPE_PLAYER12, GBTN_LEFT },
+       { SDLK_RIGHT,  IN_BINDTYPE_PLAYER12, GBTN_RIGHT },
+       { SDLK_z,      IN_BINDTYPE_PLAYER12, GBTN_A },
+       { SDLK_x,      IN_BINDTYPE_PLAYER12, GBTN_B },
+       { SDLK_c,      IN_BINDTYPE_PLAYER12, GBTN_C },
+       { SDLK_a,      IN_BINDTYPE_PLAYER12, GBTN_X },
+       { SDLK_s,      IN_BINDTYPE_PLAYER12, GBTN_Y },
+       { SDLK_d,      IN_BINDTYPE_PLAYER12, GBTN_Z },
+       { SDLK_RETURN, IN_BINDTYPE_PLAYER12, GBTN_START },
+       { SDLK_f,      IN_BINDTYPE_PLAYER12, GBTN_MODE },
+       { SDLK_ESCAPE, IN_BINDTYPE_EMU, PEVB_MENU },
+       { SDLK_F1,     IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
+       { SDLK_F2,     IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
+       { SDLK_F3,     IN_BINDTYPE_EMU, PEVB_SSLOT_PREV },
+       { SDLK_F4,     IN_BINDTYPE_EMU, PEVB_SSLOT_NEXT },
+       { SDLK_F5,     IN_BINDTYPE_EMU, PEVB_SWITCH_RND },
+       { SDLK_F6,     IN_BINDTYPE_EMU, PEVB_PICO_PPREV },
+       { SDLK_F7,     IN_BINDTYPE_EMU, PEVB_PICO_PNEXT },
+       { SDLK_F8,     IN_BINDTYPE_EMU, PEVB_PICO_SWINP },
+       { SDLK_BACKSPACE, IN_BINDTYPE_EMU, PEVB_FF },
+       { 0, 0, 0 }
+};
+
+/* YUV stuff */
+static int yuv_ry[32], yuv_gy[32], yuv_by[32];
+static unsigned char yuv_u[32 * 2], yuv_v[32 * 2];
+
+void bgr_to_uyvy_init(void)
+{
+  int i, v;
+
+  /* init yuv converter:
+    y0 = (int)((0.299f * r0) + (0.587f * g0) + (0.114f * b0));
+    y1 = (int)((0.299f * r1) + (0.587f * g1) + (0.114f * b1));
+    u = (int)(8 * 0.565f * (b0 - y0)) + 128;
+    v = (int)(8 * 0.713f * (r0 - y0)) + 128;
+  */
+  for (i = 0; i < 32; i++) {
+    yuv_ry[i] = (int)(0.299f * i * 65536.0f + 0.5f);
+    yuv_gy[i] = (int)(0.587f * i * 65536.0f + 0.5f);
+    yuv_by[i] = (int)(0.114f * i * 65536.0f + 0.5f);
+  }
+  for (i = -32; i < 32; i++) {
+    v = (int)(8 * 0.565f * i) + 128;
+    if (v < 0)
+      v = 0;
+    if (v > 255)
+      v = 255;
+    yuv_u[i + 32] = v;
+    v = (int)(8 * 0.713f * i) + 128;
+    if (v < 0)
+      v = 0;
+    if (v > 255)
+      v = 255;
+    yuv_v[i + 32] = v;
+  }
+}
+
+void rgb565_to_uyvy(void *d, const void *s, int pixels)
+{
+  unsigned int *dst = d;
+  const unsigned short *src = s;
+  const unsigned char *yu = yuv_u + 32;
+  const unsigned char *yv = yuv_v + 32;
+  int r0, g0, b0, r1, g1, b1;
+  int y0, y1, u, v;
+
+  for (; pixels > 0; src += 2, dst++, pixels -= 2)
+  {
+    r0 = (src[0] >> 11) & 0x1f;
+    g0 = (src[0] >> 6) & 0x1f;
+    b0 =  src[0] & 0x1f;
+    r1 = (src[1] >> 11) & 0x1f;
+    g1 = (src[1] >> 6) & 0x1f;
+    b1 =  src[1] & 0x1f;
+    y0 = (yuv_ry[r0] + yuv_gy[g0] + yuv_by[b0]) >> 16;
+    y1 = (yuv_ry[r1] + yuv_gy[g1] + yuv_by[b1]) >> 16;
+    u = yu[b0 - y0];
+    v = yv[r0 - y0];
+    // valid Y range seems to be 16..235
+    y0 = 16 + 219 * y0 / 31;
+    y1 = 16 + 219 * y1 / 31;
+
+    *dst = (y1 << 24) | (v << 16) | (y0 << 8) | u;
+  }
+}
+
+void plat_video_flip(void)
+{
+       if (plat_sdl_overlay != NULL) {
+               SDL_Rect dstrect =
+                       { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
+
+               SDL_LockYUVOverlay(plat_sdl_overlay);
+               rgb565_to_uyvy(plat_sdl_overlay->pixels[0], shadow_fb,
+                               g_screen_width * g_screen_height);
+               SDL_UnlockYUVOverlay(plat_sdl_overlay);
+               SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
+       }
+       else if (plat_sdl_gl_active) {
+               gl_flip(shadow_fb, g_screen_width, g_screen_height);
+       }
+       else {
+               // XXX: no locking, but should be fine with SDL_SWSURFACE?
+               SDL_Flip(plat_sdl_screen);
+               g_screen_ptr = plat_sdl_screen->pixels;
+       }
+}
+
+void plat_video_wait_vsync(void)
+{
+}
+
+void plat_video_menu_enter(int is_rom_loaded)
+{
+       plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 0);
+}
+
+void plat_video_menu_begin(void)
+{
+       if (plat_sdl_overlay != NULL || plat_sdl_gl_active) {
+               g_menuscreen_ptr = shadow_fb;
+       }
+       else {
+               SDL_LockSurface(plat_sdl_screen);
+               g_menuscreen_ptr = plat_sdl_screen->pixels;
+       }
+}
+
+void plat_video_menu_end(void)
+{
+       if (plat_sdl_overlay != NULL) {
+               SDL_Rect dstrect =
+                       { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
+
+               SDL_LockYUVOverlay(plat_sdl_overlay);
+               rgb565_to_uyvy(plat_sdl_overlay->pixels[0], shadow_fb,
+                               g_menuscreen_w * g_menuscreen_h);
+               SDL_UnlockYUVOverlay(plat_sdl_overlay);
+
+               SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
+       }
+       else if (plat_sdl_gl_active) {
+               gl_flip(g_menuscreen_ptr, g_menuscreen_w, g_menuscreen_h);
+       }
+       else {
+               SDL_UnlockSurface(plat_sdl_screen);
+               SDL_Flip(plat_sdl_screen);
+       }
+       g_menuscreen_ptr = NULL;
+
+}
+
+void plat_video_menu_leave(void)
+{
+}
+
+void plat_video_loop_prepare(void)
+{
+       plat_sdl_change_video_mode(g_screen_width, g_screen_height, 0);
+
+       if (plat_sdl_overlay != NULL || plat_sdl_gl_active) {
+               g_screen_ptr = shadow_fb;
+       }
+       else {
+               SDL_LockSurface(plat_sdl_screen);
+               g_screen_ptr = plat_sdl_screen->pixels;
+       }
+}
+
+void plat_early_init(void)
+{
+}
+
+void plat_init(void)
+{
+       int shadow_size;
+       int ret;
+
+       ret = plat_sdl_init();
+       if (ret != 0)
+               exit(1);
+
+       SDL_WM_SetCaption("PicoDrive" VERSION, NULL);
+
+       g_menuscreen_w = plat_sdl_screen->w;
+       g_menuscreen_h = plat_sdl_screen->h;
+       g_menuscreen_ptr = NULL;
+
+       shadow_size = g_menuscreen_w * g_menuscreen_h * 2;
+       if (shadow_size < 320 * 480 * 2)
+               shadow_size = 320 * 480 * 2;
+
+       shadow_fb = malloc(shadow_size);
+       g_menubg_ptr = malloc(shadow_size);
+       if (shadow_fb == NULL || g_menubg_ptr == NULL) {
+               fprintf(stderr, "OOM\n");
+               exit(1);
+       }
+
+       g_screen_width = 320;
+       g_screen_height = 240;
+       g_screen_ptr = shadow_fb;
+
+       in_sdl_init(in_sdl_defbinds, plat_sdl_event_handler);
+       in_probe();
+
+       bgr_to_uyvy_init();
+}
+
+void plat_finish(void)
+{
+       free(shadow_fb);
+       shadow_fb = NULL;
+       free(g_menubg_ptr);
+       g_menubg_ptr = NULL;
+       plat_sdl_finish();
+}
index 7bf7acb..20b1430 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 7bf7acb6d60e16e9eaa208761d019c39da396fc0
+Subproject commit 20b143089cc395dbcd51cac516a9e36f4ab6f5ac
index 931031b..591db8a 100644 (file)
@@ -171,20 +171,6 @@ void plat_video_toggle_renderer(int change, int is_menu)
        emu_status_msg(renderer_names[currentConfig.renderer]);\r
 }\r
 \r
-void plat_video_menu_enter(int is_rom_loaded)\r
-{\r
-}\r
-\r
-void plat_video_menu_begin(void)\r
-{\r
-       g_menuscreen_ptr = g_screen_ptr;\r
-}\r
-\r
-void plat_video_menu_end(void)\r
-{\r
-       plat_video_flip();\r
-}\r
-\r
 void plat_status_msg_clear(void)\r
 {\r
        unsigned short *d = (unsigned short *)g_screen_ptr + g_screen_width * g_screen_height;\r