ZVISION: Store the Puzzles in _activePuzzles and _globalPuzzles on the heap

This allows List::push_back() to not cause a data copy
This commit is contained in:
richiesams 2013-08-20 20:50:14 -05:00
parent ad5756fa31
commit 35622827f8
3 changed files with 33 additions and 23 deletions

View File

@ -52,8 +52,8 @@ void ScriptManager::parseScrFile(const Common::String &fileName, bool isGlobal)
continue;
if (line.matchString("puzzle:*", true)) {
Puzzle puzzle;
sscanf(line.c_str(),"puzzle:%u",&(puzzle.key));
Puzzle *puzzle = new Puzzle();
sscanf(line.c_str(),"puzzle:%u",&(puzzle->key));
parsePuzzle(puzzle, file);
if (isGlobal) {
@ -67,17 +67,17 @@ void ScriptManager::parseScrFile(const Common::String &fileName, bool isGlobal)
}
}
void ScriptManager::parsePuzzle(Puzzle &puzzle, Common::SeekableReadStream &stream) {
void ScriptManager::parsePuzzle(Puzzle *puzzle, Common::SeekableReadStream &stream) {
Common::String line = stream.readLine();
trimCommentsAndWhiteSpace(&line);
while (!stream.eos() && !line.contains('}')) {
if (line.matchString("criteria {", true)) {
parseCriteria(stream, puzzle.criteriaList);
parseCriteria(stream, puzzle->criteriaList);
} else if (line.matchString("results {", true)) {
parseResults(stream, puzzle.resultActions);
parseResults(stream, puzzle->resultActions);
} else if (line.matchString("flags {", true)) {
puzzle.flags = parseFlags(stream);
puzzle->flags = parseFlags(stream);
}
line = stream.readLine();

View File

@ -41,6 +41,15 @@ ScriptManager::ScriptManager(ZVision *engine)
_changeLocation(false) {
}
ScriptManager::~ScriptManager() {
for (Common::List<Puzzle *>::iterator iter = _activePuzzles.begin(); iter != _activePuzzles.end(); iter++) {
delete (*iter);
}
for (Common::List<Puzzle *>::iterator iter = _globalPuzzles.begin(); iter != _globalPuzzles.end(); iter++) {
delete (*iter);
}
}
void ScriptManager::initialize() {
parseScrFile("universe.scr", true);
changeLocation('g', 'a', 'r', 'y', 0);
@ -58,11 +67,11 @@ void ScriptManager::update(uint deltaTimeMillis) {
void ScriptManager::createReferenceTable() {
// Iterate through each local Puzzle
for (Common::List<Puzzle>::iterator activePuzzleIter = _activePuzzles.begin(); activePuzzleIter != _activePuzzles.end(); activePuzzleIter++) {
Puzzle *puzzlePtr = &(*activePuzzleIter);
for (Common::List<Puzzle *>::iterator activePuzzleIter = _activePuzzles.begin(); activePuzzleIter != _activePuzzles.end(); activePuzzleIter++) {
Puzzle *puzzlePtr = (*activePuzzleIter);
// Iterate through each CriteriaEntry and add a reference from the criteria key to the Puzzle
for (Common::List<Common::List<Puzzle::CriteriaEntry> >::iterator criteriaIter = activePuzzleIter->criteriaList.begin(); criteriaIter != (*activePuzzleIter).criteriaList.end(); criteriaIter++) {
for (Common::List<Common::List<Puzzle::CriteriaEntry> >::iterator criteriaIter = (*activePuzzleIter)->criteriaList.begin(); criteriaIter != (*activePuzzleIter)->criteriaList.end(); criteriaIter++) {
for (Common::List<Puzzle::CriteriaEntry>::iterator entryIter = (*criteriaIter).begin(); entryIter != (*criteriaIter).end(); entryIter++) {
_referenceTable[entryIter->key].push_back(puzzlePtr);
@ -75,11 +84,11 @@ void ScriptManager::createReferenceTable() {
}
// Iterate through each global Puzzle
for (Common::List<Puzzle>::iterator globalPuzzleIter = _globalPuzzles.begin(); globalPuzzleIter != _globalPuzzles.end(); globalPuzzleIter++) {
Puzzle *puzzlePtr = &(*globalPuzzleIter);
for (Common::List<Puzzle *>::iterator globalPuzzleIter = _globalPuzzles.begin(); globalPuzzleIter != _globalPuzzles.end(); globalPuzzleIter++) {
Puzzle *puzzlePtr = (*globalPuzzleIter);
// Iterate through each CriteriaEntry and add a reference from the criteria key to the Puzzle
for (Common::List<Common::List<Puzzle::CriteriaEntry> >::iterator criteriaIter = globalPuzzleIter->criteriaList.begin(); criteriaIter != (*globalPuzzleIter).criteriaList.end(); criteriaIter++) {
for (Common::List<Common::List<Puzzle::CriteriaEntry> >::iterator criteriaIter = (*globalPuzzleIter)->criteriaList.begin(); criteriaIter != (*globalPuzzleIter)->criteriaList.end(); criteriaIter++) {
for (Common::List<Puzzle::CriteriaEntry>::iterator entryIter = (*criteriaIter).begin(); entryIter != (*criteriaIter).end(); entryIter++) {
_referenceTable[entryIter->key].push_back(puzzlePtr);
@ -263,23 +272,23 @@ void ScriptManager::changeLocationIntern() {
}
// Add all the local puzzles to the queue to be checked
for (Common::List<Puzzle>::iterator iter = _activePuzzles.begin(); iter != _activePuzzles.end(); iter++) {
for (Common::List<Puzzle *>::iterator iter = _activePuzzles.begin(); iter != _activePuzzles.end(); iter++) {
// Reset any Puzzles that have the flag ONCE_PER_INST
if (((*iter).flags & Puzzle::ONCE_PER_INST) == Puzzle::ONCE_PER_INST) {
setStateValue((*iter).key, 0);
if (((*iter)->flags & Puzzle::ONCE_PER_INST) == Puzzle::ONCE_PER_INST) {
setStateValue((*iter)->key, 0);
}
_puzzlesToCheck.push(&(*iter));
_puzzlesToCheck.push((*iter));
}
// Add all the global puzzles to the queue to be checked
for (Common::List<Puzzle>::iterator iter = _globalPuzzles.begin(); iter != _globalPuzzles.end(); iter++) {
for (Common::List<Puzzle *>::iterator iter = _globalPuzzles.begin(); iter != _globalPuzzles.end(); iter++) {
// Reset any Puzzles that have the flag ONCE_PER_INST
if (((*iter).flags & Puzzle::ONCE_PER_INST) == Puzzle::ONCE_PER_INST) {
setStateValue((*iter).key, 0);
if (((*iter)->flags & Puzzle::ONCE_PER_INST) == Puzzle::ONCE_PER_INST) {
setStateValue((*iter)->key, 0);
}
_puzzlesToCheck.push(&(*iter));
_puzzlesToCheck.push((*iter));
}
// Create the puzzle reference table

View File

@ -50,6 +50,7 @@ struct Location {
class ScriptManager {
public:
ScriptManager(ZVision *engine);
~ScriptManager();
private:
ZVision *_engine;
@ -66,9 +67,9 @@ private:
/** Holds the Puzzles that should be checked this frame */
Common::Queue<Puzzle *> _puzzlesToCheck;
/** Holds the currently active puzzles */
Common::List<Puzzle> _activePuzzles;
Common::List<Puzzle *> _activePuzzles;
/** Holds the global puzzles */
Common::List<Puzzle>_globalPuzzles;
Common::List<Puzzle *>_globalPuzzles;
/** Holds the currently active controls */
Common::HashMap<uint32, Control *> _activeControls;
@ -112,7 +113,7 @@ private:
* @param puzzle The object to store what is parsed
* @param stream Scr file stream
*/
void parsePuzzle(Puzzle &puzzle, Common::SeekableReadStream &stream);
void parsePuzzle(Puzzle *puzzle, Common::SeekableReadStream &stream);
/**
* Parses the stream into a Criteria object