Skip to content

Commit

Permalink
SLUDGE: Refactor image loading function and apply in backdrop
Browse files Browse the repository at this point in the history
  • Loading branch information
yinsimei authored and sev- committed Jul 13, 2017
1 parent 8c59f8d commit 786e4c3
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 648 deletions.
600 changes: 64 additions & 536 deletions engines/sludge/backdrop.cpp

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions engines/sludge/backdrop.h
Expand Up @@ -51,6 +51,7 @@ struct parallaxLayer {
GLubyte *texture;
GLuint textureName;
#endif
Graphics::Surface surface;
int width, height, speedX, speedY;
bool wrapS, wrapT;
unsigned short fileNum, fractionX, fractionY;
Expand All @@ -59,7 +60,9 @@ struct parallaxLayer {
parallaxLayer *prev;
};

void killAllBackDrop();
bool resizeBackdrop(int x, int y);
bool killResizeBackdrop(int x, int y);
void killBackDrop();
void loadBackDrop(int fileNum, int x, int y);
void mixBackDrop(int fileNum, int x, int y);
Expand All @@ -82,9 +85,7 @@ bool getRGBIntoStack(unsigned int x, unsigned int y, stackHandler *sH);
void killLightMap();
bool loadLightMap(int v);

#if 0
extern texture lightMap;
#endif

// And background parallax scrolling

Expand Down
2 changes: 1 addition & 1 deletion engines/sludge/builtin.cpp
Expand Up @@ -441,7 +441,7 @@ builtIn(setSceneDimensions) {
trimStack(fun->stack);
if (!getValueType(x, SVT_INT, fun->stack->thisVar)) return BR_ERROR;
trimStack(fun->stack);
if (resizeBackdrop(x, y)) {
if (killResizeBackdrop(x, y)) {
blankScreen(0, 0, x, y);
return BR_CONTINUE;
}
Expand Down
90 changes: 0 additions & 90 deletions engines/sludge/bytearray.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion engines/sludge/freeze.cpp
Expand Up @@ -191,7 +191,7 @@ bool freeze() {
zBuffer.tex = NULL;
#endif
// resizeBackdrop kills parallax stuff, light map, z-buffer...
if (!resizeBackdrop(winWidth, winHeight))
if (!killResizeBackdrop(winWidth, winHeight))
return fatal("Can't create new temporary backdrop buffer");

if (!NPOT_textures) {
Expand Down
4 changes: 3 additions & 1 deletion engines/sludge/graphics.h
Expand Up @@ -31,14 +31,16 @@
#endif
#endif

#include "graphics/surface.h"

namespace Sludge {

struct texture {
#if 0
GLubyte *data;
GLuint name;
#endif
int w, h;
Graphics::Surface surface;
double texW, texH;
};

Expand Down
138 changes: 138 additions & 0 deletions engines/sludge/imgloader.cpp
@@ -0,0 +1,138 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#include "common/debug.h"
#include "common/stream.h"
#include "image/png.h"
#include "graphics/surface.h"

#include "allfiles.h"
#include "imgloader.h"
#include "colours.h"
#include "sludge.h"

namespace Sludge {

bool ImgLoader::loadImage(Common::SeekableReadStream *stream, Graphics::Surface *dest, bool reserve) {
debug("Loading image at position: %i", stream->pos());
int32 start_ptr = stream->pos();
if (!loadPNGImage(stream, dest)) {
stream->seek(start_ptr);
if (!loadReserveImage(stream, dest, reserve)) {
return false;
}
}
return true;
}

bool ImgLoader::loadImage(Common::SeekableReadStream *stream, Graphics::Surface *dest) {
debug("Loading image at position: %i", stream->pos());
int32 start_ptr = stream->pos();
if (!loadPNGImage(stream, dest)) {
stream->seek(start_ptr);
if (!loadOtherImage(stream, dest)) {
return false;
}
}
return true;
}

bool ImgLoader::loadPNGImage(Common::SeekableReadStream *stream, Graphics::Surface *dest) {
::Image::PNGDecoder png;
if (!png.loadStream(*stream))
return false;
const Graphics::Surface *sourceSurface = png.getSurface();
Graphics::Surface *pngSurface = sourceSurface->convertTo(Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0), png.getPalette());
dest->copyFrom(*pngSurface);
pngSurface->free();
delete pngSurface;
return true;
}

bool ImgLoader::loadReserveImage(Common::SeekableReadStream *stream, Graphics::Surface *dest, int reserve) {
debug("Loading image at position: %i", stream->pos());
int32_t transCol = reserve ? -1 : 63519;
int n;
uint16 width = stream->readUint16BE();
uint16 height = stream->readUint16BE();

dest->create(width, height, Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0));
for (uint16 y = 0; y < height; y++) {
uint16 x = 0;
while (x < width) {
unsigned short c = (unsigned short)stream->readUint16BE();
if (c & 32) {
n = stream->readByte() + 1;
c -= 32;
} else {
n = 1;
}
while (n--) {
byte *target = (byte *)dest->getBasePtr(x, y);
if (c == transCol || c == 2015) {
target[0] = (byte)0;
target[1] = (byte)0;
target[2] = (byte)0;
target[3] = (byte)0;
} else {
target[0] = (byte)255;
target[1] = (byte)blueValue(c);
target[2] = (byte)greenValue(c);
target[3] = (byte)redValue(c);
}
x++;
}
}
}
return true;
}

bool ImgLoader::loadOtherImage(Common::SeekableReadStream *stream, Graphics::Surface *dest) {
int n;
uint16 width = stream->readUint16BE();
uint16 height = stream->readUint16BE();

dest->create(width, height, Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0));
for (uint16 y = 0; y < height; y++) {
uint16 x = 0;
while (x < width) {
unsigned short c = (unsigned short)stream->readUint16BE();
if (c & 32) {
n = stream->readByte() + 1;
c -= 32;
} else {
n = 1;
}
while (n--) {
byte *target = (byte *)dest->getBasePtr(x, y);
target[0] = (byte)255;
target[1] = (byte)blueValue(c);
target[2] = (byte)greenValue(c);
target[3] = (byte)redValue(c);
x++;
}
}
}
return true;
}

} // End of namespace Sludge
26 changes: 11 additions & 15 deletions engines/sludge/bytearray.h → engines/sludge/imgloader.h
Expand Up @@ -20,27 +20,23 @@
*
*/

#ifndef PRINCE_MHWANH_H
#define PRINCE_MHWANH_H
#ifndef SLUDGE_IMGLOADER_H
#define SLUDGE_IMGLOADER_H

#include "image/image_decoder.h"
#include "graphics/surface.h"
#include "common/file.h"

namespace Sludge {

class ByteArrayDecoder : public Image::ImageDecoder {
class ImgLoader {
protected:
ImgLoader() {}

public:
ByteArrayDecoder();
virtual ~ByteArrayDecoder();

// ImageDecoder API
void destroy();
virtual bool loadStream(Common::SeekableReadStream &stream);
virtual Graphics::Surface *getSurface() const { return _surface; }

private:
Graphics::Surface *_surface;
static bool loadImage(Common::SeekableReadStream *stream, Graphics::Surface *dest);
static bool loadImage(Common::SeekableReadStream *stream, Graphics::Surface *dest, bool reserve);
static bool loadPNGImage(Common::SeekableReadStream *stream, Graphics::Surface *dest);
static bool loadReserveImage(Common::SeekableReadStream *stream, Graphics::Surface *dest, int reserve);
static bool loadOtherImage(Common::SeekableReadStream *stream, Graphics::Surface *dest);
};

} // End of namespace Sludge
Expand Down
2 changes: 1 addition & 1 deletion engines/sludge/main_loop.cpp
Expand Up @@ -432,7 +432,7 @@ int main_loop(char *filename)

registerWindowForFatal();

if (!resizeBackdrop(winWidth, winHeight))
if (!killResizeBackdrop(winWidth, winHeight))
return fatal("Couldn't allocate memory for backdrop");

blankScreen(0, 0, winWidth, winHeight);
Expand Down
2 changes: 1 addition & 1 deletion engines/sludge/module.mk
Expand Up @@ -4,7 +4,6 @@ MODULE_OBJS := \
backdrop.o \
bg_effects.o \
builtin.o \
bytearray.o \
console.o \
cursors.o \
debug.o \
Expand All @@ -15,6 +14,7 @@ MODULE_OBJS := \
fonttext.o \
graphics.o \
helpers.o \
imgloader.o \
language.o \
line.o \
loadsave.o \
Expand Down

0 comments on commit 786e4c3

Please sign in to comment.