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 
31 namespace Saga2 {
32 
33 struct StaticPoint16 {
34  int16 x, y;
35 
36  void set(int16 nx, int16 ny) {
37  x = nx;
38  y = ny;
39  }
40 };
41 
42 class Point16 {
43 public:
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 
130 typedef Point16 Vector16; // vectors are treated as points
131 
132 // An Extent is like a rect, but with only size, not position.
133 
134 typedef 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) {
149  StaticPoint32 p;
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) {
156  StaticPoint32 p;
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) {
163  StaticPoint32 p;
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) {
171  StaticPoint32 p;
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 
184 class Point32 {
185 public:
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 
276 typedef Point32 Vector32; // vectors are treated as points
277 
278 // An Extent is like a rect, but with only size, not position.
279 
280 typedef Point32 Extent32; // contains width and height
281 
282 struct StaticRect {
283  int16 x, y, width, height;
284 };
285 
286 /* ===================================================================== *
287  Rect16: 16-bit 2-D rectangle
288  * ===================================================================== */
289 
290 class Rect16 {
291 public:
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 
383 Rect16 intersect(const Rect16, const Rect16);
384 Rect16 bound(const Rect16, const Rect16);
385 
386 /* ===================================================================== *
387  Rect32: 16-bit 2-D rectangle
388  * ===================================================================== */
389 
390 class Rect32 {
391 public:
392  int32 x, y,
393  width, height;
394 
395  // constructors
396  Rect32() { x = y = width = height = 0; }
397  Rect32(int32 nx, int32 ny, int32 nw, int32 nh) {
398  x = nx;
399  y = ny;
400  width = nw;
401  height = nh;
402  }
403  Rect32(Point16 a, int32 nw, int32 nh) {
404  x = a.x;
405  y = a.y;
406  width = nw;
407  height = nh;
408  }
409  Rect32(Point16 a, Point16 b) {
410  x = a.x;
411  y = a.y;
412  width = (int32)(b.x - a.x);
413  height = (int32)(b.y - a.y);
414  }
415  Rect32(Point32 a, int32 nw, int32 nh) {
416  x = a.x;
417  y = a.y;
418  width = nw;
419  height = nh;
420  }
421  Rect32(Point32 a, Point32 b) {
422  x = a.x;
423  y = a.y;
424  width = (int32)(b.x - a.x);
425  height = (int32)(b.y - a.y);
426  }
427  // Conversion operators
428  Rect32(Rect16 r) {
429  x = r.x;
430  y = r.y;
431  width = r.width;
432  height = r.height;
433  }
434  operator Rect16() {
435  return Rect16((int16)x, (int16)y, (int16)width, (int16)height);
436  }
437 
438  // Rect32 operators
439 
440  friend int operator==(Rect32 a, Rect32 b) {
441  return a.x == b.x
442  && a.y == b.y
443  && a.width == b.width
444  && a.height == b.height;
445  }
446 
447  friend int operator!=(Rect32 a, Rect32 b) {
448  return a.x != b.x
449  || a.y != b.y
450  || a.width != b.width
451  || a.height != b.height;
452  }
453 
454  friend Rect32 operator+ (Rect32 a, Point16 b) {
455  return Rect32(a.x + b.x, a.y + b.y, a.width, a.height);
456  }
457 
458  friend Rect32 operator+ (Point16 b, Rect32 a) {
459  return Rect32(a.x + b.x, a.y + b.y, a.width, a.height);
460  }
461 
462  friend Rect32 operator- (Rect32 a, Point16 b) {
463  return Rect32(a.x - b.x, a.y - b.y, a.width, a.height);
464  }
465 
466  friend Rect32 operator+ (Point32 b, Rect32 a) {
467  return Rect32(a.x + b.x, a.y + b.y, a.width, a.height);
468  }
469 
470  friend Rect32 operator- (Rect32 a, Point32 b) {
471  return Rect32(a.x - b.x, a.y - b.y, a.width, a.height);
472  }
473 
474  void operator+= (Point16 a) {
475  x += a.x;
476  y += a.y;
477  }
478  void operator-= (Point16 a) {
479  x -= a.x;
480  y -= a.y;
481  }
482 
483  void operator+= (Point32 a) {
484  x += a.x;
485  y += a.y;
486  }
487  void operator-= (Point32 a) {
488  x -= a.x;
489  y -= a.y;
490  }
491  // functions
492  void normalize(); // make rect right-side out
493 
494  void expand(int32 dx, int32 dy); // grow or shrink the rect
495  void expand(int32 left, int32 top, int32 right, int32 bottom);
496 
497  int empty() const {
498  return width <= 0 || height <= 0;
499  }
500 
501  bool ptInside(int32 px, int32 py) const { // true if point inside
502  return px >= x && px < (x + width) && py >= y && py < (y + height);
503  }
504  bool ptInside(const Point16 p) const {
505  return ptInside(p.x, p.y);
506  }
507 
508  bool overlap(const Rect32 &r) const;
509 };
510 
511 // Intersect two rectangles
512 
513 Rect32 intersect(const Rect32, const Rect32);
514 Rect32 bound(const Rect32, const Rect32);
515 
516 } // end of namespace Saga2
517 
518 #endif
Definition: rect.h:184
Definition: actor.h:32
Definition: memstream.h:194
Definition: stream.h:745
Definition: rect.h:282
Definition: rect.h:390
void debug(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: rect.h:42
Definition: rect.h:140
Definition: rect.h:33
Definition: rect.h:290