File: util.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (297 lines) | stat: -rw-r--r-- 9,518 bytes parent folder | download | duplicates (2)
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "engines/nancy/nancy.h"
#include "engines/nancy/util.h"
#include "common/system.h"

namespace Nancy {

void readRect(Common::SeekableReadStream &stream, Common::Rect &inRect) {
	inRect.left = stream.readSint32LE();
	inRect.top = stream.readSint32LE();
	inRect.right = stream.readSint32LE();
	inRect.bottom = stream.readSint32LE();

	// TVD's rects are non-inclusive
	if (g_nancy->getGameType() > kGameTypeVampire && !inRect.isEmpty()) {
		++inRect.right;
		++inRect.bottom;
	}
}

void readRect(Common::Serializer &stream, Common::Rect &inRect, Common::Serializer::Version minVersion, Common::Serializer::Version maxVersion) {
	Common::Serializer::Version version = stream.getVersion();
	if (version >= minVersion && version <= maxVersion) {
		stream.syncAsSint32LE(inRect.left);
		stream.syncAsSint32LE(inRect.top);
		stream.syncAsSint32LE(inRect.right);
		stream.syncAsSint32LE(inRect.bottom);

		// TVD's rects are non-inclusive
		if (version > kGameTypeVampire && !inRect.isEmpty()) {
			++inRect.right;
			++inRect.bottom;
		}
	}
}

void readRectArray(Common::SeekableReadStream &stream, Common::Array<Common::Rect> &inArray, uint num, uint totalNum) {
	uint startSize = inArray.size();
	inArray.resize(num + startSize);

	for (Common::Rect *rect = &inArray[startSize]; rect != inArray.end(); ++rect) {
		readRect(stream, *rect);
	}

	if (totalNum == 0) {
		totalNum = num;
	}

	stream.skip(totalNum > num ? (totalNum - num) * 16 : 0);
}

void readRectArray(Common::Serializer &stream, Common::Array<Common::Rect> &inArray, uint num, uint totalNum, Common::Serializer::Version minVersion, Common::Serializer::Version maxVersion) {
	Common::Serializer::Version version = stream.getVersion();
	if (version >= minVersion && version <= maxVersion) {
		uint startSize = inArray.size();
		inArray.resize(num + startSize);

		for (Common::Rect *rect = &inArray[startSize]; rect != inArray.end(); ++rect) {
			stream.syncAsSint32LE(rect->left);
			stream.syncAsSint32LE(rect->top);
			stream.syncAsSint32LE(rect->right);
			stream.syncAsSint32LE(rect->bottom);

			// TVD's rects are non-inclusive
			if (version > kGameTypeVampire && !rect->isEmpty()) {
				++rect->right;
				++rect->bottom;
			}
		}

		if (totalNum == 0) {
			totalNum = num;
		}

		stream.skip(totalNum > num ? (totalNum - num) * 16 : 0);
	}
}

void readRect16(Common::SeekableReadStream &stream, Common::Rect &inRect) {
	inRect.left = stream.readSint16LE();
	inRect.top = stream.readSint16LE();
	inRect.right = stream.readSint16LE();
	inRect.bottom = stream.readSint16LE();

	// TVD's rects are non-inclusive
	if (g_nancy->getGameType() > kGameTypeVampire && !inRect.isEmpty()) {
		++inRect.right;
		++inRect.bottom;
	}
}

void readRect16(Common::Serializer &stream, Common::Rect &inRect, Common::Serializer::Version minVersion, Common::Serializer::Version maxVersion) {
	Common::Serializer::Version version = stream.getVersion();
	if (version >= minVersion && version <= maxVersion) {
		stream.syncAsSint16LE(inRect.left);
		stream.syncAsSint16LE(inRect.top);
		stream.syncAsSint16LE(inRect.right);
		stream.syncAsSint16LE(inRect.bottom);

		// TVD's rects are non-inclusive
		if (version > kGameTypeVampire && !inRect.isEmpty()) {
			++inRect.right;
			++inRect.bottom;
		}
	}
}

void readRectArray16(Common::SeekableReadStream &stream, Common::Array<Common::Rect> &inArray, uint num, uint totalNum) {
	uint startSize = inArray.size();
	inArray.resize(num + startSize);

	for (Common::Rect *rect = &inArray[startSize]; rect != inArray.end(); ++rect) {
		readRect16(stream, *rect);
	}

	if (totalNum == 0) {
		totalNum = num;
	}

	stream.skip(totalNum > num ? (totalNum - num) * 8 : 0);
}

void readRectArray16(Common::Serializer &stream, Common::Array<Common::Rect> &inArray, uint num, uint totalNum, Common::Serializer::Version minVersion, Common::Serializer::Version maxVersion) {
	Common::Serializer::Version version = stream.getVersion();
	if (version >= minVersion && version <= maxVersion) {
		uint startSize = inArray.size();
		inArray.resize(num + startSize);

		for (Common::Rect *rect = &inArray[startSize]; rect != inArray.end(); ++rect) {
			stream.syncAsSint16LE(rect->left);
			stream.syncAsSint16LE(rect->top);
			stream.syncAsSint16LE(rect->right);
			stream.syncAsSint16LE(rect->bottom);

			// TVD's rects are non-inclusive
			if (version > kGameTypeVampire && !rect->isEmpty()) {
				++rect->right;
				++rect->bottom;
			}
		}

		if (totalNum == 0) {
			totalNum = num;
		}

		stream.skip(totalNum > num ? (totalNum - num) * 8 : 0);
	}
}

void readFilename(Common::SeekableReadStream &stream, Common::String &inString) {
	char buf[33] = "";

	if (g_nancy->getGameType() <= kGameTypeNancy2) {
		// Older games only support 8-character filenames, and stored them in a 10 char buffer
		stream.read(buf, 10);
		buf[9] = '\0';
	} else {
		// Later games support 32-character filenames
		stream.read(buf, 33);
		buf[32] = '\0';
	}

	inString = buf;
}

void readFilename(Common::Serializer &stream, Common::String &inString, Common::Serializer::Version minVersion, Common::Serializer::Version maxVersion) {
	Common::Serializer::Version version = stream.getVersion();
	if (version >= minVersion && version <= maxVersion) {
		char buf[33] = "";

		if (version <= kGameTypeNancy2) {
			// Older games only support 8-character filenames, and stored them in a 10 char buffer
			stream.syncBytes((byte *)buf, 10);
			buf[9] = '\0';
		} else {
			// Later games support 32-character filenames
			stream.syncBytes((byte *)buf, 33);
			buf[32] = '\0';
		}

		inString = buf;
	}
}

void readFilenameArray(Common::SeekableReadStream &stream, Common::Array<Common::String> &inArray, uint num) {
	inArray.resize(num);
	for (Common::String &str : inArray) {
		readFilename(stream, str);
	}
}

void readFilenameArray(Common::Serializer &stream, Common::Array<Common::String> &inArray, uint num, Common::Serializer::Version minVersion, Common::Serializer::Version maxVersion) {
	Common::Serializer::Version version = stream.getVersion();
	if (version >= minVersion && version <= maxVersion) {
		inArray.resize(num);
		char buf[33] = "";

		for (Common::String &str : inArray) {
			if (version <= kGameTypeNancy2) {
				// Older games only support 8-character filenames, and stored them in a 10 char buffer
				stream.syncBytes((byte *)buf, 10);
				buf[9] = '\0';
			} else {
				// Later games support 32-character filenames
				stream.syncBytes((byte *)buf, 33);
				buf[32] = '\0';
			}

			str = buf;
		}
	}
}

void readFilenameArray(Common::SeekableReadStream &stream, Common::Array<Common::Path> &inArray, uint num) {
	inArray.resize(num);
	for (Common::Path &str : inArray) {
		readFilename(stream, str);
	}
}

void readFilenameArray(Common::Serializer &stream, Common::Array<Common::Path> &inArray, uint num, Common::Serializer::Version minVersion, Common::Serializer::Version maxVersion) {
	Common::Serializer::Version version = stream.getVersion();
	if (version >= minVersion && version <= maxVersion) {
		inArray.resize(num);
		for (Common::Path &str : inArray) {
			readFilename(stream, str, minVersion, maxVersion);
		}
	}
}

// A text line will often be broken up into chunks separated by nulls, use
// this function to put it back together as a Common::String
void assembleTextLine(char *rawCaption, Common::String &output, uint size) {
	for (uint i = 0; i < size; ++i) {
		// A single line can be broken up into bits, look for them and
		// concatenate them when we're done
		if (rawCaption[i] != 0) {
			Common::String newBit(rawCaption + i);
			output += newBit;
			i += newBit.size();
		}
	}

	// Fix spaces at the end of the string in nancy1
	output.trim();

	// Scan the text line for doubly-closed tokens; happens in some strings in The Vampire Diaries
	uint pos = Common::String::npos;
	while (pos = output.find(">>"), pos != Common::String::npos) {
		output.replace(pos, 2, ">");
	}
}

bool DeferredLoader::load(uint32 endTime) {
	uint32 loopStartTime = g_system->getMillis();
	uint32 loopTime = 0; // Stores the loop that took the longest time to complete

	do {
		if (loadInner()) {
			return true;
		}

		uint32 loopEndTime = g_system->getMillis();
		loopTime = MAX<uint32>(loopEndTime - loopStartTime, loopTime);
		loopStartTime = loopEndTime;

		// We do this after at least one execution to avoid the case where the game runs below
		// the target fps, and thus has no spare milliseconds until the next frame. This way
		// we ensure loading actually gets done, at the expense of some lag
		if (g_system->getMillis() < endTime) {
			break;
		}
	} while (loopTime + loopStartTime < endTime);

	return false;
}

} // End of namespace Nancy