SciSpan<const byte> Script::findBlockSCI0(ScriptObjectTypes type, bool findLastBlock) const { SciSpan<const byte> foundBlock; bool oldScriptHeader = (getSciVersion() == SCI_VERSION_0_EARLY); SciSpan<const byte> buf = *_buf; if (oldScriptHeader) buf += 2; for (;;) { const int blockType = buf.getUint16LEAt(0); if (blockType == 0) break; // the size in the block header includes the size of the header itself const int blockSize = buf.getUint16LEAt(2); assert(blockSize > 0); if (blockType == type) { foundBlock = buf.subspan(0, blockSize, Common::String::format("%s, %s block", _buf->name().c_str(), sciObjectTypeNames[type])); if (!findLastBlock) { break; } } buf += blockSize; } return foundBlock; }
void HunkPalette::write(SciSpan<byte> &out, const Palette &palette) { const uint8 numPalettes = 1; const uint16 paletteOffset = kHunkPaletteHeaderSize + 2 * numPalettes; out[kNumPaletteEntriesOffset] = numPalettes; out[kHunkPaletteHeaderSize + 2] = paletteOffset; SciSpan<byte> entry = out.subspan(paletteOffset); entry[kEntryStartColorOffset] = 0; entry.setUint16SEAt(kEntryNumColorsOffset, ARRAYSIZE(palette.colors)); entry[kEntryUsedOffset] = 1; entry[kEntrySharedUsedOffset] = 0; entry.setUint32SEAt(kEntryVersionOffset, 1); SciSpan<byte> paletteData = entry.subspan(kEntryHeaderSize); for (uint i = 0; i < ARRAYSIZE(palette.colors); ++i) { *paletteData++ = palette.colors[i].used; *paletteData++ = palette.colors[i].r; *paletteData++ = palette.colors[i].g; *paletteData++ = palette.colors[i].b; } }
void Script::load(int script_nr, ResourceManager *resMan, ScriptPatcher *scriptPatcher) { freeScript(); Resource *script = resMan->findResource(ResourceId(kResourceTypeScript, script_nr), false); if (!script) error("Script %d not found", script_nr); _nr = script_nr; uint32 scriptSize = script->size(); uint32 bufSize = scriptSize; if (getSciVersion() == SCI_VERSION_0_EARLY) { bufSize += script->getUint16LEAt(0) * 2; } else if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE) { // In SCI1.1 - SCI2.1, the heap was in a separate space from the script. We append // it to the end of the script, and adjust addressing accordingly. // However, since we address the heap with a 16-bit pointer, the // combined size of the stack and the heap must be 64KB. So far this has // worked for SCI11, SCI2 and SCI21 games. SCI3 games use a different // script format, and they can exceed the 64KB boundary using relocation. Resource *heap = resMan->findResource(ResourceId(kResourceTypeHeap, script_nr), false); bufSize += heap->size(); // Ensure that the start of the heap resource can be word-aligned. if (script->size() & 2) { ++bufSize; ++scriptSize; } // As mentioned above, the script and the heap together should not exceed 64KB if (script->size() + heap->size() > 65535) error("Script and heap %d sizes combined exceed 64K. This means a fundamental " "design bug was made regarding SCI1.1 and newer games.\n" "Please report this error to the ScummVM team", script_nr); } else if (getSciVersion() == SCI_VERSION_3 && script->size() > 0x3FFFF) { error("Script %d size exceeds 256K (it is %u bytes).\n" "Please report this error to the ScummVM team", script_nr, script->size()); } uint extraLocalsWorkaround = 0; if (g_sci->getGameId() == GID_FANMADE && _nr == 1 && script->size() == 11140) { // WORKAROUND: Script 1 in Ocean Battle doesn't have enough locals to // fit the string showing how many shots are left (a nasty script bug, // corrupting heap memory). We add 10 more locals so that it has enough // space to use as the target for its kFormat operation. Fixes bug // #3059871. extraLocalsWorkaround = 10; } bufSize += extraLocalsWorkaround * 2; SciSpan<byte> outBuffer = _buf->allocate(bufSize, script->name() + " buffer"); script->copyDataTo(outBuffer); // The word-aligned script size is used here because other parts of the code // currently rely on finding the start of the heap by reading the script // size _script = _buf->subspan(0, scriptSize, script->name()); if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE) { Resource *heap = resMan->findResource(ResourceId(kResourceTypeHeap, _nr), false); assert(heap); SciSpan<byte> outHeap = outBuffer.subspan(scriptSize, heap->size(), heap->name(), 0); heap->copyDataTo(outHeap); _heap = outHeap; } // Check scripts (+ possibly SCI 1.1 heap) for matching signatures and patch those, if found scriptPatcher->processScript(_nr, outBuffer); if (getSciVersion() <= SCI_VERSION_1_LATE) { // Some buggy game scripts contain two export tables (e.g. script 912 // in Camelot and script 306 in KQ4); in these scripts, the first table // is broken, so we ignore it and use the last one instead // Fixes bugs #3039785 and #3037595. SciSpan<const uint16> exportTable = findBlockSCI0(SCI_OBJ_EXPORTS, true).subspan<const uint16>(0); if (exportTable) { // The export table is after the block header (4 bytes / 2 uint16s) // and the number of exports (2 bytes / 1 uint16). // The exports span does not need to be explicitly sized since the // maximum size was already determined by findBlockSCI0 _exports = exportTable.subspan(3); _numExports = exportTable.getUint16SEAt(2); } SciSpan<const byte> synonymTable = findBlockSCI0(SCI_OBJ_SYNONYMS); if (synonymTable) { // the synonyms table is after the block header (4 bytes), // and each synonym entry is 4 bytes _synonyms = synonymTable.subspan(4); _numSynonyms = _synonyms.size() / 4; } SciSpan<const byte> localsTable = findBlockSCI0(SCI_OBJ_LOCALVARS); if (localsTable) { // skip header (4 bytes) _localsOffset = localsTable - *_buf + 4; _localsCount = localsTable.size() / 2 - 2; } } else if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE) { _numExports = _buf->getUint16SEAt(kSci11NumExportsOffset); if (_numExports) { _exports = _buf->subspan<const uint16>(kSci11ExportTableOffset, _numExports * sizeof(uint16)); } _localsOffset = getHeapOffset() + 4; _localsCount = _buf->getUint16SEAt(_localsOffset - 2); } else if (getSciVersion() == SCI_VERSION_3) { _localsCount = _buf->getUint16LEAt(12); _numExports = _buf->getUint16LEAt(20); if (_numExports) { _exports = _buf->subspan<const uint16>(22, _numExports * sizeof(uint16)); } // SCI3 local variables always start dword-aligned if (_numExports % 2) _localsOffset = 22 + _numExports * sizeof(uint16); else _localsOffset = 24 + _numExports * sizeof(uint16); } // WORKAROUND: Increase locals, if needed (check above) _localsCount += extraLocalsWorkaround; if (getSciVersion() == SCI_VERSION_0_EARLY) { // SCI0 early // Old script block. There won't be a localvar block in this case. // Instead, the script starts with a 16 bit int specifying the // number of locals we need; these are then allocated and zeroed. _localsCount = _buf->getUint16LEAt(0); _localsOffset = -_localsCount * 2; // Make sure it's invalid } else { // SCI0 late and newer // Does the script actually have locals? If not, set the locals offset to 0 if (!_localsCount) _localsOffset = 0; if (_localsOffset + _localsCount * 2 + 1 >= (int)_buf->size()) { error("Locals extend beyond end of script %d: offset %04x, count %u vs size %u", _nr, _localsOffset, _localsCount, _buf->size()); } } // find all strings of this script identifyOffsets(); }
void Script::identifyOffsets() { offsetLookupArrayEntry arrayEntry; SciSpan<const byte> scriptDataPtr; SciSpan<const byte> stringStartPtr; SciSpan<const byte> stringDataPtr; byte stringDataByte = 0; uint16 typeObject_id = 0; uint16 typeString_id = 0; uint16 typeSaid_id = 0; uint16 blockType = 0; uint16 blockSize = 0; _offsetLookupArray.clear(); _offsetLookupObjectCount = 0; _offsetLookupStringCount = 0; _offsetLookupSaidCount = 0; _codeOffset = 0; if (getSciVersion() < SCI_VERSION_1_1) { // SCI0 + SCI1 scriptDataPtr = *_buf; // Go through all blocks if (getSciVersion() == SCI_VERSION_0_EARLY) { if (scriptDataPtr.size() < 2) error("Script::identifyOffsets(): unexpected end of script %d", _nr); scriptDataPtr += 2; } for (;;) { if (scriptDataPtr.size() < 2) error("Script::identifyOffsets(): unexpected end of script %d", _nr); blockType = scriptDataPtr.getUint16LEAt(0); scriptDataPtr += 2; if (blockType == 0) // end of blocks detected break; if (scriptDataPtr.size() < 2) error("Script::identifyOffsets(): unexpected end of script %d", _nr); blockSize = scriptDataPtr.getUint16LEAt(0); if (blockSize < 4) error("Script::identifyOffsets(): invalid block size in script %d", _nr); blockSize -= 4; // block size includes block-type UINT16 and block-size UINT16 scriptDataPtr += 2; if (scriptDataPtr.size() < blockSize) error("Script::identifyOffsets(): invalid block size in script %d", _nr); switch (blockType) { case SCI_OBJ_OBJECT: case SCI_OBJ_CLASS: typeObject_id++; arrayEntry.type = SCI_SCR_OFFSET_TYPE_OBJECT; arrayEntry.id = typeObject_id; arrayEntry.offset = scriptDataPtr - *_buf + 8; // Calculate offset inside script data (VM uses +8) arrayEntry.stringSize = 0; _offsetLookupArray.push_back(arrayEntry); _offsetLookupObjectCount++; break; case SCI_OBJ_STRINGS: // string block detected, we now grab all NUL terminated strings out of this block stringDataPtr = scriptDataPtr.subspan(0, blockSize); arrayEntry.type = SCI_SCR_OFFSET_TYPE_STRING; for (;;) { if (stringDataPtr.size() < 1) // no more bytes left break; stringStartPtr = stringDataPtr; if (stringDataPtr.size() == 1) { // only 1 byte left and that byte is a [00], in that case we also exit stringDataByte = *stringStartPtr; if (stringDataByte == 0x00) break; } // now look for terminating [NUL] for (;;) { stringDataByte = *stringDataPtr; stringDataPtr++; if (!stringDataByte) // NUL found, exit this loop break; if (stringDataPtr.size() < 1) { // no more bytes left warning("Script::identifyOffsets(): string without terminating NUL in script %d", _nr); break; } } if (stringDataByte) break; typeString_id++; arrayEntry.id = typeString_id; arrayEntry.offset = stringStartPtr - *_buf; // Calculate offset inside script data arrayEntry.stringSize = stringDataPtr - stringStartPtr; _offsetLookupArray.push_back(arrayEntry); _offsetLookupStringCount++; } break; case SCI_OBJ_SAID: // said block detected, we now try to find every single said "string" inside this block // said strings are terminated with a 0xFF, the string itself may contain words (2 bytes), where // the second byte of a word may also be a 0xFF. stringDataPtr = scriptDataPtr.subspan(0, blockSize); arrayEntry.type = SCI_SCR_OFFSET_TYPE_SAID; for (;;) { if (stringDataPtr.size() < 1) // no more bytes left break; stringStartPtr = stringDataPtr; if (stringDataPtr.size() == 1) { // only 1 byte left and that byte is a [00], in that case we also exit // happens in some scripts, for example Conquests of Camelot, script 997 // may have been a bug in the compiler or just an intentional filler byte stringDataByte = *stringStartPtr; if (stringDataByte == 0x00) break; } // now look for terminating 0xFF for (;;) { stringDataByte = *stringDataPtr; stringDataPtr++; if (stringDataByte == 0xFF) // Terminator found, exit this loop break; if (stringDataPtr.size() < 1) // no more bytes left error("Script::identifyOffsets(): said-string without terminator in script %d", _nr); if (stringDataByte < 0xF0) { // Part of a word, skip second byte stringDataPtr++; if (stringDataPtr.size() < 1) // no more bytes left error("Script::identifyOffsets(): said-string without terminator in script %d", _nr); } } typeSaid_id++; arrayEntry.id = typeSaid_id; arrayEntry.offset = stringStartPtr - *_buf; // Calculate offset inside script data arrayEntry.stringSize = 0; _offsetLookupArray.push_back(arrayEntry); _offsetLookupSaidCount++; } break; default: break; } scriptDataPtr += blockSize; } } else if (getSciVersion() >= SCI_VERSION_1_1 && getSciVersion() <= SCI_VERSION_2_1_LATE) { // Strings in SCI1.1 up to SCI2 come after the object instances scriptDataPtr = _heap; enum { kExportSize = sizeof(uint16), kPropertySize = sizeof(uint16), kNumMethodsSize = sizeof(uint16), kPropDictEntrySize = 2, kMethDictEntrySize = 4 }; SciSpan<const byte> hunkPtr = _buf->subspan(kSci11ExportTableOffset + _numExports * kExportSize); if (scriptDataPtr.size() < 4) error("Script::identifyOffsets(): unexpected end of script in script %d", _nr); uint16 endOfStringOffset = scriptDataPtr.getUint16SEAt(0); uint16 objectStartOffset = scriptDataPtr.getUint16SEAt(2) * 2 + 4; if (scriptDataPtr.size() < objectStartOffset) error("Script::identifyOffsets(): object start is beyond heap size in script %d", _nr); if (scriptDataPtr.size() < endOfStringOffset) error("Script::identifyOffsets(): end of string is beyond heap size in script %d", _nr); SciSpan<const byte> endOfStringPtr = scriptDataPtr.subspan(endOfStringOffset); scriptDataPtr += objectStartOffset; // go through all objects for (;;) { if (scriptDataPtr.size() < 2) error("Script::identifyOffsets(): unexpected end of script %d", _nr); blockType = scriptDataPtr.getUint16SEAt(0); scriptDataPtr += 2; if (blockType != SCRIPT_OBJECT_MAGIC_NUMBER) break; // Object found, add offset of object typeObject_id++; arrayEntry.type = SCI_SCR_OFFSET_TYPE_OBJECT; arrayEntry.id = typeObject_id; arrayEntry.offset = scriptDataPtr - *_buf - 2; // the VM uses a pointer to the Magic-Number arrayEntry.stringSize = 0; _offsetLookupArray.push_back(arrayEntry); _offsetLookupObjectCount++; if (scriptDataPtr.size() < 2) error("Script::identifyOffsets(): unexpected end of script in script %d", _nr); const uint16 numProperties = scriptDataPtr.getUint16SEAt(0); blockSize = numProperties * kPropertySize; if (blockSize < 4) error("Script::identifyOffsets(): invalid block size in script %d", _nr); scriptDataPtr += 2; const uint16 scriptNum = scriptDataPtr.getUint16SEAt(6); if (scriptNum != 0xFFFF) { hunkPtr += numProperties * kPropDictEntrySize; } const uint16 numMethods = hunkPtr.getUint16SEAt(0); hunkPtr += kNumMethodsSize + numMethods * kMethDictEntrySize; blockSize -= 4; // blocksize contains UINT16 type and UINT16 size if (scriptDataPtr.size() < blockSize) error("Script::identifyOffsets(): invalid block size in script %d", _nr); scriptDataPtr += blockSize; } _codeOffset = hunkPtr - *_buf; // now scriptDataPtr points to right at the start of the strings if (scriptDataPtr > endOfStringPtr) error("Script::identifyOffsets(): string block / end-of-string block mismatch in script %d", _nr); stringDataPtr = scriptDataPtr.subspan(0, endOfStringPtr - scriptDataPtr); arrayEntry.type = SCI_SCR_OFFSET_TYPE_STRING; for (;;) { if (stringDataPtr.size() < 1) // no more bytes left break; stringStartPtr = stringDataPtr; // now look for terminating [NUL] for (;;) { stringDataByte = *stringDataPtr; stringDataPtr++; if (!stringDataByte) // NUL found, exit this loop break; if (stringDataPtr.size() < 1) { // no more bytes left warning("Script::identifyOffsets(): string without terminating NUL in script %d", _nr); break; } } if (stringDataByte) break; typeString_id++; arrayEntry.id = typeString_id; arrayEntry.offset = stringStartPtr - *_buf; // Calculate offset inside script data arrayEntry.stringSize = stringDataPtr - stringStartPtr; _offsetLookupArray.push_back(arrayEntry); _offsetLookupStringCount++; } #ifdef ENABLE_SCI32 } else if (getSciVersion() == SCI_VERSION_3) { // SCI3 uint32 sci3StringOffset = 0; uint32 sci3RelocationOffset = 0; uint32 sci3BoundaryOffset = 0; if (_buf->size() < 22) error("Script::identifyOffsets(): script %d smaller than expected SCI3-header", _nr); _codeOffset = _buf->getUint32LEAt(0); sci3StringOffset = _buf->getUint32LEAt(4); sci3RelocationOffset = _buf->getUint32LEAt(8); if (sci3RelocationOffset > _buf->size()) error("Script::identifyOffsets(): relocation offset is beyond end of script %d", _nr); // First we get all the objects scriptDataPtr = getSci3ObjectsPointer(); for (;;) { if (scriptDataPtr.size() < 2) error("Script::identifyOffsets(): unexpected end of script %d", _nr); blockType = scriptDataPtr.getUint16SEAt(0); scriptDataPtr += 2; if (blockType != SCRIPT_OBJECT_MAGIC_NUMBER) break; // Object found, add offset of object typeObject_id++; arrayEntry.type = SCI_SCR_OFFSET_TYPE_OBJECT; arrayEntry.id = typeObject_id; arrayEntry.offset = scriptDataPtr - *_buf - 2; // the VM uses a pointer to the Magic-Number arrayEntry.stringSize = 0; _offsetLookupArray.push_back(arrayEntry); _offsetLookupObjectCount++; if (scriptDataPtr.size() < 2) error("Script::identifyOffsets(): unexpected end of script in script %d", _nr); blockSize = scriptDataPtr.getUint16SEAt(0); if (blockSize < 4) error("Script::identifyOffsets(): invalid block size in script %d", _nr); scriptDataPtr += 2; blockSize -= 4; // blocksize contains UINT16 type and UINT16 size if (scriptDataPtr.size() < blockSize) error("Script::identifyOffsets(): invalid block size in script %d", _nr); scriptDataPtr += blockSize; } // And now we get all the strings if (sci3StringOffset > 0) { // string offset set, we expect strings if (sci3StringOffset > _buf->size()) error("Script::identifyOffsets(): string offset is beyond end of script %d", _nr); if (sci3RelocationOffset < sci3StringOffset) error("Script::identifyOffsets(): string offset points beyond relocation offset in script %d", _nr); stringDataPtr = _buf->subspan(sci3StringOffset, sci3RelocationOffset - sci3StringOffset); arrayEntry.type = SCI_SCR_OFFSET_TYPE_STRING; for (;;) { if (stringDataPtr.size() < 1) // no more bytes left break; stringStartPtr = stringDataPtr; if (stringDataPtr.size() == 1) { // only 1 byte left and that byte is a [00], in that case we also exit stringDataByte = *stringStartPtr; if (stringDataByte == 0x00) break; } // now look for terminating [NUL] for (;;) { stringDataByte = *stringDataPtr; stringDataPtr++; if (!stringDataByte) // NUL found, exit this loop break; if (stringDataPtr.size() < 1) { // no more bytes left warning("Script::identifyOffsets(): string without terminating NUL in script %d", _nr); break; } } if (stringDataByte) break; typeString_id++; arrayEntry.id = typeString_id; arrayEntry.offset = stringStartPtr - *_buf; // Calculate offset inside script data arrayEntry.stringSize = stringDataPtr - stringStartPtr; _offsetLookupArray.push_back(arrayEntry); _offsetLookupStringCount++; // SCI3 seems to have aligned all string on DWORD boundaries sci3BoundaryOffset = stringDataPtr - *_buf; // Calculate current offset inside script data sci3BoundaryOffset = sci3BoundaryOffset & 3; // Check boundary offset if (sci3BoundaryOffset) { // lower 2 bits are set? Then we have to adjust the offset sci3BoundaryOffset = 4 - sci3BoundaryOffset; if (stringDataPtr.size() < sci3BoundaryOffset) error("Script::identifyOffsets(): SCI3 string boundary adjustment goes beyond end of string block in script %d", _nr); stringDataPtr += sci3BoundaryOffset; } } } #endif } }
void GfxPicture::drawVectorData(const SciSpan<const byte> &data) { byte pic_op; byte pic_color = _screen->getColorDefaultVectorData(); byte pic_priority = 255, pic_control = 255; int16 x = 0, y = 0, oldx, oldy; byte EGApalettes[PIC_EGAPALETTE_TOTALSIZE] = {0}; byte *EGApalette = &EGApalettes[_EGApaletteNo * PIC_EGAPALETTE_SIZE]; byte EGApriority[PIC_EGAPRIORITY_SIZE] = {0}; bool isEGA = false; uint curPos = 0; uint16 size; byte pixel; int i; Palette palette; int16 pattern_Code = 0, pattern_Texture = 0; bool icemanDrawFix = false; bool ignoreBrokenPriority = false; memset(&palette, 0, sizeof(palette)); if (_EGApaletteNo >= PIC_EGAPALETTE_COUNT) _EGApaletteNo = 0; if (_resMan->getViewType() == kViewEga) { isEGA = true; // setup default mapping tables for (i = 0; i < PIC_EGAPALETTE_TOTALSIZE; i += PIC_EGAPALETTE_SIZE) memcpy(&EGApalettes[i], &vector_defaultEGApalette, sizeof(vector_defaultEGApalette)); memcpy(&EGApriority, &vector_defaultEGApriority, sizeof(vector_defaultEGApriority)); if (g_sci->getGameId() == GID_ICEMAN) { // WORKAROUND: we remove certain visual&priority lines in underwater // rooms of iceman, when not dithering the picture. Normally those // lines aren't shown, because they share the same color as the // dithered fill color combination. When not dithering, those lines // would appear and get distracting. if ((_screen->isUnditheringEnabled()) && ((_resourceId >= 53 && _resourceId <= 58) || (_resourceId == 61))) icemanDrawFix = true; } } // Drawing while (curPos < data.size()) { #ifdef DEBUG_PICTURE_DRAW debug("Picture op: %X (%s) at %d", data[curPos], picOpcodeNames[data[curPos] - 0xF0], curPos); _screen->copyToScreen(); g_system->updateScreen(); g_system->delayMillis(400); #endif switch (pic_op = data[curPos++]) { case PIC_OP_SET_COLOR: pic_color = data[curPos++]; if (isEGA) { pic_color = EGApalette[pic_color]; pic_color ^= pic_color << 4; } break; case PIC_OP_DISABLE_VISUAL: pic_color = 0xFF; break; case PIC_OP_SET_PRIORITY: pic_priority = data[curPos++] & 0x0F; if (isEGA) pic_priority = EGApriority[pic_priority]; if (ignoreBrokenPriority) pic_priority = 255; break; case PIC_OP_DISABLE_PRIORITY: pic_priority = 255; break; case PIC_OP_SET_CONTROL: pic_control = data[curPos++] & 0x0F; break; case PIC_OP_DISABLE_CONTROL: pic_control = 255; break; case PIC_OP_SHORT_LINES: // short line vectorGetAbsCoords(data, curPos, x, y); while (vectorIsNonOpcode(data[curPos])) { oldx = x; oldy = y; vectorGetRelCoords(data, curPos, x, y); Common::Point startPoint(oldx, oldy); Common::Point endPoint(x, y); _ports->offsetLine(startPoint, endPoint); _screen->drawLine(startPoint, endPoint, pic_color, pic_priority, pic_control); } break; case PIC_OP_MEDIUM_LINES: // medium line vectorGetAbsCoords(data, curPos, x, y); if (icemanDrawFix) { // WORKAROUND: remove certain lines in iceman - see above if ((pic_color == 1) && (pic_priority == 14)) { if ((y < 100) || (!(y & 1))) { pic_color = 255; pic_priority = 255; } } } while (vectorIsNonOpcode(data[curPos])) { oldx = x; oldy = y; vectorGetRelCoordsMed(data, curPos, x, y); Common::Point startPoint(oldx, oldy); Common::Point endPoint(x, y); _ports->offsetLine(startPoint, endPoint); _screen->drawLine(startPoint, endPoint, pic_color, pic_priority, pic_control); } break; case PIC_OP_LONG_LINES: // long line vectorGetAbsCoords(data, curPos, x, y); while (vectorIsNonOpcode(data[curPos])) { oldx = x; oldy = y; vectorGetAbsCoords(data, curPos, x, y); Common::Point startPoint(oldx, oldy); Common::Point endPoint(x, y); _ports->offsetLine(startPoint, endPoint); _screen->drawLine(startPoint, endPoint, pic_color, pic_priority, pic_control); } break; case PIC_OP_FILL: //fill while (vectorIsNonOpcode(data[curPos])) { vectorGetAbsCoords(data, curPos, x, y); vectorFloodFill(x, y, pic_color, pic_priority, pic_control); } break; // Pattern opcodes are handled in sierra sci1.1+ as actual NOPs and // normally they definitely should not occur inside picture data for // such games. case PIC_OP_SET_PATTERN: if (_resourceType >= SCI_PICTURE_TYPE_SCI11) { if (g_sci->getGameId() == GID_SQ4) { // WORKAROUND: For SQ4 / for some pictures handle this like // a terminator. This picture includes garbage data, first a // set pattern w/o parameter and then short pattern. I guess // that garbage is a left over from the sq4-floppy (sci1) to // sq4-cd (sci1.1) conversion. switch (_resourceId) { case 35: case 381: case 376: //case 390: // in the blacklisted NRS patch 1.2 (bug #3615060) return; default: break; } } error("pic-operation set pattern inside sci1.1+ vector data"); } pattern_Code = data[curPos++]; break; case PIC_OP_SHORT_PATTERNS: if (_resourceType >= SCI_PICTURE_TYPE_SCI11) error("pic-operation short pattern inside sci1.1+ vector data"); vectorGetPatternTexture(data, curPos, pattern_Code, pattern_Texture); vectorGetAbsCoords(data, curPos, x, y); vectorPattern(x, y, pic_color, pic_priority, pic_control, pattern_Code, pattern_Texture); while (vectorIsNonOpcode(data[curPos])) { vectorGetPatternTexture(data, curPos, pattern_Code, pattern_Texture); vectorGetRelCoords(data, curPos, x, y); vectorPattern(x, y, pic_color, pic_priority, pic_control, pattern_Code, pattern_Texture); } break; case PIC_OP_MEDIUM_PATTERNS: if (_resourceType >= SCI_PICTURE_TYPE_SCI11) error("pic-operation medium pattern inside sci1.1+ vector data"); vectorGetPatternTexture(data, curPos, pattern_Code, pattern_Texture); vectorGetAbsCoords(data, curPos, x, y); vectorPattern(x, y, pic_color, pic_priority, pic_control, pattern_Code, pattern_Texture); while (vectorIsNonOpcode(data[curPos])) { vectorGetPatternTexture(data, curPos, pattern_Code, pattern_Texture); vectorGetRelCoordsMed(data, curPos, x, y); vectorPattern(x, y, pic_color, pic_priority, pic_control, pattern_Code, pattern_Texture); } break; case PIC_OP_ABSOLUTE_PATTERN: if (_resourceType >= SCI_PICTURE_TYPE_SCI11) error("pic-operation absolute pattern inside sci1.1+ vector data"); while (vectorIsNonOpcode(data[curPos])) { vectorGetPatternTexture(data, curPos, pattern_Code, pattern_Texture); vectorGetAbsCoords(data, curPos, x, y); vectorPattern(x, y, pic_color, pic_priority, pic_control, pattern_Code, pattern_Texture); } break; case PIC_OP_OPX: // Extended functions if (isEGA) { #ifdef DEBUG_PICTURE_DRAW debug("* Picture ex op: %X (%s) at %d", data[curPos], picExOpcodeNamesEGA[data[curPos]], curPos); #endif switch (pic_op = data[curPos++]) { case PIC_OPX_EGA_SET_PALETTE_ENTRIES: while (vectorIsNonOpcode(data[curPos])) { pixel = data[curPos++]; if (pixel >= PIC_EGAPALETTE_TOTALSIZE) { error("picture trying to write to invalid EGA-palette"); } EGApalettes[pixel] = data[curPos++]; } break; case PIC_OPX_EGA_SET_PALETTE: pixel = data[curPos++]; if (pixel >= PIC_EGAPALETTE_COUNT) { error("picture trying to write to invalid palette %d", (int)pixel); } pixel *= PIC_EGAPALETTE_SIZE; for (i = 0; i < PIC_EGAPALETTE_SIZE; i++) { EGApalettes[pixel + i] = data[curPos++]; } break; case PIC_OPX_EGA_MONO0: curPos += 41; break; case PIC_OPX_EGA_MONO1: case PIC_OPX_EGA_MONO3: curPos++; break; case PIC_OPX_EGA_MONO2: case PIC_OPX_EGA_MONO4: break; case PIC_OPX_EGA_EMBEDDED_VIEW: vectorGetAbsCoordsNoMirror(data, curPos, x, y); size = data.getUint16LEAt(curPos); curPos += 2; // hardcoded in SSCI, 16 for SCI1early excluding Space Quest 4, 0 for anything else // fixes sq4 pictures 546+547 (Vohaul's head and Roger Jr trapped). Bug #5250 // fixes sq4 picture 631 (SQ1 view from cockpit). Bug 5249 if ((getSciVersion() <= SCI_VERSION_1_EARLY) && (g_sci->getGameId() != GID_SQ4)) { _priority = 16; } else { _priority = 0; } drawCelData(data, curPos, curPos + 8, 0, x, y, 0, 0, true); curPos += size; break; case PIC_OPX_EGA_SET_PRIORITY_TABLE: _ports->priorityBandsInit(data.subspan(curPos, 14)); curPos += 14; break; default: error("Unsupported sci1 extended pic-operation %X", pic_op); } } else { #ifdef DEBUG_PICTURE_DRAW debug("* Picture ex op: %X (%s) at %d", data[curPos], picExOpcodeNamesVGA[data[curPos]], curPos); #endif switch (pic_op = data[curPos++]) { case PIC_OPX_VGA_SET_PALETTE_ENTRIES: while (vectorIsNonOpcode(data[curPos])) { curPos++; // skip commands } break; case PIC_OPX_VGA_SET_PALETTE: if (_resMan->getViewType() == kViewAmiga || _resMan->getViewType() == kViewAmiga64) { if ((data[curPos] == 0x00) && (data[curPos + 1] == 0x01) && ((data[curPos + 32] & 0xF0) != 0xF0)) { // Left-Over VGA palette, we simply ignore it curPos += 256 + 4 + 1024; } else { // Setting half of the Amiga palette _palette->modifyAmigaPalette(data.subspan(curPos)); curPos += 32; } } else { curPos += 256 + 4; // Skip over mapping and timestamp for (i = 0; i < 256; i++) { palette.colors[i].used = data[curPos++]; palette.colors[i].r = data[curPos++]; palette.colors[i].g = data[curPos++]; palette.colors[i].b = data[curPos++]; } _palette->set(&palette, true); } break; case PIC_OPX_VGA_EMBEDDED_VIEW: // draw cel vectorGetAbsCoordsNoMirror(data, curPos, x, y); size = data.getUint16LEAt(curPos); curPos += 2; if (getSciVersion() <= SCI_VERSION_1_EARLY) { // During SCI1Early sierra always used 0 as priority for cels inside picture resources // fixes Space Quest 4 orange ship lifting off (bug #6446) _priority = 0; } else { _priority = pic_priority; // set global priority so the cel gets drawn using current priority as well } drawCelData(data, curPos, curPos + 8, 0, x, y, 0, 0, false); curPos += size; break; case PIC_OPX_VGA_PRIORITY_TABLE_EQDIST: _ports->priorityBandsInit(-1, data.getUint16LEAt(curPos), data.getUint16LEAt(curPos + 2)); curPos += 4; break; case PIC_OPX_VGA_PRIORITY_TABLE_EXPLICIT: _ports->priorityBandsInit(data.subspan(curPos, 14)); curPos += 14; break; default: error("Unsupported sci1 extended pic-operation %X", pic_op); } } break; case PIC_OP_TERMINATE: _priority = pic_priority; // Dithering EGA pictures if (isEGA) { _screen->dither(_addToFlag); switch (g_sci->getGameId()) { case GID_SQ3: switch (_resourceId) { case 154: // SQ3: intro, ship gets sucked in _screen->ditherForceDitheredColor(0xD0); break; default: break; } break; default: break; } } return; default: error("Unsupported pic-operation %X", pic_op); } if ((_EGAdrawingVisualize) && (isEGA)) { _screen->copyToScreen(); g_system->updateScreen(); g_system->delayMillis(10); } } error("picture vector data without terminator"); }
void GfxPicture::drawCelData(const SciSpan<const byte> &inbuffer, int headerPos, int rlePos, int literalPos, int16 drawX, int16 drawY, int16 pictureX, int16 pictureY, bool isEGA) { const SciSpan<const byte> headerPtr = inbuffer.subspan(headerPos); const SciSpan<const byte> rlePtr = inbuffer.subspan(rlePos); // displaceX, displaceY fields are ignored, and may contain garbage // (e.g. pic 261 in Dr. Brain 1 Spanish - bug #3614914) //int16 displaceX, displaceY; byte priority = _priority; byte clearColor; bool compression = true; byte curByte; int16 y, lastY, x, leftX, rightX; int pixelCount; uint16 width, height; // if the picture is not an overlay and we are also not in EGA mode, use priority 0 if (!isEGA && !_addToFlag) priority = 0; // Width/height here are always LE, even in Mac versions width = headerPtr.getUint16LEAt(0); height = headerPtr.getUint16LEAt(2); //displaceX = (signed char)headerPtr[4]; //displaceY = (unsigned char)headerPtr[5]; if (_resourceType == SCI_PICTURE_TYPE_SCI11) // SCI1.1 uses hardcoded clearcolor for pictures, even if cel header specifies otherwise clearColor = _screen->getColorWhite(); else clearColor = headerPtr[6]; //if (displaceX || displaceY) // error("unsupported embedded cel-data in picture"); // We will unpack cel-data into a temporary buffer and then plot it to screen // That needs to be done cause a mirrored picture may be requested pixelCount = width * height; Common::SpanOwner<SciSpan<byte> > celBitmap; celBitmap->allocate(pixelCount, _resource->name()); if (g_sci->getPlatform() == Common::kPlatformMacintosh && getSciVersion() >= SCI_VERSION_2) { // See GfxView::unpackCel() for why this black/white swap is done // This picture swap is only needed in SCI32, not SCI1.1 if (clearColor == 0) clearColor = 0xff; else if (clearColor == 0xff) clearColor = 0; } if (compression) { unpackCelData(inbuffer, *celBitmap, clearColor, rlePos, literalPos, _resMan->getViewType(), width, false); } else // No compression (some SCI32 pictures) memcpy(celBitmap->getUnsafeDataAt(0, pixelCount), rlePtr.getUnsafeDataAt(0, pixelCount), pixelCount); Common::Rect displayArea = _coordAdjuster->pictureGetDisplayArea(); // Horizontal clipping uint16 skipCelBitmapPixels = 0; int16 displayWidth = width; if (pictureX) { // horizontal scroll position for picture active, we need to adjust drawX accordingly drawX -= pictureX; if (drawX < 0) { skipCelBitmapPixels = -drawX; displayWidth -= skipCelBitmapPixels; drawX = 0; } } // Vertical clipping uint16 skipCelBitmapLines = 0; int16 displayHeight = height; if (pictureY) { // vertical scroll position for picture active, we need to adjust drawY accordingly // TODO: Finish this /*drawY -= pictureY; if (drawY < 0) { skipCelBitmapLines = -drawY; displayHeight -= skipCelBitmapLines; drawY = 0; }*/ } if (displayWidth > 0 && displayHeight > 0) { y = displayArea.top + drawY; lastY = MIN<int16>(height + y, displayArea.bottom); leftX = displayArea.left + drawX; rightX = MIN<int16>(displayWidth + leftX, displayArea.right); uint16 sourcePixelSkipPerRow = 0; if (width > rightX - leftX) sourcePixelSkipPerRow = width - (rightX - leftX); // Change clearcolor to white, if we dont add to an existing picture. That way we will paint everything on screen // but white and that won't matter because the screen is supposed to be already white. It seems that most (if not all) // SCI1.1 games use color 0 as transparency and SCI1 games use color 255 as transparency. Sierra SCI seems to paint // the whole data to screen and wont skip over transparent pixels. So this will actually make it work like Sierra. if (!_addToFlag) clearColor = _screen->getColorWhite(); byte drawMask = priority > 15 ? GFX_SCREEN_MASK_VISUAL : GFX_SCREEN_MASK_VISUAL | GFX_SCREEN_MASK_PRIORITY; SciSpan<const byte> ptr = *celBitmap; ptr += skipCelBitmapPixels; ptr += skipCelBitmapLines * width; if ((!isEGA) || (priority < 16)) { // VGA + EGA, EGA only checks priority, when given priority is below 16 if (!_mirroredFlag) { // Draw bitmap to screen x = leftX; while (y < lastY) { curByte = *ptr++; if ((curByte != clearColor) && (priority >= _screen->getPriority(x, y))) _screen->putPixel(x, y, drawMask, curByte, priority, 0); x++; if (x >= rightX) { ptr += sourcePixelSkipPerRow; x = leftX; y++; } } } else { // Draw bitmap to screen (mirrored) x = rightX - 1; while (y < lastY) { curByte = *ptr++; if ((curByte != clearColor) && (priority >= _screen->getPriority(x, y))) _screen->putPixel(x, y, drawMask, curByte, priority, 0); if (x == leftX) { ptr += sourcePixelSkipPerRow; x = rightX; y++; } x--; } } } else { // EGA, when priority is above 15 // we don't check priority and also won't set priority at all // fixes picture 48 of kq5 (island overview). Bug #5182 if (!_mirroredFlag) { // EGA+priority>15: Draw bitmap to screen x = leftX; while (y < lastY) { curByte = *ptr++; if (curByte != clearColor) _screen->putPixel(x, y, GFX_SCREEN_MASK_VISUAL, curByte, 0, 0); x++; if (x >= rightX) { ptr += sourcePixelSkipPerRow; x = leftX; y++; } } } else { // EGA+priority>15: Draw bitmap to screen (mirrored) x = rightX - 1; while (y < lastY) { curByte = *ptr++; if (curByte != clearColor) _screen->putPixel(x, y, GFX_SCREEN_MASK_VISUAL, curByte, 0, 0); if (x == leftX) { ptr += sourcePixelSkipPerRow; x = rightX; y++; } x--; } } } } }