ScummVM API documentation
tcoords.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_TCOORDS_H
27 #define SAGA2_TCOORDS_H
28 
29 #include "common/savefile.h"
30 #include "common/memstream.h"
31 
32 namespace Saga2 {
33 
34 enum facingDirections {
35  kDirUp = 0,
36  kDirUpLeft,
37  kDirLeft,
38  kDirDownLeft,
39  kDirDown,
40  kDirDownRight,
41  kDirRight,
42  kDirUpRight,
43 
44  kDirInvalid
45 };
46 typedef uint8 Direction;
47 
49  int16 u, v, z;
50 
51  void set(int nu, int nv, int nz) {
52  u = nu;
53  v = nv;
54  z = nz;
55  }
56 
57  friend StaticTilePoint operator+(StaticTilePoint a, StaticTilePoint b) {
58  int16 nu = a.u + b.u;
59  int16 nv = a.v + b.v;
60  int16 nz = a.z + b.z;
61  StaticTilePoint p = {nu, nv, nz};
62 
63  return p;
64  }
65 
66  friend StaticTilePoint operator-(StaticTilePoint a, StaticTilePoint b) {
67  int16 nu = a.u - b.u;
68  int16 nv = a.v - b.v;
69  int16 nz = a.z - b.z;
70  StaticTilePoint p = {nu, nv, nz};
71 
72  return p;
73  }
74 
75  friend StaticTilePoint operator*(StaticTilePoint a, int b) {
76  int16 nu = a.u * b;
77  int16 nv = a.v * b;
78  int16 nz = a.z * b;
79  StaticTilePoint p = {nu, nv, nz};
80 
81  return p;
82  }
83 
84  friend StaticTilePoint operator/(StaticTilePoint a, int b) {
85  int16 nu = a.u / b;
86  int16 nv = a.v / b;
87  int16 nz = a.z / b;
88  StaticTilePoint p = {nu, nv, nz};
89 
90  return p;
91  }
92 
93  friend StaticTilePoint operator<<(StaticTilePoint a, int b) {
94  int16 nu = a.u << b;
95  int16 nv = a.v << b;
96  int16 nz = a.z << b;
97  StaticTilePoint p = {nu, nv, nz};
98 
99  return p;
100  }
101 
102  friend StaticTilePoint operator>>(StaticTilePoint a, int b) {
103  int16 nu = a.u >> b;
104  int16 nv = a.v >> b;
105  int16 nz = a.z >> b;
106  StaticTilePoint p = {nu, nv, nz};
107 
108  return p;
109  }
110 
111  friend int operator==(StaticTilePoint a, StaticTilePoint b) {
112  return a.u == b.u && a.v == b.v && a.z == b.z;
113  }
114 
115  friend int operator!=(StaticTilePoint a, StaticTilePoint b) {
116  return a.u != b.u || a.v != b.v || a.z != b.z;
117  }
118 
119  void operator+=(StaticTilePoint a) {
120  u += a.u;
121  v += a.v;
122  z += a.z;
123  }
124 };
125 
126 #include "common/pack-start.h"
127 struct TilePoint {
128  // When an object is within a World, the u & v represent
129  // the U and V coordinates on the tilemap, and the Z
130  // represents the height.
131  //
132  // When an object is contained within a container, the
133  // u and v represent where the object is displayed within
134  // the container's rectangle. (Z is unused for now?)
135  int16 u,v,z;
136 
137  // constructors
138  TilePoint() { u = v = z = 0; }
139  TilePoint(int16 nu, int16 nv, int16 nz) { u = nu; v = nv; z = nz; }
142  u = p.u;
143  v = p.v;
144  z = p.z;
145  }
146 
147  void load(Common::SeekableReadStream *stream) {
148  u = stream->readSint16LE();
149  v = stream->readSint16LE();
150  z = stream->readSint16LE();
151  }
152 
153  void write(Common::MemoryWriteStreamDynamic *out) const {
154  out->writeSint16LE(u);
155  out->writeSint16LE(v);
156  out->writeSint16LE(z);
157  }
158 
159  // TilePoint operators
160  friend TilePoint operator+ (TilePoint a, TilePoint b)
161  { return TilePoint( (int16) (a.u + b.u), (int16) (a.v + b.v), (int16) (a.z + b.z) ); }
162 
163  friend TilePoint operator- (TilePoint a)
164  { return TilePoint( (int16) (-a.u), (int16) (-a.v), (int16) (-a.z) ); }
165 
166  friend TilePoint operator- (TilePoint a, TilePoint b)
167  { return TilePoint( (int16) (a.u - b.u), (int16) (a.v - b.v), (int16) (a.z - b.z) ); }
168 
169  friend TilePoint operator* (TilePoint a, int b)
170  { return TilePoint( (int16) (a.u * (int16)b), (int16) (a.v * (int16)b), (int16) (a.z * (int16)b) ); }
171 
172  friend TilePoint operator/ (TilePoint a, int b)
173  { return TilePoint( (int16) (a.u / (int16)b), (int16) (a.v / (int16)b), (int16) (a.z / (int16)b) ); }
174 
175  friend TilePoint operator>>(TilePoint a, int b)
176  { return TilePoint( (int16) (a.u >> (int16)b), (int16) (a.v >> (int16)b), (int16) (a.z >> (int16)b) ); }
177 
178  friend TilePoint operator<<(TilePoint a, int b)
179  { return TilePoint( (int16) (a.u << (int16)b), (int16) (a.v << (int16)b), (int16) (a.z << (int16)b) ); }
180 
181  friend int operator==(TilePoint a, TilePoint b)
182  { return a.u == b.u && a.v == b.v && a.z == b.z; }
183 
184  friend int operator!=(TilePoint a, TilePoint b)
185  { return a.u != b.u || a.v != b.v || a.z != b.z; }
186 
187  void operator+= (TilePoint a) { u += a.u; v += a.v; z += a.z; }
188  void operator-= (TilePoint a) { u -= a.u; v -= a.v; z -= a.z; }
189 
190  int16 quickHDistance() {
191  int16 au = (int16)ABS(u),
192  av = (int16)ABS(v);
193 
194  if (au > av)
195  return (int16)(au + (av >> 1));
196  else
197  return (int16)(av + (au >> 1));
198  }
199 
200  int16 quickDir();
201 
202  int16 magnitude();
203 
204  void debugPrint(int level = 0, const char *msg = "TilePoint:") {
205  debug(level, "%s %d, %d, %d", msg, u, v, z);
206  }
207 } PACKED_STRUCT;
208 #include "common/pack-end.h"
209 
210 
211 /* ============================================================================ *
212  Constants
213  * ============================================================================ */
214 
215  // A TilePoint defining a NULL location
216 const extern StaticTilePoint Nowhere;
217 
218 /* ============================================================================ *
219  TileRegion: Specifies a rectangular region of tiles using min/max
220  * ============================================================================ */
221 
222 struct TileRegion {
223  TilePoint min,
224  max;
225 
226  void read(Common::InSaveFile *in) {
227  min.load(in);
228  max.load(in);
229  }
230 
231  void write(Common::MemoryWriteStreamDynamic *out) {
232  min.write(out);
233  max.write(out);
234  }
235 };
236 
237 /* ============================================================================ *
238  TilePoint32
239  * ============================================================================ */
240 
241  // int32 tilepoint for more precise calculations
242 
243 class TilePoint32 {
244 public:
245  // When an object is within a World, the u & v represent
246  // the U and V coordinates on the tilemap, and the Z
247  // represents the height.
248  //
249  // When an object is contained within a container, the
250  // u and v represent where the object is displayed within
251  // the container's rectangle. (Z is unused for now?)
252  int32 u,v,z;
253 
254  // constructors
255  TilePoint32(){}
256  TilePoint32( int32 nu, int32 nv, int32 nz ) { u = nu; v = nv; z = nz; }
257  TilePoint32( TilePoint &tp ) { u = tp.u; v = tp.v; z = tp.z; }
258 
259  // TilePoint32 operators
260  friend TilePoint32 operator+ (TilePoint32 a, TilePoint32 b)
261  { return TilePoint32( a.u + b.u, a.v + b.v, a.z + b.z ); }
262 
263  friend TilePoint32 operator- (TilePoint32 a)
264  { return TilePoint32( -a.u, -a.v, -a.z ); }
265 
266  friend TilePoint32 operator- (TilePoint32 a, TilePoint32 b)
267  { return TilePoint32( a.u - b.u, a.v - b.v, a.z - b.z ); }
268 
269  friend TilePoint32 operator* (TilePoint32 a, int b)
270  { return TilePoint32( a.u * (int32)b, a.v * (int32)b, a.z * (int32)b ); }
271 
272  friend TilePoint32 operator/ (TilePoint32 a, int b)
273  { return TilePoint32( a.u / (int32)b, a.v / (int32)b, a.z / (int32)b ); }
274 
275  friend TilePoint32 operator>>(TilePoint32 a, int b)
276  { return TilePoint32( a.u >> (int32)b, a.v >> (int32)b, a.z >> (int32)b ); }
277 
278  friend TilePoint32 operator<<(TilePoint32 a, int b)
279  { return TilePoint32( a.u << (int32)b, a.v << (int32)b, a.z << (int32)b ); }
280 
281  friend int operator==(TilePoint32 a, TilePoint32 b)
282  { return a.u == b.u && a.v == b.v && a.z == b.z; }
283 
284  friend int operator!=(TilePoint32 a, TilePoint32 b)
285  { return a.u != b.u || a.v != b.v || a.z != b.z; }
286 
287  void operator+= (TilePoint32 a) { u += a.u; v += a.v; z += a.z; }
288  void operator-= (TilePoint32 a) { u -= a.u; v -= a.v; z -= a.z; }
289 
290  int32 quickHDistance( void ) {
291  int32 au = ABS( u ),
292  av = ABS( v );
293 
294  if (au > av) return au + (av >> 1);
295  else return av + (au >> 1);
296  }
297 };
298 
299 } // end of namespace Saga2
300 
301 #endif
Definition: actor.h:32
Definition: memstream.h:194
Definition: tcoords.h:127
Definition: stream.h:745
Definition: tcoords.h:222
Definition: tcoords.h:48
void debug(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: tcoords.h:243
FORCEINLINE int16 readSint16LE()
Definition: stream.h:543
FORCEINLINE void writeSint16LE(int16 value)
Definition: stream.h:194
T ABS(T x)
Definition: util.h:56