aboutsummaryrefslogtreecommitdiff
path: root/engines/wintermute/Ad/AdSpriteSet.cpp
blob: 4fc5a12c4c2378652c8c90d4d9a9a62ce1b929ca (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* 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.
 *
 */

/*
 * This file is based on WME Lite.
 * http://dead-code.org/redir.php?target=wmelite
 * Copyright (c) 2011 Jan Nedoma
 */

#include "engines/wintermute/dcgf.h"
#include "engines/wintermute/Ad/AdSpriteSet.h"
#include "engines/wintermute/Base/BParser.h"
#include "engines/wintermute/Base/BDynBuffer.h"
#include "engines/wintermute/Base/BGame.h"
#include "engines/wintermute/Base/BFileManager.h"

namespace WinterMute {

IMPLEMENT_PERSISTENT(CAdSpriteSet, false)

//////////////////////////////////////////////////////////////////////////
CAdSpriteSet::CAdSpriteSet(CBGame *inGame, CBObject *Owner): CBObject(inGame) {
	_owner = Owner;

	for (int i = 0; i < NUM_DIRECTIONS; i++)
		_sprites[i] = NULL;
}


//////////////////////////////////////////////////////////////////////////
CAdSpriteSet::~CAdSpriteSet() {
	for (int i = 0; i < NUM_DIRECTIONS; i++) {
		delete _sprites[i];
		_sprites[i] = NULL;
	}

	_owner = NULL;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CAdSpriteSet::LoadFile(const char *Filename, int LifeTime, TSpriteCacheType CacheType) {
	byte *Buffer = Game->_fileManager->ReadWholeFile(Filename);
	if (Buffer == NULL) {
		Game->LOG(0, "CAdSpriteSet::LoadFile failed for file '%s'", Filename);
		return E_FAIL;
	}

	HRESULT ret;

	if (FAILED(ret = LoadBuffer(Buffer, true))) Game->LOG(0, "Error parsing SPRITESET file '%s'", Filename);

	delete [] Buffer;

	return ret;
}


TOKEN_DEF_START
TOKEN_DEF(SPRITESET)
TOKEN_DEF(NAME)
TOKEN_DEF(UP_LEFT)
TOKEN_DEF(DOWN_LEFT)
TOKEN_DEF(LEFT)
TOKEN_DEF(UP_RIGHT)
TOKEN_DEF(DOWN_RIGHT)
TOKEN_DEF(RIGHT)
TOKEN_DEF(UP)
TOKEN_DEF(DOWN)
TOKEN_DEF(TEMPLATE)
TOKEN_DEF(EDITOR_PROPERTY)
TOKEN_DEF_END
//////////////////////////////////////////////////////////////////////////
HRESULT CAdSpriteSet::LoadBuffer(byte  *Buffer, bool Complete, int LifeTime, TSpriteCacheType CacheType) {
	TOKEN_TABLE_START(commands)
	TOKEN_TABLE(SPRITESET)
	TOKEN_TABLE(NAME)
	TOKEN_TABLE(UP_LEFT)
	TOKEN_TABLE(DOWN_LEFT)
	TOKEN_TABLE(LEFT)
	TOKEN_TABLE(UP_RIGHT)
	TOKEN_TABLE(DOWN_RIGHT)
	TOKEN_TABLE(RIGHT)
	TOKEN_TABLE(UP)
	TOKEN_TABLE(DOWN)
	TOKEN_TABLE(TEMPLATE)
	TOKEN_TABLE(EDITOR_PROPERTY)
	TOKEN_TABLE_END

	byte *params;
	int cmd;
	CBParser parser(Game);

	if (Complete) {
		if (parser.GetCommand((char **)&Buffer, commands, (char **)&params) != TOKEN_SPRITESET) {
			Game->LOG(0, "'SPRITESET' keyword expected.");
			return E_FAIL;
		}
		Buffer = params;
	}

	CBSprite *spr = NULL;
	while ((cmd = parser.GetCommand((char **)&Buffer, commands, (char **)&params)) > 0) {
		switch (cmd) {
		case TOKEN_TEMPLATE:
			if (FAILED(LoadFile((char *)params, LifeTime, CacheType))) cmd = PARSERR_GENERIC;
			break;

		case TOKEN_NAME:
			SetName((char *)params);
			break;

		case TOKEN_LEFT:
			delete _sprites[DI_LEFT];
			_sprites[DI_LEFT] = NULL;
			spr = new CBSprite(Game, _owner);
			if (!spr || FAILED(spr->LoadFile((char *)params, LifeTime, CacheType))) cmd = PARSERR_GENERIC;
			else _sprites[DI_LEFT] = spr;
			break;

		case TOKEN_RIGHT:
			delete _sprites[DI_RIGHT];
			_sprites[DI_RIGHT] = NULL;
			spr = new CBSprite(Game, _owner);
			if (!spr || FAILED(spr->LoadFile((char *)params, LifeTime, CacheType))) cmd = PARSERR_GENERIC;
			else _sprites[DI_RIGHT] = spr;
			break;

		case TOKEN_UP:
			delete _sprites[DI_UP];
			_sprites[DI_UP] = NULL;
			spr = new CBSprite(Game, _owner);
			if (!spr || FAILED(spr->LoadFile((char *)params, LifeTime, CacheType))) cmd = PARSERR_GENERIC;
			else _sprites[DI_UP] = spr;
			break;

		case TOKEN_DOWN:
			delete _sprites[DI_DOWN];
			_sprites[DI_DOWN] = NULL;
			spr = new CBSprite(Game, _owner);
			if (!spr || FAILED(spr->LoadFile((char *)params, LifeTime, CacheType))) cmd = PARSERR_GENERIC;
			else _sprites[DI_DOWN] = spr;
			break;

		case TOKEN_UP_LEFT:
			delete _sprites[DI_UPLEFT];
			_sprites[DI_UPLEFT] = NULL;
			spr = new CBSprite(Game, _owner);
			if (!spr || FAILED(spr->LoadFile((char *)params, LifeTime, CacheType))) cmd = PARSERR_GENERIC;
			else _sprites[DI_UPLEFT] = spr;
			break;

		case TOKEN_UP_RIGHT:
			delete _sprites[DI_UPRIGHT];
			_sprites[DI_UPRIGHT] = NULL;
			spr = new CBSprite(Game, _owner);
			if (!spr || FAILED(spr->LoadFile((char *)params, LifeTime, CacheType))) cmd = PARSERR_GENERIC;
			else _sprites[DI_UPRIGHT] = spr;
			break;

		case TOKEN_DOWN_LEFT:
			delete _sprites[DI_DOWNLEFT];
			_sprites[DI_DOWNLEFT] = NULL;
			spr = new CBSprite(Game, _owner);
			if (!spr || FAILED(spr->LoadFile((char *)params, LifeTime, CacheType))) cmd = PARSERR_GENERIC;
			else _sprites[DI_DOWNLEFT] = spr;
			break;

		case TOKEN_DOWN_RIGHT:
			delete _sprites[DI_DOWNRIGHT];
			_sprites[DI_DOWNRIGHT] = NULL;
			spr = new CBSprite(Game, _owner);
			if (!spr || FAILED(spr->LoadFile((char *)params, LifeTime, CacheType))) cmd = PARSERR_GENERIC;
			else _sprites[DI_DOWNRIGHT] = spr;
			break;

		case TOKEN_EDITOR_PROPERTY:
			ParseEditorProperty(params, false);
			break;
		}
	}
	if (cmd == PARSERR_TOKENNOTFOUND) {
		Game->LOG(0, "Syntax error in SPRITESET definition");
		return E_FAIL;
	}

	if (cmd == PARSERR_GENERIC) {
		Game->LOG(0, "Error loading SPRITESET definition");
		if (spr) delete spr;
		return E_FAIL;
	}

	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CAdSpriteSet::Persist(CBPersistMgr *PersistMgr) {

	CBObject::Persist(PersistMgr);

	PersistMgr->Transfer(TMEMBER(_owner));
	for (int i = 0; i < NUM_DIRECTIONS; i++) {
		PersistMgr->Transfer("", &_sprites[i]);
	}

	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
CBSprite *CAdSpriteSet::GetSprite(TDirection Direction) {
	int Dir = (int)Direction;
	if (Dir < 0) Dir = 0;
	if (Dir >= NUM_DIRECTIONS) Dir = NUM_DIRECTIONS - 1;

	CBSprite *ret = NULL;

	// find nearest set sprite
	int i;
	int NumSteps = 0;
	for (i = Dir, NumSteps = 0; i >= 0; i--) {
		if (_sprites[i] != NULL) {
			ret = _sprites[i];
			NumSteps = Dir - i;
			break;
		}
	}

	for (i = Dir; i < NUM_DIRECTIONS; i++) {
		if (_sprites[i] != NULL) {
			if (ret == NULL || NumSteps > i - Dir) return _sprites[i];
			else return ret;
		}
	}

	return ret;
}



//////////////////////////////////////////////////////////////////////////
HRESULT CAdSpriteSet::SaveAsText(CBDynBuffer *Buffer, int Indent) {
	Buffer->PutTextIndent(Indent, "SPRITESET {\n");
	if (_name) Buffer->PutTextIndent(Indent + 2, "NAME=\"%s\"\n", _name);
	for (int i = 0; i < NUM_DIRECTIONS; i++) {
		if (_sprites[i]) {
			switch (i) {
			case DI_UP:
				Buffer->PutTextIndent(Indent + 2, "UP=\"%s\"\n",         _sprites[i]->_filename);
				break;
			case DI_UPRIGHT:
				Buffer->PutTextIndent(Indent + 2, "UP_RIGHT=\"%s\"\n",   _sprites[i]->_filename);
				break;
			case DI_RIGHT:
				Buffer->PutTextIndent(Indent + 2, "RIGHT=\"%s\"\n",      _sprites[i]->_filename);
				break;
			case DI_DOWNRIGHT:
				Buffer->PutTextIndent(Indent + 2, "DOWN_RIGHT=\"%s\"\n", _sprites[i]->_filename);
				break;
			case DI_DOWN:
				Buffer->PutTextIndent(Indent + 2, "DOWN=\"%s\"\n",       _sprites[i]->_filename);
				break;
			case DI_DOWNLEFT:
				Buffer->PutTextIndent(Indent + 2, "DOWN_LEFT=\"%s\"\n",  _sprites[i]->_filename);
				break;
			case DI_LEFT:
				Buffer->PutTextIndent(Indent + 2, "LEFT=\"%s\"\n",       _sprites[i]->_filename);
				break;
			case DI_UPLEFT:
				Buffer->PutTextIndent(Indent + 2, "UP_LEFT=\"%s\"\n",    _sprites[i]->_filename);
				break;
			}
		}
	}

	CBBase::SaveAsText(Buffer, Indent + 2);

	Buffer->PutTextIndent(Indent, "}\n");

	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
bool CAdSpriteSet::ContainsSprite(CBSprite *Sprite) {
	if (!Sprite) return NULL;

	for (int i = 0; i < NUM_DIRECTIONS; i++) {
		if (_sprites[i] == Sprite) return true;
	}
	return false;
}

} // end of namespace WinterMute