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 "neverhood/graphics.h"
24 #include "neverhood/resource.h"
25 #include "neverhood/screen.h"
26
27 namespace Neverhood {
28
BaseSurface(NeverhoodEngine * vm,int priority,int16 width,int16 height,Common::String name)29 BaseSurface::BaseSurface(NeverhoodEngine *vm, int priority, int16 width, int16 height, Common::String name)
30 : _vm(vm), _priority(priority), _visible(true), _transparent(true),
31 _clipRects(NULL), _clipRectsCount(0), _version(0), _name(name) {
32
33 _drawRect.x = 0;
34 _drawRect.y = 0;
35 _drawRect.width = width;
36 _drawRect.height = height;
37 _sysRect.x = 0;
38 _sysRect.y = 0;
39 _sysRect.width = (width + 3) & 0xFFFC; // align by 4 bytes
40 _sysRect.height = height;
41 _clipRect.x1 = 0;
42 _clipRect.y1 = 0;
43 _clipRect.x2 = 640;
44 _clipRect.y2 = 480;
45 _surface = new Graphics::Surface();
46 _surface->create(_sysRect.width, _sysRect.height, Graphics::PixelFormat::createFormatCLUT8());
47 }
48
~BaseSurface()49 BaseSurface::~BaseSurface() {
50 _surface->free();
51 delete _surface;
52 }
53
draw()54 void BaseSurface::draw() {
55 if (_surface && _visible && _drawRect.width > 0 && _drawRect.height > 0) {
56 if (_clipRects && _clipRectsCount) {
57 _vm->_screen->drawSurfaceClipRects(_surface, _drawRect, _clipRects, _clipRectsCount, _transparent, _version);
58 } else if (_sysRect.x == 0 && _sysRect.y == 0) {
59 _vm->_screen->drawSurface2(_surface, _drawRect, _clipRect, _transparent, _version);
60 } else {
61 _vm->_screen->drawUnk(_surface, _drawRect, _sysRect, _clipRect, _transparent, _version);
62 }
63 }
64 }
65
clear()66 void BaseSurface::clear() {
67 _surface->fillRect(Common::Rect(0, 0, _surface->w, _surface->h), 0);
68 ++_version;
69 }
70
drawSpriteResource(SpriteResource & spriteResource)71 void BaseSurface::drawSpriteResource(SpriteResource &spriteResource) {
72 if (spriteResource.getDimensions().width <= _drawRect.width &&
73 spriteResource.getDimensions().height <= _drawRect.height) {
74 clear();
75 spriteResource.draw(_surface, false, false);
76 ++_version;
77 }
78 }
79
drawSpriteResourceEx(SpriteResource & spriteResource,bool flipX,bool flipY,int16 width,int16 height)80 void BaseSurface::drawSpriteResourceEx(SpriteResource &spriteResource, bool flipX, bool flipY, int16 width, int16 height) {
81 if (spriteResource.getDimensions().width <= _sysRect.width &&
82 spriteResource.getDimensions().height <= _sysRect.height) {
83 if (width > 0 && width <= _sysRect.width)
84 _drawRect.width = width;
85 if (height > 0 && height <= _sysRect.height)
86 _drawRect.height = height;
87 if (_surface) {
88 clear();
89 spriteResource.draw(_surface, flipX, flipY);
90 ++_version;
91 }
92 }
93 }
94
drawAnimResource(AnimResource & animResource,uint frameIndex,bool flipX,bool flipY,int16 width,int16 height)95 void BaseSurface::drawAnimResource(AnimResource &animResource, uint frameIndex, bool flipX, bool flipY, int16 width, int16 height) {
96 if (width > 0 && width <= _sysRect.width)
97 _drawRect.width = width;
98 if (height > 0 && height <= _sysRect.height)
99 _drawRect.height = height;
100 if (_surface) {
101 clear();
102 if (frameIndex < animResource.getFrameCount()) {
103 animResource.draw(frameIndex, _surface, flipX, flipY);
104 ++_version;
105 }
106 }
107 }
108
drawMouseCursorResource(MouseCursorResource & mouseCursorResource,int frameNum)109 void BaseSurface::drawMouseCursorResource(MouseCursorResource &mouseCursorResource, int frameNum) {
110 if (frameNum < 3) {
111 mouseCursorResource.draw(frameNum, _surface);
112 ++_version;
113 }
114 }
115
copyFrom(Graphics::Surface * sourceSurface,int16 x,int16 y,NDrawRect & sourceRect)116 void BaseSurface::copyFrom(Graphics::Surface *sourceSurface, int16 x, int16 y, NDrawRect &sourceRect) {
117 // Copy a rectangle from sourceSurface, 0 is the transparent color
118 // Clipping is performed against the right/bottom border since x, y will always be >= 0
119
120 if (x + sourceRect.width > _surface->w)
121 sourceRect.width = _surface->w - x - 1;
122
123 if (y + sourceRect.height > _surface->h)
124 sourceRect.height = _surface->h - y - 1;
125
126 byte *source = (byte*)sourceSurface->getBasePtr(sourceRect.x, sourceRect.y);
127 byte *dest = (byte*)_surface->getBasePtr(x, y);
128 int height = sourceRect.height;
129 while (height--) {
130 for (int xc = 0; xc < sourceRect.width; xc++)
131 if (source[xc] != 0)
132 dest[xc] = source[xc];
133 source += sourceSurface->pitch;
134 dest += _surface->pitch;
135 }
136 ++_version;
137 }
138
139 // ShadowSurface
140
ShadowSurface(NeverhoodEngine * vm,int priority,int16 width,int16 height,BaseSurface * shadowSurface)141 ShadowSurface::ShadowSurface(NeverhoodEngine *vm, int priority, int16 width, int16 height, BaseSurface *shadowSurface)
142 : BaseSurface(vm, priority, width, height, "shadow"), _shadowSurface(shadowSurface) {
143 // Empty
144 }
145
draw()146 void ShadowSurface::draw() {
147 if (_surface && _visible && _drawRect.width > 0 && _drawRect.height > 0) {
148 _vm->_screen->drawSurface2(_surface, _drawRect, _clipRect, _transparent, _version, _shadowSurface->getSurface());
149 }
150 }
151
152 // FontSurface
153
FontSurface(NeverhoodEngine * vm,NPointArray * tracking,uint charsPerRow,uint16 numRows,byte firstChar,uint16 charWidth,uint16 charHeight)154 FontSurface::FontSurface(NeverhoodEngine *vm, NPointArray *tracking, uint charsPerRow, uint16 numRows, byte firstChar, uint16 charWidth, uint16 charHeight)
155 : BaseSurface(vm, 0, charWidth * charsPerRow, charHeight * numRows, "font"), _charsPerRow(charsPerRow), _numRows(numRows),
156 _firstChar(firstChar), _charWidth(charWidth), _charHeight(charHeight), _tracking(NULL) {
157
158 _tracking = new NPointArray();
159 *_tracking = *tracking;
160
161 }
162
FontSurface(NeverhoodEngine * vm,uint32 fileHash,uint charsPerRow,uint16 numRows,byte firstChar,uint16 charWidth,uint16 charHeight)163 FontSurface::FontSurface(NeverhoodEngine *vm, uint32 fileHash, uint charsPerRow, uint16 numRows, byte firstChar, uint16 charWidth, uint16 charHeight)
164 : BaseSurface(vm, 0, charWidth * charsPerRow, charHeight * numRows, "font"), _charsPerRow(charsPerRow), _numRows(numRows),
165 _firstChar(firstChar), _charWidth(charWidth), _charHeight(charHeight), _tracking(NULL) {
166
167 SpriteResource fontSpriteResource(_vm);
168 fontSpriteResource.load(fileHash, true);
169 drawSpriteResourceEx(fontSpriteResource, false, false, 0, 0);
170 }
171
~FontSurface()172 FontSurface::~FontSurface() {
173 delete _tracking;
174 }
175
drawChar(BaseSurface * destSurface,int16 x,int16 y,byte chr)176 void FontSurface::drawChar(BaseSurface *destSurface, int16 x, int16 y, byte chr) {
177 NDrawRect sourceRect;
178 chr -= _firstChar;
179 sourceRect.x = (chr % _charsPerRow) * _charWidth;
180 sourceRect.y = (chr / _charsPerRow) * _charHeight;
181 sourceRect.width = _charWidth;
182 sourceRect.height = _charHeight;
183 destSurface->copyFrom(_surface, x, y, sourceRect);
184 }
185
drawString(BaseSurface * destSurface,int16 x,int16 y,const byte * string,int stringLen)186 void FontSurface::drawString(BaseSurface *destSurface, int16 x, int16 y, const byte *string, int stringLen) {
187
188 if (stringLen < 0)
189 stringLen = strlen((const char*)string);
190
191 for (; stringLen > 0; --stringLen, ++string) {
192 drawChar(destSurface, x, y, *string);
193 x += _tracking ? (*_tracking)[*string - _firstChar].x : _charWidth;
194 }
195
196 }
197
getStringWidth(const byte * string,int stringLen)198 int16 FontSurface::getStringWidth(const byte *string, int stringLen) {
199 return string ? stringLen * _charWidth : 0;
200 }
201
createFontSurface(NeverhoodEngine * vm,uint32 fileHash)202 FontSurface *FontSurface::createFontSurface(NeverhoodEngine *vm, uint32 fileHash) {
203 FontSurface *fontSurface;
204 DataResource fontData(vm);
205 SpriteResource fontSprite(vm);
206 fontData.load(calcHash("asRecFont"));
207 uint16 numRows = fontData.getPoint(calcHash("meNumRows")).x;
208 uint16 firstChar = fontData.getPoint(calcHash("meFirstChar")).x;
209 uint16 charWidth = fontData.getPoint(calcHash("meCharWidth")).x;
210 uint16 charHeight = fontData.getPoint(calcHash("meCharHeight")).x;
211 NPointArray *tracking = fontData.getPointArray(calcHash("meTracking"));
212 fontSprite.load(fileHash, true);
213 fontSurface = new FontSurface(vm, tracking, 16, numRows, firstChar, charWidth, charHeight);
214 fontSurface->drawSpriteResourceEx(fontSprite, false, false, 0, 0);
215 return fontSurface;
216 }
217
218 // Misc
219
220 enum BitmapFlags {
221 BF_RLE = 1,
222 BF_HAS_DIMENSIONS = 2,
223 BF_HAS_POSITION = 4,
224 BF_HAS_PALETTE = 8,
225 BF_HAS_IMAGE = 16
226 };
227
parseBitmapResource(const byte * sprite,bool * rle,NDimensions * dimensions,NPoint * position,const byte ** palette,const byte ** pixels)228 void parseBitmapResource(const byte *sprite, bool *rle, NDimensions *dimensions, NPoint *position, const byte **palette, const byte **pixels) {
229
230 uint16 flags;
231
232 flags = READ_LE_UINT16(sprite);
233 sprite += 2;
234
235 if (rle)
236 *rle = flags & BF_RLE;
237
238 if (flags & BF_HAS_DIMENSIONS) {
239 if (dimensions) {
240 dimensions->width = READ_LE_UINT16(sprite);
241 dimensions->height = READ_LE_UINT16(sprite + 2);
242 }
243 sprite += 4;
244 } else if (dimensions) {
245 dimensions->width = 1;
246 dimensions->height = 1;
247 }
248
249 if (flags & BF_HAS_POSITION) {
250 if (position) {
251 position->x = READ_LE_UINT16(sprite);
252 position->y = READ_LE_UINT16(sprite + 2);
253 }
254 sprite += 4;
255 } else if (position) {
256 position->x = 0;
257 position->y = 0;
258 }
259
260 if (flags & BF_HAS_PALETTE) {
261 if (palette)
262 *palette = sprite;
263 sprite += 1024;
264 } else if (palette)
265 *palette = NULL;
266
267 if (flags & BF_HAS_IMAGE) {
268 if (pixels)
269 *pixels = sprite;
270 } else if (pixels)
271 *pixels = NULL;
272
273 }
274
unpackSpriteRle(const byte * source,int width,int height,byte * dest,int destPitch,bool flipX,bool flipY,byte oldColor,byte newColor)275 void unpackSpriteRle(const byte *source, int width, int height, byte *dest, int destPitch, bool flipX, bool flipY, byte oldColor, byte newColor) {
276
277 const bool replaceColors = oldColor != newColor;
278
279 int16 rows, chunks;
280 int16 skip, copy;
281
282 if (flipY) {
283 dest += destPitch * (height - 1);
284 destPitch = -destPitch;
285 }
286
287 rows = READ_LE_UINT16(source);
288 chunks = READ_LE_UINT16(source + 2);
289 source += 4;
290
291 do {
292 if (chunks == 0) {
293 dest += rows * destPitch;
294 } else {
295 while (rows-- > 0) {
296 uint16 rowChunks = chunks;
297 while (rowChunks-- > 0) {
298 skip = READ_LE_UINT16(source);
299 copy = READ_LE_UINT16(source + 2);
300 source += 4;
301 if (!flipX) {
302 memcpy(dest + skip, source, copy);
303 } else {
304 byte *flipDest = dest + width - skip - 1;
305 for (int xc = 0; xc < copy; xc++) {
306 *flipDest-- = source[xc];
307 }
308 }
309 source += copy;
310 }
311 if (replaceColors)
312 for (int xc = 0; xc < width; xc++)
313 if (dest[xc] == oldColor)
314 dest[xc] = newColor;
315 dest += destPitch;
316 }
317 }
318 rows = READ_LE_UINT16(source);
319 chunks = READ_LE_UINT16(source + 2);
320 source += 4;
321 } while (rows > 0);
322
323 }
324
unpackSpriteNormal(const byte * source,int width,int height,byte * dest,int destPitch,bool flipX,bool flipY)325 void unpackSpriteNormal(const byte *source, int width, int height, byte *dest, int destPitch, bool flipX, bool flipY) {
326
327 const int sourcePitch = (width + 3) & 0xFFFC;
328
329 if (flipY) {
330 dest += destPitch * (height - 1);
331 destPitch = -destPitch;
332 }
333
334 if (!flipX) {
335 while (height-- > 0) {
336 memcpy(dest, source, width);
337 source += sourcePitch;
338 dest += destPitch;
339 }
340 } else {
341 while (height-- > 0) {
342 dest += width - 1;
343 for (int xc = 0; xc < width; xc++)
344 *dest-- = source[xc];
345 source += sourcePitch;
346 dest += destPitch;
347 }
348 }
349
350 }
351
calcDistance(int16 x1,int16 y1,int16 x2,int16 y2)352 int calcDistance(int16 x1, int16 y1, int16 x2, int16 y2) {
353 const int16 deltaX = ABS(x1 - x2);
354 const int16 deltaY = ABS(y1 - y2);
355 return (int)sqrt((double)(deltaX * deltaX + deltaY * deltaY));
356 }
357
358 } // End of namespace Neverhood
359