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 "image/png.h"
24 
25 #include "sludge/fileset.h"
26 #include "sludge/graphics.h"
27 #include "sludge/newfatal.h"
28 #include "sludge/sludge.h"
29 #include "sludge/zbuffer.h"
30 
31 namespace Sludge {
32 
killZBuffer()33 void GraphicsManager::killZBuffer() {
34 	if (_zBuffer->sprites) {
35 		for (int i = 0; i < _zBuffer->numPanels; ++i) {
36 			_zBuffer->sprites[i].free();
37 		}
38 		delete []_zBuffer->sprites;
39 		_zBuffer->sprites = nullptr;
40 	}
41 	_zBuffer->numPanels = 0;
42 	_zBuffer->originalNum = -1;
43 }
44 
sortZPal(int * oldpal,int * newpal,int size)45 void GraphicsManager::sortZPal(int *oldpal, int *newpal, int size) {
46 	int i, tmp;
47 
48 	for (i = 0; i < size; i++) {
49 		newpal[i] = i;
50 	}
51 
52 	if (size < 2)
53 		return;
54 
55 	for (i = 1; i < size; i++) {
56 		if (oldpal[newpal[i]] < oldpal[newpal[i - 1]]) {
57 			tmp = newpal[i];
58 			newpal[i] = newpal[i - 1];
59 			newpal[i - 1] = tmp;
60 			i = 0;
61 		}
62 	}
63 }
64 
setZBuffer(int num)65 bool GraphicsManager::setZBuffer(int num) {
66 	// if the backdrop has not been set yet
67 	// set zbuffer later
68 	if (!_backdropSurface.getPixels()) {
69 		_zBuffer->originalNum = num;
70 		return true;
71 	}
72 
73 	debug (kSludgeDebugGraphics, "Setting zBuffer");
74 	uint32 stillToGo = 0;
75 	int yPalette[16], sorted[16];
76 
77 	killZBuffer();
78 
79 	setResourceForFatal(num);
80 
81 	_zBuffer->originalNum = num;
82 	uint fsize = g_sludge->_resMan->openFileFromNum(num);
83 	if (!fsize)
84 		return false;
85 
86 	Common::SeekableReadStream *readStream = g_sludge->_resMan->getData();
87 
88 	g_sludge->_resMan->dumpFile(num, "zbuffer%04d.zbu");
89 
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 	int n = 0;
139 
140 	for (uint y = 0; y < _sceneHeight; y++) {
141 		for (uint x = 0; x < _sceneWidth; x++) {
142 			if (stillToGo == 0) {
143 				n = readStream->readByte();
144 				stillToGo = n >> 4;
145 				if (stillToGo == 15)
146 					stillToGo = readStream->readUint16BE() + 16l;
147 				else
148 					stillToGo++;
149 				n &= 15;
150 			}
151 
152 			for (int i = 0; i < _zBuffer->numPanels; ++i) {
153 				byte *target = (byte *)_zBuffer->sprites[i].getBasePtr(x, y);
154 				if (sorted[i] == n || i == 0) {
155 					byte *source = (byte *)_backdropSurface.getBasePtr(x, y);
156 					target[0] = source[0];
157 					target[1] = source[1];
158 					target[2] = source[2];
159 					target[3] = source[3];
160 				} else {
161 					target[0] = 0;
162 					target[1] = 0;
163 					target[2] = 0;
164 					target[3] = 0;
165 				}
166 			}
167 			stillToGo--;
168 		}
169 	}
170 
171 	g_sludge->_resMan->finishAccess();
172 	setResourceForFatal(-1);
173 
174 	if (!g_sludge->_dumpScripts)
175 		return true;
176 
177 	// Debug code to output light map image
178 
179 	for (int i = 0; i < _zBuffer->numPanels; ++i) {
180 		Common::DumpFile *outFile = new Common::DumpFile();
181 
182 		outFile->open(Common::String::format("dumps/zbuffer%04d-%d.png", num, i));
183 		Image::writePNG(*outFile, _zBuffer->sprites[i]);
184 		outFile->finalize();
185 		outFile->close();
186 		delete outFile;
187 	}
188 
189 	return true;
190 }
191 
drawZBuffer(int x,int y,bool upsidedown)192 void GraphicsManager::drawZBuffer(int x, int y, bool upsidedown) {
193 	if (!_zBuffer->numPanels || !_zBuffer->sprites)
194 		return;
195 
196 	g_sludge->_gfxMan->resetSpriteLayers(_zBuffer, x, y, upsidedown);
197 }
198 
saveZBuffer(Common::WriteStream * stream)199 void GraphicsManager::saveZBuffer(Common::WriteStream *stream) {
200 	if (_zBuffer->numPanels > 0) {
201 		stream->writeByte(1);
202 		stream->writeUint16BE(_zBuffer->originalNum);
203 	} else {
204 		stream->writeByte(0);
205 	}
206 }
207 
loadZBuffer(Common::SeekableReadStream * stream)208 bool GraphicsManager::loadZBuffer(Common::SeekableReadStream *stream) {
209 	if (stream->readByte()) {
210 		if (!setZBuffer(stream->readUint16BE()))
211 			return false;
212 	}
213 	return true;
214 }
215 
216 } // End of namespace Sludge
217