From 019f3d4b626f290fa7e2bc6c6be0ab5c43ecd468 Mon Sep 17 00:00:00 2001 From: Borja Lorente Date: Sat, 13 Aug 2016 17:45:49 +0200 Subject: MACVENTURE: Add wrapper class for global settings --- engines/macventure/gui.cpp | 10 ++--- engines/macventure/macventure.cpp | 88 ++++++++++++++++++++++++--------------- engines/macventure/macventure.h | 49 +++++++++++++--------- engines/macventure/world.cpp | 22 +++++----- 4 files changed, 99 insertions(+), 70 deletions(-) diff --git a/engines/macventure/gui.cpp b/engines/macventure/gui.cpp index f8f9497b8b..834ffc1e19 100644 --- a/engines/macventure/gui.cpp +++ b/engines/macventure/gui.cpp @@ -314,14 +314,14 @@ WindowReference Gui::createInventoryWindow(ObjID objRef) { if (_windowData->back().refcon < 0x80) { // There is already another inventory window newData.bounds = _windowData->back().bounds; // Inventory windows are always last - newData.bounds.translate(newData.bounds.left + settings.invOffsetX, newData.bounds.top + settings.invOffsetY); + newData.bounds.translate(newData.bounds.left + settings._invOffsetX, newData.bounds.top + settings._invOffsetY); } else { BorderBounds bbs = borderBounds(kInvWindow); newData.bounds = Common::Rect( - settings.invLeft - bbs.leftOffset, - settings.invTop - bbs.topOffset, - settings.invLeft + settings.invWidth, - settings.invTop + settings.invHeight); + settings._invLeft - bbs.leftOffset, + settings._invTop - bbs.topOffset, + settings._invLeft + settings._invWidth, + settings._invTop + settings._invHeight); } newData.type = kInvWindow; newData.hasCloseBox = true; diff --git a/engines/macventure/macventure.cpp b/engines/macventure/macventure.cpp index 81bb267dfa..9c601dde9c 100644 --- a/engines/macventure/macventure.cpp +++ b/engines/macventure/macventure.cpp @@ -56,6 +56,7 @@ MacVentureEngine::MacVentureEngine(OSystem *syst, const ADGameDescription *gameD _debugger = NULL; _resourceManager = NULL; + _globalSettings = NULL; _gui = NULL; _world = NULL; _scriptEngine = NULL; @@ -87,6 +88,9 @@ MacVentureEngine::~MacVentureEngine() { if (_resourceManager) delete _resourceManager; + if (_globalSettings) + delete _globalSettings; + if (_gui) delete _gui; @@ -487,7 +491,7 @@ Common::String MacVentureEngine::getStartGameFileName() { } const GlobalSettings& MacVentureEngine::getGlobalSettings() const { - return _globalSettings; + return *_globalSettings; } // Private engine methods @@ -979,7 +983,7 @@ uint32 MacVentureEngine::randBetween(uint32 min, uint32 max) { } uint32 MacVentureEngine::getInvolvedObjects() { - return (_selectedControl ? _globalSettings.cmdArgCnts[_selectedControl - 1] : 3000); + return (_selectedControl ? getGlobalSettings()._cmdArgCnts[_selectedControl - 1] : 3000); } Common::Point MacVentureEngine::getObjPosition(ObjID objID) { @@ -1084,37 +1088,8 @@ bool MacVentureEngine::loadGlobalSettings() { Common::SeekableReadStream *res; res = _resourceManager->getResource(MKTAG('G', 'N', 'R', 'L'), kGlobalSettingsID); if (res) { - _globalSettings.numObjects = res->readUint16BE(); - _globalSettings.numGlobals = res->readUint16BE(); - _globalSettings.numCommands = res->readUint16BE(); - _globalSettings.numAttributes = res->readUint16BE(); - _globalSettings.numGroups = res->readUint16BE(); - res->readUint16BE(); // unknown - _globalSettings.invTop = res->readUint16BE(); - _globalSettings.invLeft = res->readUint16BE(); - _globalSettings.invWidth = res->readUint16BE(); - _globalSettings.invHeight = res->readUint16BE(); - _globalSettings.invOffsetY = res->readUint16BE(); - _globalSettings.invOffsetX = res->readSint16BE(); - _globalSettings.defaultFont = res->readUint16BE(); - _globalSettings.defaultSize = res->readUint16BE(); - - _globalSettings.attrIndices = new uint8[_globalSettings.numAttributes]; - res->read(_globalSettings.attrIndices, _globalSettings.numAttributes); - - _globalSettings.attrMasks = new uint16[_globalSettings.numAttributes]; - for (int i = 0; i < _globalSettings.numAttributes; i++) - _globalSettings.attrMasks[i] = res->readUint16BE(); - - _globalSettings.attrShifts = new uint8[_globalSettings.numAttributes]; - res->read(_globalSettings.attrShifts, _globalSettings.numAttributes); - - _globalSettings.cmdArgCnts = new uint8[_globalSettings.numCommands]; - res->read(_globalSettings.cmdArgCnts, _globalSettings.numCommands); - - _globalSettings.commands = new uint8[_globalSettings.numCommands]; - res->read(_globalSettings.commands, _globalSettings.numCommands); - + _globalSettings = new GlobalSettings(); + _globalSettings->loadSettings(res); delete res; return true; } @@ -1149,7 +1124,6 @@ bool MacVentureEngine::loadTextHuffman() { _textHuffman = new HuffmanLists(numEntries, lengths, masks, values); debugC(4, kMVDebugMain, "Text is huffman-encoded"); - delete res; delete masks; delete lengths; @@ -1159,6 +1133,52 @@ bool MacVentureEngine::loadTextHuffman() { return false; } +// Global Settings +GlobalSettings::GlobalSettings() { +} + +GlobalSettings::~GlobalSettings() { + +} +void GlobalSettings::loadSettings(Common::SeekableReadStream *dataStream) { + _numObjects = dataStream->readUint16BE(); + _numGlobals = dataStream->readUint16BE(); + _numCommands = dataStream->readUint16BE(); + _numAttributes = dataStream->readUint16BE(); + _numGroups = dataStream->readUint16BE(); + dataStream->readUint16BE(); // unknown + _invTop = dataStream->readUint16BE(); + _invLeft = dataStream->readUint16BE(); + _invWidth = dataStream->readUint16BE(); + _invHeight = dataStream->readUint16BE(); + _invOffsetY = dataStream->readUint16BE(); + _invOffsetX = dataStream->readSint16BE(); + _defaultFont = dataStream->readUint16BE(); + _defaultSize = dataStream->readUint16BE(); + + uint8 *attrIndices = new uint8[_numAttributes]; + dataStream->read(attrIndices, _numAttributes); + _attrIndices = Common::Array(attrIndices, _numAttributes); + delete[] attrIndices; + + for (int i = 0; i < _numAttributes; i++) + _attrMasks.push_back(dataStream->readUint16BE()); + + uint8 *attrShifts = new uint8[_numAttributes]; + dataStream->read(attrShifts, _numAttributes); + _attrShifts = Common::Array(attrShifts, _numAttributes); + delete[] attrShifts; + + uint8 *cmdArgCnts = new uint8[_numCommands]; + dataStream->read(cmdArgCnts, _numCommands); + _cmdArgCnts = Common::Array(cmdArgCnts, _numCommands); + delete[] cmdArgCnts; + + uint8 *commands = new uint8[_numCommands]; + dataStream->read(commands, _numCommands); + _commands = Common::Array(commands, _numCommands); + delete[] commands; +} } // End of namespace MacVenture diff --git a/engines/macventure/macventure.h b/engines/macventure/macventure.h index e78774db0c..7e53eb229d 100644 --- a/engines/macventure/macventure.h +++ b/engines/macventure/macventure.h @@ -91,25 +91,34 @@ enum FilePathID { }; -struct GlobalSettings { - uint16 numObjects; // number of game objects defined - uint16 numGlobals; // number of globals defined - uint16 numCommands; // number of commands defined - uint16 numAttributes; // number of attributes - uint16 numGroups; // number of object groups - uint16 invTop; // inventory window bounds - uint16 invLeft; - uint16 invHeight; - uint16 invWidth; - uint16 invOffsetY; // positioning offset for - uint16 invOffsetX; // new inventory windows - uint16 defaultFont; // default font - uint16 defaultSize; // default font size - uint8 *attrIndices; // attribute indices into attribute table - uint16 *attrMasks; // attribute masks - uint8 *attrShifts; // attribute bit shifts - uint8 *cmdArgCnts; // command argument counts - uint8 *commands; // command buttons +class GlobalSettings { +public: + GlobalSettings(); + ~GlobalSettings(); + + void loadSettings(Common::SeekableReadStream *dataStream); + +// HACK MAybe this should be private, but the class is only here to handle +// memory allocation/deallocation +public: + uint16 _numObjects; // number of game objects defined + uint16 _numGlobals; // number of globals defined + uint16 _numCommands; // number of commands defined + uint16 _numAttributes; // number of attributes + uint16 _numGroups; // number of object groups + uint16 _invTop; // inventory window bounds + uint16 _invLeft; + uint16 _invHeight; + uint16 _invWidth; + uint16 _invOffsetY; // positioning offset for + uint16 _invOffsetX; // new inventory windows + uint16 _defaultFont; // default font + uint16 _defaultSize; // default font size + Common::Array _attrIndices; // attribute indices into attribute table + Common::Array _attrMasks; // attribute masks + Common::Array _attrShifts; // attribute bit shifts + Common::Array _cmdArgCnts; // command argument counts + Common::Array _commands; // command buttons }; enum GameState { @@ -327,7 +336,7 @@ private: // Attributes // Engine state GameState _gameState; - GlobalSettings _globalSettings; + GlobalSettings *_globalSettings; HuffmanLists *_textHuffman; bool _oldTextEncoding; bool _paused, _halted, _cmdReady, _prepared; diff --git a/engines/macventure/world.cpp b/engines/macventure/world.cpp index 708ccfec33..20695dbff6 100644 --- a/engines/macventure/world.cpp +++ b/engines/macventure/world.cpp @@ -56,7 +56,7 @@ void World::startNewGame() { uint32 World::getObjAttr(ObjID objID, uint32 attrID) { uint res; - uint32 index = _engine->getGlobalSettings().attrIndices[attrID]; + uint32 index = _engine->getGlobalSettings()._attrIndices[attrID]; // HACK, but if I try to initialize it in the else clause, it goes out of scope and segfaults Common::SeekableReadStream *objStream = _objectConstants->getItem(objID); if (!(index & 0x80)) { // It's not a constant @@ -69,8 +69,8 @@ uint32 World::getObjAttr(ObjID objID, uint32 attrID) { res = objStream->readByte() << 8; res |= objStream->readByte(); } - res &= _engine->getGlobalSettings().attrMasks[attrID]; - res >>= _engine->getGlobalSettings().attrShifts[attrID]; + res &= _engine->getGlobalSettings()._attrMasks[attrID]; + res >>= _engine->getGlobalSettings()._attrShifts[attrID]; if (res & 0x8000) res = -((res ^ 0xffff) + 1); debugC(5, kMVDebugMain, "Attribute %x from object %x is %x", attrID, objID, res); @@ -88,11 +88,11 @@ void World::setObjAttr(ObjID objID, uint32 attrID, Attribute value) { if (attrID < kAttrOtherDoor) _engine->enqueueObject(kUpdateObject, objID); - uint32 idx = _engine->getGlobalSettings().attrIndices[attrID]; - value <<= _engine->getGlobalSettings().attrShifts[attrID]; - value &= _engine->getGlobalSettings().attrMasks[attrID]; + uint32 idx = _engine->getGlobalSettings()._attrIndices[attrID]; + value <<= _engine->getGlobalSettings()._attrShifts[attrID]; + value &= _engine->getGlobalSettings()._attrMasks[attrID]; Attribute oldVal = _saveGame->getAttr(objID, idx); - oldVal &= ~_engine->getGlobalSettings().attrMasks[attrID]; + oldVal &= ~_engine->getGlobalSettings()._attrMasks[attrID]; _saveGame->setAttr(idx, objID, (value | oldVal)); _engine->gameChanged(); } @@ -195,7 +195,7 @@ bool World::intersects(ObjID objID, Common::Rect rect) { void World::calculateObjectRelations() { _relations.clear(); ObjID val, next; - uint32 numObjs = _engine->getGlobalSettings().numObjects; + uint32 numObjs = _engine->getGlobalSettings()._numObjects; const AttributeGroup &parents = *_saveGame->getGroup(0); for (uint i = 0; i < numObjs * 2; i++) { _relations.push_back(0); @@ -305,9 +305,9 @@ void SaveGame::saveInto(Common::OutSaveFile *file) { void SaveGame::loadGroups(MacVentureEngine *engine, Common::SeekableReadStream * res) { GlobalSettings settings = engine->getGlobalSettings(); - for (int i = 0; i < settings.numGroups; ++i) { + for (int i = 0; i < settings._numGroups; ++i) { AttributeGroup g; - for (int j = 0; j < settings.numObjects; ++j) + for (int j = 0; j < settings._numObjects; ++j) g.push_back(res->readUint16BE()); _groups.push_back(g); @@ -316,7 +316,7 @@ void SaveGame::loadGroups(MacVentureEngine *engine, Common::SeekableReadStream * void SaveGame::loadGlobals(MacVentureEngine *engine, Common::SeekableReadStream * res) { GlobalSettings settings = engine->getGlobalSettings(); - for (int i = 0; i < settings.numGlobals; ++i) { + for (int i = 0; i < settings._numGlobals; ++i) { _globals.push_back(res->readUint16BE()); } } -- cgit v1.2.3