savestate loader in menu
authornotaz <notasas@gmail.com>
Sun, 25 Feb 2007 19:20:45 +0000 (19:20 +0000)
committernotaz <notasas@gmail.com>
Sun, 25 Feb 2007 19:20:45 +0000 (19:20 +0000)
git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@56 be3aeb3a-fb24-0410-a615-afba39da0efa

gp2x/emu.c
gp2x/emu.h
gp2x/gp2x.c
gp2x/gp2x.h
gp2x/menu.c

index 1c81c87..cdb69b7 100644 (file)
@@ -6,6 +6,8 @@
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include <sys/time.h>\r
+#include <sys/stat.h>\r
+#include <sys/types.h>\r
 #include <linux/limits.h>\r
 #include <ctype.h>\r
 #include <unistd.h>\r
@@ -394,22 +396,36 @@ void emu_Init(void)
                printf("framebuff == 0\n");\r
        }\r
 \r
+       // make dirs for saves, cfgs, etc.\r
+       mkdir("mds", 0777);\r
+       mkdir("srm", 0777);\r
+       mkdir("brm", 0777);\r
+       mkdir("cfg", 0777);\r
+\r
        PicoInit();\r
 \r
 //     logf = fopen("log.txt", "w");\r
 }\r
 \r
 \r
-static void romfname_ext(char *dst, char *ext)\r
+static void romfname_ext(char *dst, const char *prefix, const char *ext)\r
 {\r
        char *p;\r
+       int prefix_len = 0;\r
 \r
        // make save filename\r
        for (p = romFileName+strlen(romFileName)-1; p >= romFileName && *p != '/'; p--); p++;\r
-       strncpy(dst, p, 511);\r
+       *dst = 0;\r
+       if (prefix) {\r
+               strcpy(dst, prefix);\r
+               prefix_len = strlen(prefix);\r
+       }\r
+       strncpy(dst + prefix_len, p, 511-prefix_len);\r
        dst[511-8] = 0;\r
-       if(dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;\r
-       strcat(dst, ext);\r
+       if (dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;\r
+       if (ext) strcat(dst, ext);\r
+\r
+       printf("romfname_ext: %s\n", dst);\r
 }\r
 \r
 \r
@@ -479,7 +495,10 @@ int emu_ReadConfig(int game)
                strncpy(cfg, PicoConfigFile, 511);\r
                cfg[511] = 0;\r
        } else {\r
-               romfname_ext(cfg, ".pbcfg");\r
+               romfname_ext(cfg, "cfg/", ".pbcfg");\r
+               f = fopen(cfg, "rb");\r
+               if (!f) romfname_ext(cfg, NULL, ".pbcfg");\r
+               else fclose(f);\r
        }\r
 \r
        printf("emu_ReadConfig: %s ", cfg);\r
@@ -522,7 +541,7 @@ int emu_WriteConfig(int game)
                strncpy(cfg, PicoConfigFile, 511);\r
                cfg[511] = 0;\r
        } else {\r
-               romfname_ext(cfg, ".pbcfg");\r
+               romfname_ext(cfg, "cfg", ".pbcfg");\r
        }\r
 \r
        printf("emu_WriteConfig: %s ", cfg);\r
@@ -745,25 +764,6 @@ static void vidResetMode(void)
 }\r
 \r
 \r
-static int check_save_file(void)\r
-{\r
-       char saveFname[512];\r
-       char ext[16];\r
-       FILE *f;\r
-\r
-       ext[0] = 0;\r
-       if(state_slot > 0 && state_slot < 10) sprintf(ext, ".%i", state_slot);\r
-       strcat(ext, ".mds");\r
-       if(currentConfig.EmuOpt & 8) strcat(ext, ".gz");\r
-\r
-       romfname_ext(saveFname, ext);\r
-       if ((f = fopen(saveFname, "rb"))) {\r
-               fclose(f);\r
-               return 1;\r
-       }\r
-       return 0;\r
-}\r
-\r
 static void emu_state_cb(const char *str)\r
 {\r
        clearArea(0);\r
@@ -774,7 +774,7 @@ static void RunEvents(unsigned int which)
 {\r
        if(which & 0x1800) { // save or load (but not both)\r
                int do_it = 1;\r
-               if (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200) && check_save_file()) {\r
+               if (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200) && emu_check_save_file(state_slot)) {\r
                        unsigned long keys;\r
                        blit("", "OVERWRITE SAVE? (Y=yes, X=no)");\r
                        while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )\r
@@ -817,7 +817,7 @@ static void RunEvents(unsigned int which)
                        state_slot += 1;\r
                        if(state_slot > 9) state_slot = 0;\r
                }\r
-               sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, check_save_file() ? "USED" : "FREE");\r
+               sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_check_save_file(state_slot) ? "USED" : "FREE");\r
                gettimeofday(&noticeMsgTime, 0);\r
        }\r
        if(which & 0x0080) {\r
@@ -964,6 +964,24 @@ static void SkipFrame(int do_sound)
 }\r
 \r
 \r
+void emu_forced_frame(void)\r
+{\r
+       int po_old = PicoOpt;\r
+\r
+       PicoOpt |= 0x10;\r
+       PicoFrameFull();\r
+       PicoOpt = po_old;\r
+\r
+       if (!(Pico.video.reg[12]&1)) {\r
+               vidCpyM2 = vidCpyM2_40col;\r
+               clearArea(1);\r
+       } else  vidCpyM2 = vidCpyM2_32col;\r
+\r
+       vidCpyM2((unsigned char *)gp2x_screen+320*8, framebuff+328*8);\r
+       vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
+       gp2x_video_setpalette(localPal, 0x40);\r
+}\r
+\r
 static void simpleWait(int thissec, int lim_time)\r
 {\r
        struct timeval tval;\r
@@ -1239,18 +1257,11 @@ if (Pico.m.frame_count == 31563) {
        }\r
 \r
        // if in 16bit mode, generate 8it image for menu background\r
-       if (!(PicoOpt&0x10) && (currentConfig.EmuOpt&0x80)) {\r
-               PicoOpt |= 0x10;\r
-               if (!(Pico.video.reg[12]&1)) clearArea(1);\r
-               PicoFrameFull();\r
-               vidCpyM2((unsigned char *)gp2x_screen+320*8, framebuff+328*8);\r
-               vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
-               gp2x_video_setpalette(localPal, 0x40);\r
-               PicoOpt &= ~0x10;\r
-       }\r
+       if (!(PicoOpt&0x10) && (currentConfig.EmuOpt&0x80))\r
+               emu_forced_frame();\r
 \r
        // for menu bg\r
-       gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);\r
+       gp2x_memcpy_buffers((1<<2), gp2x_screen, 0, 320*240*2);\r
 }\r
 \r
 \r
@@ -1272,18 +1283,95 @@ size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)
        return gzwrite(file, p, _n);\r
 }\r
 \r
+static int try_ropen_file(const char *fname)\r
+{\r
+       FILE *f;\r
+\r
+       f = fopen(fname, "rb");\r
+       if (f) {\r
+               fclose(f);\r
+               return 1;\r
+       }\r
+       return 0;\r
+}\r
+\r
+char *emu_GetSaveFName(int load, int is_sram, int slot)\r
+{\r
+       static char saveFname[512];\r
+       char ext[16];\r
+\r
+       if (is_sram)\r
+       {\r
+               romfname_ext(saveFname, (PicoMCD&1) ? "brm/" : "srm/", (PicoMCD&1) ? ".brm" : ".srm");\r
+               if (load) {\r
+                       if (try_ropen_file(saveFname)) return saveFname;\r
+                       // try in current dir..\r
+                       romfname_ext(saveFname, NULL, (PicoMCD&1) ? ".brm" : ".srm");\r
+                       if (try_ropen_file(saveFname)) return saveFname;\r
+                       return NULL; // give up\r
+               }\r
+       }\r
+       else\r
+       {\r
+               ext[0] = 0;\r
+               if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);\r
+               strcat(ext, (currentConfig.EmuOpt & 8) ? ".mds.gz" : ".mds");\r
+\r
+               romfname_ext(saveFname, "mds/", ext);\r
+               if (load) {\r
+                       if (try_ropen_file(saveFname)) return saveFname;\r
+                       romfname_ext(saveFname, NULL, ext);\r
+                       if (try_ropen_file(saveFname)) return saveFname;\r
+                       if (currentConfig.EmuOpt & 8) {\r
+                               ext[0] = 0;\r
+                               if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);\r
+                               strcat(ext, ".mds");\r
+\r
+                               romfname_ext(saveFname, "mds/", ext);\r
+                               if (try_ropen_file(saveFname)) return saveFname;\r
+                               romfname_ext(saveFname, NULL, ext);\r
+                               if (try_ropen_file(saveFname)) return saveFname;\r
+                       }\r
+                       return NULL;\r
+               }\r
+       }\r
+\r
+       return saveFname;\r
+}\r
+\r
+int emu_check_save_file(int slot)\r
+{\r
+       return emu_GetSaveFName(1, 0, slot) ? 1 : 0;\r
+}\r
+\r
+void emu_set_save_cbs(int gz)\r
+{\r
+       if (gz) {\r
+               areaRead  = gzRead2;\r
+               areaWrite = gzWrite2;\r
+               areaEof   = (areaeof *) gzeof;\r
+               areaSeek  = (areaseek *) gzseek;\r
+               areaClose = (areaclose *) gzclose;\r
+       } else {\r
+               areaRead  = (arearw *) fread;\r
+               areaWrite = (arearw *) fwrite;\r
+               areaEof   = (areaeof *) feof;\r
+               areaSeek  = (areaseek *) fseek;\r
+               areaClose = (areaclose *) fclose;\r
+       }\r
+}\r
 \r
 int emu_SaveLoadGame(int load, int sram)\r
 {\r
        int ret = 0;\r
-       char saveFname[512];\r
+       char *saveFname;\r
 \r
        // make save filename\r
-       romfname_ext(saveFname, "");\r
-       if(sram) strcat(saveFname, (PicoMCD&1) ? ".brm" : ".srm");\r
-       else {\r
-               if(state_slot > 0 && state_slot < 10) sprintf(saveFname, "%s.%i", saveFname, state_slot);\r
-               strcat(saveFname, ".mds");\r
+       saveFname = emu_GetSaveFName(load, sram, state_slot);\r
+       if (saveFname == NULL) {\r
+               strcpy(noticeMsg, load ? "LOAD FAILED (missing file)" : "SAVE FAILED  ");\r
+               gettimeofday(&noticeMsgTime, 0);\r
+               return -1;\r
        }\r
 \r
        printf("saveLoad (%i, %i): %s\n", load, sram, saveFname);\r
@@ -1326,31 +1414,21 @@ int emu_SaveLoadGame(int load, int sram)
        else\r
        {\r
                void *PmovFile = NULL;\r
-               // try gzip first\r
-               if(currentConfig.EmuOpt & 8) {\r
-                       strcat(saveFname, ".gz");\r
+               if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0) {\r
                        if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {\r
-                               areaRead  = gzRead2;\r
-                               areaWrite = gzWrite2;\r
-                               areaEof   = (areaeof *) gzeof;\r
-                               areaSeek  = (areaseek *) gzseek;\r
+                               emu_set_save_cbs(1);\r
                                if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);\r
-                       } else\r
-                               saveFname[strlen(saveFname)-3] = 0;\r
+                       }\r
                }\r
-               if(!PmovFile) { // gzip failed or was disabled\r
+               else\r
+               {\r
                        if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {\r
-                               areaRead  = (arearw *) fread;\r
-                               areaWrite = (arearw *) fwrite;\r
-                               areaEof   = (areaeof *) feof;\r
-                               areaSeek  = (areaseek *) fseek;\r
+                               emu_set_save_cbs(0);\r
                        }\r
                }\r
                if(PmovFile) {\r
                        ret = PmovState(load ? 6 : 5, PmovFile);\r
-                       if(areaRead == gzRead2)\r
-                                gzclose(PmovFile);\r
-                       else fclose ((FILE *) PmovFile);\r
+                       areaClose(PmovFile);\r
                        PmovFile = 0;\r
                        if (!load) sync();\r
                        else Pico.m.dirtyPal=1;\r
index 674929b..42d732b 100644 (file)
@@ -46,5 +46,9 @@ void emu_Loop(void);
 void emu_ResetGame(void);\r
 int  emu_ReadConfig(int game);\r
 int  emu_WriteConfig(int game);\r
+char *emu_GetSaveFName(int load, int is_sram, int slot);\r
+int  emu_check_save_file(int slot);\r
+void emu_set_save_cbs(int gz);\r
+void emu_forced_frame(void);\r
 int  find_bios(int region, char **bios_file);\r
 \r
index 552318c..28c0a93 100644 (file)
@@ -145,12 +145,18 @@ void gp2x_video_wait_vsync(void)
 }\r
 \r
 \r
+void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len)\r
+{\r
+       if (buffers & (1<<0)) memcpy((char *)gp2x_screens[0] + offset, data, len);\r
+       if (buffers & (1<<1)) memcpy((char *)gp2x_screens[1] + offset, data, len);\r
+       if (buffers & (1<<2)) memcpy((char *)gp2x_screens[2] + offset, data, len);\r
+       if (buffers & (1<<3)) memcpy((char *)gp2x_screens[3] + offset, data, len);\r
+}\r
+\r
+\r
 void gp2x_memcpy_all_buffers(void *data, int offset, int len)\r
 {\r
-       memcpy((char *)gp2x_screens[0] + offset, data, len);\r
-       memcpy((char *)gp2x_screens[1] + offset, data, len);\r
-       memcpy((char *)gp2x_screens[2] + offset, data, len);\r
-       memcpy((char *)gp2x_screens[3] + offset, data, len);\r
+       gp2x_memcpy_buffers(0xf, data, offset, len);\r
 }\r
 \r
 \r
index bddfc29..9883b22 100644 (file)
@@ -14,6 +14,7 @@ void gp2x_video_changemode2(int bpp);
 void gp2x_video_setpalette(int *pal, int len);\r
 void gp2x_video_RGB_setscaling(int W, int H);\r
 void gp2x_video_wait_vsync(void);\r
+void gp2x_memcpy_buffers(int buffers, void *data, int offset, int len);\r
 void gp2x_memcpy_all_buffers(void *data, int offset, int len);\r
 void gp2x_memset_all_buffers(int offset, int byte, int len);\r
 void gp2x_pd_clone_buffer2(void);\r
index 9ffbd40..9a2914a 100644 (file)
@@ -18,6 +18,7 @@
 #include "version.h"\r
 \r
 #include "Pico/PicoInt.h"\r
+#include "zlib/zlib.h"\r
 \r
 #ifndef _DIRENT_HAVE_D_TYPE\r
 #error "need d_type for file browser\r
@@ -390,6 +391,139 @@ static char *romsel_loop(char *curr_path)
        return ret;\r
 }\r
 \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
+{\r
+       int slot;\r
+\r
+       state_slot_flags = 0;\r
+\r
+       for (slot = 0; slot < 10; slot++)\r
+       {\r
+               if (emu_check_save_file(slot))\r
+               {\r
+                       state_slot_flags |= 1 << slot;\r
+               }\r
+       }\r
+}\r
+\r
+static void draw_savestate_bg(int slot)\r
+{\r
+       struct PicoVideo tmp_pv;\r
+       unsigned short tmp_cram[0x40];\r
+       unsigned short tmp_vsram[0x40];\r
+       void *tmp_vram, *file;\r
+       char *fname;\r
+\r
+       fname = emu_GetSaveFName(1, 0, slot);\r
+       if (!fname) return;\r
+\r
+       tmp_vram = malloc(sizeof(Pico.vram));\r
+       if (tmp_vram == NULL) return;\r
+\r
+       memcpy(tmp_vram, Pico.vram, sizeof(Pico.vram));\r
+       memcpy(tmp_cram, Pico.cram, sizeof(Pico.cram));\r
+       memcpy(tmp_vsram, Pico.vsram, sizeof(Pico.vsram));\r
+       memcpy(&tmp_pv, &Pico.video, sizeof(Pico.video));\r
+\r
+       if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {\r
+               file = gzopen(fname, "rb");\r
+               emu_set_save_cbs(1);\r
+       } else {\r
+               file = fopen(fname, "rb");\r
+               emu_set_save_cbs(0);\r
+       }\r
+\r
+       if (file) {\r
+               if (PicoMCD & 1) {\r
+                       PicoCdLoadStateGfx(file);\r
+               } else {\r
+                       areaSeek(file, 0x10020, SEEK_SET);  // skip header and RAM in state file\r
+                       areaRead(Pico.vram, 1, sizeof(Pico.vram), file);\r
+                       areaSeek(file, 0x2000, SEEK_CUR);\r
+                       areaRead(Pico.cram, 1, sizeof(Pico.cram), file);\r
+                       areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);\r
+                       areaSeek(file, 0x221a0, SEEK_SET);\r
+                       areaRead(&Pico.video, 1, sizeof(Pico.video), file);\r
+               }\r
+               areaClose(file);\r
+       }\r
+\r
+       emu_forced_frame();\r
+       gp2x_memcpy_buffers((1<<2), gp2x_screen, 0, 320*240*2);\r
+       menu_prepare_bg();\r
+\r
+       memcpy(Pico.vram, tmp_vram, sizeof(Pico.vram));\r
+       memcpy(Pico.cram, tmp_cram, sizeof(Pico.cram));\r
+       memcpy(Pico.vsram, tmp_vsram, sizeof(Pico.vsram));\r
+       memcpy(&Pico.video, &tmp_pv,  sizeof(Pico.video));\r
+       free(tmp_vram);\r
+}\r
+\r
+static void draw_savestate_menu(int menu_sel, int is_loading)\r
+{\r
+       int tl_x = 25, tl_y = 60, y, i;\r
+\r
+       if (state_slot_flags & (1 << menu_sel))\r
+               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
+\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
+       }\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
+\r
+       gp2x_video_flip2();\r
+}\r
+\r
+static int savestate_menu_loop(int is_loading)\r
+{\r
+       int menu_sel = 10, menu_sel_max = 10;\r
+       unsigned long inp = 0;\r
+\r
+       state_check_slots();\r
+\r
+       for(;;)\r
+       {\r
+               draw_savestate_menu(menu_sel, is_loading);\r
+               inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
+               if(inp & GP2X_UP  ) {\r
+                       do {\r
+                               menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;\r
+                       } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
+               }\r
+               if(inp & GP2X_DOWN) {\r
+                       do {\r
+                               menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;\r
+                       } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
+               }\r
+               if(inp & GP2X_B) { // save/load\r
+                       if (menu_sel < 10) {\r
+                               state_slot = menu_sel;\r
+                               if (emu_SaveLoadGame(is_loading, 0)) {\r
+                                       strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");\r
+                                       return 1;\r
+                               }\r
+                               return 0;\r
+                       } else  return 1;\r
+               }\r
+               if(inp & GP2X_X) return 1;\r
+       }\r
+}\r
+\r
 // -------------- key config --------------\r
 \r
 static char *usb_joy_key_name(int joy, int num)\r
@@ -1018,20 +1152,16 @@ static void menu_loop_root(void)
                                        break;\r
                                case 1: // save state\r
                                        if (rom_data) {\r
-                                               if(emu_SaveLoadGame(0, 0)) {\r
-                                                       strcpy(menuErrorMsg, "save failed");\r
+                                               if(savestate_menu_loop(0))\r
                                                        continue;\r
-                                               }\r
                                                engineState = PGS_Running;\r
                                                return;\r
                                        }\r
                                        break;\r
                                case 2: // load state\r
                                        if (rom_data) {\r
-                                               if(emu_SaveLoadGame(1, 0)) {\r
-                                                       strcpy(menuErrorMsg, "load failed");\r
+                                               if(savestate_menu_loop(1))\r
                                                        continue;\r
-                                               }\r
                                                engineState = PGS_Running;\r
                                                return;\r
                                        }\r
@@ -1074,21 +1204,29 @@ static void menu_loop_root(void)
 }\r
 \r
 \r
-static void menu_gfx_prepare(void)\r
+static void menu_prepare_bg(void)\r
 {\r
        extern int localPal[0x100];\r
-       int i;\r
+       int c, i;\r
 \r
        // don't clear old palette just for fun (but make it dark)\r
-       for (i = 0x100-1; i >= 0; i--)\r
-               localPal[i] = (localPal[i] >> 2) & 0x003f3f3f;\r
+       for (i = 0x100-1; i >= 0; i--) {\r
+               c = localPal[i];\r
+               localPal[i] = ((c >> 1) & 0x007f7f7f) - ((c >> 3) & 0x001f1f1f);\r
+       }\r
        localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
        localPal[0xf0] = 0x00ffffff;\r
 \r
+       gp2x_video_setpalette(localPal, 0x100);\r
+}\r
+\r
+static void menu_gfx_prepare(void)\r
+{\r
+       menu_prepare_bg();\r
+\r
        // switch to 8bpp\r
        gp2x_video_changemode2(8);\r
        gp2x_video_RGB_setscaling(320, 240);\r
-       gp2x_video_setpalette(localPal, 0x100);\r
        gp2x_video_flip2();\r
 }\r
 \r