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; _hasDefaultDescription = false; }
168  void dump(int level = 0) const;
169  void setRealName();
170  void load(byte *addr, byte sceneId = 0);
171  void save() const;
172 
173  bool hasDefaultDescription() { return _hasDefaultDescription; }
174  uint32 getAddr() { return _addr; };
175 
176  static Common::String parseDescription(const char *desc);
177 
178 protected:
179  byte *_base;
180  size_t _nameSize;
181 
182  // New name that will be set when certain event is triggered
183  Common::String _realName;
184 
185  bool _hasDefaultDescription;
186  uint32 _addr = 0; // Address inside eseg
187 };
188 
190  byte id;
191  byte animated;
192  Common::String name, description;
193 
194  InventoryObject(): id(0), animated(0), _base(0) {}
195  void load(byte *addr);
196 
197 protected:
198  byte *_base;
199 };
200 
201 struct UseHotspot {
202  byte inventoryId;
203  byte objectId;
204  byte orientation;
205  uint16 actorX, actorY;
206  uint16 callback;
207  void load(byte *src);
208  void dump(int level = 0) const;
209 };
210 
211 struct Walkbox {
212  byte type;
213  byte orientation;
214  Rect rect;
215  byte sideHint[4];
216 
217  Walkbox() {
218  _base = nullptr;
219  type = 0;
220  orientation = 0;
221  // rect cleared by Rect constructor
222  for (uint i = 0; i < ARRAYSIZE(sideHint); i++) {
223  sideHint[i] = 0;
224  }
225  }
226  void dump(int level = 0) const;
227  void load(byte *src);
228  void save() const;
229 
230 protected:
231  byte *_base;
232 };
233 
234 struct FadeType {
235  Rect rect;
236  byte value;
237 
238  void load(byte *src);
239 };
240 
241 //\todo move it to util.h?
242 template<typename T> inline T SIGN(T x) { return (x > 0) ? 1 : ((x < 0) ? -1 : 0); }
243 
244 } // End of namespace TeenAgent
245 
246 #endif
#define ARRAYSIZE(x)
Definition: util.h:103
Definition: str.h:59
Definition: surface.h:67
T left
Definition: rect.h:170
Definition: objects.h:211
Definition: rect.h:524
Definition: objects.h:158
void SWAP(T &a, T &b)
Definition: util.h:84
Definition: objects.h:189
void void void void void debugC(int level, uint32 debugChannel, MSVC_PRINTF const char *s,...) GCC_PRINTF(3
Definition: objects.h:35
Definition: objects.h:234
T right
Definition: rect.h:171
uint sqrDist(const ConcretePoint &p) const
Definition: rect.h:112
T y
Definition: rect.h:49
T x
Definition: rect.h:48
Definition: rect.h:144
Definition: objects.h:201
Definition: actor.h:29