ScummVM API documentation
rect.h
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 modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (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, see <http://www.gnu.org/licenses/>.
19 *
20 *
21 * Based on the original sources
22 * Faery Tale II -- The Halls of the Dead
23 * (c) 1993-1996 The Wyrmkeep Entertainment Co.
24 */
25
26#ifndef SAGA2_RECT_H
27#define SAGA2_RECT_H
28
29#include "common/memstream.h"
30
31namespace Saga2 {
32
34 int16 x, y;
35
36 void set(int16 nx, int16 ny) {
37 x = nx;
38 y = ny;
39 }
40};
41
42class Point16 {
43public:
44 int16 x, y;
45
46 // constructors
47 Point16() { x = y = 0; }
48 Point16(int16 nx, int16 ny) {
49 x = nx;
50 y = ny;
51 }
52
54 x = p.x;
55 y = p.y;
56 }
57
58 void load(Common::SeekableReadStream *stream);
59 void write(Common::MemoryWriteStreamDynamic *out);
60
61 // Point16 operators
62 friend Point16 operator+ (Point16 a, Point16 b) {
63 return Point16(a.x + b.x, a.y + b.y);
64 }
65
66 friend Point16 operator- (Point16 a) {
67 return Point16(-a.x, -a.y);
68 }
69
70 friend Point16 operator- (Point16 a, Point16 b) {
71 return Point16(a.x - b.x, a.y - b.y);
72 }
73
74 friend Point16 operator* (Point16 a, int b) {
75 return Point16(a.x * (int16)b, a.y * (int16)b);
76 }
77
78 friend Point16 operator/ (Point16 a, int b) {
79 return Point16(a.x / (int16)b, a.y / (int16)b);
80 }
81
82 friend Point16 operator>>(Point16 a, int b) {
83 return Point16(a.x >> (int16)b, a.y >> (int16)b);
84 }
85
86 friend Point16 operator<<(Point16 a, int b) {
87 return Point16(a.x << (int16)b, a.y << (int16)b);
88 }
89
90 friend int operator==(Point16 a, Point16 b) {
91 return a.x == b.x && a.y == b.y;
92 }
93
94 friend int operator!=(Point16 a, Point16 b) {
95 return a.x != b.x || a.y != b.y;
96 }
97
98 void operator+= (Point16 a) {
99 x += a.x;
100 y += a.y;
101 }
102 void operator-= (Point16 a) {
103 x -= a.x;
104 y -= a.y;
105 }
106 void operator*= (int a) {
107 x *= (int16)a;
108 y *= (int16)a;
109 }
110 void operator/= (int a) {
111 x /= (int16)a;
112 y /= (int16)a;
113 }
114 void operator>>=(int a) {
115 x >>= (int16)a;
116 y >>= (int16)a;
117 }
118 void operator<<=(int a) {
119 x <<= (int16)a;
120 y <<= (int16)a;
121 }
122
123 void debugPrint(int level = 0, const char *msg = "Point32:") {
124 debug(level, "%s %d,%d", msg, x, y);
125 }
126 // Point16 functions
127
128};
129
130typedef Point16 Vector16; // vectors are treated as points
131
132// An Extent is like a rect, but with only size, not position.
133
134typedef Point16 Extent16; // contains width and height
135
136/* ===================================================================== *
137 Point32: 32-bit 2-D point
138 * ===================================================================== */
139
141 int32 x, y;
142
143 void set(int16 nx, int16 ny) {
144 x = nx;
145 y = ny;
146 }
147
148 friend StaticPoint32 operator+ (StaticPoint32 a, StaticPoint32 b) {
150 p.x = a.x + b.x;
151 p.y = a.y + b.y;
152 return p;
153 }
154
155 friend StaticPoint32 operator- (StaticPoint32 a, StaticPoint32 b) {
157 p.x = a.x - b.x;
158 p.y = a.y - b.y;
159 return p;
160 }
161
162 friend StaticPoint32 operator*(StaticPoint32 a, int b) {
164 p.x = a.x * b;
165 p.y = a.y * b;
166
167 return p;
168 }
169
170 friend StaticPoint32 operator/(StaticPoint32 a, int b) {
172 p.x = a.x / b;
173 p.y = a.y / b;
174
175 return p;
176 }
177
178 void operator+= (StaticPoint32 a) {
179 x += a.x;
180 y += a.y;
181 }
182};
183
184class Point32 {
185public:
186 int32 x, y;
187
188 // constructors
189 Point32() { x = y = 0; }
190 Point32(int32 nx, int32 ny) {
191 x = nx;
192 y = ny;
193 }
194
195 // Conversion operators
196 Point32(Point16 p) {
197 x = p.x;
198 y = p.y;
199 }
200 operator Point16() {
201 return Point16((int16)x, (int16)y);
202 }
203
205 x = p.x;
206 y = p.y;
207 }
208
209 // Point32 operators
210 friend Point32 operator+ (Point32 a, Point32 b) {
211 return Point32(a.x + b.x, a.y + b.y);
212 }
213
214 friend Point32 operator- (Point32 a) {
215 return Point32(-a.x, -a.y);
216 }
217
218 friend Point32 operator- (Point32 a, Point32 b) {
219 return Point32(a.x - b.x, a.y - b.y);
220 }
221
222 friend Point32 operator* (Point32 a, int b) {
223 return Point32(a.x * (int32)b, a.y * (int32)b);
224 }
225
226 friend Point32 operator/ (Point32 a, int b) {
227 return Point32(a.x / (int32)b, a.y / (int32)b);
228 }
229
230 friend Point32 operator>>(Point32 a, int b) {
231 return Point32(a.x >> (int32)b, a.y >> (int32)b);
232 }
233
234 friend Point32 operator<<(Point32 a, int b) {
235 return Point32(a.x << (int32)b, a.y << (int32)b);
236 }
237
238 friend int operator==(Point32 a, Point32 b) {
239 return a.x == b.x && a.y == b.y;
240 }
241
242 friend int operator!=(Point32 a, Point32 b) {
243 return a.x != b.x || a.y != b.y;
244 }
245
246 void operator+= (Point32 a) {
247 x += a.x;
248 y += a.y;
249 }
250 void operator-= (Point32 a) {
251 x -= a.x;
252 y -= a.y;
253 }
254 void operator*= (int a) {
255 x *= (int32)a;
256 y *= (int32)a;
257 }
258 void operator/= (int a) {
259 x /= (int32)a;
260 y /= (int32)a;
261 }
262 void operator>>=(int a) {
263 x >>= (int32)a;
264 y >>= (int32)a;
265 }
266 void operator<<=(int a) {
267 x <<= (int32)a;
268 y <<= (int32)a;
269 }
270
271 void debugPrint(int level = 0, const char *msg = "Point32:") {
272 debug(level, "%s %d,%d", msg, x, y);
273 }
274};
275
276typedef Point32 Vector32; // vectors are treated as points
277
278// An Extent is like a rect, but with only size, not position.
279
280typedef Point32 Extent32; // contains width and height
281
283 int16 x, y, width, height;
284};
285
286/* ===================================================================== *
287 Rect16: 16-bit 2-D rectangle
288 * ===================================================================== */
289
290class Rect16 {
291public:
292 int16 x, y,
293 width, height;
294
295 // constructors
296 Rect16() { x = y = width = height = 0; }
297 Rect16(int16 nx, int16 ny, int16 nw, int16 nh) {
298 x = nx;
299 y = ny;
300 width = nw;
301 height = nh;
302 }
303 Rect16(Point16 a, int16 nw, int16 nh) {
304 x = a.x;
305 y = a.y;
306 width = nw;
307 height = nh;
308 }
309 Rect16(Point16 a, Point16 b) {
310 x = a.x;
311 y = a.y;
312 width = (int16)(b.x - a.x);
313 height = (int16)(b.y - a.y);
314 }
315
316 Rect16(StaticRect r) {
317 x = r.x;
318 y = r.y;
319 width = r.width;
320 height = r.height;
321 }
322
323 void read(Common::InSaveFile *in);
324 void write(Common::MemoryWriteStreamDynamic *out);
325
326 // Rect16 operators
327
328 friend int operator==(Rect16 a, Rect16 b) {
329 return a.x == b.x
330 && a.y == b.y
331 && a.width == b.width
332 && a.height == b.height;
333 }
334
335 friend int operator!=(Rect16 a, Rect16 b) {
336 return a.x != b.x
337 || a.y != b.y
338 || a.width != b.width
339 || a.height != b.height;
340 }
341
342 friend Rect16 operator+ (Rect16 a, Point16 b) {
343 return Rect16(a.x + b.x, a.y + b.y, a.width, a.height);
344 }
345
346 friend Rect16 operator+ (Point16 b, Rect16 a) {
347 return Rect16(a.x + b.x, a.y + b.y, a.width, a.height);
348 }
349
350 friend Rect16 operator- (Rect16 a, Point16 b) {
351 return Rect16(a.x - b.x, a.y - b.y, a.width, a.height);
352 }
353
354 void operator+= (Point16 a) {
355 x += a.x;
356 y += a.y;
357 }
358 void operator-= (Point16 a) {
359 x -= a.x;
360 y -= a.y;
361 }
362
363 // functions
364 void normalize(); // make rect right-side out
365 void expand(int16 dx, int16 dy); // grow or shrink the rect
366 void expand(int16 left, int16 top, int16 right, int16 bottom);
367
368 int empty() const {
369 return width <= 0 || height <= 0;
370 }
371
372 bool ptInside(int16 px, int16 py) const { // true if point inside
373 return px >= x && px < (x + width) && py >= y && py < (y + height);
374 }
375 bool ptInside(const Point16 p) const {
376 return ptInside(p.x, p.y);
377 }
378 bool overlap(const Rect16 &r) const;
379};
380
381// Intersect two rectangles
382
383Rect16 intersect(const Rect16, const Rect16);
384Rect16 bound(const Rect16, const Rect16);
385
386} // end of namespace Saga2
387
388#endif
Definition: memstream.h:194
Definition: stream.h:745
Definition: rect.h:42
Definition: rect.h:184
Definition: rect.h:290
void debug(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: rect.h:33
Definition: rect.h:140
Definition: rect.h:282