aboutsummaryrefslogtreecommitdiff
path: root/engines/gnap/gamesys.h
blob: 5e996bf8445197c6b7764f10f343f649898be572 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef GNAP_GAMESYS_H
#define GNAP_GAMESYS_H

#include "gnap/gnap.h"
#include "gnap/resource.h"
#include "common/array.h"
#include "common/rect.h"
#include "graphics/surface.h"

namespace Gnap {

const int kMaxSequenceItems = 40;
const int kMaxSpriteDrawItems = 30;
const int kMaxSoundIds = 50;
const int kMaxSeqItems = 50;
const int kMaxUpdRects = 20;
const int kMaxGfxItems = 50;
const int kMaxAnimations = 12;

enum {
	kSeqNone		= 0x00,
	kSeqScale		= 0x01, // Enable scaling
	kSeqLoop		= 0x02, // Loop
	kSeqUnk			= 0x04,	// Unknown
	kSeqSyncWait	= 0x08,	// Start if other sequence is done
	kSeqSyncExists	= 0x20	// Start if other sequence exists
};

struct Sequence {
	int32 sequenceId;
	int32 id;
	int32 sequenceId2;
	int32 id2;
	uint32 flags;
	int32 totalDuration;
	int16 x, y;
};

struct SpriteDrawItem {
	int id;
	Common::Rect rect;
	Graphics::Surface *surface;
};

struct RemoveSequenceItem {
	int sequenceId;
	int id;
	int a3;
};

struct RemoveSpriteDrawItem {
	int id;
	Graphics::Surface *surface;
};

struct GfxItem {
	int sequenceId;
	int id;
	int flags;
	SequenceAnimation *animation;
	int soundValue;
	int field_18;
	int field_1C;
	int currFrameNum;
	int delayTicks;
	bool updFlag;
	int updRectsCount;
	Graphics::Surface *surface;
	Common::Rect updRects[kMaxUpdRects];
	SequenceFrame prevFrame;
	SequenceFrame currFrame;
	void testUpdRect(const Common::Rect &updRect);
};

struct Animation {
	int sequenceId;
	int id;
	int status;
};

class GameSys {
public:
	GameSys(GnapEngine *vm);
	~GameSys();
	void insertSequence(int sequenceId, int a2, int sequenceId2, int a4, int flags, int totalDuration, int16 x, int16 y);
	void insertDirtyRect(const Common::Rect &rect);
	void removeSequence(int sequenceId, int a2, int a3);
	void invalidateGrabCursorSprite(int id, Common::Rect &rect, Graphics::Surface *surface1, Graphics::Surface *surface2);
	void requestClear2(int a1);
	void requestClear1();
	void requestRemoveSequence(int sequenceId, int a2);
	void waitForUpdate();
	int isSequenceActive(int sequenceId, int a2);
	void setBackgroundSurface(Graphics::Surface *surface, int a4, int a5, int a6, int a7);
	void setScaleValues(int a1, int a2, int a3, int a4);
	void insertSpriteDrawItem(Graphics::Surface *surface, int x, int y, int id);
	void removeSpriteDrawItem(Graphics::Surface *surface, int a2);
	void drawSpriteToBackground(int x, int y, int resourceId);
	Graphics::Surface *allocSurface(int width, int height);
	Graphics::Surface *createSurface(int resourceId);
	void drawSpriteToSurface(Graphics::Surface *surface, int x, int y, int resourceId);
	void drawTextToSurface(Graphics::Surface *surface, int x, int y, byte r, byte g, byte b, const char *text);
	int getTextWidth(const char *text);
	void fillSurface(Graphics::Surface *surface, int x, int y, int width, int height, byte r, byte g, byte b);
	void setAnimation(int sequenceId, int id, int animationIndex);
	int getAnimationStatus(int animationIndex);
	int getSpriteWidthById(int resourceId);
	int getSpriteHeightById(int resourceId);
	Graphics::Surface *loadBitmap(int resourceId);
	void drawBitmap(int resourceId);
public:
	GnapEngine *_vm;
	
	Common::Array<Common::Rect> _dirtyRects;

	SpriteDrawItem _newSpriteDrawItems[kMaxSpriteDrawItems];	
	int _newSpriteDrawItemsCount;

	bool _spriteDrawItems1Changed;
	
	RemoveSequenceItem _removeSequenceItems[kMaxSequenceItems];
	int _removeSequenceItemsCount;
	
	RemoveSpriteDrawItem _removeSpriteDrawItems[kMaxSpriteDrawItems];
	int _removeSpriteDrawItemsCount;
	
	int _grabSpriteId;
	Common::Rect _grabSpriteRect;
	bool _grabSpriteChanged;
	Graphics::Surface *_grabSpriteSurface1, *_grabSpriteSurface2;
	
	bool _reqRemoveSequenceItem;
	int _removeSequenceItemSequenceId, _removeSequenceItemValue;

	Common::Array<int> _soundIds;
	
	////////////////////////////////////////////////////////////////////////////
	
	Common::Array<Sequence> _seqItems;
	Common::Array<Sequence> _fatSequenceItems;
	
	GfxItem _gfxItems[kMaxGfxItems];
	int _gfxItemsCount;
	
	Animation _animations[kMaxAnimations];
	int _animationsCount;
	
	int _backgroundImageValue3, _backgroundImageValue1;
	int _backgroundImageValue4, _backgroundImageValue2;
	bool _backgroundImageError;
	
	int32 _gameSysClock, _lastUpdateClock;

	Graphics::Surface *_backgroundSurface;
	Graphics::Surface *_frontSurface;
	Common::Rect _screenRect;

	Sequence *seqFind(int sequenceId, int a2, int *outIndex);
	int seqLocateGfx(int sequenceId, int id, int *outGfxIndex);
	void seqInsertGfx(int index, int duration);
	void seqRemoveGfx(int sequenceId, int id);
	bool updateSequenceDuration(int sequenceId, int id, int *outDuration);
	void updateAnimationsStatus(int sequenceId, int a2);
	
	void restoreBackgroundRect(const Common::Rect &rect);

	void blitSurface32(Graphics::Surface *destSurface, int x, int y, Graphics::Surface *sourceSurface,
		Common::Rect &sourceRect, bool transparent);
	void blitSprite32(Graphics::Surface *destSurface, int x, int y, byte *sourcePixels,
		int sourceWidth, Common::Rect &sourceRect, uint32 *sourcePalette, bool transparent);
	void blitSpriteScaled32(Graphics::Surface *destSurface, Common::Rect &frameRect,
		Common::Rect &destRect, byte *sourcePixels, int sourceWidth, Common::Rect &sourceRect, uint32 *sourcePalette);

	void seqDrawStaticFrame(Graphics::Surface *surface, SequenceFrame &frame, Common::Rect *subRect);
	void seqDrawSpriteFrame(SpriteResource *spriteResource, SequenceFrame &frame, Common::Rect *subRect);

	void drawSprites();
	void updateRect(const Common::Rect &r);
	void updateScreen();
	
	void handleReqRemoveSequenceItem();
	void handleReqRemoveSequenceItems();
	void handleReqRemoveSpriteDrawItems();
	void fatUpdateFrame();
	void fatUpdate();
	void updatePlaySounds();

};

bool intersectRect(Common::Rect &intersectingRect, const Common::Rect &r1, const Common::Rect &r2);

} // End of namespace Gnap

#endif // GNAP_GAMESYS_H