Minor reworking of the Snapshot class, making the savePNG method static.

There was really no need to have this as an object, since it's all static
code anyway.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@1145 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2006-11-10 06:14:14 +00:00
parent 12d9de32cc
commit 90723af108
3 changed files with 19 additions and 44 deletions

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // this file, and for a DISCLAIMER OF ALL WARRANTIES.
// //
// $Id: Snapshot.cxx,v 1.11 2006-11-09 03:06:42 stephena Exp $ // $Id: Snapshot.cxx,v 1.12 2006-11-10 06:14:14 stephena Exp $
//============================================================================ //============================================================================
#ifdef SNAPSHOT_SUPPORT #ifdef SNAPSHOT_SUPPORT
@ -26,15 +26,7 @@
#include "Snapshot.hxx" #include "Snapshot.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Snapshot::Snapshot(FrameBuffer& framebuffer) void Snapshot::savePNG(FrameBuffer& framebuffer, const string& filename)
: myFrameBuffer(framebuffer)
{
// Make sure we have a 'clean' image, with no onscreen messages
myFrameBuffer.hideMessage();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string Snapshot::savePNG(string filename)
{ {
uInt8* buffer = (uInt8*) NULL; uInt8* buffer = (uInt8*) NULL;
uInt8* compmem = (uInt8*) NULL; uInt8* compmem = (uInt8*) NULL;
@ -42,10 +34,13 @@ string Snapshot::savePNG(string filename)
try try
{ {
// Make sure we have a 'clean' image, with no onscreen messages
framebuffer.hideMessage();
// Get actual image dimensions. which are not always the same // Get actual image dimensions. which are not always the same
// as the framebuffer dimensions // as the framebuffer dimensions
int width = myFrameBuffer.imageWidth(); int width = framebuffer.imageWidth();
int height = myFrameBuffer.imageHeight(); int height = framebuffer.imageHeight();
out.open(filename.c_str(), ios_base::binary); out.open(filename.c_str(), ios_base::binary);
if(!out) if(!out)
@ -79,7 +74,7 @@ string Snapshot::savePNG(string filename)
for(int row = 0; row < height; row++) for(int row = 0; row < height; row++)
{ {
*buf_ptr++ = 0; // first byte of row is filter type *buf_ptr++ = 0; // first byte of row is filter type
myFrameBuffer.scanline(row, buf_ptr); // get another scanline framebuffer.scanline(row, buf_ptr); // get another scanline
buf_ptr += rowbytes; // add pitch buf_ptr += rowbytes; // add pitch
} }
@ -101,21 +96,14 @@ string Snapshot::savePNG(string filename)
if(compmem) delete[] compmem; if(compmem) delete[] compmem;
out.close(); out.close();
return "Snapshot saved"; framebuffer.showMessage("Snapshot saved");
} }
catch(const char *msg) catch(const char *msg)
{ {
if(buffer) delete[] buffer; if(buffer) delete[] buffer;
if(compmem) delete[] compmem; if(compmem) delete[] compmem;
out.close(); out.close();
return msg; framebuffer.showMessage(msg);
}
catch(...)
{
if(buffer) delete[] buffer;
if(compmem) delete[] compmem;
out.close();
return "Couldn't create snapshot file";
} }
} }

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // this file, and for a DISCLAIMER OF ALL WARRANTIES.
// //
// $Id: Snapshot.hxx,v 1.6 2006-11-09 03:06:42 stephena Exp $ // $Id: Snapshot.hxx,v 1.7 2006-11-10 06:14:14 stephena Exp $
//============================================================================ //============================================================================
#ifndef SNAPSHOT_HXX #ifndef SNAPSHOT_HXX
@ -29,27 +29,16 @@ class FrameBuffer;
class Snapshot class Snapshot
{ {
public: public:
/**
Create a new shapshot class for taking snapshots in PNG format.
@param framebuffer The SDL framebuffer containing the image data
*/
Snapshot(FrameBuffer& framebuffer);
/** /**
Save the current frame buffer to a PNG file. Save the current frame buffer to a PNG file.
@param framebuffer The framebuffer containing the image data
@param filename The filename of the PNG file @param filename The filename of the PNG file
@return The resulting string to print to the framebuffer
*/ */
string savePNG(string filename); static void savePNG(FrameBuffer& framebuffer, const string& filename);
private: private:
static void writePNGChunk(ofstream& out, char* type, uInt8* data, int size); static void writePNGChunk(ofstream& out, char* type, uInt8* data, int size);
private:
// The Framebuffer for the system
FrameBuffer& myFrameBuffer;
}; };
#endif // SNAPSHOT_SUPPORT #endif // SNAPSHOT_SUPPORT

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // this file, and for a DISCLAIMER OF ALL WARRANTIES.
// //
// $Id: EventHandler.cxx,v 1.168 2006-11-06 00:52:03 stephena Exp $ // $Id: EventHandler.cxx,v 1.169 2006-11-10 06:14:14 stephena Exp $
//============================================================================ //============================================================================
#include <sstream> #include <sstream>
@ -2159,10 +2159,8 @@ void EventHandler::takeSnapshot()
else else
filename = sspath + ".png"; filename = sspath + ".png";
// Now create a Snapshot object and save the PNG // Now create a PNG snapshot
Snapshot snapshot(myOSystem->frameBuffer()); Snapshot::savePNG(myOSystem->frameBuffer(), filename);
string result = snapshot.savePNG(filename);
myOSystem->frameBuffer().showMessage(result);
#else #else
myOSystem->frameBuffer().showMessage("Snapshots unsupported"); myOSystem->frameBuffer().showMessage("Snapshots unsupported");
#endif #endif