2017-05-26 05:24:38 +02:00
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
*/
|
2017-06-05 19:10:47 +02:00
|
|
|
|
2017-06-21 14:47:44 +02:00
|
|
|
#include "common/debug.h"
|
|
|
|
#include "graphics/pixelformat.h"
|
|
|
|
#include "graphics/transparent_surface.h"
|
|
|
|
|
2017-06-05 19:10:47 +02:00
|
|
|
#include "sludge/allfiles.h"
|
|
|
|
#include "sludge/zbuffer.h"
|
|
|
|
#include "sludge/fileset.h"
|
|
|
|
#include "sludge/moreio.h"
|
|
|
|
#include "sludge/newfatal.h"
|
2017-06-21 14:47:44 +02:00
|
|
|
#include "sludge/sludge.h"
|
2017-06-21 16:24:55 +02:00
|
|
|
#include "sludge/sprites.h"
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-05-26 21:25:11 +02:00
|
|
|
namespace Sludge {
|
|
|
|
|
2017-07-01 22:01:26 +02:00
|
|
|
int zBufferToSet = -1;
|
2017-05-26 05:24:38 +02:00
|
|
|
zBufferData zBuffer;
|
2017-07-13 23:42:41 -04:00
|
|
|
extern uint sceneWidth, sceneHeight;
|
2017-06-21 14:47:44 +02:00
|
|
|
extern Graphics::Surface backdropSurface;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
|
|
|
void killZBuffer() {
|
2017-06-21 14:47:44 +02:00
|
|
|
if (zBuffer.sprites) {
|
|
|
|
for (int i = 0; i < zBuffer.numPanels; ++i) {
|
|
|
|
zBuffer.sprites[i].free();
|
|
|
|
}
|
|
|
|
delete []zBuffer.sprites;
|
|
|
|
zBuffer.sprites = nullptr;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
|
|
|
zBuffer.numPanels = 0;
|
|
|
|
zBuffer.originalNum = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sortZPal(int *oldpal, int *newpal, int size) {
|
|
|
|
int i, tmp;
|
|
|
|
|
2017-05-29 08:02:59 +02:00
|
|
|
for (i = 0; i < size; i++) {
|
2017-05-26 05:24:38 +02:00
|
|
|
newpal[i] = i;
|
|
|
|
}
|
|
|
|
|
2017-05-29 08:02:59 +02:00
|
|
|
if (size < 2)
|
|
|
|
return;
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-05-29 08:02:59 +02:00
|
|
|
for (i = 1; i < size; i++) {
|
2017-05-26 05:24:38 +02:00
|
|
|
if (oldpal[newpal[i]] < oldpal[newpal[i - 1]]) {
|
|
|
|
tmp = newpal[i];
|
|
|
|
newpal[i] = newpal[i - 1];
|
|
|
|
newpal[i - 1] = tmp;
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 14:47:44 +02:00
|
|
|
bool setZBuffer(int num) {
|
2017-07-01 22:01:26 +02:00
|
|
|
// if the backdrop has not been set yet
|
|
|
|
// set zbuffer later
|
|
|
|
if (!backdropSurface.getPixels()) {
|
|
|
|
zBufferToSet = num;
|
|
|
|
return true;
|
|
|
|
}
|
2017-07-01 22:06:46 +02:00
|
|
|
|
|
|
|
debug (kSludgeDebugGraphics, "Setting zBuffer");
|
2017-06-05 19:35:03 +02:00
|
|
|
uint32 stillToGo = 0;
|
2017-05-26 05:24:38 +02:00
|
|
|
int yPalette[16], sorted[16], sortback[16];
|
|
|
|
|
|
|
|
killZBuffer();
|
|
|
|
|
2017-06-21 14:47:44 +02:00
|
|
|
setResourceForFatal(num);
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-06-21 14:47:44 +02:00
|
|
|
zBuffer.originalNum = num;
|
2017-07-18 19:03:45 +02:00
|
|
|
if (!g_sludge->_resMan->openFileFromNum(num))
|
2017-06-05 11:49:19 +02:00
|
|
|
return false;
|
2017-07-18 19:03:45 +02:00
|
|
|
Common::ReadStream *readStream = g_sludge->_resMan->getData();
|
|
|
|
if (readStream->readByte() != 'S')
|
2017-06-05 11:49:19 +02:00
|
|
|
return fatal("Not a Z-buffer file");
|
2017-07-18 19:03:45 +02:00
|
|
|
if (readStream->readByte() != 'z')
|
2017-06-05 11:49:19 +02:00
|
|
|
return fatal("Not a Z-buffer file");
|
2017-07-18 19:03:45 +02:00
|
|
|
if (readStream->readByte() != 'b')
|
2017-06-05 11:49:19 +02:00
|
|
|
return fatal("Not a Z-buffer file");
|
|
|
|
|
2017-07-14 11:59:38 +01:00
|
|
|
uint width, height;
|
2017-07-18 19:03:45 +02:00
|
|
|
switch (readStream->readByte()) {
|
2017-06-21 14:47:44 +02:00
|
|
|
case 0:
|
|
|
|
width = 640;
|
|
|
|
height = 480;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2017-07-18 19:03:45 +02:00
|
|
|
width = readStream->readUint16BE();
|
|
|
|
height = readStream->readUint16BE();
|
2017-06-21 14:47:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fatal("Extended Z-buffer format not supported in this version of the SLUDGE engine");
|
|
|
|
}
|
|
|
|
if (width != sceneWidth || height != sceneHeight) {
|
2017-07-15 14:18:17 +02:00
|
|
|
Common::String tmp = Common::String::format("Z-w: %d Z-h:%d w: %d, h:%d", width, height, sceneWidth, sceneHeight);
|
2017-06-21 14:47:44 +02:00
|
|
|
return fatal("Z-buffer width and height don't match scene width and height", tmp);
|
|
|
|
}
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-18 19:03:45 +02:00
|
|
|
zBuffer.numPanels = readStream->readByte();
|
2017-06-21 14:47:44 +02:00
|
|
|
for (int y = 0; y < zBuffer.numPanels; y++) {
|
2017-07-18 19:03:45 +02:00
|
|
|
yPalette[y] = readStream->readUint16BE();
|
2017-06-21 14:47:44 +02:00
|
|
|
}
|
|
|
|
sortZPal(yPalette, sorted, zBuffer.numPanels);
|
|
|
|
for (int y = 0; y < zBuffer.numPanels; y++) {
|
|
|
|
zBuffer.panel[y] = yPalette[sorted[y]];
|
|
|
|
sortback[sorted[y]] = y;
|
|
|
|
}
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-06-21 14:47:44 +02:00
|
|
|
int picWidth = sceneWidth;
|
|
|
|
int picHeight = sceneHeight;
|
|
|
|
|
|
|
|
zBuffer.sprites = nullptr;
|
|
|
|
zBuffer.sprites = new Graphics::Surface[zBuffer.numPanels];
|
|
|
|
|
|
|
|
for (int i = 0; i < zBuffer.numPanels; ++i) {
|
|
|
|
zBuffer.sprites[i].create(picWidth, picHeight, *g_sludge->getScreenPixelFormat());
|
|
|
|
}
|
|
|
|
|
2017-07-14 11:59:38 +01:00
|
|
|
for (uint y = 0; y < sceneHeight; y++) {
|
|
|
|
for (uint x = 0; x < sceneWidth; x++) {
|
2017-06-21 14:47:44 +02:00
|
|
|
int n;
|
|
|
|
if (stillToGo == 0) {
|
2017-07-18 19:03:45 +02:00
|
|
|
n = readStream->readByte();
|
2017-06-21 14:47:44 +02:00
|
|
|
stillToGo = n >> 4;
|
|
|
|
if (stillToGo == 15)
|
2017-07-18 19:03:45 +02:00
|
|
|
stillToGo = readStream->readUint16BE() + 16l;
|
2017-06-21 14:47:44 +02:00
|
|
|
else
|
|
|
|
stillToGo++;
|
|
|
|
n &= 15;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < zBuffer.numPanels; ++i) {
|
|
|
|
byte *target = (byte *)zBuffer.sprites[i].getBasePtr(x, y);
|
|
|
|
if (n && (sortback[i] == n || i == 0)) {
|
|
|
|
byte *source = (byte *)backdropSurface.getBasePtr(x, y);
|
|
|
|
target[0] = source[0];
|
|
|
|
target[1] = source[1];
|
|
|
|
target[2] = source[2];
|
|
|
|
target[3] = source[3];
|
|
|
|
} else {
|
|
|
|
target[0] = 0;
|
|
|
|
target[1] = 0;
|
|
|
|
target[2] = 0;
|
|
|
|
target[3] = 0;
|
2017-06-05 11:49:19 +02:00
|
|
|
}
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
2017-06-21 14:47:44 +02:00
|
|
|
stillToGo--;
|
2017-05-26 05:24:38 +02:00
|
|
|
}
|
2017-06-21 14:47:44 +02:00
|
|
|
}
|
2017-07-18 19:03:45 +02:00
|
|
|
g_sludge->_resMan->finishAccess();
|
2017-06-21 14:47:44 +02:00
|
|
|
setResourceForFatal(-1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawZBuffer(int x, int y, bool upsidedown) {
|
|
|
|
if (!zBuffer.numPanels || !zBuffer.sprites)
|
|
|
|
return;
|
|
|
|
|
2017-06-21 16:24:55 +02:00
|
|
|
resetSpriteLayers(&zBuffer, x, y, upsidedown);
|
2017-07-15 17:33:10 +02:00
|
|
|
}
|
2017-05-26 05:24:38 +02:00
|
|
|
|
2017-07-15 17:33:10 +02:00
|
|
|
} // End of namespace Sludge
|