From f0e69a7d0bd5881bf574c4bcbed159c4f7addecd Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Thu, 28 May 2009 08:49:56 +0000 Subject: Replaced the original implementation of walk box handling with a cleaner, structure array based one svn-id: r40954 --- engines/cruise/actor.cpp | 55 ++++++++++++++----------------------- engines/cruise/cruise.cpp | 8 +++++- engines/cruise/cruise.h | 1 + engines/cruise/ctp.cpp | 67 ++++++++++++++------------------------------- engines/cruise/ctp.h | 30 ++++++++++++++++---- engines/cruise/perso.cpp | 4 ++- engines/cruise/saveload.cpp | 2 ++ 7 files changed, 77 insertions(+), 90 deletions(-) (limited to 'engines/cruise') diff --git a/engines/cruise/actor.cpp b/engines/cruise/actor.cpp index 535620c7df..fcc89d15a2 100644 --- a/engines/cruise/actor.cpp +++ b/engines/cruise/actor.cpp @@ -73,37 +73,22 @@ int nclick_noeud; int flag_aff_chemin; void getPixel(int x, int y) { - int x_min, x_max, y_min, y_max; - int16* polygone = (int16*)polyStruct0; - int16* next; + for (uint i = 0; i < polyStructs->size(); ++i) { + CtStruct &ct = (*polyStructs)[i]; + numPoly = ct.num; - while ((next = *(int16**)polygone) != (int16 *) - 1) { - polygone += sizeof(uint16*); - - int16* tableau = polygone + 2; - - x_min = *tableau++; - x_max = *tableau++; - y_min = *tableau++; - y_max = *tableau++; - - numPoly = *polygone++; - - if (walkboxState[numPoly] == 0 && ((x >= x_min && x <= x_max) && (y >= y_min && y <= y_max))) { + if (walkboxState[numPoly] == 0 && ct.bounds.contains(x, y)) { // click was in given box - int u = y - y_min; - tableau += u * 2; - x_min = *tableau++; - x_max = *tableau++; + int u = y - ct.bounds.top; + CtEntry &cte = ct.slices[u]; - if ((x >= x_min && x <= x_max)) { + if ((x >= cte.minX && x <= cte.maxX)) { flag_obstacle = walkboxColor[numPoly]; return; } } - polygone = next; } flag_obstacle = 0; @@ -188,7 +173,7 @@ void polydroite(int x1, int y1, int x2, int y2) { return; } - while (--cx) { + while (--cx >= 0) { if (dx > 0) { ax += mD0; bx += mD1; @@ -279,7 +264,7 @@ void poly2(int x1, int y1, int x2, int y2) { return; } - while (--cx) { + while (--cx >= 0) { if (dx > 0) { ax += mD0; bx += mD1; @@ -308,7 +293,7 @@ int point_proche(int16 table[][2]) { int x1, y1, i, x, y, p; int d1 = 1000; - polyStruct0 = polyStructNorm; + polyStructs = &polyStructNorm; if (nclick_noeud == 1) { x = x_mouse; @@ -316,19 +301,19 @@ int point_proche(int16 table[][2]) { x1 = table_ptselect[0][0]; y1 = table_ptselect[0][1]; - polyStruct0 = polyStructExp; + polyStructs = &polyStructExp; getPixel(x, y); if (!flag_obstacle) { - polyStruct0 = polyStructNorm; + polyStructs = &polyStructNorm; getPixel(x, y); if (flag_obstacle) { polydroite(x1, y1, x, y); } - polyStruct0 = polyStructExp; + polyStructs = &polyStructExp; } if (!flag_obstacle) { /* dans flag_obstacle --> couleur du point */ x1 = table_ptselect[0][0]; @@ -340,7 +325,7 @@ int point_proche(int16 table[][2]) { y_mouse = Y; } } - polyStruct0 = polyStructNorm; + polyStructs = &polyStructNorm; p = -1; for (i = 0; i < ctp_routeCoordCount; i++) { @@ -468,7 +453,7 @@ void valide_noeud(int16 table[], int16 p, int *nclick, int16 solution0[20 + 3][2 table_ptselect[*nclick][0] = x_mouse; table_ptselect[*nclick][1] = y_mouse; (*nclick)++; - polyStruct0 = polyStructNorm; + polyStructs = &polyStructNorm; if (*nclick == 2) { // second point x1 = table_ptselect[0][0]; @@ -479,7 +464,7 @@ void valide_noeud(int16 table[], int16 p, int *nclick, int16 solution0[20 + 3][2 return; } flag_aff_chemin = 1; - polyStruct0 = polyStructExp; + polyStructs = &polyStructExp; // can we go there directly ? polydroite(x1, y1, x2, y2); @@ -487,7 +472,7 @@ void valide_noeud(int16 table[], int16 p, int *nclick, int16 solution0[20 + 3][2 if (!flag_obstacle) { solution0[0][0] = x1; solution0[0][1] = y1; - polyStruct0 = polyStructExp; + polyStructs = &polyStructExp; poly2(x2, y2, ctp_routeCoords[select_noeud[1]][0], ctp_routeCoords[select_noeud[1]][1]); @@ -531,7 +516,7 @@ void valide_noeud(int16 table[], int16 p, int *nclick, int16 solution0[20 + 3][2 solution0[++i][1] = ctp_routeCoords[p1][1]; } - polyStruct0 = polyStructExp; + polyStructs = &polyStructExp; poly2(x2, y2, ctp_routeCoords[select_noeud[1]][0], ctp_routeCoords[select_noeud[1]][1]); @@ -556,7 +541,7 @@ void valide_noeud(int16 table[], int16 p, int *nclick, int16 solution0[20 + 3][2 while (flag_obstacle && i != d) { x2 = solution0[i][0]; y2 = solution0[i][1]; - polyStruct0 = polyStructExp; + polyStructs = &polyStructExp; polydroite(x1, y1, x2, y2); i--; } @@ -636,7 +621,7 @@ int16 computePathfinding(MovementEntry &moveInfo, int16 x, int16 y, int16 destX, } nclick_noeud = 0; - polyStruct0 = polyStructNorm; + polyStructs = &polyStructNorm; flag_aff_chemin = 0; if (x == destX && y == destY) { diff --git a/engines/cruise/cruise.cpp b/engines/cruise/cruise.cpp index 1edd48dbed..2ce5d55175 100644 --- a/engines/cruise/cruise.cpp +++ b/engines/cruise/cruise.cpp @@ -102,12 +102,13 @@ Common::Error CruiseEngine::run() { mainLoop(); + deinitialise(); + return Common::kNoError; } void CruiseEngine::initialize() { PCFadeFlag = 0; - workBuffer = (uint8 *) mallocAndZero(8192); /*volVar1 = 0; * fileData1 = 0; */ @@ -136,6 +137,11 @@ void CruiseEngine::initialize() { _driver->property(MidiDriver::PROP_CHANNEL_MASK, 0x03FE); } +void CruiseEngine::deinitialise() { + polyStructNorm.clear(); + polyStructExp.clear(); +} + bool CruiseEngine::loadLanguageStrings() { Common::File f; diff --git a/engines/cruise/cruise.h b/engines/cruise/cruise.h index 18dfba7ccc..491a833788 100644 --- a/engines/cruise/cruise.h +++ b/engines/cruise/cruise.h @@ -65,6 +65,7 @@ private: uint32 lastTick, lastTickDebug; void initialize(void); + void deinitialise(void); bool loadLanguageStrings(); bool makeLoad(char *saveName); void mainLoop(); diff --git a/engines/cruise/ctp.cpp b/engines/cruise/ctp.cpp index e40df8ad0f..4c566f57cf 100644 --- a/engines/cruise/ctp.cpp +++ b/engines/cruise/ctp.cpp @@ -28,14 +28,12 @@ namespace Cruise { -uint8 *workBuffer; -uint8 *polyStruct; -uint8 *adrStructPoly; -uint8 *polyStructNorm; -uint8 *polyStructExp; - uint8 *ctpVar17; -uint8 *polyStruct0; + +Common::Array polyStructNorm; +Common::Array polyStructExp; +Common::Array *polyStructs = NULL; +Common::Array *polyStruct = NULL; int currentWalkBoxCenterX; int currentWalkBoxCenterY; @@ -135,7 +133,7 @@ void renderCTPWalkBox(int16 *walkboxData, int hotPointX, int hotPointY, int X, i } // this process the walkboxes -void makeCtStruct(uint8* str, int16 table[][40], int num, int z) { +void makeCtStruct(Common::Array &lst, int16 table[][40], int num, int z) { int minX = 1000; int maxX = -1; @@ -149,11 +147,8 @@ void makeCtStruct(uint8* str, int16 table[][40], int num, int z) { renderCTPWalkBox(&table[num][0], currentWalkBoxCenterX, currentWalkBoxCenterY, currentWalkBoxCenterX, currentWalkBoxCenterY, z + 0x200); - int16* a1; - int16* a2; - - a1 = a2 = (int16*)str; - a2 += sizeof(int16*) / sizeof(int16) + 6; // skip header + lst.push_back(CtStruct()); + CtStruct &ct = lst[lst.size() - 1]; int16* XArray = XMIN_XMAX; int minY = *XArray++; @@ -170,23 +165,16 @@ void makeCtStruct(uint8* str, int16 table[][40], int num, int z) { if (x2 > maxX) maxX = x2; - *a2++ = x1; - *a2++ = x2; + ct.slices.push_back(CtEntry(x1, x2)); i++; } - *(int16**)a1 = a2; - - adrStructPoly = (uint8*)a2; - *(uint16**)a2 = (uint16*) - 1; //chained list terminator - - a1 += sizeof(int16*); - *a1++ = num; - *a1++ = walkboxColor[num]; - *a1++ = minX; - *a1++ = maxX; - *a1++ = minY; - *a1++ = minY + i + 2; + ct.num = num; + ct.colour = walkboxColor[num]; + ct.bounds.left = minX; + ct.bounds.right = maxX; + ct.bounds.top = minY; + ct.bounds.bottom = minY + i; } int getNode(int nodeResult[2], int nodeId) { @@ -332,34 +320,19 @@ int initCt(const char *ctpName) { computeAllDistance(distanceTable, ctp_routeCoordCount); // process path-finding stuff - polyStruct = polyStructNorm = adrStructPoly = workBuffer; - - ptr = (uint8 *) polyStruct; - - for (int i = numberOfWalkboxes - 1; i >= 0; i--) { - makeCtStruct(adrStructPoly, ctp_walkboxTable, i, 0); - } - - polyStructExp = adrStructPoly += sizeof(int16 *); - - for (int i = numberOfWalkboxes - 1; i >= 0; i--) { - makeCtStruct(adrStructPoly, ctp_walkboxTable, i, walkboxZoom[i] * 20); - } - - int ctSize = (adrStructPoly - ptr) + sizeof(int16 *); // for now, the +sizeof(int16 *) is a safe zone - adrStructPoly = polyStructNorm = polyStruct = (uint8 *) malloc(ctSize); + // Load the polyStructNorm list for (int i = numberOfWalkboxes - 1; i >= 0; i--) { - makeCtStruct(adrStructPoly, ctp_walkboxTable, i, 0); + makeCtStruct(polyStructNorm, ctp_walkboxTable, i, 0); } - polyStructExp = adrStructPoly += sizeof(int16 *); + // Load the polyStructExp list for (int i = numberOfWalkboxes - 1; i >= 0; i--) { - makeCtStruct(adrStructPoly, ctp_walkboxTable, i, walkboxZoom[i] * 20); + makeCtStruct(polyStructExp, ctp_walkboxTable, i, walkboxZoom[i] * 20); } - polyStruct0 = polyStructNorm; + polyStruct = polyStructs = &polyStructNorm; return (1); } diff --git a/engines/cruise/ctp.h b/engines/cruise/ctp.h index f2ce4c86c5..6cc5ea48ef 100644 --- a/engines/cruise/ctp.h +++ b/engines/cruise/ctp.h @@ -26,6 +26,8 @@ #ifndef CRUISE_CTP_H #define CRUISE_CTP_H +#include "common/rect.h" + namespace Cruise { struct ctpVar19SubStruct { @@ -42,14 +44,30 @@ struct ctpVar19Struct { ctpVar19SubStruct subStruct; }; -extern uint8 *workBuffer; -extern uint8 *polyStruct; -extern uint8 *adrStructPoly; -extern uint8 *polyStructNorm; -extern uint8 *polyStructExp; +class CtEntry { +public: + CtEntry(int16 xs, int16 xe) { minX = xs; maxX = xe; } + CtEntry() { minX = 0; maxX = 0; } + + int16 minX; + int16 maxX; +}; + +class CtStruct { +public: + CtStruct *next; + int16 num; + int16 colour; + Common::Rect bounds; + Common::Array slices; +}; extern uint8 *ctpVar17; -extern uint8 *polyStruct0; + +extern Common::Array polyStructNorm; +extern Common::Array polyStructExp; +extern Common::Array *polyStructs; +extern Common::Array *polyStruct; int initCt(const char * ctpName); int computeDistance(int varX, int varY, int paramX, int paramY); diff --git a/engines/cruise/perso.cpp b/engines/cruise/perso.cpp index cd61c596f7..8bb4164d85 100644 --- a/engines/cruise/perso.cpp +++ b/engines/cruise/perso.cpp @@ -46,7 +46,9 @@ void freeCTP(void) { } if (polyStruct) { - free(polyStruct); + polyStructNorm.clear(); + polyStructExp.clear(); + polyStruct = NULL; } ctpVar17 = NULL; diff --git a/engines/cruise/saveload.cpp b/engines/cruise/saveload.cpp index 0c393ca568..ed7b2a90c3 100644 --- a/engines/cruise/saveload.cpp +++ b/engines/cruise/saveload.cpp @@ -558,6 +558,8 @@ static void syncSongs(Common::Serializer &s) { static void syncCT(Common::Serializer &s) { int v = (polyStruct) ? 1 : 0; s.syncAsSint32LE(v); + if (s.isLoading()) + polyStruct = (v != 0) ? &polyStructNorm : NULL; if (v == 0) // There is no further data to load or save -- cgit v1.2.3