From 7e310422c3d704a5d7faa5219082be827c71e534 Mon Sep 17 00:00:00 2001 From: Nicola Mettifogo Date: Sun, 18 Mar 2007 20:18:19 +0000 Subject: - added constructors for most structs - structs are now allocated via new instead of malloc's - respective free's have been replaced with delete svn-id: r26228 --- engines/parallaction/animation.cpp | 17 ++--- engines/parallaction/commands.cpp | 8 +-- engines/parallaction/commands.h | 10 +++ engines/parallaction/defs.h | 11 +++ engines/parallaction/dialogue.cpp | 13 ++-- engines/parallaction/location.cpp | 4 +- engines/parallaction/parallaction.cpp | 4 +- engines/parallaction/walk.cpp | 1 + engines/parallaction/zone.cpp | 35 ++++------ engines/parallaction/zone.h | 125 +++++++++++++++++++++++++++++++--- 10 files changed, 174 insertions(+), 54 deletions(-) (limited to 'engines') diff --git a/engines/parallaction/animation.cpp b/engines/parallaction/animation.cpp index 0805917698..701abc9065 100644 --- a/engines/parallaction/animation.cpp +++ b/engines/parallaction/animation.cpp @@ -77,8 +77,7 @@ Animation *Parallaction::findAnimation(const char *name) { Animation *Parallaction::parseAnimation(Script& script, Node *list, char *name) { // printf("parseAnimation(%s)\n", name); - Animation *vD0 = (Animation*)malloc(sizeof(Animation)); - memset(vD0, 0, sizeof(Animation)); + Animation *vD0 = new Animation; vD0->_zone._label._text = (char*)malloc(strlen(name)+1); strcpy(vD0->_zone._label._text, name); @@ -158,7 +157,7 @@ void freeScript(Program *program) { if (!program) return; - free(program->_locals); + delete[] program->_locals; freeNodeList(program); return; @@ -258,13 +257,12 @@ void Parallaction::loadProgram(Animation *a, char *filename) { fillBuffers(*script); - a->_program = (Program*)malloc(sizeof(Program)); - memset(a->_program, 0, sizeof(Program)); - a->_program->_locals = (LocalVariable*)malloc(sizeof(LocalVariable)*10); + a->_program = new Program; + + a->_program->_locals = new LocalVariable[10]; Node *vD0 = a->_program; - Instruction *vCC = (Instruction*)malloc(sizeof(Instruction)); - memset(vCC, 0, sizeof(Instruction)); + Instruction *vCC = new Instruction; while (scumm_stricmp(_tokens[0], "endscript")) { @@ -272,8 +270,7 @@ void Parallaction::loadProgram(Animation *a, char *filename) { addNode(vD0, vCC); vD0 = vCC; - vCC = (Instruction*)malloc(sizeof(Instruction)); - memset(vCC, 0, sizeof(Instruction)); + vCC = new Instruction; fillBuffers(*script); } diff --git a/engines/parallaction/commands.cpp b/engines/parallaction/commands.cpp index a6bbebc081..5e50912bee 100644 --- a/engines/parallaction/commands.cpp +++ b/engines/parallaction/commands.cpp @@ -56,8 +56,7 @@ Command *parseCommands(Script &script) { while (scumm_stricmp(_tokens[0], "ENDCOMMANDS") && scumm_stricmp(_tokens[0], "ENDZONE")) { // printf("token[0] = %s", _tokens[0]); - Command *cmd = (Command*)malloc(sizeof(Command)); - memset(cmd, 0, sizeof(Command)); + Command *cmd = new Command; cmd->_id = _vm->searchTable(_tokens[0], commands_names); uint16 _si = 1; @@ -200,8 +199,8 @@ void freeCommands(Command *list) { while (cmd) { Command *v4 = (Command*)cmd->_next; - if (cmd->_id == 6) free(cmd->u._zone); // open - free(cmd); + if (cmd->_id == 6) delete cmd->u._zone; // open + delete cmd; cmd = v4; } @@ -350,3 +349,4 @@ void runCommands(Command *list, Zone *z) { } // namespace Parallaction + diff --git a/engines/parallaction/commands.h b/engines/parallaction/commands.h index 046f413469..40c5c1b626 100644 --- a/engines/parallaction/commands.h +++ b/engines/parallaction/commands.h @@ -34,6 +34,7 @@ enum CommandFlags { kFlagsGlobal = 0x40000000 }; +// TODO: turn this into a struct union CommandData { uint32 _flags; Animation * _animation; @@ -45,6 +46,10 @@ union CommandData { int16 _x; int16 _y; } _move; + + CommandData() { + _flags = 0; + } }; struct Command : public Node { @@ -53,6 +58,11 @@ struct Command : public Node { uint32 _flagsOn; uint32 _flagsOff; + Command() { + _id = 0; + _flagsOn = 0; + _flagsOff = 0; + } }; } // namespace Parallaction diff --git a/engines/parallaction/defs.h b/engines/parallaction/defs.h index 8757b2c5f6..d1b5730a40 100644 --- a/engines/parallaction/defs.h +++ b/engines/parallaction/defs.h @@ -74,6 +74,11 @@ struct StaticCnv { uint16 _height; // byte* _data0; // bitmap byte* _data1; // unused + + StaticCnv() { + _width = _height = 0; + _data0 = _data1 = NULL; + } }; @@ -85,6 +90,11 @@ struct Cnv { byte** _array; // frames data public: + Cnv() { + _width = _height = _count = 0; + _array = NULL; + } + byte* getFramePtr(uint16 index) { if (index >= _count) error("frame %i does not exist", index); @@ -125,3 +135,4 @@ enum { #endif + diff --git a/engines/parallaction/dialogue.cpp b/engines/parallaction/dialogue.cpp index 73567f3ca1..105212eacc 100644 --- a/engines/parallaction/dialogue.cpp +++ b/engines/parallaction/dialogue.cpp @@ -76,9 +76,8 @@ Dialogue *Parallaction::parseDialogue(Script &script) { while (scumm_stricmp(_tokens[0], "enddialogue")) { if (scumm_stricmp(_tokens[0], "Question")) continue; - _questions[num_questions] = (Dialogue*)malloc(sizeof(Dialogue)); + _questions[num_questions] = new Dialogue; Dialogue *vB4 = _questions[num_questions]; - memset(_questions[num_questions], 0, sizeof(Dialogue)); _questions_names[num_questions] = (char*)malloc(strlen(_tokens[1])+1); strcpy(_questions_names[num_questions], _tokens[1]); @@ -202,18 +201,18 @@ void freeDialogue(Dialogue *d) { if (!d) return; uint16 _si; - for (_si = 0; _si < 5; _si++) { + for (_si = 0; _si < NUM_ANSWERS; _si++) { if (d->_answer_moods[_si] & 0x10) freeDialogue(d->_following._questions[_si]); } - for (_si = 0; _si < 5; _si++) { + for (_si = 0; _si < NUM_ANSWERS; _si++) { freeCommands(d->_commands[_si]); free(d->_answers[_si]); } free(d->_text); - free(d); + delete d; return; } @@ -305,7 +304,7 @@ bool displayAnswers(Dialogue *q) { uint16 i = 0; - while (i < 5 && q->_answers[i]) { + while (i < NUM_ANSWERS && q->_answers[i]) { if (displayAnswer(q, i)) { displayed = true; } else { @@ -514,7 +513,7 @@ int16 getHoverAnswer(int16 x, int16 y, Question *q) { int16 top = 1000; int16 bottom = 1000; - for (int16 _si = 0; _si < 5; _si++) { + for (int16 _si = 0; _si < NUM_ANSWERS; _si++) { if (q->_answers[_si] == NULL) break; if (_answerBalloonY[_si] != SKIPPED_ANSWER) { diff --git a/engines/parallaction/location.cpp b/engines/parallaction/location.cpp index be8d5902e7..3b98cf2b32 100644 --- a/engines/parallaction/location.cpp +++ b/engines/parallaction/location.cpp @@ -38,7 +38,7 @@ void parseWalkNodes(Script &script, Node *list); void Parallaction::parseLocation(const char *filename) { -// printf("parseLocation(%s)", filename); + printf("parseLocation(%s)", filename); debugC(1, kDebugLocation, "parseLocation('%s')", filename); uint16 _si = 1; @@ -49,7 +49,7 @@ void Parallaction::parseLocation(const char *filename) { fillBuffers(*_locationScript, true); while (scumm_stricmp(_tokens[0], "ENDLOCATION")) { -// printf("token[0] = %s", _tokens[0]); + printf("token[0] = %s", _tokens[0]); if (!scumm_stricmp(_tokens[0], "LOCATION")) { // The parameter for location is 'location.mask'. diff --git a/engines/parallaction/parallaction.cpp b/engines/parallaction/parallaction.cpp index a73f48598b..5b8a6ff0e1 100644 --- a/engines/parallaction/parallaction.cpp +++ b/engines/parallaction/parallaction.cpp @@ -876,7 +876,7 @@ void removeNode(Node *n) { Job *Parallaction::addJob(JobFn fn, void *parm, uint16 tag) { - Job *v8 = (Job*)malloc(sizeof(Job)); + Job *v8 = new Job; v8->_parm = parm; v8->_fn = fn; @@ -897,7 +897,7 @@ Job *Parallaction::addJob(JobFn fn, void *parm, uint16 tag) { void Parallaction::removeJob(Job *j) { removeNode(j); - free(j); + delete j; return; } diff --git a/engines/parallaction/walk.cpp b/engines/parallaction/walk.cpp index 2c18b85b6b..ef17775327 100644 --- a/engines/parallaction/walk.cpp +++ b/engines/parallaction/walk.cpp @@ -464,3 +464,4 @@ void initWalk() { } // namespace Parallaction + diff --git a/engines/parallaction/zone.cpp b/engines/parallaction/zone.cpp index 8790d07eda..b49463c9e8 100644 --- a/engines/parallaction/zone.cpp +++ b/engines/parallaction/zone.cpp @@ -59,8 +59,7 @@ void Parallaction::parseZone(Script &script, Node *list, char *name) { return; } - Zone *z = (Zone*)malloc(sizeof(Zone)); - memset(z, 0, sizeof(Zone)); + Zone *z = new Zone; z->_label._text = (char*)malloc(strlen(name)+1); strcpy(z->_label._text, name); @@ -147,33 +146,33 @@ void Parallaction::freeZones(Node *list) { case kZoneExamine: free(z->u.examine->_filename); free(z->u.examine->_description); - free(z->u.examine); + delete z->u.examine; break; case kZoneDoor: free(z->u.door->_location); free(z->u.door->_background); _vm->_gfx->freeCnv(&z->u.door->_cnv); - free(z->u.door); + delete z->u.door; break; case kZoneSpeak: freeDialogue(z->u.speak->_dialogue); - free(z->u.speak); + delete z->u.speak; break; case kZoneGet: free(z->u.get->_backup); _vm->_gfx->freeStaticCnv(&z->u.get->_cnv); - free(z->u.get); + delete z->u.get; break; case kZoneHear: - free(z->u.hear); + delete z->u.hear; break; case kZoneMerge: - free(z->u.merge); + delete z->u.merge; break; default: @@ -185,6 +184,8 @@ void Parallaction::freeZones(Node *list) { _vm->_gfx->freeStaticCnv(&z->_label._cnv); freeCommands(z->_commands); + // TODO: delete Zone + z=(Zone*)z->_next; } @@ -207,33 +208,27 @@ void Parallaction::parseZoneTypeBlock(Script &script, Zone *z) { switch (z->_type & 0xFFFF) { case kZoneExamine: // examine Zone alloc - u->examine = (ExamineData*)malloc(sizeof(ExamineData)); - memset(u->examine, 0, sizeof(ExamineData)); + u->examine = new ExamineData; break; case kZoneDoor: // door Zone alloc - u->door = (DoorData*)malloc(sizeof(DoorData)); - memset(u->door, 0, sizeof(DoorData)); + u->door = new DoorData; break; case kZoneGet: // get Zone alloc - u->get = (GetData*)malloc(sizeof(GetData)); - memset(u->get, 0, sizeof(GetData)); + u->get = new GetData; break; case kZoneMerge: // merge Zone alloc - u->merge = (MergeData*)malloc(sizeof(MergeData)); - memset(u->merge, 0, sizeof(MergeData)); + u->merge = new MergeData; break; case kZoneHear: // hear Zone alloc - u->hear = (HearData*)malloc(sizeof(HearData)); - memset(u->hear, 0, sizeof(HearData)); + u->hear = new HearData; break; case kZoneSpeak: // speak Zone alloc - u->speak = (SpeakData*)malloc(sizeof(SpeakData)); - memset(u->speak, 0, sizeof(SpeakData)); + u->speak = new SpeakData; break; } diff --git a/engines/parallaction/zone.h b/engines/parallaction/zone.h index 775bf0bca8..31ac168096 100644 --- a/engines/parallaction/zone.h +++ b/engines/parallaction/zone.h @@ -59,20 +59,34 @@ enum ZoneFlags { }; - +#define NUM_ANSWERS 5 struct Question { char* _text; - char* _answers[5]; + char* _answers[NUM_ANSWERS]; uint16 _mood; - uint16 _answer_moods[5]; + uint16 _answer_moods[NUM_ANSWERS]; union { - Question* _questions[5]; - char* _names[5]; + Question* _questions[NUM_ANSWERS]; + char* _names[NUM_ANSWERS]; } _following; - Command* _commands[5]; - uint32 _noFlags[5]; - uint32 _yesFlags[5]; + Command* _commands[NUM_ANSWERS]; + uint32 _noFlags[NUM_ANSWERS]; + uint32 _yesFlags[NUM_ANSWERS]; + + Question() { + _text = NULL; + _mood = 0; + + for (uint32 i = 0; i < NUM_ANSWERS; i++) { + _answers[i] = NULL; + _answer_moods[i] = 0; + _following._questions[i] = NULL; + _commands[i] = NULL; + _noFlags[i] = 0; + _yesFlags[i] = 0; + } + } }; struct GetData { // size = 24 @@ -81,10 +95,20 @@ struct GetData { // size = 24 byte *_backup; uint16 field_14; // unused uint16 field_16; // unused + + GetData() { + _icon = 0; + _backup = NULL; + } }; struct SpeakData { // size = 36 char _name[32]; Dialogue *_dialogue; + + SpeakData() { + _name[0] = '\0'; + _dialogue = NULL; + } }; struct ExamineData { // size = 28 StaticCnv _cnv; @@ -92,6 +116,12 @@ struct ExamineData { // size = 28 uint16 field_12; // unused char* _description; char* _filename; + + ExamineData() { + _opBase = 0; + _description = NULL; + _filename = NULL; + } }; struct DoorData { // size = 28 char* _location; @@ -99,14 +129,28 @@ struct DoorData { // size = 28 byte* _background; Common::Point _startPos; uint16 _startFrame; + + DoorData() { + _location = NULL; + _background = NULL; + _startFrame = 0; + } }; struct HearData { // size = 20 char _name[20]; + + HearData() { + _name[0] = '\0'; + } }; struct MergeData { // size = 12 uint32 _obj1; uint32 _obj2; uint32 _obj3; + + MergeData() { + _obj1 = _obj2 = _obj3 = 0; + } }; struct TypeData { @@ -116,11 +160,24 @@ struct TypeData { DoorData *door; HearData *hear; MergeData *merge; + + TypeData() { + get = NULL; + speak = NULL; + examine = NULL; + door = NULL; + hear = NULL; + merge = NULL; + } }; struct Label { char* _text; StaticCnv _cnv; + + Label() { + _text = NULL; + } }; struct Zone : public Node { @@ -141,6 +198,15 @@ struct Zone : public Node { Command *_commands; Common::Point _moveTo; + Zone() { + _left = _top = _right = _bottom = 0; + _oldLeft = _oldTop = 0; + + _type = 0; + _flags = 0; + _commands = NULL; + } + void getRect(Common::Rect& r) const { r.left = _left; r.right = _right; @@ -168,12 +234,22 @@ struct LocalVariable { int16 _value; int16 _min; int16 _max; + + LocalVariable() { + _value = 0; + _min = -10000; + _max = 10000; + } }; union LValue { int16 _value; int16* _pvalue; LocalVariable* _local; + + LValue() { + _local = NULL; + } }; enum InstructionFlags { @@ -186,7 +262,7 @@ enum InstructionFlags { struct Instruction : public Node { uint32 _index; uint32 _flags; - union { + struct { Animation *_a; Zone *_z; uint32 _index; @@ -194,6 +270,12 @@ struct Instruction : public Node { } _opBase; LValue _opA; LValue _opB; + + Instruction() { + _index = 0; + _flags = 0; + _opBase._a = NULL; + } }; @@ -202,6 +284,13 @@ struct Program : public Node { uint16 _loopCounter; Instruction *_ip; Instruction *_loopStart; + + Program() { + _locals = NULL; + _loopCounter = 0; + _ip = NULL; + _loopStart = NULL; + } }; @@ -220,6 +309,24 @@ struct Animation { uint16 field_5C; // unused uint16 field_5E; // unused + Animation() { + + // FIXME: temporary hack until Animation become a subclass of Zone + _zone._left = _zone._top = _zone._right = _zone._bottom = 0; + _zone._oldLeft = _zone._oldTop = 0; + + _zone._type = 0; + _zone._flags = 0; + _zone._commands = NULL; + + + + + _program = NULL; + _frame = 0; + _z = 0; + } + uint16 width() const { return _cnv._width; } -- cgit v1.2.3