Commit 1738194a authored by alistert's avatar alistert

Fixed sprite loading issue. Introduced optional upper limit for loading of shorts.

parent b3ef0f9a
......@@ -62,7 +62,7 @@ int Bonus::loadSprites () {
file->seek(2, true);
sprites = file->loadShort();
sprites = file->loadShort(256);
spriteSet = new Sprite[sprites];
for (count = 0; count < sprites; count++) {
......@@ -71,8 +71,8 @@ int Bonus::loadSprites () {
spriteSet[count].yOffset = 0;
// Load dimensions
width = file->loadShort();
height = file->loadShort();
width = file->loadShort(SW);
height = file->loadShort(SH);
pixelsLength = file->loadShort();
maskLength = file->loadShort();
......@@ -83,10 +83,10 @@ int Bonus::loadSprites () {
// Masked
width <<= 2;
pos = file->tell() + (pixelsLength << 2) + maskLength;
pos = file->tell() + (pixelsLength << 2) + maskLength;
// Read scrambled, masked pixel data
pixels = file->loadPixels(width * height, 0);
pixels = file->loadPixels(width * height, 0);
spriteSet[count].setPixels(pixels, width, height, 0);
delete[] pixels;
......
......@@ -169,6 +169,25 @@ unsigned short int File::loadShort () {
}
unsigned short int File::loadShort (unsigned short int max) {
unsigned short int val;
val = loadShort();
if (val > max) {
logError("Oversized value in file", filePath);
return max;
}
return val;
}
void File::storeShort (unsigned short int val) {
fputc(val & 255, file);
......@@ -387,7 +406,7 @@ unsigned char* File::loadPixels (int length) {
}
unsigned char* File::loadPixels (int length, int key) {
unsigned char* File::loadPixels (int length, int key) {
unsigned char* pixels;
unsigned char* sorted;
......
......@@ -50,6 +50,7 @@ class File {
unsigned char loadChar ();
void storeChar (unsigned char val);
unsigned short int loadShort ();
unsigned short int loadShort (unsigned short int max);
void storeShort (unsigned short int val);
signed long int loadInt ();
void storeInt (signed long int val);
......
......@@ -82,7 +82,7 @@ Font::Font (const char* fileName) {
size = file->loadShort();
if (size) {
if (size > 4) {
pixels = file->loadRLE(size);
......@@ -91,7 +91,10 @@ Font::Font (const char* fileName) {
height = pixels[2];
height += pixels[3] << 8;
characters[count] = createSurface(pixels + 4, width, height);
if (size - 4 >= width * height)
characters[count] = createSurface(pixels + 4, width, height);
else
characters[count] = createSurface(blank, 3, 1);
delete[] pixels;
......@@ -230,7 +233,7 @@ Font::Font (bool bonus) {
fileSize = file->getSize();
nCharacters = file->loadShort();
nCharacters = file->loadShort(256);
if (bonus) {
......@@ -264,8 +267,8 @@ Font::Font (bool bonus) {
}
width = file->loadShort();
height = file->loadShort();
width = file->loadShort(SW);
height = file->loadShort(SH);
if (bonus) width = (width + 3) & ~3;
else width <<= 2;
......
......@@ -61,8 +61,8 @@ DemoLevel::DemoLevel (const char* fileName) {
if (file->loadShort() == 0) throw E_DEMOTYPE;
// Level file to load
lNum = file->loadShort();
wNum = file->loadShort();
lNum = file->loadShort(9);
wNum = file->loadShort(999);
levelFile = createFileName(F_LEVEL, lNum, wNum);
// Difficulty
......
......@@ -155,20 +155,20 @@ void Level::loadSprite (File* file, Sprite* sprite) {
maskOffset = file->loadShort();
pos = file->loadShort() << 2;
// Sprites can be either masked or not masked.
if (maskOffset) {
// Masked
height++;
// Skip to mask
file->seek(maskOffset, false);
// Find the end of the data
// Find the end of the data
pos += file->tell() + ((width >> 2) * height);
// Read scrambled, masked pixel data
pixels = file->loadPixels(width * height, SKEY);
sprite->setPixels(pixels, width, height, SKEY);
......@@ -199,7 +199,8 @@ int Level::loadSprites (char * fileName) {
File* mainFile = NULL;
File* specFile = NULL;
int count;
int count;
bool loaded;
// Open fileName
......@@ -214,7 +215,7 @@ int Level::loadSprites (char * fileName) {
}
// This function loads all the sprites, not fust those in fileName
// This function loads all the sprites, not just those in fileName
try {
mainFile = new File(F_MAINCHAR, false);
......@@ -228,7 +229,7 @@ int Level::loadSprites (char * fileName) {
}
sprites = specFile->loadShort();
sprites = specFile->loadShort(256);
// Include space in the sprite set for the blank sprite at the end
spriteSet = new Sprite[sprites + 1];
......@@ -248,47 +249,48 @@ int Level::loadSprites (char * fileName) {
// Loop through all the sprites to be loaded
for (count = 0; count < sprites; count++) {
loaded = false;
if (mainFile->loadChar() == 0xFF) {
// Go to the next sprite/file indicator
mainFile->seek(1, false);
} else {
// Return to the start of the sprite
mainFile->seek(-1, false);
// Load the individual sprite data
loadSprite(mainFile, spriteSet + count);
loaded = true;
}
if (specFile->loadChar() == 0xFF) {
// Go to the next sprite/file indicator
specFile->seek(1, false);
if (mainFile->loadChar() == 0xFF) {
// Go to the next sprite/file indicator
mainFile->seek(1, false);
/* Both fileName and mainchar.000 have file indicators, so
create a blank sprite */
spriteSet[count].clearPixels();
continue;
} else {
// Return to the start of the sprite
mainFile->seek(-1, false);
// Load the individual sprite data
loadSprite(mainFile, spriteSet + count);
}
} else {
// Return to the start of the sprite
specFile->seek(-1, false);
// Go to the main file's next sprite/file indicator
mainFile->seek(2, false);
// Load the individual sprite data
loadSprite(specFile, spriteSet + count);
loadSprite(specFile, spriteSet + count);
loaded = true;
}
/* If both fileName and mainchar.000 have file indicators, create a
blank sprite */
if (!loaded) spriteSet[count].clearPixels();
// Check if the next sprite exists
// If not, create blank sprites for the remainder
if (specFile->tell() >= specFile->getSize()) {
......@@ -848,8 +850,8 @@ int Level::load (char* fileName, unsigned char diff, bool checkpoint) {
// The players' initial coordinates
startX = file->loadShort();
startY = file->loadShort() + 1;
startX = file->loadShort(LW);
startY = file->loadShort(LH) + 1;
// Next level
......
......@@ -211,8 +211,8 @@ int loadMain (int argc, char *argv[]) {
if (file && (file->loadChar() == 3)) {
// Read video settings
screenW = file->loadShort();
screenH = file->loadShort();
screenW = file->loadShort(7680);
screenH = file->loadShort(4800);
scaleFactor = file->loadChar();
#ifndef FULLSCREEN_ONLY
......
......@@ -285,7 +285,7 @@ void Scene::loadAni (File *f, int dataIndex) {
}
} else if (type == EPlayListAniHeader) {// PL
} else if (type == EPlayListAniHeader) {// PL
int pos = f->tell();
int nextPos = f->tell();
LOG("PL Read position", pos);
......@@ -354,13 +354,13 @@ void Scene::loadAni (File *f, int dataIndex) {
case EFFAniHeader:
{
unsigned char* blockData = f->loadBlock(size);
animations->addFrame(EFFAniHeader, blockData, size);
animations->addFrame(EFFAniHeader, blockData, size);
}
break;
case ERNAniHeader:
case ERBAniHeader:
case ERLAniHeader:
case ERBAniHeader:
case ERLAniHeader:
case EMXAniHeader:
break;
case ERRAniHeader: // Reverse animation when end found
......@@ -374,9 +374,9 @@ void Scene::loadAni (File *f, int dataIndex) {
}break;
case ESquareAniHeader: // Full screen animation frame, that does n't clear the screen first.
{
{
unsigned char* blockData = f->loadBlock(size);
animations->addFrame(ESquareAniHeader, blockData, size);
animations->addFrame(ESquareAniHeader, blockData, size);
}
break;
......@@ -477,11 +477,11 @@ void Scene::loadData (File *f) {
LOG("Data Type", "Image");
LOG("Data Type Image index", loop);
unsigned short int width = f->loadShort(); // get width
unsigned short int width = f->loadShort(SW); // get width
unsigned short int height;
if (type == 3) height = f->loadChar(); // Get height
else height = f->loadShort(); // Get height
else height = f->loadShort(SH); // Get height
f->seek(-2, false);
images = new SceneImage(images);
......@@ -520,7 +520,7 @@ void Scene::loadScripts (File *f) {
int textAlignment = 0;
int textFont = 0;
int textShadow = -1;
for(loop = 0; loop < scriptItems; loop++) {
LOG("\nParse Script", loop);
......@@ -563,12 +563,12 @@ void Scene::loadScripts (File *f) {
case ESceneAnimation:
{
pages[loop].animLoops = f->loadInt();
pages[loop].animSpeed = f->loadShort();
pages[loop].animSpeed = f->loadShort();
pages[loop].animIndex = f->loadShort();
LOG("ESceneAnimation loops", pages[loop].animLoops);
LOG("ESceneAnimation speed", pages[loop].animSpeed);
LOG("ESceneAnimation anim num", pages[loop].animIndex);
LOG("ESceneAnimation anim num", pages[loop].animIndex);
}
break;
......@@ -584,7 +584,7 @@ void Scene::loadScripts (File *f) {
{
unsigned char fadein = f->loadChar();
LOG("ESceneFadeType", fadein);
LOG("ESceneFadeType", fadein);
}
......@@ -742,7 +742,7 @@ void Scene::loadScripts (File *f) {
f->loadChar(); // Skip this value since shadows are turned off
textShadow = -1; // Turn off shadow , -1 means no shadow colour
}
LOG("ESceneTextShadow", textShadow);
}
......@@ -828,7 +828,7 @@ void Scene::loadScripts (File *f) {
text->alignment = textAlignment;
text->fontId = textFont;
text->shadowColour = textShadow;
if(textPosX != -1) {
text->x = textPosX;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment