Added error checking for cheat names and codes. The '"' and ':' characters

are stripped, since they have special meaning in the cheat file.

Added support for one-shot cheats.  Basically these work exactly the same
way as normal cheats, but they are only evaluated once, and are never
saved.

Cleaned up the IFDEF mess for FrameBuffer and Sound objects in OSystem class
by creating a MediaFactory class that deals with it.  This might seem like
just moving the problem elsewhere, but it does help remove the clutter in
OSystem, and make compiling slightly faster whenever the FrameBuffer classes
are changed.  No porter should ever have to touch the emulation core classes.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@915 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2005-12-18 18:37:03 +00:00
parent 9888b78f19
commit e3b7a56833
18 changed files with 279 additions and 136 deletions

View File

@ -13,12 +13,13 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: Cheat.hxx,v 1.2 2005-11-27 22:37:24 stephena Exp $
// $Id: Cheat.hxx,v 1.3 2005-12-18 18:37:01 stephena Exp $
//============================================================================
#ifndef CHEAT_HXX
#define CHEAT_HXX
#include "StringList.hxx"
#include "OSystem.hxx"
#include "bspf.hxx"
@ -29,7 +30,11 @@ class Cheat
: myOSystem(osystem),
myName(name),
myCode(code),
myEnabled(false) { if(name == "") myName = code; }
myEnabled(false)
{
if(name == "") myName = code;
myName = StringList::removePattern(myName, "\":");
}
virtual ~Cheat() { }
bool enabled() const { return myEnabled; }

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: CheatCodeDialog.cxx,v 1.5 2005-11-27 22:37:24 stephena Exp $
// $Id: CheatCodeDialog.cxx,v 1.6 2005-12-18 18:37:01 stephena Exp $
//
// Based on code from ScummVM - Scumm Interpreter
// Copyright (C) 2002-2004 The ScummVM project
@ -38,10 +38,11 @@
enum {
kAddCheatCmd = 'CHTa',
kEditCheatCmd = 'CHTe',
kAddOneShotCmd = 'CHTo',
kCheatAdded = 'CHad',
kCheatEdited = 'CHed',
kRemCheatCmd = 'CHTr',
kAddOneShotCmd = 'CHTo'
kOneShotCheatAdded = 'CHoa',
kRemCheatCmd = 'CHTr'
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -161,6 +162,16 @@ void CheatCodeDialog::removeCheat()
loadConfig(); // reload the cheat list
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatCodeDialog::addOneShotCheat()
{
myCheatInput->setEditString("One-shot cheat", 0);
myCheatInput->setEditString("", 1);
myCheatInput->setTitle("");
myCheatInput->setEmitSignal(kOneShotCheatAdded);
parent()->addDialog(myCheatInput);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatCodeDialog::handleCommand(CommandSender* sender, int cmd,
int data, int id)
@ -227,9 +238,23 @@ void CheatCodeDialog::handleCommand(CommandSender* sender, int cmd,
break;
case kAddOneShotCmd:
cerr << "add one-shot cheat\n";
addOneShotCheat();
break;
case kOneShotCheatAdded:
{
const string& name = myCheatInput->getResult(0);
const string& code = myCheatInput->getResult(1);
if(instance()->cheat().isValidCode(code))
{
instance()->cheat().addOneShot(name, code);
parent()->removeDialog();
}
else
myCheatInput->setTitle("Invalid code");
break;
}
default:
Dialog::handleCommand(sender, cmd, data, 0);
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: CheatCodeDialog.hxx,v 1.3 2005-11-27 22:37:24 stephena Exp $
// $Id: CheatCodeDialog.hxx,v 1.4 2005-12-18 18:37:01 stephena Exp $
//
// Based on code from ScummVM - Scumm Interpreter
// Copyright (C) 2002-2004 The ScummVM project
@ -28,6 +28,7 @@ class ButtonWidget;
class StaticTextWidget;
class CheckListWidget;
class InputTextDialog;
class OptionsDialog;
#include "OSystem.hxx"
#include "Dialog.hxx"
@ -53,6 +54,7 @@ class CheatCodeDialog : public Dialog
void addCheat();
void editCheat();
void removeCheat();
void addOneShotCheat();
private:
CheckListWidget* myCheatList;

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: CheatManager.cxx,v 1.4 2005-11-27 22:37:24 stephena Exp $
// $Id: CheatManager.cxx,v 1.5 2005-12-18 18:37:01 stephena Exp $
//============================================================================
#include <sstream>
@ -44,11 +44,10 @@ CheatManager::~CheatManager()
const Cheat* CheatManager::add(const string& name, const string& code,
bool enable, int idx)
{
if(!isValidCode(code))
Cheat* cheat = (Cheat*) createCheat(name, code);
if(!cheat)
return NULL;
Cheat* cheat = (Cheat*) NULL;
// Delete duplicate entries
for(unsigned int i = 0; i < myCheatList.size(); i++)
{
@ -59,25 +58,7 @@ const Cheat* CheatManager::add(const string& name, const string& code,
}
}
// Create new cheat based on string length
switch(code.size())
{
case 4:
cheat = new RamCheat(myOSystem, name, code);
break;
case 6:
cheat = new CheetahCheat(myOSystem, name, code);
break;
case 8:
cheat = new BankRomCheat(myOSystem, name, code);
break;
}
// Add the cheat to the main cheat list
if(cheat)
{
if(idx == -1)
myCheatList.push_back(cheat);
else
@ -88,7 +69,6 @@ const Cheat* CheatManager::add(const string& name, const string& code,
cheat->enable();
else
cheat->disable();
}
return cheat;
}
@ -139,6 +119,44 @@ void CheatManager::addPerFrame(Cheat* cheat, bool enable)
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatManager::addOneShot(const string& name, const string& code)
{
Cheat* cheat = (Cheat*) createCheat(name, code);
if(!cheat)
return;
// Enable this cheat once, and then immediately delete it
cheat->enable();
delete cheat;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const Cheat* CheatManager::createCheat(const string& name, const string& code)
{
if(!isValidCode(code))
return NULL;
// Create new cheat based on string length
switch(code.size())
{
case 4:
return new RamCheat(myOSystem, name, code);
break;
case 6:
return new CheetahCheat(myOSystem, name, code);
break;
case 8:
return new BankRomCheat(myOSystem, name, code);
break;
default:
return NULL;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatManager::parse(const string& cheats)
{

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: CheatManager.hxx,v 1.5 2005-12-09 01:16:13 stephena Exp $
// $Id: CheatManager.hxx,v 1.6 2005-12-18 18:37:01 stephena Exp $
//============================================================================
#ifndef CHEAT_MANAGER_HXX
@ -36,7 +36,7 @@ typedef map<string,string> CheatCodeMap;
the list of all cheats currently in use.
@author Stephen Anthony
@version $Id: CheatManager.hxx,v 1.5 2005-12-09 01:16:13 stephena Exp $
@version $Id: CheatManager.hxx,v 1.6 2005-12-18 18:37:01 stephena Exp $
*/
class CheatManager
{
@ -74,6 +74,16 @@ class CheatManager
*/
void addPerFrame(Cheat* cheat, bool enable);
/**
Creates and enables a one-shot cheat. One-shot cheats are the
same as normal cheats, except they are only enabled once, and
they're not saved at all.
@param name Name of the cheat (not absolutely required)
@param code The actual cheatcode (in hex)
*/
void addOneShot(const string& name, const string& code);
/**
Enable/disabled the cheat specified by the given code.
@ -118,6 +128,16 @@ class CheatManager
bool isValidCode(const string& code);
private:
/**
Create a cheat defined by the given code.
@param name Name of the cheat (not absolutely required)
@param code The actual cheatcode (in hex)
@return The cheat (if was created), else NULL.
*/
const Cheat* createCheat(const string& name, const string& code);
/**
Parses a list of cheats and adds/enables each one.

View File

@ -13,15 +13,10 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: mainSDL.cxx,v 1.57 2005-11-28 15:05:43 stephena Exp $
// $Id: mainSDL.cxx,v 1.58 2005-12-18 18:37:02 stephena Exp $
//============================================================================
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <SDL.h>

View File

@ -13,15 +13,12 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: DataGridWidget.cxx,v 1.3 2005-10-13 18:53:07 stephena Exp $
// $Id: DataGridWidget.cxx,v 1.4 2005-12-18 18:37:02 stephena Exp $
//
// Based on code from ScummVM - Scumm Interpreter
// Copyright (C) 2002-2004 The ScummVM project
//============================================================================
#include <cctype>
#include <algorithm>
#include "OSystem.hxx"
#include "Widget.hxx"
#include "Dialog.hxx"

View File

@ -13,10 +13,9 @@
// 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.130 2005-12-18 14:15:53 stephena Exp $
// $Id: EventHandler.cxx,v 1.131 2005-12-18 18:37:03 stephena Exp $
//============================================================================
#include <algorithm>
#include <sstream>
#include <SDL.h>
@ -506,11 +505,6 @@ void EventHandler::poll(uInt32 time)
{
switch(int(key))
{
case SDLK_c:
enterMenuMode(S_MENU);
myOSystem->menu().enterCheatMode();
break;
case SDLK_0: // Ctrl-0 sets the mouse to paddle 0
setPaddleMode(0, true);
break;

View File

@ -0,0 +1,84 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2005 by Bradford W. Mott and the Stella team
//
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: MediaFactory.cxx,v 1.1 2005-12-18 18:37:03 stephena Exp $
//============================================================================
////////////////////////////////////////////////////////////////////
// I think you can see why this mess was put into a factory class :)
////////////////////////////////////////////////////////////////////
#include "MediaFactory.hxx"
#include "FrameBuffer.hxx"
#include "FrameBufferSoft.hxx"
#ifdef DISPLAY_OPENGL
#include "FrameBufferGL.hxx"
#endif
#if defined(PSP)
#include "FrameBufferPSP.hxx"
#elif defined (_WIN32_WCE)
#include "FrameBufferWinCE.hxx"
#endif
#include "Sound.hxx"
#include "SoundNull.hxx"
#ifdef SOUND_SUPPORT
#ifndef _WIN32_WCE
#include "SoundSDL.hxx"
#else
#include "SoundWinCE.hxx"
#endif
#endif
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FrameBuffer* MediaFactory::createVideo(const string& type, OSystem* parent)
{
FrameBuffer* fb = (FrameBuffer*) NULL;
if(type == "soft")
#if defined (PSP)
fb = new FrameBufferPSP(parent);
#elif defined (_WIN32_WCE)
fb = new FrameBufferWinCE(parent);
#else
fb = new FrameBufferSoft(parent);
#endif
#ifdef DISPLAY_OPENGL
else if(type == "gl")
fb = new FrameBufferGL(parent);
#endif
return fb;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Sound* MediaFactory::createAudio(const string& type, OSystem* parent)
{
Sound* sound = (Sound*) NULL;
#ifdef SOUND_SUPPORT
#if defined (_WIN32_WCE)
sound = new SoundWinCE(parent);
#else
sound = new SoundSDL(parent);
#endif
#else
sound = new SoundNull(parent);
#endif
return sound;
}

View File

@ -0,0 +1,43 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2005 by Bradford W. Mott and the Stella team
//
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: MediaFactory.hxx,v 1.1 2005-12-18 18:37:03 stephena Exp $
//============================================================================
#ifndef MEDIA_FACTORY_HXX
#define MEDIA_FACTORY_HXX
#include "bspf.hxx"
class FrameBuffer;
class Sound;
class OSystem;
/**
This class deals with the different framebuffer/sound implementations
for the various ports of Stella, and always returns a valid media object
based on the specific port and restrictions on that port.
@author Stephen Anthony
@version $Id: MediaFactory.hxx,v 1.1 2005-12-18 18:37:03 stephena Exp $
*/
class MediaFactory
{
public:
static FrameBuffer* createVideo(const string& type, OSystem* parent);
static Sound* createAudio(const string& type, OSystem* parent);
};
#endif

View File

@ -13,35 +13,14 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: OSystem.cxx,v 1.49 2005-11-27 22:37:24 stephena Exp $
// $Id: OSystem.cxx,v 1.50 2005-12-18 18:37:03 stephena Exp $
//============================================================================
#include <cassert>
#include <sstream>
#include <fstream>
// FIXME - clean up this mess of platform-specific ifdefs
#include "FrameBuffer.hxx"
#include "FrameBufferSoft.hxx"
#ifdef DISPLAY_OPENGL
#include "FrameBufferGL.hxx"
#endif
#if defined(PSP)
#include "FrameBufferPSP.hxx"
#elif defined (_WIN32_WCE)
#include "FrameBufferWinCE.hxx"
#endif
#include "Sound.hxx"
#include "SoundNull.hxx"
#ifdef SOUND_SUPPORT
#ifndef _WIN32_WCE
#include "SoundSDL.hxx"
#else
#include "SoundWinCE.hxx"
#endif
#endif
#include "MediaFactory.hxx"
#ifdef DEVELOPER_SUPPORT
#include "Debugger.hxx"
@ -203,20 +182,7 @@ bool OSystem::createFrameBuffer(bool showmessage)
// And recreate a new one
string video = mySettings->getString("video");
if(video == "soft")
#if defined (PSP)
myFrameBuffer = new FrameBufferPSP(this);
#elif defined (_WIN32_WCE)
myFrameBuffer = new FrameBufferWinCE(this);
#else
myFrameBuffer = new FrameBufferSoft(this);
#endif
#ifdef DISPLAY_OPENGL
else if(video == "gl")
myFrameBuffer = new FrameBufferGL(this);
#endif
myFrameBuffer = MediaFactory::createVideo(video, this);
if(!myFrameBuffer)
return false;
@ -281,15 +247,9 @@ void OSystem::createSound()
delete mySound; mySound = NULL;
// And recreate a new sound device
#ifdef SOUND_SUPPORT
#if defined (_WIN32_WCE)
mySound = new SoundWinCE(this);
#else
mySound = new SoundSDL(this);
#endif
#else
mySound = MediaFactory::createAudio("", this);
#ifndef SOUND_SUPPORT
mySettings->setBool("sound", false);
mySound = new SoundNull(this);
#endif
// Re-initialize the sound object to current settings

View File

@ -46,7 +46,8 @@ MODULE_OBJS := \
src/emucore/Switches.o \
src/emucore/TIA.o \
src/emucore/TIASnd.o \
src/emucore/unzip.o
src/emucore/unzip.o \
src/emucore/MediaFactory.o
MODULE_DIRS += \
src/emucore

View File

@ -13,13 +13,12 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: LauncherDialog.cxx,v 1.33 2005-10-22 20:33:57 stephena Exp $
// $Id: LauncherDialog.cxx,v 1.34 2005-12-18 18:37:03 stephena Exp $
//
// Based on code from ScummVM - Scumm Interpreter
// Copyright (C) 2002-2004 The ScummVM project
//============================================================================
#include <algorithm>
#include <sstream>
#include "OSystem.hxx"

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: Menu.cxx,v 1.11 2005-09-25 23:14:00 urchlay Exp $
// $Id: Menu.cxx,v 1.12 2005-12-18 18:37:03 stephena Exp $
//============================================================================
#include "Dialog.hxx"
@ -46,9 +46,3 @@ void Menu::setGameProfile(Properties& props)
{
((OptionsDialog*)myBaseDialog)->setGameProfile(props);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Menu::enterCheatMode()
{
((OptionsDialog*)myBaseDialog)->enterCheatMode();
}

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: Menu.hxx,v 1.10 2005-09-25 23:14:00 urchlay Exp $
// $Id: Menu.hxx,v 1.11 2005-12-18 18:37:03 stephena Exp $
//============================================================================
#ifndef MENU_HXX
@ -28,7 +28,7 @@ class OSystem;
The base dialog for all configuration menus in Stella.
@author Stephen Anthony
@version $Id: Menu.hxx,v 1.10 2005-09-25 23:14:00 urchlay Exp $
@version $Id: Menu.hxx,v 1.11 2005-12-18 18:37:03 stephena Exp $
*/
class Menu : public DialogContainer
{
@ -55,11 +55,6 @@ class Menu : public DialogContainer
@param props The properties of the current game
*/
void setGameProfile(Properties& props);
/**
Ugly hack: enter cheat mode
*/
void enterCheatMode();
};
#endif

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: OptionsDialog.cxx,v 1.34 2005-11-26 21:23:35 stephena Exp $
// $Id: OptionsDialog.cxx,v 1.35 2005-12-18 18:37:03 stephena Exp $
//
// Based on code from ScummVM - Scumm Interpreter
// Copyright (C) 2002-2004 The ScummVM project
@ -202,11 +202,3 @@ void OptionsDialog::handleCommand(CommandSender* sender, int cmd,
Dialog::handleCommand(sender, cmd, data, 0);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void OptionsDialog::enterCheatMode()
{
#ifdef CHEATCODE_SUPPORT
parent()->addDialog(myCheatCodeDialog);
#endif
}

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: OptionsDialog.hxx,v 1.16 2005-11-13 22:25:47 stephena Exp $
// $Id: OptionsDialog.hxx,v 1.17 2005-12-18 18:37:03 stephena Exp $
//
// Based on code from ScummVM - Scumm Interpreter
// Copyright (C) 2002-2004 The ScummVM project
@ -47,7 +47,6 @@ class OptionsDialog : public Dialog
virtual void handleCommand(CommandSender* sender, int cmd, int data, int id);
void setGameProfile(Properties& props) { myGameInfoDialog->setGameProfile(props); }
void enterCheatMode();
protected:
VideoDialog* myVideoDialog;

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: StringList.hxx,v 1.4 2005-12-09 01:16:14 stephena Exp $
// $Id: StringList.hxx,v 1.5 2005-12-18 18:37:03 stephena Exp $
//
// Based on code from ScummVM - Scumm Interpreter
// Copyright (C) 2002-2004 The ScummVM project
@ -40,6 +40,26 @@ class StringList : public Common::Array<string>
ensureCapacity(_size + 1);
_data[_size++] = str;
}
static string removePattern(const string& str, const string& pattern)
{
// This can probably be made more efficient ...
string tmp;
for(unsigned int i = 0; i < str.length(); ++i)
{
bool match = false;
for(unsigned int j = 0; j < pattern.length(); ++j)
{
if(str[i] == pattern[j])
{
match = true;
break;
}
}
if(!match) tmp += str[i];
}
return tmp;
}
};
#endif