From 78476c6bd68834fa0a283c1c4b5ecbb140a94472 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sun, 7 Sep 2003 03:18:27 +0000 Subject: more endian fixes svn-id: r10058 --- sword2/driver/render.cpp | 6 +-- sword2/interpreter.cpp | 42 +++++++++--------- sword2/interpreter.h | 6 +-- sword2/logic.cpp | 2 +- sword2/protocol.cpp | 111 +++++++++++++++++++++++++++++++++++++---------- 5 files changed, 116 insertions(+), 51 deletions(-) diff --git a/sword2/driver/render.cpp b/sword2/driver/render.cpp index 7997d8e3dd..e2bade672e 100644 --- a/sword2/driver/render.cpp +++ b/sword2/driver/render.cpp @@ -1143,9 +1143,9 @@ int32 InitialiseBackgroundLayer(_parallax *p) { if (p->offset[i] == 0) continue; - line = (_parallaxLine *) ((uint8 *) p + p->offset[i]); + line = (_parallaxLine *) ((uint8 *) p + FROM_LE_32(p->offset[i])); data = (uint8 *) line + sizeof(_parallaxLine); - x = line->offset; + x = FROM_LE_16(line->offset); dst = memchunk + i * p->w + x; @@ -1155,7 +1155,7 @@ int32 InitialiseBackgroundLayer(_parallax *p) { continue; } - for (j = 0; j < line->packets; j++) { + for (j = 0; j < FROM_LE_16(line->packets); j++) { if (zeros) { dst += *data; x += *data; diff --git a/sword2/interpreter.cpp b/sword2/interpreter.cpp index 0dc01f1683..f6fc161578 100644 --- a/sword2/interpreter.cpp +++ b/sword2/interpreter.cpp @@ -330,10 +330,10 @@ int RunScript ( char * scriptData , char * objectData , uint32 *offset ) // FIXME: 'scriptData' and 'variables' used to be const. However, // this code writes into 'variables' so it can not be const. char *variables = scriptData + sizeof(int); - const char *code = scriptData + (int)READ_LE_UINT32(scriptData) + sizeof(int); - uint32 noScripts = (int)READ_LE_UINT32(code); + const char *code = scriptData + (int32)READ_LE_UINT32(scriptData) + sizeof(int); + uint32 noScripts = (int32)READ_LE_UINT32(code); if ( (*offset) < noScripts) - { ip = (int)READ_LE_UINT32((const int *)code + (*offset) + 1); + { ip = (int32)READ_LE_UINT32((const int *)code + (*offset) + 1); DEBUG2("Start script %d with offset %d",*offset,ip); } else @@ -354,7 +354,7 @@ int RunScript ( char * scriptData , char * objectData , uint32 *offset ) const int *checksumBlock = (const int *)code; code += sizeof(int) * 3; - if ((int)READ_LE_UINT32(checksumBlock) != 12345678) + if ((int32)READ_LE_UINT32(checksumBlock) != 12345678) { #ifdef INSIDE_LINC AfxMessageBox(CVString("Invalid script in object %s",header->name)); @@ -363,11 +363,11 @@ int RunScript ( char * scriptData , char * objectData , uint32 *offset ) #endif return(0); } - int codeLen = (int)READ_LE_UINT32(checksumBlock + 1); + int codeLen = (int32)READ_LE_UINT32(checksumBlock + 1); int checksum = 0; for (int count = 0 ; count < codeLen ; count++) checksum += (unsigned char)code[count]; - if ( checksum != (int)READ_LE_UINT32(checksumBlock + 2) ) + if ( checksum != (int32)READ_LE_UINT32(checksumBlock + 2) ) { #ifdef INSIDE_LINC AfxMessageBox(CVString("Checksum error in script %s",header->name)); @@ -396,8 +396,8 @@ int RunScript ( char * scriptData , char * objectData , uint32 *offset ) case CP_PUSH_LOCAL_VAR32: // 1 Push the contents of a local variable Read16ip(parameter) - DEBUG2("Push local var %d (%d)",parameter,*((int *)(variables+parameter))); - PUSHONSTACK ( *((int *)(variables+parameter)) ); + DEBUG2("Push local var %d (%d)",parameter,(int32)READ_LE_UINT32(variables+parameter)); + PUSHONSTACK ( (int32)READ_LE_UINT32(variables+parameter) ); break; @@ -417,7 +417,7 @@ int RunScript ( char * scriptData , char * objectData , uint32 *offset ) Read16ip(parameter) POPOFFSTACK ( value ); DEBUG2("Pop %d into var %d",value,parameter); - *((int *)(variables+parameter)) = value; + *((int *)(variables+parameter)) = TO_LE_32(value); break; case CP_CALL_MCODE: // 4 Call an mcode routine @@ -499,10 +499,10 @@ int RunScript ( char * scriptData , char * objectData , uint32 *offset ) int foundCase = 0; for (int count = 0 ; (count < caseCount) && (!foundCase) ; count++) { - if (value == *((const int32 *)(code+ip))) + if (value == (int32)READ_LE_UINT32(code+ip)) { // We have found the case, so lets jump to it foundCase = 1; - ip += *((const int32 *)(code+ip+sizeof(int32))); + ip += (int32)READ_LE_UINT32(code+ip+sizeof(int32)); } else ip += sizeof(int32) * 2; @@ -510,7 +510,7 @@ int RunScript ( char * scriptData , char * objectData , uint32 *offset ) // If we found no matching case then use the default if (!foundCase) { - ip += *((const int32 *)(code+ip)); + ip += (int32)READ_LE_UINT32(code+ip); } } break; @@ -518,15 +518,15 @@ int RunScript ( char * scriptData , char * objectData , uint32 *offset ) case CP_ADDNPOP_LOCAL_VAR32: // 10 Read16ip(parameter) POPOFFSTACK ( value ); - *((int *)(variables+parameter)) += value; - DEBUG3("+= %d into var %d->%d",value,parameter,*((int *)(variables+parameter))); + *((int *)(variables+parameter)) = TO_LE_32((int32)READ_LE_UINT32(variables+parameter) + value); + DEBUG3("+= %d into var %d->%d",value,parameter,(int32)READ_LE_UINT32(variables+parameter)); break; case CP_SUBNPOP_LOCAL_VAR32: // 11 Read16ip(parameter) POPOFFSTACK ( value ); - *((int *)(variables+parameter)) -= value; - DEBUG3("-= %d into var %d->%d",value,parameter,*((int *)(variables+parameter))); + *((int *)(variables+parameter)) = TO_LE_32((int32)READ_LE_UINT32(variables+parameter) - value); + DEBUG3("-= %d into var %d->%d",value,parameter,(int32)READ_LE_UINT32(variables+parameter)); break; case CP_SKIPONTRUE: // 12 Skip if the value on the stack is TRUE @@ -574,7 +574,7 @@ int RunScript ( char * scriptData , char * objectData , uint32 *offset ) VS_COL_GREY); #else globalInterpreterVariables2[parameter] += value; - DEBUG3("+= %d into global var %d->%d",value,parameter,*((int *)(variables+parameter))); + DEBUG3("+= %d into global var %d->%d",value,parameter,(int32)READ_LE_UINT32(variables+parameter)); #endif break; } @@ -593,7 +593,7 @@ int RunScript ( char * scriptData , char * objectData , uint32 *offset ) VS_COL_GREY); #else globalInterpreterVariables2[parameter] -= value; - DEBUG3("-= %d into global var %d->%d",value,parameter,*((int *)(variables+parameter))); + DEBUG3("-= %d into global var %d->%d",value,parameter,(int32)READ_LE_UINT32(variables+parameter)); #endif break; } @@ -696,10 +696,10 @@ int RunScript ( char * scriptData , char * objectData , uint32 *offset ) #ifdef INSIDE_LINC TRACE("ip %d: Parameter %d skip %d\r\n", ip, parameterReturnedFromMcodeFunction, - ((const int32 *)(code+ip))[parameterReturnedFromMcodeFunction] ); + (int32)READ_LE_UINT32(code + ip + parameterReturnedFromMcodeFunction * 4) ); #endif - ip += ((const int32 *)(code+ip))[parameterReturnedFromMcodeFunction]; + ip += (int32)READ_LE_UINT32(code + ip + parameterReturnedFromMcodeFunction * 4); } break; @@ -723,7 +723,7 @@ int RunScript ( char * scriptData , char * objectData , uint32 *offset ) case CP_RESTART_SCRIPT: // 32 { // Start the script again // Do a ip search to find the script we are running - const char *tempScrPtr = scriptData + *((int *)scriptData) + sizeof(int); + const char *tempScrPtr = scriptData + (int32)READ_LE_UINT32(scriptData) + sizeof(int); int scriptNumber = 0; int foundScript = 0; uint32 count = 0; diff --git a/sword2/interpreter.h b/sword2/interpreter.h index 9c8a2ce173..13493c06aa 100644 --- a/sword2/interpreter.h +++ b/sword2/interpreter.h @@ -61,9 +61,9 @@ extern int g_debugFlag; // Get parameter fix so that the playstation version can handle words not on word boundaries -#define Read16ip(var) {var = *((const int16 *)(code+ip));ip+=sizeof(int16);} -#define Read32ip(var) {var = *((const int32 *)(code+ip));ip+=sizeof(int32);} -#define Read32ipLeaveip(var) {var = *((const int32 *)(code+ip));} +#define Read16ip(var) {var = (int16)READ_LE_UINT16(code+ip);ip+=sizeof(int16);} +#define Read32ip(var) {var = (int32)READ_LE_UINT32(code+ip);ip+=sizeof(int32);} +#define Read32ipLeaveip(var) {var = (int32)READ_LE_UINT32(code+ip);} void SetGlobalInterpreterVariables(int32 *vars); diff --git a/sword2/logic.cpp b/sword2/logic.cpp index d2285b7b8a..533d8c3e9b 100644 --- a/sword2/logic.cpp +++ b/sword2/logic.cpp @@ -81,7 +81,7 @@ int logic::Process_session(void) //Tony6June96 (first run 21Oct96) Con_fatal_error("Logic_engine %d not a run_list", run_list); game_object_list = (uint32 *) (head+1); - ID = game_object_list[pc++]; //read the next id + ID = FROM_LE_32(game_object_list[pc++]); //read the next id id=ID; res_man.Res_close(run_list); //release the list again so it can float in memory - at this point not one thing should be locked diff --git a/sword2/protocol.cpp b/sword2/protocol.cpp index c3031f18ac..232113cd13 100644 --- a/sword2/protocol.cpp +++ b/sword2/protocol.cpp @@ -40,7 +40,7 @@ uint8 *FetchPalette(uint8 *screenFile) // Chris 04Oct96 _multiScreenHeader *mscreenHeader = (_multiScreenHeader *) (screenFile + sizeof(_standardHeader)); - palette = (uint8 *)mscreenHeader + mscreenHeader->palette; + palette = (uint8 *)mscreenHeader + FROM_LE_32(mscreenHeader->palette); palette[0] = 0; // always set colour 0 to black palette[1] = 0; // because most background screen palettes have a bright colour 0 @@ -57,7 +57,7 @@ uint8 *FetchPaletteMatchTable(uint8 *screenFile) // James 09dec96 { _multiScreenHeader *mscreenHeader = (_multiScreenHeader *) (screenFile + sizeof(_standardHeader)); - return (uint8 *) mscreenHeader + mscreenHeader->paletteTable; + return (uint8 *) mscreenHeader + FROM_LE_32(mscreenHeader->paletteTable); } //----------------------------------------------------------------------------------------------------------------------- @@ -67,9 +67,15 @@ _screenHeader *FetchScreenHeader(uint8 *screenFile) //Chris 04Oct96 { // Get the table _multiScreenHeader *mscreenHeader = (_multiScreenHeader *) (screenFile + sizeof(_standardHeader)); + _screenHeader *screenHeader = (_screenHeader*) ((uint8 *) mscreenHeader + FROM_LE_32(mscreenHeader->screen)); - return (_screenHeader*) ((uint8 *) mscreenHeader + mscreenHeader->screen); +#if defined(SCUMM_BIG_ENDIAN) + screenHeader->width = SWAP_BYTES_16(screenHeader->width); + screenHeader->height = SWAP_BYTES_16(screenHeader->height); + screenHeader->noLayers = SWAP_BYTES_16(screenHeader->noLayers); +#endif + return screenHeader; } //----------------------------------------------------------------------------------------------------------------------- // returns a pointer to the requested layer header, given the pointer to the start of the screen file @@ -77,19 +83,27 @@ _screenHeader *FetchScreenHeader(uint8 *screenFile) //Chris 04Oct96 // assumes it has been passed a pointer to a valid screen file _layerHeader *FetchLayerHeader(uint8 *screenFile, uint16 layerNo) //Chris 04Oct96 { - _screenHeader *screenHead; - - - screenHead = FetchScreenHeader(screenFile); - #ifdef _SWORD2_DEBUG + _screenHeader *screenHead = FetchScreenHeader(screenFile); + if (layerNo > (screenHead->noLayers-1)) // layer number too large! Con_fatal_error("FetchLayerHeader(%d) invalid layer number! (%s line %u)",layerNo,__FILE__,__LINE__); #endif _multiScreenHeader *mscreenHeader = (_multiScreenHeader *) (screenFile + sizeof(_standardHeader)); - return (_layerHeader *) ((uint8 *) mscreenHeader + mscreenHeader->layers + (layerNo * sizeof(_layerHeader))); + _layerHeader *layerHeader = (_layerHeader *) ((uint8 *) mscreenHeader + FROM_LE_32(mscreenHeader->layers) + (layerNo * sizeof(_layerHeader))); + +#if defined(SCUMM_BIG_ENDIAN) + layerHeader->x = SWAP_BYTES_16(layerHeader->x); + layerHeader->y = SWAP_BYTES_16(layerHeader->y); + layerHeader->width = SWAP_BYTES_16(layerHeader->width); + layerHeader->height = SWAP_BYTES_16(layerHeader->height); + layerHeader->maskSize = SWAP_BYTES_32(layerHeader->maskSize); + layerHeader->offset = SWAP_BYTES_32(layerHeader->offset); +#endif + + return layerHeader; } //--------------------------------------------------------------- @@ -100,7 +114,7 @@ uint8 *FetchShadingMask(uint8 *screenFile) // James 08apr97 { _multiScreenHeader *mscreenHeader = (_multiScreenHeader *) (screenFile + sizeof(_standardHeader)); - return (uint8 *) mscreenHeader + mscreenHeader->maskOffset; + return (uint8 *) mscreenHeader + FROM_LE_32(mscreenHeader->maskOffset); } //----------------------------------------------------------------------------------------------------------------------- @@ -109,7 +123,19 @@ uint8 *FetchShadingMask(uint8 *screenFile) // James 08apr97 _animHeader *FetchAnimHeader(uint8 *animFile) // (25sep96JEL) { - return (_animHeader *) (animFile + sizeof(_standardHeader)); + _animHeader *animHead; + animHead = (_animHeader *) (animFile + sizeof(_standardHeader)); + +#if defined(SCUMM_BIG_ENDIAN) + animHead->noAnimFrames = SWAP_BYTES_16(animHead->noAnimFrames); + animHead->feetStartX = SWAP_BYTES_16(animHead->feetStartX); + animHead->feetStartY = SWAP_BYTES_16(animHead->feetStartY); + animHead->feetEndX = SWAP_BYTES_16(animHead->feetEndX); + animHead->feetEndY = SWAP_BYTES_16(animHead->feetEndY); + animHead->blend = SWAP_BYTES_16(animHead->blend); +#endif + + return animHead; } //--------------------------------------------------------------- @@ -128,8 +154,18 @@ _cdtEntry *FetchCdtEntry(uint8 *animFile, uint16 frameNo) // Chris 09Oct96 Con_fatal_error("FetchCdtEntry(animFile,%d) - anim only %d frames (%s line %u)",frameNo,animHead->noAnimFrames,__FILE__,__LINE__); #endif - return (_cdtEntry *) ( (uint8 *)animHead + sizeof(_animHeader) + frameNo * sizeof(_cdtEntry) ); + _cdtEntry *cdtEntry; + cdtEntry = (_cdtEntry *) ( (uint8 *)animHead + sizeof(_animHeader) + frameNo * sizeof(_cdtEntry) ); + +#if defined(SCUMM_BIG_ENDIAN) + cdtEntry->x = (int16)SWAP_BYTES_16(cdtEntry->x); + cdtEntry->y = (int16)SWAP_BYTES_16(cdtEntry->y); + cdtEntry->frameOffset = SWAP_BYTES_32(cdtEntry->frameOffset); +#endif + + return cdtEntry; } + //--------------------------------------------------------------- // returns a pointer to the requested frame number's header, given the pointer to the start of the anim file // drops out if the requested frame number exceeds the number of frames in this anim @@ -138,7 +174,15 @@ _cdtEntry *FetchCdtEntry(uint8 *animFile, uint16 frameNo) // Chris 09Oct96 _frameHeader *FetchFrameHeader(uint8 *animFile, uint16 frameNo) // James 31oct96 { // required address = (address of the start of the anim header) + frameOffset - return (_frameHeader *) (animFile + sizeof(_standardHeader) + (FetchCdtEntry(animFile,frameNo)->frameOffset) ); + _frameHeader *frameHeader = (_frameHeader *) (animFile + sizeof(_standardHeader) + (FetchCdtEntry(animFile,frameNo)->frameOffset) ); + +#if defined(SCUMM_BIG_ENDIAN) + frameHeader->compSize = SWAP_BYTES_32(frameHeader->compSize); + frameHeader->width = SWAP_BYTES_16(frameHeader->width); + frameHeader->height = SWAP_BYTES_16(frameHeader->height); +#endif + + return frameHeader; } //--------------------------------------------------------------- // Returns a pointer to the requested parallax layer data. @@ -148,11 +192,18 @@ _parallax *FetchBackgroundParallaxLayer(uint8 *screenFile, int layer) // Chris 0 _multiScreenHeader *mscreenHeader = (_multiScreenHeader *) (screenFile + sizeof(_standardHeader)); #ifdef _SWORD2_DEBUG - if (mscreenHeader->bg_parallax[layer] == 0) + if (FROM_LE_32(mscreenHeader->bg_parallax[layer]) == 0) Con_fatal_error("FetchBackgroundParallaxLayer(%d) - No parallax layer exists (%s line %u)",layer,__FILE__,__LINE__); #endif - return (_parallax *) ((uint8 *) mscreenHeader + mscreenHeader->bg_parallax[layer]); + _parallax *parallax = (_parallax *) ((uint8 *) mscreenHeader + FROM_LE_32(mscreenHeader->bg_parallax[layer])); + +#if defined(SCUMM_BIG_ENDIAN) + parallax->w = SWAP_BYTES_16(parallax->w); + parallax->h = SWAP_BYTES_16(parallax->h); +#endif + + return parallax; } //--------------------------------------------------------------- _parallax *FetchBackgroundLayer(uint8 *screenFile) // Chris 04Oct96 @@ -160,11 +211,18 @@ _parallax *FetchBackgroundLayer(uint8 *screenFile) // Chris 04Oct96 _multiScreenHeader *mscreenHeader = (_multiScreenHeader *) (screenFile + sizeof(_standardHeader)); #ifdef _SWORD2_DEBUG - if (mscreenHeader->screen == 0) + if (FROM_LE_32(mscreenHeader->screen) == 0) Con_fatal_error("FetchBackgroundLayer (%d) - No background layer exists (%s line %u)",__FILE__,__LINE__); #endif - return (_parallax *) ((uint8 *) mscreenHeader + mscreenHeader->screen + sizeof(_screenHeader)); + _parallax *parallax = (_parallax *) ((uint8 *) mscreenHeader + FROM_LE_32(mscreenHeader->screen) + sizeof(_screenHeader)); + +#if defined(SCUMM_BIG_ENDIAN) + parallax->w = SWAP_BYTES_16(parallax->w); + parallax->h = SWAP_BYTES_16(parallax->h); +#endif + + return parallax; } //--------------------------------------------------------------- _parallax *FetchForegroundParallaxLayer(uint8 *screenFile, int layer) // Chris 04Oct96 @@ -172,11 +230,18 @@ _parallax *FetchForegroundParallaxLayer(uint8 *screenFile, int layer) // Chris 0 _multiScreenHeader *mscreenHeader = (_multiScreenHeader *) (screenFile + sizeof(_standardHeader)); #ifdef _SWORD2_DEBUG - if (mscreenHeader->fg_parallax[layer] == 0) + if (FROM_LE_32(mscreenHeader->fg_parallax[layer]) == 0) Con_fatal_error("FetchForegroundParallaxLayer(%d) - No parallax layer exists (%s line %u)",layer,__FILE__,__LINE__); #endif - return (_parallax *) ((uint8 *) mscreenHeader + mscreenHeader->fg_parallax[layer]); + _parallax *parallax = (_parallax *) ((uint8 *) mscreenHeader + FROM_LE_32(mscreenHeader->fg_parallax[layer])); + +#if defined(SCUMM_BIG_ENDIAN) + parallax->w = SWAP_BYTES_16(parallax->w); + parallax->h = SWAP_BYTES_16(parallax->h); +#endif + + return parallax; } //--------------------------------------------------------------- uint8 errorLine[128]; @@ -191,16 +256,16 @@ uint8 *FetchTextLine(uint8 *file, uint32 text_line) //Tony24Oct96 _textHeader *text_header = (_textHeader *) (file + sizeof(_standardHeader)); - if (text_line>=text_header->noOfLines) // (James08aug97) + if (text_line>=FROM_LE_32(text_header->noOfLines)) // (James08aug97) { fileHeader = (_standardHeader*)file; - sprintf ((char*)errorLine, "xxMissing line %d of %s (only 0..%d)", text_line, fileHeader->name, text_header->noOfLines-1); + sprintf ((char*)errorLine, "xxMissing line %d of %s (only 0..%d)", text_line, fileHeader->name, FROM_LE_32(text_header->noOfLines)-1); errorLine[0]=0; // first 2 chars are NULL so that actor-number comes out as '0' errorLine[1]=0; return(errorLine); // GOT RID OF CON_FATAL_ERROR HERE BECAUSE WE DON'T WANT IT TO CRASH OUT ANY MORE! -// Con_fatal_error("FetchTextLine cannot get %d, only 0..%d avail (%s line %u)", text_line, text_header->noOfLines-1,__FILE__,__LINE__); +// Con_fatal_error("FetchTextLine cannot get %d, only 0..%d avail (%s line %u)", text_line, FROM_LE_32(text_header->noOfLines)-1,__FILE__,__LINE__); } @@ -214,7 +279,7 @@ uint8 CheckTextLine(uint8 *file, uint32 text_line) // (James26jun97) { _textHeader *text_header = (_textHeader *) (file + sizeof(_standardHeader)); - if (text_line>=text_header->noOfLines) + if (text_line>=FROM_LE_32(text_header->noOfLines)) return(0); // out of range => invalid else return(1); // valid -- cgit v1.2.3