preparing emu for gui snapshot support
authorJavier S. Pedro <maemo@javispedro.com>
Mon, 17 Aug 2009 17:38:57 +0000 (19:38 +0200)
committerJavier S. Pedro <maemo@javispedro.com>
Mon, 17 Aug 2009 17:38:57 +0000 (19:38 +0200)
Bumping to 0.9.4
Handle more hgw commands
Implement statef functions (using gzip)
Simple snapshot support in cmdline (rom_basename.frz.gz)

Makefile
platform/config.cpp
platform/hgw.cpp
platform/platform.h
platform/sdl.cpp
platform/statef.c
snapshot.cpp
snapshot.h

index d7f7a0f..85c5c24 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ CFLAGS ?= -march=armv6j -mtune=arm1136jf-s -mfpu=vfp -mfloat-abi=softfp -Os -g -
 ASFLAGS ?= -march=armv6j -mfpu=vfp -mfloat-abi=softfp
 CXXFLAGS ?= $(CFLAGS)
 
-GAME_VERSION ?= 0.9.3
+GAME_VERSION ?= 0.9.4
 export GAME_VERSION
 export DESTDIR
 
index 393eb98..de0c8ff 100644 (file)
@@ -42,6 +42,8 @@ static const struct poptOption optionsTable[] = {
        "Enable mouse on controller NUM", "NUM"},
        { "superscope", 'e', POPT_ARG_NONE, 0, 12,
        "Enable SuperScope", 0},
+       { "snapshot", 'o', POPT_ARG_NONE, 0, 13,
+       "Unfreeze previous game on start and freeze game on exit", 0 },
        { "scancode", '\0', POPT_ARG_INT, 0, 100,
        "Scancode to map", "CODE" },
        { "button", '\0', POPT_ARG_STRING, 0, 101,
@@ -299,6 +301,10 @@ static void parseArgs(poptContext optCon)
                                Settings.SuperScope = TRUE;
                                Settings.ControllerOption = SNES_SUPERSCOPE;
                                break;
+                       case 13:
+                               Config.snapshotLoad = true;
+                               Config.snapshotSave = true;
+                               break;
                        case 100:
                                scancode = atoi(poptGetOptArg(optCon));
                                break;
index 1aad71b..a549780 100644 (file)
@@ -36,16 +36,36 @@ void HgwInit()
        
        hgwLaunched = true;
        HgwStartCommand cmd = hgw_context_get_start_command(hgw);
-       // TODO Handle cmd in some way other than assuming HGW_COMM_RESTART
-       
+
+       switch (cmd) {
+               default:
+               case HGW_COMM_NONE:     // called from libosso
+               case HGW_COMM_CONT:
+                       Config.snapshotLoad = true;
+                       Config.snapshotSave = true;
+                       break;
+               case HGW_COMM_RESTART:
+                       Config.snapshotLoad = false;
+                       Config.snapshotSave = true;
+                       break;
+               case HGW_COMM_QUIT:
+                       // hum, what?
+                       Config.snapshotLoad = false;
+                       Config.snapshotSave = false;
+                       Config.quitting = true;
+                       break;
+       }
+
        printf("Loading in HGW mode\n");
 }
 
 void HgwDeinit()
 {
        if (!hgwLaunched) return;
-       
-       hgw_context_destroy(hgw, HGW_BYE_INACTIVE);  // TODO
+
+       hgw_context_destroy(hgw,
+               (Config.snapshotSave ? HGW_BYE_PAUSED : HGW_BYE_INACTIVE));
+
        hgw = 0;
 }
 
@@ -82,6 +102,16 @@ void HgwPollEvents()
                                                break;
                                }
                                break;
+                       case HGW_MSG_TYPE_DEVSTATE:
+                               switch (msg.e_val) {
+                                       case HGW_DEVICE_STATE_SHUTDOWN:
+                                               Config.quitting = true; // try to quit gracefully
+                                               break;
+                               }
+                               break;
+                       default:
+                               // do nothing
+                               break;
                }
                
                hgw_msg_free_data(&msg);
index 8584d7c..1837dd6 100644 (file)
@@ -7,10 +7,17 @@
 void S9xLoadConfig(int argc, const char ** argv);
 void S9xSetRomFile(const char * file);
 extern struct config {
-       char romFile[PATH_MAX];
-       char hacksFile[PATH_MAX];
+       char romFile[PATH_MAX + 1];
+       char hacksFile[PATH_MAX + 1];
+       /** Unfreeze from .frz.gz snapshot on start */
+       bool snapshotLoad;
+       /** Freeze to .frz.gz on exit */
+       bool snapshotSave;
+       /** Create fullscreen surface */
        bool fullscreen;
+       /** Using xsp (thus take care of doubling coordinates where appropiate) */
        bool xsp;
+       /** Audio output enabled */
        bool enableAudio;
        unsigned short joypad1Mapping[256];
        unsigned char action[256];
index 73a22bc..9790da8 100644 (file)
@@ -11,6 +11,7 @@
 #include "memmap.h"
 #include "soundux.h"
 #include "hacks.h"
+#include "snapshot.h"
 #include "hgw.h"
 
 #define kPollEveryNFrames              5               //Poll input only every this many frames
@@ -70,6 +71,51 @@ static void loadRom()
        Memory.LoadSRAM(file); 
 }
 
+static void resumeGame()
+{
+       if (!Config.snapshotLoad) return;
+
+       const char * file = S9xGetFilename(".frz.gz");
+       int result = S9xUnfreezeGame(file);
+
+       printf("Unfreeze: %s", file);
+
+       if (!result) {
+               printf(" failed");
+               FILE* fp = fopen(file, "rb");
+               if (fp) {
+                       if (Config.snapshotSave) {
+                               puts(", but the file exists, so I'm not going to overwrite it");
+                               Config.snapshotSave = false;
+                       } else {
+                               puts(" (bad file?)");
+                       }
+                       fclose(fp);
+               } else {
+                       puts(" (file does not exist)");
+               }
+       } else {
+               puts(" ok");
+       }
+}
+
+static void pauseGame()
+{
+       if (!Config.snapshotSave) return;
+
+       const char * file = S9xGetFilename(".frz.gz");
+       int result = S9xFreezeGame(file);
+
+       printf("Freeze: %s", file);
+
+       if (!result) {
+               Config.snapshotSave = false; // Serves as a flag to Hgw
+               puts(" failed");
+       } else {
+               puts(" ok");
+       }
+}
+
 /* This comes nearly straight from snes9x */
 static void frameSync() {
        static struct timeval next1 = {0, 0};
@@ -202,8 +248,9 @@ int main(int argc, const char ** argv) {
        S9xInit();
        S9xReset();
        
-       // Load rom and related files: state
+       // Load rom and related files: state, unfreeze if needed
        loadRom();
+       resumeGame();
        
        // Late initialization
        sprintf(String, "DrNokSnes - %s", Memory.ROMName);
@@ -227,6 +274,7 @@ int main(int argc, const char ** argv) {
 
        // Save state
        Memory.SaveSRAM(S9xGetFilename(".srm"));
+       pauseGame();
 
        // Late deinitialization
        S9xGraphicsDeinit();
index 268802b..7994755 100644 (file)
@@ -1,6 +1,37 @@
-int  (*statef_open)(const char *fname, const char *mode);
-int  (*statef_read)(void *p, int l);
-int  (*statef_write)(void *p, int l);
-void (*statef_close)();
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include <zlib.h>
 
+#include "port.h"
+
+static gzFile gfd;
+
+static int zlib_open(const char *fname, const char *mode)
+{
+       gfd = gzopen(fname, mode);
+       if (!gfd) return 0;
+
+       return 1;
+}
+
+static int zlib_read(void *p, int l)
+{
+       return gzread(gfd, p, l);
+}
+
+static int zlib_write(void *p, int l)
+{
+       return gzwrite(gfd, p, l);
+}
+
+static void zlib_close()
+{
+       gzclose(gfd);
+}
+
+int  (*statef_open)(const char *fname, const char *mode) = zlib_open;
+int  (*statef_read)(void *p, int l) = zlib_read;
+int  (*statef_write)(void *p, int l) = zlib_write;
+void (*statef_close)() = zlib_close;
 
index fbcd6b9..46ab45d 100644 (file)
@@ -417,11 +417,6 @@ static int UnfreezeStruct (const char *name, void *base, FreezeData *fields,
                    int num_fields);
 static int UnfreezeBlock (const char *name, uint8 *block, int size);
 
-bool8 Snapshot (const char *filename)
-{
-    return (S9xFreezeGame (filename));
-}
-
 bool8 S9xFreezeGame (const char *filename)
 {
     if(statef_open(filename, "wb"))
index 43f7949..022e689 100644 (file)
@@ -55,8 +55,6 @@
 START_EXTERN_C
 bool8 S9xFreezeGame (const char *filename);
 bool8 S9xUnfreezeGame (const char *filename);
-bool8 Snapshot (const char *filename);
-bool8 S9xLoadSnapshot (const char *filename);
 bool8 S9xSPCDump (const char *filename);
 END_EXTERN_C