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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef GNAP_GAMESYS_H
24 #define GNAP_GAMESYS_H
25 
26 #include "gnap/gnap.h"
27 #include "gnap/resource.h"
28 #include "common/array.h"
29 #include "common/rect.h"
30 #include "graphics/surface.h"
31 
32 namespace Gnap {
33 
34 const int kMaxSequenceItems = 40;
35 const int kMaxSpriteDrawItems = 30;
36 const int kMaxSoundIds = 50;
37 const int kMaxSeqItems = 50;
38 const int kMaxUpdRects = 20;
39 const int kMaxGfxItems = 50;
40 const int kMaxAnimations = 12;
41 
42 enum {
43 	kSeqNone		= 0x00,
44 	kSeqScale		= 0x01, // Enable scaling
45 	kSeqLoop		= 0x02, // Loop
46 	kSeqUnk			= 0x04,	// Unknown
47 	kSeqSyncWait	= 0x08,	// Start if other sequence is done
48 	kSeqSyncExists	= 0x20	// Start if other sequence exists
49 };
50 
51 struct Sequence {
52 	int32 _sequenceId;
53 	int32 _id;
54 	int32 _sequenceId2;
55 	int32 _id2;
56 	uint32 _flags;
57 	int32 _totalDuration;
58 	int16 _x, _y;
59 };
60 
61 struct SpriteDrawItem {
62 	int _id;
63 	Common::Rect _rect;
64 	Graphics::Surface *_surface;
65 };
66 
67 struct RemoveSequenceItem {
68 	int _sequenceId;
69 	int _id;
70 	bool _forceFrameReset;
71 };
72 
73 struct RemoveSpriteDrawItem {
74 	int _id;
75 	Graphics::Surface *_surface;
76 };
77 
78 struct GfxItem {
79 	int _sequenceId;
80 	int _id;
81 	int _flags;
82 	SequenceAnimation *_animation;
83 	int _currFrameNum;
84 	int _delayTicks;
85 	bool _updFlag;
86 	int _updRectsCount;
87 	Graphics::Surface *_surface;
88 	Common::Rect _updRects[kMaxUpdRects];
89 	SequenceFrame _prevFrame;
90 	SequenceFrame _currFrame;
91 	void testUpdRect(const Common::Rect &updRect);
92 };
93 
94 struct Animation {
95 	int _sequenceId;
96 	int _id;
97 	int _status;
98 };
99 
100 class GameSys {
101 public:
102 	GameSys(GnapEngine *vm);
103 	~GameSys();
104 	void insertSequence(int sequenceId, int id, int sequenceId2, int id2, int flags, int totalDuration, int16 x, int16 y);
105 	void insertDirtyRect(const Common::Rect &rect);
106 	void removeSequence(int sequenceId, int id, bool resetFl);
107 	void invalidateGrabCursorSprite(int id, Common::Rect &rect, Graphics::Surface *surface1, Graphics::Surface *surface2);
108 	void requestClear2(bool resetFl);
109 	void requestClear1();
110 	void requestRemoveSequence(int sequenceId, int id);
111 	void waitForUpdate();
112 	int isSequenceActive(int sequenceId, int id);
113 	void setBackgroundSurface(Graphics::Surface *surface, int a4, int a5, int a6, int a7);
114 	void setScaleValues(int a1, int a2, int a3, int a4);
115 	void insertSpriteDrawItem(Graphics::Surface *surface, int x, int y, int id);
116 	void removeSpriteDrawItem(Graphics::Surface *surface, int id);
117 	void drawSpriteToBackground(int x, int y, int resourceId);
118 	Graphics::Surface *allocSurface(int width, int height);
119 	Graphics::Surface *createSurface(int resourceId);
120 	void drawSpriteToSurface(Graphics::Surface *surface, int x, int y, int resourceId);
121 	void drawTextToSurface(Graphics::Surface *surface, int x, int y, byte r, byte g, byte b, const char *text);
122 	int getTextHeight(const char *text);
123 	int getTextWidth(const char *text);
124 	void fillSurface(Graphics::Surface *surface, int x, int y, int width, int height, byte r, byte g, byte b);
125 	void setAnimation(int sequenceId, int id, int animationIndex);
126 	int getAnimationStatus(int animationIndex);
127 	int getSpriteWidthById(int resourceId);
128 	int getSpriteHeightById(int resourceId);
129 	Graphics::Surface *loadBitmap(int resourceId);
130 	void drawBitmap(int resourceId);
131 public:
132 	GnapEngine *_vm;
133 
134 	Common::Array<Common::Rect> _dirtyRects;
135 
136 	SpriteDrawItem _newSpriteDrawItems[kMaxSpriteDrawItems];
137 	int _newSpriteDrawItemsCount;
138 
139 	RemoveSequenceItem _removeSequenceItems[kMaxSequenceItems];
140 	int _removeSequenceItemsCount;
141 
142 	RemoveSpriteDrawItem _removeSpriteDrawItems[kMaxSpriteDrawItems];
143 	int _removeSpriteDrawItemsCount;
144 
145 	int _grabSpriteId;
146 	Common::Rect _grabSpriteRect;
147 	bool _grabSpriteChanged;
148 	Graphics::Surface *_grabSpriteSurface1, *_grabSpriteSurface2;
149 
150 	bool _reqRemoveSequenceItem;
151 	int _removeSequenceItemSequenceId, _removeSequenceItemValue;
152 
153 	Common::Array<int> _soundIds;
154 
155 	////////////////////////////////////////////////////////////////////////////
156 
157 	Common::Array<Sequence> _seqItems;
158 	Common::Array<Sequence> _fatSequenceItems;
159 
160 	GfxItem _gfxItems[kMaxGfxItems];
161 	int _gfxItemsCount;
162 
163 	Animation _animations[kMaxAnimations];
164 	int _animationsCount;
165 
166 	int _backgroundImageValue3, _backgroundImageValue1;
167 	int _backgroundImageValue4, _backgroundImageValue2;
168 
169 	int32 _gameSysClock, _lastUpdateClock;
170 	bool _animationsDone;
171 
172 
173 	Graphics::Surface *_backgroundSurface;
174 	Graphics::Surface *_frontSurface;
175 	Common::Rect _screenRect;
176 
177 	Sequence *seqFind(int sequenceId, int id, int *outIndex);
178 	int seqLocateGfx(int sequenceId, int id, int *outGfxIndex);
179 	void seqInsertGfx(int index, int duration);
180 	void seqRemoveGfx(int sequenceId, int id);
181 	bool updateSequenceDuration(int sequenceId, int id, int *outDuration);
182 	void updateAnimationsStatus(int sequenceId, int id);
183 
184 	void restoreBackgroundRect(const Common::Rect &rect);
185 
186 	void blitSurface32(Graphics::Surface *destSurface, int x, int y, Graphics::Surface *sourceSurface,
187 		Common::Rect &sourceRect, bool transparent);
188 	void blitSprite32(Graphics::Surface *destSurface, int x, int y, byte *sourcePixels,
189 		int sourceWidth, Common::Rect &sourceRect, uint32 *sourcePalette, bool transparent);
190 	void blitSpriteScaled32(Graphics::Surface *destSurface, Common::Rect &frameRect,
191 		Common::Rect &destRect, byte *sourcePixels, int sourceWidth, Common::Rect &sourceRect, uint32 *sourcePalette);
192 
193 	void seqDrawStaticFrame(Graphics::Surface *surface, SequenceFrame &frame, Common::Rect *subRect);
194 	void seqDrawSpriteFrame(SpriteResource *spriteResource, SequenceFrame &frame, Common::Rect *subRect);
195 
196 	void drawSprites();
197 	void updateRect(const Common::Rect &r);
198 	void updateScreen();
199 
200 	void handleReqRemoveSequenceItem();
201 	void handleReqRemoveSequenceItems();
202 	void handleReqRemoveSpriteDrawItems();
203 	void fatUpdateFrame();
204 	void fatUpdate();
205 	void updatePlaySounds();
206 
207 };
208 
209 bool intersectRect(Common::Rect &intersectingRect, const Common::Rect &r1, const Common::Rect &r2);
210 
211 } // End of namespace Gnap
212 
213 #endif // GNAP_GAMESYS_H
214