snes9xgx/source/ngc/freeze.cpp

273 lines
5.8 KiB
C++
Raw Normal View History

/****************************************************************************
* Snes9x 1.51 Nintendo Wii/Gamecube Port
2008-08-14 00:44:59 +02:00
*
* softdev July 2006
* crunchy2 May 2007-July 2007
2008-11-12 08:50:39 +01:00
* Michniewski 2008
2009-03-11 18:28:37 +01:00
* Tantric 2008-2009
2008-08-14 00:44:59 +02:00
*
* freeze.cpp
2009-11-30 09:14:38 +01:00
*
* Snapshots Memory File System
*
* This is a single global memory file controller.
* Don't even think of opening two at the same time!
***************************************************************************/
2009-02-07 04:01:10 +01:00
#include <malloc.h>
#include <gccore.h>
#include <stdio.h>
2009-11-30 09:14:38 +01:00
#include <stdlib.h>
#include <string.h>
#include <fat.h>
#include <zlib.h>
2009-03-11 18:28:37 +01:00
#include "snes9x.h"
#include "memmap.h"
2009-11-30 09:14:38 +01:00
#include "soundux.h"
#include "snapshot.h"
2009-11-30 09:14:38 +01:00
#include "srtc.h"
2008-10-03 07:26:01 +02:00
#include "snes9xGX.h"
2009-11-30 09:14:38 +01:00
#include "freeze.h"
2008-12-18 19:36:30 +01:00
#include "fileop.h"
2009-03-11 18:28:37 +01:00
#include "filebrowser.h"
#include "menu.h"
#include "video.h"
2009-11-25 07:35:14 +01:00
#include "pngu.h"
2009-11-30 09:14:38 +01:00
extern void S9xSRTCPreSaveState ();
extern void NGCFreezeStruct ();
extern bool8 S9xUnfreezeGame (const char *filename);
static int bufoffset;
/****************************************************************************
* GetMem
*
* Return x bytes from memory buffer
***************************************************************************/
int
GetMem (char *buffer, int len)
{
2009-11-30 09:14:38 +01:00
memcpy (buffer, savebuffer + bufoffset, len);
bufoffset += len;
return len;
}
2009-11-30 09:14:38 +01:00
/****************************************************************************
* PutMem
*
* Put some values in memory buffer
***************************************************************************/
static void
PutMem (char *buffer, int len)
{
2009-11-30 09:14:38 +01:00
memcpy (savebuffer + bufoffset, buffer, len);
bufoffset += len;
}
2009-11-30 09:14:38 +01:00
void
NGCFreezeBlock (char *name, uint8 * block, int size)
{
char buffer[512];
sprintf (buffer, "%s:%06d:", name, size);
PutMem (buffer, strlen (buffer));
PutMem ((char *) block, size);
}
/****************************************************************************
2009-11-30 09:14:38 +01:00
* NGCFreezeMembuffer
*
* Copies a snapshot of Snes9x state into memory
***************************************************************************/
2009-11-30 09:14:38 +01:00
static int
NGCFreezeMemBuffer ()
{
int i;
char buffer[1024];
2009-11-30 09:14:38 +01:00
bufoffset = 0;
S9xUpdateRTC ();
S9xSRTCPreSaveState ();
for (i = 0; i < 8; i++)
{
SoundData.channels[i].previous16[0] =
(int16) SoundData.channels[i].previous[0];
SoundData.channels[i].previous16[1] =
(int16) SoundData.channels[i].previous[1];
}
sprintf (buffer, "%s:%04d\n", SNAPSHOT_MAGIC, SNAPSHOT_VERSION);
PutMem (buffer, strlen (buffer));
sprintf (buffer, "NAM:%06d:%s%c", (int) strlen (Memory.ROMFilename) + 1,
Memory.ROMFilename, 0);
PutMem (buffer, strlen (buffer) + 1);
NGCFreezeStruct ();
return 0;
}
/****************************************************************************
* SaveSnapshot
***************************************************************************/
int
2009-11-25 07:35:14 +01:00
SaveSnapshot (char * filepath, bool silent)
{
2009-11-30 09:14:38 +01:00
int offset = 0; // bytes written (actual)
int woffset = 0; // bytes written (expected)
2009-03-12 08:07:52 +01:00
int imgSize = 0; // image screenshot bytes written
2009-10-02 00:35:12 +02:00
int device;
if(!FindDevice(filepath, &device))
2008-11-12 08:50:39 +01:00
return 0;
// save screenshot - I would prefer to do this from gameScreenTex
2009-11-29 09:12:40 +01:00
if(gameScreenTex2 != NULL)
{
AllocSaveBuffer ();
IMGCTX pngContext = PNGU_SelectImageFromBuffer(savebuffer);
if (pngContext != NULL)
{
imgSize = PNGU_EncodeFromGXTexture(pngContext, screenwidth, screenheight, gameScreenTex2, 0);
PNGU_ReleaseImageContext(pngContext);
}
if(imgSize > 0)
{
char screenpath[1024];
strncpy(screenpath, filepath, 1024);
screenpath[strlen(screenpath)-4] = 0;
sprintf(screenpath, "%s.png", screenpath);
2009-10-02 00:35:12 +02:00
SaveFile(screenpath, imgSize, silent);
}
FreeSaveBuffer ();
}
2009-11-30 09:14:38 +01:00
S9xSetSoundMute (TRUE);
S9xPrepareSoundForSnapshotSave (FALSE);
AllocSaveBuffer ();
// copy freeze mem into savebuffer - bufoffset contains # bytes written
NGCFreezeMemBuffer ();
woffset = bufoffset;
S9xPrepareSoundForSnapshotSave (TRUE);
S9xSetSoundMute (FALSE);
offset = SaveFile(filepath, woffset, silent);
FreeSaveBuffer ();
if(offset > 0) // save successful!
{
2009-11-25 07:35:14 +01:00
if(!silent)
2009-11-30 09:14:38 +01:00
InfoPrompt("Save successful");
return 1;
}
2009-11-30 09:14:38 +01:00
return 0;
}
2009-03-11 18:28:37 +01:00
int
2009-11-25 07:35:14 +01:00
SaveSnapshotAuto (bool silent)
2009-03-11 18:28:37 +01:00
{
char filepath[1024];
2009-10-02 00:35:12 +02:00
if(!MakeFilePath(filepath, FILE_SNAPSHOT, Memory.ROMFilename, 0))
2009-03-20 09:26:10 +01:00
return false;
2009-11-25 07:35:14 +01:00
return SaveSnapshot(filepath, silent);
2009-03-11 18:28:37 +01:00
}
2009-11-30 09:14:38 +01:00
/****************************************************************************
* NGCUnFreezeBlock
***************************************************************************/
int
NGCUnFreezeBlock (char *name, uint8 * block, int size)
{
char buffer[20], *e;
int len = 0;
int rem = 0;
GetMem (buffer, 11);
if (strncmp (buffer, name, 3) != 0 || buffer[3] != ':' ||
buffer[10] != ':' || (len = strtol (&buffer[4], &e, 10)) == 0 ||
e != buffer + 10)
{
bufoffset -= 11; // go back to where we started
return WRONG_FORMAT;
}
if (len > size)
{
rem = len - size;
len = size;
}
ZeroMemory (block, size);
GetMem ((char *) block, len);
if (rem)
{
bufoffset += rem;
}
return SUCCESS;
}
/****************************************************************************
2009-11-25 07:35:14 +01:00
* LoadSnapshot
***************************************************************************/
int
2009-11-25 07:35:14 +01:00
LoadSnapshot (char * filepath, bool silent)
{
2009-11-30 09:14:38 +01:00
int offset = 0;
int result = 0;
bufoffset = 0;
2009-10-02 00:35:12 +02:00
int device;
if(!FindDevice(filepath, &device))
2009-03-11 18:28:37 +01:00
return 0;
2009-11-30 09:14:38 +01:00
AllocSaveBuffer();
2009-11-30 09:14:38 +01:00
offset = LoadFile(filepath, silent);
2009-11-30 09:14:38 +01:00
if(offset > 0)
2008-11-12 08:50:39 +01:00
{
2009-11-30 09:14:38 +01:00
if (S9xUnfreezeGame ("AGAME") == SUCCESS)
result = 1;
else
ErrorPrompt("Error thawing");
2009-11-25 07:35:14 +01:00
}
2009-11-30 09:14:38 +01:00
else
2008-08-12 05:25:16 +02:00
{
2009-11-30 09:14:38 +01:00
if(!silent)
ErrorPrompt("Freeze file not found");
2008-08-12 05:25:16 +02:00
}
2009-11-30 09:14:38 +01:00
FreeSaveBuffer();
return result;
}
2009-03-11 18:28:37 +01:00
int
2009-11-25 07:35:14 +01:00
LoadSnapshotAuto (bool silent)
2009-03-11 18:28:37 +01:00
{
char filepath[1024];
2009-10-02 00:35:12 +02:00
if(!MakeFilePath(filepath, FILE_SNAPSHOT, Memory.ROMFilename, 0))
2009-03-16 07:58:50 +01:00
return false;
2009-03-11 18:28:37 +01:00
2009-11-25 07:35:14 +01:00
return LoadSnapshot(filepath, silent);
2009-03-11 18:28:37 +01:00
}