fceugx/source/fceustate.cpp

133 lines
2.6 KiB
C++
Raw Normal View History

2008-09-02 03:57:21 +02:00
/****************************************************************************
2009-07-22 04:05:49 +02:00
* FCE Ultra
2008-09-02 03:57:21 +02:00
* Nintendo Wii/Gamecube Port
*
2009-03-28 18:23:08 +01:00
* Tantric 2008-2009
2008-09-02 03:57:21 +02:00
*
2009-03-28 18:23:08 +01:00
* fceustate.cpp
2008-09-02 03:57:21 +02:00
*
* Memory Based Load/Save State Manager
*
* These are simply the state routines, brought together as GCxxxxx
* The original file I/O is replaced with Memory Read/Writes to the
* statebuffer below
****************************************************************************/
#include <gccore.h>
#include <string.h>
#include <malloc.h>
#include <fat.h>
2009-07-18 08:19:04 +02:00
#include <zlib.h>
2009-04-06 09:27:40 +02:00
#include "fceugx.h"
2009-07-17 19:27:04 +02:00
#include "fceusupport.h"
2009-03-28 18:23:08 +01:00
#include "menu.h"
#include "filebrowser.h"
2008-09-02 03:57:21 +02:00
#include "fileop.h"
2009-04-06 09:27:40 +02:00
#include "gcvideo.h"
2010-03-22 00:49:24 +01:00
#include "utils/pngu.h"
2008-09-02 03:57:21 +02:00
2009-10-02 00:21:25 +02:00
bool SaveState (char * filepath, bool silent)
2008-09-02 03:57:21 +02:00
{
bool retval = false;
int datasize;
int offset = 0;
2009-04-06 09:27:40 +02:00
int imgSize = 0; // image screenshot bytes written
2009-10-02 00:21:25 +02:00
int device;
if(!FindDevice(filepath, &device))
return 0;
2008-11-12 09:40:09 +01:00
2009-04-06 09:27:40 +02:00
// save screenshot - I would prefer to do this from gameScreenTex
2009-11-29 08:55:21 +01:00
if(gameScreenTex2 != NULL)
2009-04-06 09:27:40 +02:00
{
AllocSaveBuffer ();
IMGCTX pngContext = PNGU_SelectImageFromBuffer(savebuffer);
if (pngContext != NULL)
{
2010-04-14 01:33:52 +02:00
imgSize = PNGU_EncodeFromGXTexture(pngContext, vmode->fbWidth, vmode->efbHeight, gameScreenTex2, 0);
2009-04-06 09:27:40 +02:00
PNGU_ReleaseImageContext(pngContext);
}
2008-09-02 03:57:21 +02:00
2009-04-06 09:27:40 +02:00
if(imgSize > 0)
2008-09-02 03:57:21 +02:00
{
2009-04-06 09:27:40 +02:00
char screenpath[1024];
strncpy(screenpath, filepath, 1024);
screenpath[strlen(screenpath)-4] = 0;
sprintf(screenpath, "%s.png", screenpath);
2009-10-02 00:21:25 +02:00
SaveFile(screenpath, imgSize, silent);
2008-09-02 03:57:21 +02:00
}
2009-04-06 09:27:40 +02:00
FreeSaveBuffer ();
}
memorystream save(SAVEBUFFERSIZE);
FCEUSS_SaveMS(&save, Z_BEST_COMPRESSION);
2009-07-18 08:19:04 +02:00
save.sync();
datasize = save.tellp();
if (datasize)
2009-10-02 00:21:25 +02:00
offset = SaveFile(save.buf(), filepath, datasize, silent);
2009-04-06 09:27:40 +02:00
if (offset > 0)
{
if (!silent)
InfoPrompt("Save successful");
retval = true;
2008-09-02 03:57:21 +02:00
}
return retval;
}
2009-03-28 18:23:08 +01:00
bool
2009-10-02 00:21:25 +02:00
SaveStateAuto (bool silent)
2008-09-02 03:57:21 +02:00
{
2008-11-12 09:40:09 +01:00
char filepath[1024];
2009-03-28 18:23:08 +01:00
2009-10-02 00:21:25 +02:00
if(!MakeFilePath(filepath, FILE_STATE, romFilename, 0))
2009-03-28 18:23:08 +01:00
return false;
2009-10-02 00:21:25 +02:00
return SaveState(filepath, silent);
2009-03-28 18:23:08 +01:00
}
2009-10-02 00:21:25 +02:00
bool LoadState (char * filepath, bool silent)
2009-03-28 18:23:08 +01:00
{
2008-11-12 09:40:09 +01:00
int offset = 0;
bool retval = false;
2009-10-02 00:21:25 +02:00
int device;
2008-09-02 03:57:21 +02:00
2009-10-02 00:21:25 +02:00
if(!FindDevice(filepath, &device))
return 0;
2008-09-09 20:03:41 +02:00
AllocSaveBuffer ();
2008-09-02 03:57:21 +02:00
2009-10-02 00:21:25 +02:00
offset = LoadFile(filepath, silent);
2008-09-02 03:57:21 +02:00
if (offset > 0)
{
2009-07-18 08:19:04 +02:00
memorystream save((char *)savebuffer, offset);
FCEUSS_LoadFP(&save, SSLOADPARAM_NOBACKUP);
retval = true;
2008-09-02 03:57:21 +02:00
}
else
{
// if we reached here, nothing was done!
if(!silent)
2009-03-28 18:23:08 +01:00
ErrorPrompt ("State file not found");
}
FreeSaveBuffer ();
return retval;
2008-09-02 03:57:21 +02:00
}
2009-03-28 18:23:08 +01:00
bool
2009-10-02 00:21:25 +02:00
LoadStateAuto (bool silent)
2009-03-28 18:23:08 +01:00
{
char filepath[1024];
2009-10-02 00:21:25 +02:00
if(!MakeFilePath(filepath, FILE_STATE, romFilename, 0))
2009-03-28 18:23:08 +01:00
return false;
2009-10-02 00:21:25 +02:00
return LoadState(filepath, silent);
2009-03-28 18:23:08 +01:00
}