HUGO: Replace cypher by a global variable, clean engine destructor

This commit is contained in:
strangerke 2011-02-15 19:30:15 +01:00
parent 325a301a4f
commit 2f0b35bbb5
10 changed files with 119 additions and 154 deletions

View file

@ -585,17 +585,12 @@ void Screen::loadPalette(Common::ReadStream &in) {
} }
/** /**
* Free main and current palettes * Free fonts, main and current palettes
*/ */
void Screen::freePalette() { void Screen::freeScreen() {
free(_curPalette); free(_curPalette);
free(_mainPalette); free(_mainPalette);
}
/**
* Free fonts
*/
void Screen::freeFonts() {
for (int i = 0; i < kNumFonts; i++) { for (int i = 0; i < kNumFonts; i++) {
if (_arrayFont[i]) if (_arrayFont[i])
free(_arrayFont[i]); free(_arrayFont[i]);

View file

@ -65,8 +65,7 @@ public:
void drawRectangle(const bool filledFl, const int16 x1, const int16 y1, const int16 x2, const int16 y2, const int color); void drawRectangle(const bool filledFl, const int16 x1, const int16 y1, const int16 x2, const int16 y2, const int color);
void drawShape(const int x, const int y, const int color1, const int color2); void drawShape(const int x, const int y, const int color1, const int color2);
void drawStatusText(); void drawStatusText();
void freeFonts(); void freeScreen();
void freePalette();
void hideCursor(); void hideCursor();
void initDisplay(); void initDisplay();
void initNewScreenDisplay(); void initNewScreenDisplay();

View file

@ -369,14 +369,7 @@ bool FileManager::saveGame(const int16 slot, const Common::String &descrip) {
for (int i = 0; i < _vm->_numScreens; i++) for (int i = 0; i < _vm->_numScreens; i++)
out->writeByte(_vm->_screenStates[i]); out->writeByte(_vm->_screenStates[i]);
_vm->_scheduler->savePoints(out); _vm->_scheduler->saveSchedulerData(out);
// Now save current time and all current events in event queue
_vm->_scheduler->saveEvents(out);
// Now save current actions
_vm->_scheduler->saveActions(out);
// Save palette table // Save palette table
_vm->_screen->savePal(out); _vm->_screen->savePal(out);
@ -471,14 +464,7 @@ bool FileManager::restoreGame(const int16 slot) {
for (int i = 0; i < _vm->_numScreens; i++) for (int i = 0; i < _vm->_numScreens; i++)
_vm->_screenStates[i] = in->readByte(); _vm->_screenStates[i] = in->readByte();
_vm->_scheduler->restorePoints(in); _vm->_scheduler->restoreSchedulerData(in);
_vm->_object->restoreAllSeq();
// Now restore time of the save and the event queue
_vm->_scheduler->restoreEvents(in);
// Now restore actions
_vm->_scheduler->restoreActions(in);
// Restore palette and change it if necessary // Restore palette and change it if necessary
_vm->_screen->restorePal(in); _vm->_screen->restorePal(in);

View file

@ -74,30 +74,22 @@ HugoEngine::HugoEngine(OSystem *syst, const HugoGameDescription *gd) : Engine(sy
} }
HugoEngine::~HugoEngine() { HugoEngine::~HugoEngine() {
shutdown(); _file->closeDatabaseFiles();
_screen->freePalette();
_text->freeAllTexts();
_intro->freeIntroData(); _intro->freeIntroData();
_parser->freeArrayReqs();
_mouse->freeHotspots();
_inventory->freeInvent(); _inventory->freeInvent();
_object->freeObjectUses(); _mouse->freeHotspots();
_parser->freeCatchallList(); _object->freeObjects();
_parser->freeBackgroundObjects(); _parser->freeParser();
_scheduler->freePoints(); _scheduler->freeScheduler();
_parser->freeCmdList(); _screen->freeScreen();
_scheduler->freeScreenAct(); _text->freeAllTexts();
_object->freeObjectArr();
_scheduler->freeActListArr();
free(_defltTunes); free(_defltTunes);
free(_screenStates); free(_screenStates);
_screen->freeFonts();
delete _topMenu; delete _topMenu;
delete _object; delete _object;
delete _sound; delete _sound;
delete _route; delete _route;
@ -203,11 +195,12 @@ Common::Error HugoEngine::run() {
_screen->setCursorPal(); _screen->setCursorPal();
_screen->resetInventoryObjId(); _screen->resetInventoryObjId();
_scheduler->initCypher();
initStatus(); // Initialize game status initStatus(); // Initialize game status
initConfig(); // Initialize user's config initConfig(); // Initialize user's config
initialize(); initialize();
resetConfig(); // Reset user's config resetConfig(); // Reset user's config
initMachine(); initMachine();
// Start the state machine // Start the state machine
@ -576,16 +569,6 @@ void HugoEngine::initialize() {
} }
} }
/**
* Restore all resources before termination
*/
void HugoEngine::shutdown() {
debugC(1, kDebugEngine, "shutdown");
_file->closeDatabaseFiles();
_object->freeObjects();
}
/** /**
* Read scenery, overlay files for given screen number * Read scenery, overlay files for given screen number
*/ */

View file

@ -213,19 +213,16 @@ void ObjectHandler::lookObject(object_t *obj) {
} }
/** /**
* Free all object images * Free all object images, uses and ObjArr (before exiting)
*/ */
void ObjectHandler::freeObjects() { void ObjectHandler::freeObjects() {
debugC(1, kDebugObject, "freeObjects"); debugC(1, kDebugObject, "freeObjects");
// Nothing to do if not allocated yet if (_vm->_hero != 0 && _vm->_hero->seqList[0].seqPtr != 0) {
if (_vm->_hero == 0 || _vm->_hero->seqList[0].seqPtr == 0)
return;
// Free all sequence lists and image data // Free all sequence lists and image data
for (int i = 0; i < _numObj; i++) { for (int16 i = 0; i < _numObj; i++) {
object_t *obj = &_objects[i]; object_t *obj = &_objects[i];
for (int j = 0; j < obj->seqNumb; j++) { for (int16 j = 0; j < obj->seqNumb; j++) {
seq_t *seq = obj->seqList[j].seqPtr; seq_t *seq = obj->seqList[j].seqPtr;
seq_t *next; seq_t *next;
if (seq == 0) // Failure during database load if (seq == 0) // Failure during database load
@ -247,18 +244,23 @@ void ObjectHandler::freeObjects() {
free(seq); free(seq);
} }
} }
} }
/**
* Free all object uses
*/
void ObjectHandler::freeObjectUses() {
if (_uses) { if (_uses) {
for (int i = 0; i < _usesSize; i++) for (int16 i = 0; i < _usesSize; i++)
free(_uses[i].targets); free(_uses[i].targets);
free(_uses); free(_uses);
} }
for(int16 i = 0; i < _objCount; i++) {
free(_objects[i].stateDataIndex);
_objects[i].stateDataIndex = 0;
}
free(_objects);
_objects = 0;
} }
/** /**
* Compare function for the quicksort. The sort is to order the objects in * Compare function for the quicksort. The sort is to order the objects in
* increasing vertical position, using y+y2 as the baseline * increasing vertical position, using y+y2 as the baseline
@ -366,18 +368,6 @@ bool ObjectHandler::findObjectSpace(object_t *obj, int16 *destx, int16 *desty) {
return foundFl; return foundFl;
} }
/**
* Free ObjectArr (before exiting)
*/
void ObjectHandler::freeObjectArr() {
for(int16 i = 0; i < _objCount; i++) {
free(_objects[i].stateDataIndex);
_objects[i].stateDataIndex = 0;
}
free(_objects);
_objects = 0;
}
/** /**
* Load _uses from Hugo.dat * Load _uses from Hugo.dat
*/ */

View file

@ -73,10 +73,8 @@ public:
int calcMaxScore(); int calcMaxScore();
int16 findObject(uint16 x, uint16 y); int16 findObject(uint16 x, uint16 y);
void freeObjects(); void freeObjects();
void freeObjectUses();
void loadObjectArr(Common::ReadStream &in); void loadObjectArr(Common::ReadStream &in);
void loadObjectUses(Common::ReadStream &in); void loadObjectUses(Common::ReadStream &in);
void freeObjectArr();
void loadNumObj(Common::ReadStream &in); void loadNumObj(Common::ReadStream &in);
void lookObject(object_t *obj); void lookObject(object_t *obj);
void readObjectImages(); void readObjectImages();

View file

@ -105,14 +105,6 @@ void Parser::loadCmdList(Common::ReadStream &in) {
} }
} }
void Parser::freeCmdList() {
if (_cmdList) {
for (int i = 0; i < _cmdListSize; i++)
free(_cmdList[i]);
free(_cmdList);
}
}
/** /**
* Read _backgrounObjects from Hugo.dat * Read _backgrounObjects from Hugo.dat
*/ */
@ -146,14 +138,6 @@ void Parser::loadBackgroundObjects(Common::ReadStream &in) {
} }
} }
void Parser::freeBackgroundObjects() {
if (_backgroundObjects) {
for (int i = 0; i < _backgroundObjectsSize; i++)
free(_backgroundObjects[i]);
free(_backgroundObjects);
}
}
/** /**
* Read _catchallList from Hugo.dat * Read _catchallList from Hugo.dat
*/ */
@ -178,10 +162,6 @@ void Parser::loadCatchallList(Common::ReadStream &in) {
} }
} }
void Parser::freeCatchallList() {
free(_catchallList);
}
void Parser::loadArrayReqs(Common::ReadStream &in) { void Parser::loadArrayReqs(Common::ReadStream &in) {
_arrayReqs = _vm->loadLongArray(in); _arrayReqs = _vm->loadLongArray(in);
} }
@ -204,12 +184,26 @@ const char *Parser::useBG(const char *name) {
return 0; return 0;
} }
void Parser::freeArrayReqs() { void Parser::freeParser() {
if (_arrayReqs) { if (_arrayReqs) {
for (int i = 0; _arrayReqs[i] != 0; i++) for (int i = 0; _arrayReqs[i] != 0; i++)
free(_arrayReqs[i]); free(_arrayReqs[i]);
free(_arrayReqs); free(_arrayReqs);
} }
free(_catchallList);
if (_backgroundObjects) {
for (int i = 0; i < _backgroundObjectsSize; i++)
free(_backgroundObjects[i]);
free(_backgroundObjects);
}
if (_cmdList) {
for (int i = 0; i < _cmdListSize; i++)
free(_cmdList[i]);
free(_cmdList);
}
} }
void Parser::switchTurbo() { void Parser::switchTurbo() {

View file

@ -53,10 +53,7 @@ public:
void charHandler(); void charHandler();
void command(const char *format, ...); void command(const char *format, ...);
void freeArrayReqs(); void freeParser();
void freeBackgroundObjects();
void freeCatchallList();
void freeCmdList();
void keyHandler(Common::Event event); void keyHandler(Common::Event event);
void loadArrayReqs(Common::ReadStream &in); void loadArrayReqs(Common::ReadStream &in);
void loadBackgroundObjects(Common::ReadStream &in); void loadBackgroundObjects(Common::ReadStream &in);

View file

@ -57,6 +57,10 @@ Scheduler::Scheduler(HugoEngine *vm) : _vm(vm), _actListArr(0), _curTick(0), _ol
Scheduler::~Scheduler() { Scheduler::~Scheduler() {
} }
void Scheduler::initCypher() {
_cypher = getCypher();
}
/** /**
* Initialise the timer event queue * Initialise the timer event queue
*/ */
@ -840,21 +844,6 @@ void Scheduler::loadActListArr(Common::ReadStream &in) {
} }
} }
void Scheduler::freeActListArr() {
debugC(6, kDebugSchedule, "freeActListArr()");
if (_actListArr) {
for (int i = 0; i < _actListArrSize; i++) {
for (int j = 0; _actListArr[i][j].a0.actType != ANULL; j++) {
if (_actListArr[i][j].a0.actType == PROMPT)
free(_actListArr[i][j].a3.responsePtr);
}
free(_actListArr[i]);
}
free(_actListArr);
}
}
/** /**
* Read _screenActs * Read _screenActs
*/ */
@ -885,12 +874,27 @@ void Scheduler::loadScreenAct(Common::ReadStream &in) {
} }
} }
void Scheduler::freeScreenAct() { void Scheduler::freeScheduler() {
debugC(6, kDebugSchedule, "freeActListArr()");
free(_points);
if (_screenActs) { if (_screenActs) {
for (int i = 0; i < _screenActsSize; i++) for (int i = 0; i < _screenActsSize; i++)
free(_screenActs[i]); free(_screenActs[i]);
free(_screenActs); free(_screenActs);
} }
if (_actListArr) {
for (int i = 0; i < _actListArrSize; i++) {
for (int j = 0; _actListArr[i][j].a0.actType != ANULL; j++) {
if (_actListArr[i][j].a0.actType == PROMPT)
free(_actListArr[i][j].a3.responsePtr);
}
free(_actListArr[i]);
}
free(_actListArr);
}
} }
/** /**
@ -1020,7 +1024,6 @@ int16 Scheduler::calcMaxPoints() const {
/* /*
* Save the action data in the file with handle f * Save the action data in the file with handle f
*/ */
void Scheduler::saveActions(Common::WriteStream* f) const { void Scheduler::saveActions(Common::WriteStream* f) const {
for (int i = 0; i < _actListArrSize; i++) { for (int i = 0; i < _actListArrSize; i++) {
// write all the sub elems data // write all the sub elems data
@ -1061,6 +1064,27 @@ void Scheduler::findAction(act* action, int16* index, int16* subElem) {
assert(0); assert(0);
} }
void Scheduler::saveSchedulerData(Common::WriteStream *out) {
savePoints(out);
// Now save current time and all current events in event queue
saveEvents(out);
// Now save current actions
saveActions(out);
}
void Scheduler::restoreSchedulerData(Common::ReadStream *in) {
restorePoints(in);
_vm->_object->restoreAllSeq();
// Now restore time of the save and the event queue
restoreEvents(in);
// Now restore actions
restoreActions(in);
}
/** /**
* Restore the event list from file with handle f * Restore the event list from file with handle f
*/ */
@ -1455,7 +1479,7 @@ void Scheduler::delEventType(const action_t actTypeDel) {
/** /**
* Save the points table * Save the points table
*/ */
void Scheduler::savePoints(Common::WriteStream *out) { void Scheduler::savePoints(Common::WriteStream *out) const {
for (int i = 0; i < _numBonuses; i++) { for (int i = 0; i < _numBonuses; i++) {
out->writeByte(_points[i].score); out->writeByte(_points[i].score);
out->writeByte((_points[i].scoredFl) ? 1 : 0); out->writeByte((_points[i].scoredFl) ? 1 : 0);
@ -1533,11 +1557,9 @@ void Scheduler_v1d::promptAction(act *action) {
void Scheduler_v1d::decodeString(char *line) { void Scheduler_v1d::decodeString(char *line) {
debugC(1, kDebugSchedule, "decodeString(%s)", line); debugC(1, kDebugSchedule, "decodeString(%s)", line);
static const Common::String cypher = getCypher();
uint16 linelength = strlen(line); uint16 linelength = strlen(line);
for(uint16 i = 0; i < linelength; i++) { for(uint16 i = 0; i < linelength; i++) {
line[i] = (line[i] + cypher.c_str()[i % cypher.size()]) % '~'; line[i] = (line[i] + _cypher.c_str()[i % _cypher.size()]) % '~';
if (line[i] < ' ') if (line[i] < ' ')
line[i] += ' '; line[i] += ' ';
} }
@ -1586,11 +1608,10 @@ void Scheduler_v2d::promptAction(act *action) {
void Scheduler_v2d::decodeString(char *line) { void Scheduler_v2d::decodeString(char *line) {
debugC(1, kDebugSchedule, "decodeString(%s)", line); debugC(1, kDebugSchedule, "decodeString(%s)", line);
static const Common::String cypher = getCypher();
int16 lineLength = strlen(line); int16 lineLength = strlen(line);
for (uint16 i = 0; i < lineLength; i++) for (uint16 i = 0; i < lineLength; i++)
line[i] -= cypher.c_str()[i % cypher.size()]; line[i] -= _cypher.c_str()[i % _cypher.size()];
debugC(1, kDebugSchedule, "result : %s", line); debugC(1, kDebugSchedule, "result : %s", line);
} }

View file

@ -458,12 +458,10 @@ public:
virtual void decodeString(char *line) = 0; virtual void decodeString(char *line) = 0;
virtual void runScheduler() = 0; virtual void runScheduler() = 0;
void freePoints() { free(_points); }
int16 calcMaxPoints() const; int16 calcMaxPoints() const;
void freeActListArr(); void freeScheduler();
void freeScreenAct(); void initCypher();
void initEventQueue(); void initEventQueue();
void insertActionList(const uint16 actIndex); void insertActionList(const uint16 actIndex);
void loadActListArr(Common::ReadStream &in); void loadActListArr(Common::ReadStream &in);
@ -473,24 +471,20 @@ public:
void newScreen(const int screenIndex); void newScreen(const int screenIndex);
void processBonus(const int bonusIndex); void processBonus(const int bonusIndex);
void processMaze(const int x1, const int x2, const int y1, const int y2); void processMaze(const int x1, const int x2, const int y1, const int y2);
void restoreSchedulerData(Common::ReadStream *in);
void restoreScreen(const int screenIndex); void restoreScreen(const int screenIndex);
void restoreEvents(Common::ReadStream *f); void saveSchedulerData(Common::WriteStream *out);
void restorePoints(Common::ReadStream *in);
void saveEvents(Common::WriteStream *f);
void savePoints(Common::WriteStream *out);
void screenActions(const int screenNum);
void waitForRefresh(); void waitForRefresh();
void findAction(act* action, int16* index, int16* subElem); void findAction(act* action, int16* index, int16* subElem);
void saveActions(Common::WriteStream* f) const;
void restoreActions(Common::ReadStream *f);
protected: protected:
HugoEngine *_vm; HugoEngine *_vm;
static const int kFilenameLength = 12; // Max length of a DOS file name static const int kFilenameLength = 12; // Max length of a DOS file name
static const int kMaxEvents = 50; // Max events in event queue static const int kMaxEvents = 50; // Max events in event queue
static const int kShiftSize = 8; // Place hero this far inside bounding box static const int kShiftSize = 8; // Place hero this far inside bounding box
Common::String _cypher;
uint16 _actListArrSize; uint16 _actListArrSize;
uint16 _alNewscrIndex; uint16 _alNewscrIndex;
uint16 _screenActsSize; uint16 _screenActsSize;
@ -525,6 +519,14 @@ protected:
void delEventType(const action_t actTypeDel); void delEventType(const action_t actTypeDel);
void delQueue(event_t *curEvent); void delQueue(event_t *curEvent);
void insertAction(act *action); void insertAction(act *action);
void restoreActions(Common::ReadStream *f);
void restoreEvents(Common::ReadStream *f);
void restorePoints(Common::ReadStream *in);
void saveActions(Common::WriteStream* f) const;
void saveEvents(Common::WriteStream *f);
void savePoints(Common::WriteStream *out) const;
void screenActions(const int screenNum);
}; };
class Scheduler_v1d : public Scheduler { class Scheduler_v1d : public Scheduler {