Preliminary support for playing back recorded eventstream files. It's

currently buggy, in that it doesn't seem to play back what was actually
recorded :)  But it seems that each time I play back the state file, the
same things happen, so at least I know it's deterministic that way.  I just
need to figure out which is being done incorrectly, the save or the load.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@932 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2005-12-29 01:25:07 +00:00
parent 1f426d245f
commit 989c58ebfd
3 changed files with 80 additions and 21 deletions

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: EventHandler.cxx,v 1.135 2005-12-28 22:56:36 stephena Exp $
// $Id: EventHandler.cxx,v 1.136 2005-12-29 01:25:07 stephena Exp $
//============================================================================
#include <sstream>
@ -334,9 +334,14 @@ void EventHandler::mapStelladaptors(const string& sa1, const string& sa2)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EventHandler::poll(uInt32 time)
{
SDL_Event event;
// Check if we have an event from the eventstreamer
// TODO - should we lock out input from the user while getting synthetic events?
int type, value;
if(myEventStreamer->pollEvent(type, value))
myEvent->set((Event::Type)type, value);
// Check for an event
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
@ -455,10 +460,13 @@ void EventHandler::poll(uInt32 time)
else
myOSystem->frameBuffer().showMessage("Error opening eventstream");
}
return;
break;
case SDLK_l: // Alt-l loads a recording
myEventStreamer->loadRecording();
if(myEventStreamer->loadRecording())
myOSystem->frameBuffer().showMessage("Playing recording");
return;
break;
////////////////////////////////////////////////////////////////////////
}

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: EventStreamer.cxx,v 1.1 2005-12-28 22:56:36 stephena Exp $
// $Id: EventStreamer.cxx,v 1.2 2005-12-29 01:25:07 stephena Exp $
//============================================================================
#include "bspf.hxx"
@ -27,7 +27,10 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
EventStreamer::EventStreamer(OSystem* osystem)
: myOSystem(osystem),
myEventRecordFlag(false)
myEventWriteFlag(false),
myEventReadFlag(false),
myFrameCounter(-1),
myEventPos(0)
{
}
@ -53,13 +56,16 @@ bool EventStreamer::startRecording()
myOSystem->console().system().saveState(md5, myStreamWriter);
myEventHistory.clear();
return myEventRecordFlag = true;
myEventWriteFlag = true;
myEventReadFlag = false;
return myEventWriteFlag;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool EventStreamer::stopRecording()
{
if(!myStreamWriter.isOpen() || !myEventRecordFlag)
if(!myStreamWriter.isOpen() || !myEventWriteFlag)
return false;
// Append the event history to the eventstream
@ -76,8 +82,6 @@ bool EventStreamer::stopRecording()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool EventStreamer::loadRecording()
{
cerr << "EventStreamer::loadRecording()\n";
string eventfile = /*myOSystem->baseDir() + BSPF_PATH_SEPARATOR +*/ "test.inp";
if(!myStreamReader.open(eventfile))
return false;
@ -95,25 +99,60 @@ cerr << "EventStreamer::loadRecording()\n";
for(int i = 0; i < size; ++i)
myEventHistory.push_back(myStreamReader.getInt());
cerr << "event queue contains " << myEventHistory.size() << " items\n";
myEventWriteFlag = false;
myEventReadFlag = myEventHistory.size() > 0;
myFrameCounter = -1;
myEventPos = 0;
return myEventRecordFlag = false;
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EventStreamer::addEvent(int type, int value)
{
if(myEventRecordFlag)
if(myEventWriteFlag)
{
myEventHistory.push_back(type);
myEventHistory.push_back(value);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool EventStreamer::pollEvent(int& type, int& value)
{
if(!myEventReadFlag)
return false;
bool status = false;
// Read a new event from the stream when we've waited the appropriate
// number of frames
++myFrameCounter;
if(myFrameCounter >= 0)
{
int first = myEventHistory[myEventPos++];
if(first < 0)
{
myFrameCounter = first;
cerr << "wait " << -myFrameCounter << " frames\n";
}
else if(myEventPos < (int)myEventHistory.size())
{
type = first;
value = myEventHistory[myEventPos++];
cerr << "type = " << type << ", value = " << value << endl;
status = true;
}
}
myEventReadFlag = myEventPos < (int)myEventHistory.size() - 2;
return status;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EventStreamer::nextFrame()
{
if(myEventRecordFlag)
if(myEventWriteFlag)
{
int idx = myEventHistory.size() - 1;
if(idx >= 0 && myEventHistory[idx] < 0)

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: EventStreamer.hxx,v 1.1 2005-12-28 22:56:36 stephena Exp $
// $Id: EventStreamer.hxx,v 1.2 2005-12-29 01:25:07 stephena Exp $
//============================================================================
#ifndef EVENTSTREAMER_HXX
@ -48,7 +48,7 @@ class OSystem;
the correct order at the correct time.
@author Stephen Anthony
@version $Id: EventStreamer.hxx,v 1.1 2005-12-28 22:56:36 stephena Exp $
@version $Id: EventStreamer.hxx,v 1.2 2005-12-29 01:25:07 stephena Exp $
*/
class EventStreamer
{
@ -84,10 +84,15 @@ class EventStreamer
*/
void addEvent(int type, int value);
/**
Gets the next event from the event history
*/
bool pollEvent(int& type, int& value);
/**
Answers if we're in recording mode
*/
bool isRecording() { return myEventRecordFlag; }
bool isRecording() { return myEventWriteFlag; }
/**
Indicate that a new frame has been processed
@ -100,15 +105,22 @@ class EventStreamer
// Global OSystem object
OSystem* myOSystem;
// Indicates if we're in recording mode
bool myEventRecordFlag;
// Indicates if we're in save/write or load/read mode
bool myEventWriteFlag;
bool myEventReadFlag;
// Current frame count (used for waiting while polling)
int myFrameCounter;
// Current position in the event history queue
int myEventPos;
// Stores the history/record of all events that have been set
IntArray myEventHistory;
// Serializer classes used to save/load the eventstream
Serializer myStreamWriter;
Deserializer myStreamReader;
// Stores the history/record of all events that have been set
IntArray myEventHistory;
};
#endif