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 "bladerunner/shape.h"
24 
25 #include "bladerunner/bladerunner.h"
26 
27 #include "common/debug.h"
28 #include "common/ptr.h"
29 #include "common/util.h"
30 
31 #include "graphics/surface.h"
32 
33 namespace BladeRunner {
34 
load(Common::SeekableReadStream * stream)35 bool Shape::load(Common::SeekableReadStream *stream) {
36 	_width = stream->readUint32LE();
37 	_height = stream->readUint32LE();
38 	uint32 size = stream->readUint32LE();
39 
40 	if (size != (uint32)(_width * _height * 2)) {
41 		warning("Shape::load size mismatch (w %d, h %d, sz %d)", _width, _height, size);
42 		return false;
43 	}
44 
45 	// Enfoce a reasonable size limit
46 	if (_width >= 2048 || _height >= 2048) {
47 		warning("Shape::load shape too big (%d, %d)", _width, _height);
48 	}
49 
50 	_data = new byte[size];
51 
52 	if (stream->read(_data, size) != size) {
53 		warning("Shape::load error reading shape (w %d, h %d, sz %d)", _width, _height, size);
54 		return false;
55 	}
56 
57 	return true;
58 }
59 
~Shape()60 Shape::~Shape() {
61 	delete[] _data;
62 }
63 
draw(Graphics::Surface & surface,int x,int y) const64 void Shape::draw(Graphics::Surface &surface, int x, int y) const {
65 	int src_x = CLIP(-x, 0, _width);
66 	int src_y = CLIP(-y, 0, _height);
67 
68 	int dst_x = CLIP<int>(x, 0, surface.w);
69 	int dst_y = CLIP<int>(y, 0, surface.h);
70 
71 	int rect_w = MIN(CLIP(_width + x, 0, _width), surface.w - x);
72 	int rect_h = MIN(CLIP(_height + y, 0, _height), surface.h - y);
73 
74 	if (rect_w == 0 || rect_h == 0) {
75 		return;
76 	}
77 
78 	const uint8 *src_p = _data + 2 * (src_y * _width + src_x);
79 
80 	for (int yi = 0; yi != rect_h; ++yi) {
81 		for (int xi = 0; xi != rect_w; ++xi) {
82 			uint16 shpColor = READ_LE_UINT16(src_p);
83 			src_p += 2;
84 
85 			uint8 a, r, g, b;
86 			getGameDataColor(shpColor, a, r, g, b);
87 
88 			if (!a) {
89 				// Ignore the alpha in the output as it is inversed in the input
90 				void *dstPtr = surface.getBasePtr(CLIP(dst_x + xi, 0, surface.w - 1), CLIP(dst_y + yi, 0, surface.h - 1));
91 				drawPixel(surface, dstPtr, surface.format.RGBToColor(r, g, b));
92 			}
93 		}
94 		src_p += 2 * (_width - rect_w);
95 	}
96 }
97 
Shapes(BladeRunnerEngine * vm)98 Shapes::Shapes(BladeRunnerEngine *vm) {
99 	_vm = vm;
100 }
101 
~Shapes()102 Shapes::~Shapes() {
103 	unload();
104 }
105 
load(const Common::String & container)106 bool Shapes::load(const Common::String &container) {
107 	unload();
108 
109 	Common::ScopedPtr<Common::SeekableReadStream> stream(_vm->getResourceStream(container));
110 	if (!stream) {
111 		warning("Shape::open failed to open '%s'", container.c_str());
112 		return false;
113 	}
114 
115 	uint32 count = stream->readUint32LE();
116 
117 	_shapes.resize(count);
118 
119 	for (uint32 i = 0; i < count; ++i) {
120 		if (!_shapes[i].load(stream.get())) {
121 			return false;
122 		}
123 	}
124 
125 	return true;
126 }
127 
unload()128 void Shapes::unload() {
129 	_shapes.clear();
130 }
131 
132 
133 } // End of namespace BladeRunner
134