ScummVM API documentation
objects.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 
22 
23 #ifndef TEENAGENT_OBJECTS_H
24 #define TEENAGENT_OBJECTS_H
25 
26 #include "common/rect.h"
27 #include "graphics/surface.h"
28 
29 #include "teenagent/teenagent.h"
30 
31 namespace TeenAgent {
32 
33 enum {kActorUp = 1, kActorRight = 2, kActorDown = 3, kActorLeft = 4 };
34 
35 struct Rect {
36  int16 left, top, right, bottom;
37 
38  inline Rect() : left(0), top(0), right(0), bottom(0), _base(NULL) {}
39  inline Rect(const Common::Rect &r) : left(r.left), top(r.top), right(r.right), bottom(r.bottom), _base(NULL) {}
40  inline Rect(uint16 l, uint16 t, uint16 r, uint16 b) : left(l), top(t), right(r), bottom(b), _base(NULL) {}
41 
42  inline bool in(const Common::Point &point) const {
43  return point.x >= left && point.x <= right && point.y >= top && point.y <= bottom;
44  }
45 
46  inline Common::Point center() const {
47  return Common::Point((right + left) / 2, (bottom + top) / 2);
48  }
49 
50  inline bool valid() const {
51  return left >= 0 && left < kScreenWidth && right >= 0 && right < kScreenWidth && top >= 0 && top < kScreenHeight && bottom >= 0 && bottom < kScreenHeight;
52  }
53 
54  void render(Graphics::Surface *surface, uint8 color) const;
55 
56  void dump(int level = 0) const {
57  debugC(level, kDebugObject, "rect[%u, %u, %u, %u]", left, top, right, bottom);
58  }
59 
60  inline void clear() {
61  left = top = right = bottom = 0;
62  }
63 
64  void load(byte *src); //8 bytes
65  void save() const;
66 
67  inline bool intersects_hline(int x1, int x2, int y) const {
68  if (x1 > x2)
69  SWAP(x1, x2);
70  return y >= top && y <= bottom && x1 <= right && x2 >= left;
71  }
72 
73  inline bool intersects_vline(int x, int y1, int y2) const {
74  if (y1 > y2)
75  SWAP(y1, y2);
76  return x >= left && x <= right && y1 <= bottom && y2 >= top;
77  }
78 
79  inline bool contains(const Rect &rect) const {
80  return rect.left >= left && rect.right <= right && rect.top >= top && rect.bottom <= bottom;
81  }
82 
83  static inline bool inside(int x, int a, int b) {
84  if (a > b)
85  SWAP(a, b);
86  return x >= a && x <= b;
87  }
88 
89  int intersects_line(const Common::Point &a, const Common::Point &b) const {
90  int dy = b.y - a.y, dx = b.x - a.x;
91 
92  int mask = 0; //orientation bitmask: 1 - top, 2 - right, 3 - bottom, 4 - left
93 
94  if (dx != 0) {
95  int yl = (left - a.x) * dy / dx + a.y;
96  if (yl > top && yl < bottom && inside(yl, a.y, b.y) && inside(left, a.x, b.x)) {
97  //c[idx++] = Common::Point(left, yl);
98  mask |= 8;
99  }
100  int yr = (right - a.x) * dy / dx + a.y;
101  if (yr > top && yr < bottom && inside(yr, a.y, b.y) && inside(right, a.x, b.x)) {
102  //c[idx++] = Common::Point(right, yr);
103  mask |= 2;
104  }
105  }
106 
107  if (dy != 0) {
108  int xt = (top - a.y) * dx / dy + a.x;
109  if (xt > left && xt < right && inside(xt, a.x, b.x) && inside(top, a.y, b.y)) {
110  //assert(idx < 2);
111  //c[idx++] = Common::Point(xt, top);
112  mask |= 1;
113  }
114  int xb = (bottom - a.y) * dx / dy + a.x;
115  if (xb > left && xb < right && inside(xb, a.x, b.x) && inside(bottom, a.y, b.y)) {
116  //assert(idx < 2);
117  //c[idx++] = Common::Point(xb, bottom);
118  mask |= 4;
119  }
120  }
121  return mask;
122  }
123 
124  void side(Common::Point &p1, Common::Point &p2, int o, const Common::Point &nearest) const {
125  switch (o) {
126  case kActorLeft:
127  p1 = Common::Point(left, top);
128  p2 = Common::Point(left, bottom);
129  break;
130 
131  case kActorRight:
132  p1 = Common::Point(right, top);
133  p2 = Common::Point(right, bottom);
134  break;
135 
136  case kActorUp:
137  p1 = Common::Point(left, top);
138  p2 = Common::Point(right, top);
139  break;
140 
141  case kActorDown:
142  p1 = Common::Point(left, bottom);
143  p2 = Common::Point(right, bottom);
144  break;
145 
146  default:
147  p1 = Common::Point();
148  p2 = Common::Point();
149  }
150  if (p1.sqrDist(nearest) >= p2.sqrDist(nearest))
151  SWAP(p1, p2);
152  }
153 
154 protected:
155  byte *_base;
156 };
157 
158 struct Object {
159  byte id; //0
160  Rect rect; //1
161  Rect actorRect; //9
162  byte actorOrientation; //17
163  byte enabled; //18
164  //19
165  Common::String name, description;
166 
167  Object(): _base(NULL), _nameSize(0) { id = 0; actorOrientation = 0; enabled = 0; }
168  void dump(int level = 0) const;
169  void setName(const Common::String &newName);
170  void load(byte *addr);
171  void save() const;
172 
173  static Common::String parseDescription(const char *desc);
174 
175 protected:
176  byte *_base;
177  size_t _nameSize;
178 };
179 
181  byte id;
182  byte animated;
183  Common::String name, description;
184 
185  InventoryObject(): id(0), animated(0), _base(0) {}
186  void load(byte *addr);
187 
188 protected:
189  byte *_base;
190 };
191 
192 struct UseHotspot {
193  byte inventoryId;
194  byte objectId;
195  byte orientation;
196  uint16 actorX, actorY;
197  uint16 callback;
198  void load(byte *src);
199  void dump(int level = 0) const;
200 };
201 
202 struct Walkbox {
203  byte type;
204  byte orientation;
205  Rect rect;
206  byte sideHint[4];
207 
208  Walkbox() {
209  _base = nullptr;
210  type = 0;
211  orientation = 0;
212  // rect cleared by Rect constructor
213  for (uint i = 0; i < ARRAYSIZE(sideHint); i++) {
214  sideHint[i] = 0;
215  }
216  }
217  void dump(int level = 0) const;
218  void load(byte *src);
219  void save() const;
220 
221 protected:
222  byte *_base;
223 };
224 
225 struct FadeType {
226  Rect rect;
227  byte value;
228 
229  void load(byte *src);
230 };
231 
232 //\todo move it to util.h?
233 template<typename T> inline T SIGN(T x) { return (x > 0) ? 1 : ((x < 0) ? -1 : 0); }
234 
235 } // End of namespace TeenAgent
236 
237 #endif
#define ARRAYSIZE(x)
Definition: util.h:91
Definition: str.h:59
Definition: surface.h:66
Definition: objects.h:202
int16 right
Definition: rect.h:146
Definition: rect.h:144
Definition: objects.h:158
void SWAP(T &a, T &b)
Definition: util.h:82
Definition: objects.h:180
Definition: objects.h:35
Definition: objects.h:225
Definition: rect.h:45
Definition: objects.h:192
int16 left
Definition: rect.h:145
Definition: actor.h:29
int16 x
Definition: rect.h:46
int16 y
Definition: rect.h:47
void void void void void debugC(int level, uint32 debugChannels, MSVC_PRINTF const char *s,...) GCC_PRINTF(3
uint sqrDist(const Point &p) const
Definition: rect.h:110