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 #ifdef ENABLE_MYST
#include "mohawk/myst.h" #include "mohawk/myst.h"
#include "mohawk/myst_scripts.h"
#endif #endif
#ifdef ENABLE_RIVEN #ifdef ENABLE_RIVEN

View file

@ -78,13 +78,10 @@ MohawkEngine_Myst::MohawkEngine_Myst(OSystem *syst, const MohawkGameDescription
_video = nullptr; _video = nullptr;
_gfx = nullptr; _gfx = nullptr;
_console = nullptr; _console = nullptr;
_scriptParser = nullptr;
_gameState = nullptr; _gameState = nullptr;
_optionsDialog = nullptr; _optionsDialog = nullptr;
_rnd = nullptr; _rnd = nullptr;
_prevStack = nullptr;
_mouseClicked = false; _mouseClicked = false;
_mouseMoved = false; _mouseMoved = false;
_escapePressed = false; _escapePressed = false;
@ -103,10 +100,8 @@ MohawkEngine_Myst::~MohawkEngine_Myst() {
delete _video; delete _video;
delete _sound; delete _sound;
delete _console; delete _console;
delete _scriptParser;
delete _gameState; delete _gameState;
delete _optionsDialog; delete _optionsDialog;
delete _prevStack;
delete _rnd; delete _rnd;
} }
@ -565,64 +560,60 @@ void MohawkEngine_Myst::changeToStack(uint16 stack, uint16 card, uint16 linkSrcS
if (linkSrcSound) if (linkSrcSound)
playSoundBlocking(linkSrcSound); playSoundBlocking(linkSrcSound);
_card->leave(); if (_card) {
_card.reset(); _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; _curStack = stack;
switch (_curStack) { switch (_curStack) {
case kChannelwoodStack: case kChannelwoodStack:
_gameState->_globals.currentAge = kChannelwood; _gameState->_globals.currentAge = kChannelwood;
_scriptParser = new MystStacks::Channelwood(this); _scriptParser = MystScriptParserPtr(new MystStacks::Channelwood(this));
break; break;
case kCreditsStack: case kCreditsStack:
_scriptParser = new MystStacks::Credits(this); _scriptParser = MystScriptParserPtr(new MystStacks::Credits(this));
break; break;
case kDemoStack: case kDemoStack:
_gameState->_globals.currentAge = kSelenitic; _gameState->_globals.currentAge = kSelenitic;
_scriptParser = new MystStacks::Demo(this); _scriptParser = MystScriptParserPtr(new MystStacks::Demo(this));
break; break;
case kDniStack: case kDniStack:
_gameState->_globals.currentAge = kDni; _gameState->_globals.currentAge = kDni;
_scriptParser = new MystStacks::Dni(this); _scriptParser = MystScriptParserPtr(new MystStacks::Dni(this));
break; break;
case kIntroStack: case kIntroStack:
_scriptParser = new MystStacks::Intro(this); _scriptParser = MystScriptParserPtr(new MystStacks::Intro(this));
break; break;
case kMakingOfStack: case kMakingOfStack:
_scriptParser = new MystStacks::MakingOf(this); _scriptParser = MystScriptParserPtr(new MystStacks::MakingOf(this));
break; break;
case kMechanicalStack: case kMechanicalStack:
_gameState->_globals.currentAge = kMechanical; _gameState->_globals.currentAge = kMechanical;
_scriptParser = new MystStacks::Mechanical(this); _scriptParser = MystScriptParserPtr(new MystStacks::Mechanical(this));
break; break;
case kMystStack: case kMystStack:
_gameState->_globals.currentAge = kMystLibrary; _gameState->_globals.currentAge = kMystLibrary;
_scriptParser = new MystStacks::Myst(this); _scriptParser = MystScriptParserPtr(new MystStacks::Myst(this));
break; break;
case kDemoPreviewStack: case kDemoPreviewStack:
_scriptParser = new MystStacks::Preview(this); _scriptParser = MystScriptParserPtr(new MystStacks::Preview(this));
break; break;
case kSeleniticStack: case kSeleniticStack:
_gameState->_globals.currentAge = kSelenitic; _gameState->_globals.currentAge = kSelenitic;
_scriptParser = new MystStacks::Selenitic(this); _scriptParser = MystScriptParserPtr(new MystStacks::Selenitic(this));
break; break;
case kDemoSlidesStack: case kDemoSlidesStack:
_gameState->_globals.currentAge = kStoneship; _gameState->_globals.currentAge = kStoneship;
_scriptParser = new MystStacks::Slides(this); _scriptParser = MystScriptParserPtr(new MystStacks::Slides(this));
break; break;
case kStoneshipStack: case kStoneshipStack:
_gameState->_globals.currentAge = kStoneship; _gameState->_globals.currentAge = kStoneship;
_scriptParser = new MystStacks::Stoneship(this); _scriptParser = MystScriptParserPtr(new MystStacks::Stoneship(this));
break; break;
default: default:
error("Unknown Myst stack"); error("Unknown Myst stack %d", _curStack);
} }
// If the array is empty, add a new one. Otherwise, delete the first // 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/console.h"
#include "mohawk/mohawk.h" #include "mohawk/mohawk.h"
#include "mohawk/resource_cache.h" #include "mohawk/resource_cache.h"
#include "mohawk/myst_scripts.h"
#include "mohawk/video.h" #include "mohawk/video.h"
#include "audio/mixer.h" #include "audio/mixer.h"
@ -123,6 +122,7 @@ enum {
}; };
typedef Common::SharedPtr<MystCard> MystCardPtr; typedef Common::SharedPtr<MystCard> MystCardPtr;
typedef Common::SharedPtr<MystScriptParser> MystScriptParserPtr;
class MohawkEngine_Myst : public MohawkEngine { class MohawkEngine_Myst : public MohawkEngine {
protected: protected:
@ -163,7 +163,7 @@ public:
MystSound *_sound; MystSound *_sound;
MystGraphics *_gfx; MystGraphics *_gfx;
MystGameState *_gameState; MystGameState *_gameState;
MystScriptParser *_scriptParser; MystScriptParserPtr _scriptParser;
Common::RandomSource *_rnd; Common::RandomSource *_rnd;
MystArea *loadResource(Common::SeekableReadStream *rlstStream, MystArea *parent); MystArea *loadResource(Common::SeekableReadStream *rlstStream, MystArea *parent);
@ -199,7 +199,6 @@ public:
private: private:
MystConsole *_console; MystConsole *_console;
MystOptionsDialog *_optionsDialog; MystOptionsDialog *_optionsDialog;
MystScriptParser *_prevStack;
ResourceCache _cache; ResourceCache _cache;
uint16 _curStack; uint16 _curStack;

View file

@ -147,7 +147,10 @@ MystAreaAction::MystAreaAction(MohawkEngine_Myst *vm, ResourceType type, Common:
} }
void MystAreaAction::handleMouseUp() { 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() { const Common::String MystAreaAction::describe() {

View file

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