MOHAWK: MYST: Simplify memory management of the active stack

This commit is contained in:
Bastien Bouclet 2018-05-27 21:39:15 +02:00
parent 13b3371f1a
commit ea60aef8a8
5 changed files with 25 additions and 30 deletions

View file

@ -32,6 +32,7 @@
#ifdef ENABLE_MYST
#include "mohawk/myst.h"
#include "mohawk/myst_scripts.h"
#endif
#ifdef ENABLE_RIVEN

View file

@ -78,13 +78,10 @@ MohawkEngine_Myst::MohawkEngine_Myst(OSystem *syst, const MohawkGameDescription
_video = nullptr;
_gfx = nullptr;
_console = nullptr;
_scriptParser = nullptr;
_gameState = nullptr;
_optionsDialog = nullptr;
_rnd = nullptr;
_prevStack = nullptr;
_mouseClicked = false;
_mouseMoved = false;
_escapePressed = false;
@ -103,10 +100,8 @@ MohawkEngine_Myst::~MohawkEngine_Myst() {
delete _video;
delete _sound;
delete _console;
delete _scriptParser;
delete _gameState;
delete _optionsDialog;
delete _prevStack;
delete _rnd;
}
@ -565,64 +560,60 @@ void MohawkEngine_Myst::changeToStack(uint16 stack, uint16 card, uint16 linkSrcS
if (linkSrcSound)
playSoundBlocking(linkSrcSound);
if (_card) {
_card->leave();
_card.reset();
// Delete the previous stack and move the current stack to the previous one
// There's probably a better way to do this, but the script classes shouldn't
// take up much memory.
delete _prevStack;
_prevStack = _scriptParser;
}
_curStack = stack;
switch (_curStack) {
case kChannelwoodStack:
_gameState->_globals.currentAge = kChannelwood;
_scriptParser = new MystStacks::Channelwood(this);
_scriptParser = MystScriptParserPtr(new MystStacks::Channelwood(this));
break;
case kCreditsStack:
_scriptParser = new MystStacks::Credits(this);
_scriptParser = MystScriptParserPtr(new MystStacks::Credits(this));
break;
case kDemoStack:
_gameState->_globals.currentAge = kSelenitic;
_scriptParser = new MystStacks::Demo(this);
_scriptParser = MystScriptParserPtr(new MystStacks::Demo(this));
break;
case kDniStack:
_gameState->_globals.currentAge = kDni;
_scriptParser = new MystStacks::Dni(this);
_scriptParser = MystScriptParserPtr(new MystStacks::Dni(this));
break;
case kIntroStack:
_scriptParser = new MystStacks::Intro(this);
_scriptParser = MystScriptParserPtr(new MystStacks::Intro(this));
break;
case kMakingOfStack:
_scriptParser = new MystStacks::MakingOf(this);
_scriptParser = MystScriptParserPtr(new MystStacks::MakingOf(this));
break;
case kMechanicalStack:
_gameState->_globals.currentAge = kMechanical;
_scriptParser = new MystStacks::Mechanical(this);
_scriptParser = MystScriptParserPtr(new MystStacks::Mechanical(this));
break;
case kMystStack:
_gameState->_globals.currentAge = kMystLibrary;
_scriptParser = new MystStacks::Myst(this);
_scriptParser = MystScriptParserPtr(new MystStacks::Myst(this));
break;
case kDemoPreviewStack:
_scriptParser = new MystStacks::Preview(this);
_scriptParser = MystScriptParserPtr(new MystStacks::Preview(this));
break;
case kSeleniticStack:
_gameState->_globals.currentAge = kSelenitic;
_scriptParser = new MystStacks::Selenitic(this);
_scriptParser = MystScriptParserPtr(new MystStacks::Selenitic(this));
break;
case kDemoSlidesStack:
_gameState->_globals.currentAge = kStoneship;
_scriptParser = new MystStacks::Slides(this);
_scriptParser = MystScriptParserPtr(new MystStacks::Slides(this));
break;
case kStoneshipStack:
_gameState->_globals.currentAge = kStoneship;
_scriptParser = new MystStacks::Stoneship(this);
_scriptParser = MystScriptParserPtr(new MystStacks::Stoneship(this));
break;
default:
error("Unknown Myst stack");
error("Unknown Myst stack %d", _curStack);
}
// If the array is empty, add a new one. Otherwise, delete the first

View file

@ -26,7 +26,6 @@
#include "mohawk/console.h"
#include "mohawk/mohawk.h"
#include "mohawk/resource_cache.h"
#include "mohawk/myst_scripts.h"
#include "mohawk/video.h"
#include "audio/mixer.h"
@ -123,6 +122,7 @@ enum {
};
typedef Common::SharedPtr<MystCard> MystCardPtr;
typedef Common::SharedPtr<MystScriptParser> MystScriptParserPtr;
class MohawkEngine_Myst : public MohawkEngine {
protected:
@ -163,7 +163,7 @@ public:
MystSound *_sound;
MystGraphics *_gfx;
MystGameState *_gameState;
MystScriptParser *_scriptParser;
MystScriptParserPtr _scriptParser;
Common::RandomSource *_rnd;
MystArea *loadResource(Common::SeekableReadStream *rlstStream, MystArea *parent);
@ -199,7 +199,6 @@ public:
private:
MystConsole *_console;
MystOptionsDialog *_optionsDialog;
MystScriptParser *_prevStack;
ResourceCache _cache;
uint16 _curStack;

View file

@ -147,7 +147,10 @@ MystAreaAction::MystAreaAction(MohawkEngine_Myst *vm, ResourceType type, Common:
}
void MystAreaAction::handleMouseUp() {
_vm->_scriptParser->runScript(_script, this);
// Keep a reference to the stack so it is not freed if a script switches to another stack
MystScriptParserPtr parser = _vm->_scriptParser;
parser->runScript(_script, this);
}
const Common::String MystAreaAction::describe() {

View file

@ -24,6 +24,7 @@
#define MYST_AREAS_H
#include "mohawk/myst.h"
#include "mohawk/myst_scripts.h"
#include "mohawk/video.h"
#include "common/rect.h"