1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "common/debug.h"
24 #include "graphics/pixelformat.h"
25 #include "graphics/transparent_surface.h"
26
27 #include "sludge/allfiles.h"
28 #include "sludge/fileset.h"
29 #include "sludge/graphics.h"
30 #include "sludge/moreio.h"
31 #include "sludge/newfatal.h"
32 #include "sludge/sludge.h"
33 #include "sludge/sprites.h"
34 #include "sludge/zbuffer.h"
35
36 namespace Sludge {
37
killZBuffer()38 void GraphicsManager::killZBuffer() {
39 if (_zBuffer->sprites) {
40 for (int i = 0; i < _zBuffer->numPanels; ++i) {
41 _zBuffer->sprites[i].free();
42 }
43 delete []_zBuffer->sprites;
44 _zBuffer->sprites = nullptr;
45 }
46 _zBuffer->numPanels = 0;
47 _zBuffer->originalNum = -1;
48 }
49
sortZPal(int * oldpal,int * newpal,int size)50 void GraphicsManager::sortZPal(int *oldpal, int *newpal, int size) {
51 int i, tmp;
52
53 for (i = 0; i < size; i++) {
54 newpal[i] = i;
55 }
56
57 if (size < 2)
58 return;
59
60 for (i = 1; i < size; i++) {
61 if (oldpal[newpal[i]] < oldpal[newpal[i - 1]]) {
62 tmp = newpal[i];
63 newpal[i] = newpal[i - 1];
64 newpal[i - 1] = tmp;
65 i = 0;
66 }
67 }
68 }
69
setZBuffer(int num)70 bool GraphicsManager::setZBuffer(int num) {
71 // if the backdrop has not been set yet
72 // set zbuffer later
73 if (!_backdropSurface.getPixels()) {
74 _zBuffer->originalNum = num;
75 return true;
76 }
77
78 debug (kSludgeDebugGraphics, "Setting zBuffer");
79 uint32 stillToGo = 0;
80 int yPalette[16], sorted[16];
81
82 killZBuffer();
83
84 setResourceForFatal(num);
85
86 _zBuffer->originalNum = num;
87 if (!g_sludge->_resMan->openFileFromNum(num))
88 return false;
89 Common::ReadStream *readStream = g_sludge->_resMan->getData();
90 if (readStream->readByte() != 'S')
91 return fatal("Not a Z-buffer file");
92 if (readStream->readByte() != 'z')
93 return fatal("Not a Z-buffer file");
94 if (readStream->readByte() != 'b')
95 return fatal("Not a Z-buffer file");
96
97 uint width, height;
98 switch (readStream->readByte()) {
99 case 0:
100 width = 640;
101 height = 480;
102 break;
103
104 case 1:
105 width = readStream->readUint16BE();
106 height = readStream->readUint16BE();
107 break;
108
109 default:
110 return fatal("Extended Z-buffer format not supported in this version of the SLUDGE engine");
111 }
112 if (width != _sceneWidth || height != _sceneHeight) {
113 Common::String tmp = Common::String::format("Z-w: %d Z-h:%d w: %d, h:%d", width, height, _sceneWidth, _sceneHeight);
114 return fatal("Z-buffer width and height don't match scene width and height", tmp);
115 }
116
117 _zBuffer->numPanels = readStream->readByte();
118 debugC(2, kSludgeDebugZBuffer, "Loading zBuffer : %i panels", _zBuffer->numPanels);
119 for (int y = 0; y < _zBuffer->numPanels; y++) {
120 yPalette[y] = readStream->readUint16BE();
121 }
122 sortZPal(yPalette, sorted, _zBuffer->numPanels);
123 for (int y = 0; y < _zBuffer->numPanels; y++) {
124 _zBuffer->panel[y] = yPalette[sorted[y]];
125 debugC(2, kSludgeDebugZBuffer, "Y-value : %i", _zBuffer->panel[y]);
126 }
127
128 int picWidth = _sceneWidth;
129 int picHeight = _sceneHeight;
130
131 _zBuffer->sprites = nullptr;
132 _zBuffer->sprites = new Graphics::Surface[_zBuffer->numPanels];
133
134 for (int i = 0; i < _zBuffer->numPanels; ++i) {
135 _zBuffer->sprites[i].create(picWidth, picHeight, *g_sludge->getScreenPixelFormat());
136 }
137
138 for (uint y = 0; y < _sceneHeight; y++) {
139 for (uint x = 0; x < _sceneWidth; x++) {
140 int n = 0;
141 if (stillToGo == 0) {
142 n = readStream->readByte();
143 stillToGo = n >> 4;
144 if (stillToGo == 15)
145 stillToGo = readStream->readUint16BE() + 16l;
146 else
147 stillToGo++;
148 n &= 15;
149 }
150 for (int i = 0; i < _zBuffer->numPanels; ++i) {
151 byte *target = (byte *)_zBuffer->sprites[i].getBasePtr(x, y);
152 if (n && (sorted[i] == n || i == 0)) {
153 byte *source = (byte *)_backdropSurface.getBasePtr(x, y);
154 target[0] = source[0];
155 target[1] = source[1];
156 target[2] = source[2];
157 target[3] = source[3];
158 } else {
159 target[0] = 0;
160 target[1] = 0;
161 target[2] = 0;
162 target[3] = 0;
163 }
164 }
165 stillToGo--;
166 }
167 }
168 g_sludge->_resMan->finishAccess();
169 setResourceForFatal(-1);
170 return true;
171 }
172
drawZBuffer(int x,int y,bool upsidedown)173 void GraphicsManager::drawZBuffer(int x, int y, bool upsidedown) {
174 if (!_zBuffer->numPanels || !_zBuffer->sprites)
175 return;
176
177 g_sludge->_gfxMan->resetSpriteLayers(_zBuffer, x, y, upsidedown);
178 }
179
saveZBuffer(Common::WriteStream * stream)180 void GraphicsManager::saveZBuffer(Common::WriteStream *stream) {
181 if (_zBuffer->numPanels > 0) {
182 stream->writeByte(1);
183 stream->writeUint16BE(_zBuffer->originalNum);
184 } else {
185 stream->writeByte(0);
186 }
187 }
188
loadZBuffer(Common::SeekableReadStream * stream)189 bool GraphicsManager::loadZBuffer(Common::SeekableReadStream *stream) {
190 if (stream->readByte()) {
191 if (!setZBuffer(stream->readUint16BE()))
192 return false;
193 }
194 return true;
195 }
196
197 } // End of namespace Sludge
198