ScummVM API documentation
runtime_entity.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 #ifndef HARVESTER_RUNTIME_ENTITY_H
23 #define HARVESTER_RUNTIME_ENTITY_H
24 
25 #include "common/array.h"
26 #include "common/rect.h"
27 #include "common/str.h"
28 #include "harvester/art.h"
29 
30 namespace Graphics {
31 class Screen;
32 }
33 
34 namespace Harvester {
35 
36 class ResourceManager;
37 
38 enum RuntimeEntityClass {
39  kRuntimeEntityClassObject = 0,
40  kRuntimeEntityClassAnimation = 1,
41  kRuntimeEntityClassCursor = 2,
42  kRuntimeEntityClassBackground = 3,
43  kRuntimeEntityClassNpc = 4,
44  kRuntimeEntityClassPlayer = 5,
45  kRuntimeEntityClassMonster = 6,
46  kRuntimeEntityClassRectHotspot = 0x15,
47  kRuntimeEntityClassDisabledHotspot = 0x16,
48  kRuntimeEntityClassTimer = 0x17,
49  kRuntimeEntityClassRectHotspot18 = 0x18,
50  kRuntimeEntityClassRectHotspot19 = 0x19
51 };
52 
53 enum RuntimeEntityHitTestMode {
54  kRuntimeEntityHitTestNone = 0,
55  kRuntimeEntityHitTestBounds = 1,
56  kRuntimeEntityHitTestOpaquePixels = 2
57 };
58 
59 enum RuntimeEntityAnchorMode {
60  kRuntimeEntityAnchorTopLeft = 0,
61  kRuntimeEntityAnchorCentered = 1
62 };
63 
64 class Entity {
65 public:
66  bool loadBitmapResource(ResourceManager &resources, const Common::String &path);
67  bool loadAbmResource(ResourceManager &resources, const Common::String &path);
68 
69  void setName(const Common::String &name) { _name = name; }
70  const Common::String &getName() const { return _name; }
71 
72  void setClassId(int classId) { _classId = classId; }
73  int getClassId() const { return _classId; }
74  void setAnchorMode(RuntimeEntityAnchorMode anchorMode);
75  void setZExtent(float zExtent) { _zExtent = zExtent; }
76  float getZExtent() const { return _zExtent; }
77 
78  void setLooping(bool looping) { _looping = looping; }
79  void setPingPong(bool pingPong) { _pingPong = pingPong; }
80  void setPlayBackwards(bool playBackwards) { _playBackwards = playBackwards; }
81  void setVisible(bool visible) { _visible = visible; }
82  bool isVisible() const { return _visible; }
83 
84  void setPosition(int x, int y, float z);
85  int getX() const { return _x; }
86  int getY() const { return _y; }
87  float getZ() const { return _z; }
88 
89  void setAnimationRate(int rate);
90  int getAnimationRate() const { return _animationRate; }
91  void setAnimationEnabled(bool enabled);
92  bool isAnimationEnabled() const { return _animationEnabled; }
93  void setCurrentFrame(int frame);
94  int getCurrentFrame() const { return _currentFrame; }
95  int getLastFrame() const { return _lastFrame; }
96  uint getFrameCount() const { return _frames.size(); }
97  bool didAnimationAdvanceLastTick() const { return _animationAdvancedLastTick; }
98  void setAnimationFrameRange(int firstFrame, int lastFrame, bool looping);
99  void setAnimationSequence(int sequence);
100  int getAnimationSequence() const { return _animationSequence; }
101  void configureHotspotBounds(int width, int height);
102  void setHitTestMode(RuntimeEntityHitTestMode mode) { _hitTestMode = mode; }
103  int getBoundsWidth() const { return _boundsWidth; }
104  int getBoundsHeight() const { return _boundsHeight; }
105  bool getCurrentFrameMetrics(int &width, int &height, int &xOffset, int &yOffset) const;
106  bool hasOpaqueFramesInRange(int firstFrame, int lastFrame) const;
107  void setDepthScale(float scale);
108  float getDepthScale() const { return _depthScale; }
109  Common::Rect getScreenRect() const;
110  void configureTimerCountdown(int initialValue, int currentValue, bool enabled, bool looping, bool global);
111  bool tickTimerState(uint32 now, Common::Array<Common::String> &expiredTimerNames);
112  void pauseTimerCountdown(uint32 now);
113  void resumeTimerCountdown(uint32 now);
114  void setTimerEnabled(bool enabled);
115  bool isTimerEnabled() const { return _timerEnabled; }
116  bool isTimerLooping() const { return _timerLooping; }
117  bool isTimerGlobal() const { return _timerGlobal; }
118  int getTimerInitialValue() const { return _timerInitialValue; }
119  int getTimerCurrentValue() const { return _timerCurrentValue; }
120 
121  bool hasFrames() const { return !_frames.empty(); }
122  bool tickVisualState(uint32 now);
123  void draw(Graphics::Screen &screen) const;
124  bool hitTest(const Common::Point &point) const;
125  bool overlapsEntity(const Entity &other) const;
126  bool measureCurrentFrameTransparency(uint32 &framePixels, uint32 &transparentPixels,
127  uint32 &preservedPixels) const;
128 
129 private:
130  Common::Point getDrawOrigin() const;
131  Common::Rect getFrameRect() const;
132  bool hasOpaqueFrame() const;
133  bool isOpaqueAt(const Common::Point &point) const;
134  void advanceAnimationFrame(int directive);
135  void updateBoundsFromCurrentFrame();
136  void updateScreenBaseFromCurrentFrame();
137  void rebuildScaledFrames();
138 
139  Common::String _name;
140  Common::String _resourcePath;
141  Common::Array<AbmFrame> _frames;
142  Common::Array<AbmFrame> _baseFrames;
143  int _classId = 0;
144  int _x = 0;
145  int _y = 0;
146  float _z = 0.0f;
147  int _screenBaseX = 0;
148  int _screenBaseY = 0;
149  int _currentFrame = -1;
150  int _firstFrame = -1;
151  int _lastFrame = -1;
152  int _animationRate = 0;
153  uint32 _animationTickInterval = 0;
154  uint32 _nextAnimationTick = 0;
155  int _animationSequence = -1;
156  bool _looping = true;
157  bool _pingPong = false;
158  bool _playBackwards = false;
159  bool _animationEnabled = true;
160  bool _animationAdvancedLastTick = false;
161  bool _visible = true;
162  bool _drawEnabled = true;
163  int _boundsWidth = 0;
164  int _boundsHeight = 0;
165  RuntimeEntityHitTestMode _hitTestMode = kRuntimeEntityHitTestNone;
166  RuntimeEntityAnchorMode _anchorMode = kRuntimeEntityAnchorTopLeft;
167  float _zExtent = 0.0f;
168  float _depthScale = 1.0f;
169  int _timerInitialValue = 0;
170  int _timerCurrentValue = 0;
171  uint32 _timerStartTick = 0;
172  uint32 _timerNextFireTick = 0;
173  uint32 _timerPauseTick = 0;
174  bool _timerEnabled = false;
175  bool _timerLooping = false;
176  bool _timerGlobal = false;
177  bool _timerPaused = false;
178 };
179 
181 public:
182  explicit EntityManager(ResourceManager &resources);
183  ~EntityManager();
184 
185  void clear();
186  void clearSceneEntities(bool preserveGlobalTimers = false);
187  Entity *spawnAbmEntityFromResource(const Common::String &name, const Common::String &resourcePath,
188  int classId, const Common::Point &position, float z, int animationRate, bool looping, bool pingPong);
189  Entity *spawnBitmapEntityFromResource(const Common::String &name, const Common::String &resourcePath,
190  int classId, const Common::Point &position, float z);
191  Entity *spawnCursorEntity(const Common::Point &position);
192  Entity *spawnSceneBitmapEntity(const Common::String &name, const Common::String &resourcePath,
193  const Common::Point &position, float z);
194  Entity *spawnSceneHotspotEntity(const Common::String &name, const Common::Rect &bounds, float z);
195  Entity *spawnSceneAnimationEntity(const Common::String &name, const Common::String &resourcePath,
196  const Common::Point &position, float z, int animationRate, bool active, bool visible, bool looping,
197  bool playBackwards, bool pingPong, int initialFrame = -1);
198  Entity *spawnSceneActorEntity(const Common::String &name, const Common::String &resourcePath,
199  const Common::Point &position, float z, int initialFrame);
200  Entity *spawnSceneTimerEntity(const Common::String &name, int initialValue, int currentValue,
201  bool enabled, bool looping, bool global);
202  void configureSceneTimerEntity(Entity &entity, int initialValue, int currentValue,
203  bool enabled, bool looping, bool global);
204  Entity *getCursorEntity() const { return _cursorEntity; }
205  void hideCursor();
206  void showCursor();
207  void pauseTimerCountdowns();
208  void resumeTimerCountdowns();
209  bool takeExpiredTimerNames(Common::Array<Common::String> &expiredTimerNames);
210  bool tickSceneEntities();
211  bool syncCursorEntityPosition(const Common::Point &position);
212  void drawSceneEntities(Graphics::Screen &screen) const;
213  void drawCursor(Graphics::Screen &screen) const;
214  const Entity *findTopSceneEntityAt(const Common::Point &point, int classIdFilter = -1) const;
215  int findSceneEntityDrawIndexByName(const Common::String &name) const;
216  const Entity *findSceneEntityByName(const Common::String &name) const;
217  Entity *findSceneEntityByName(const Common::String &name);
218  Entity *detachSceneEntityByName(const Common::String &name);
219  void adoptSceneEntity(Entity *entity);
220  void reinsertSceneEntity(Entity *entity);
221 
222 private:
223  void insertSceneEntity(Entity *entity);
224 
225  ResourceManager &_resources;
226  Common::Array<Entity *> _sceneEntities;
227  Common::Array<Common::String> _expiredTimerNames;
228  Entity *_cursorEntity = nullptr;
229  int _timerPauseDepth = 0;
230 };
231 
232 } // End of namespace Harvester
233 
234 #endif // HARVESTER_RUNTIME_ENTITY_H
Definition: str.h:59
Definition: art.h:31
Definition: rect.h:536
Definition: atari-screen.h:58
Definition: screen.h:47
Definition: runtime_entity.h:64
Definition: runtime_entity.h:180
Definition: formatinfo.h:28
Definition: rect.h:144
Definition: resources.h:32