aboutsummaryrefslogtreecommitdiff
path: root/engines/chewy/text.cpp
blob: fcf07a684b7282387d37d8722fe164318206e394 (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
/* 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.
 *
 */

#include "common/rect.h"
#include "common/system.h"

#include "chewy/resource.h"
#include "chewy/text.h"

namespace Chewy {

Text::Text() : Resource("atds.tap") {
}

Text::~Text() {
}

TextEntryList *Text::getDialog(uint dialogNum, uint entryNum) {
	if (dialogNum >= kADSTextMax)
		error("getDialog(): Invalid entry number requested, %d (max %d)", dialogNum, kADSTextMax - 1);

	TextEntryList *l = new TextEntryList();

	byte *data = getChunkData(dialogNum);
	byte *ptr = data;

	ptr += 2;	// entry number
	ptr += 2;	// number of persons
	ptr += 2;	// automove count
	ptr += 2;	// cursor number
	ptr += 13;	// misc data

	for (uint i = 0; i <= entryNum; i++) {
		do {
			TextEntry curDialog;
			ptr++;	// current entry
			ptr += 2;
			curDialog.speechId = READ_LE_UINT16(ptr) - VOICE_OFFSET;	ptr += 2;

			do {
				curDialog.text += *ptr++;

				if (*ptr == 0 && *(ptr + 1) != kEndText) {
					// TODO: Split lines
					*ptr = ' ';
				}
			} while (*ptr != kEndText);

			if (i == entryNum)
				l->push_back(curDialog);

		} while (*(ptr + 1) != kEndEntry);

		ptr += 2;	// kEndText, kEndEntry

		if (*ptr == kEndBlock)	// not found
			break;
	}

	delete[] data;

	return l;
}

TextEntry *Text::getText(uint dialogNum, uint entryNum) {
	if (dialogNum < kADSTextMax)
		error("getText(): Invalid entry number requested, %d (min %d)", dialogNum, kADSTextMax);

	TextEntry *d = new TextEntry();
	bool isText = (dialogNum >= kADSTextMax && dialogNum < kADSTextMax + kATSTextMax);
	bool isAutoDialog = (dialogNum >= kADSTextMax + kATSTextMax && dialogNum < kADSTextMax + kATSTextMax + kAADTextMax);
	//bool isInvText = (dialogNum >= kADSTextMax + kATSTextMax + kAADTextMax && dialogNum < kADSTextMax + kATSTextMax + kAADTextMax + kINVTextMax);

	byte *data = getChunkData(dialogNum);
	byte *ptr = data;

	if (isAutoDialog)
		ptr += 3;

	for (uint i = 0; i <= entryNum; i++) {
		ptr += 13;
		d->speechId = READ_LE_UINT16(ptr) - VOICE_OFFSET;	ptr += 2;

		do {
			if (i == entryNum)
				d->text += *ptr++;
			else
				ptr++;

			if (*ptr == 0 && *(ptr + 1) != kEndText) {
				// TODO: Split lines
				*ptr = ' ';
			}
		} while (*ptr);

		if (*(ptr + 1) != kEndText || *(ptr + 2) != kEndChunk)
			error("Invalid text resource - %d", dialogNum);

		if (!isText)
			ptr += 3;	// 0, kEndText, kEndChunk
		if (isAutoDialog)
			ptr += 3;

		if (i == entryNum) {
			// Found
			delete[] data;
			return d;
		}
	}

	// Not found
	delete[] data;
	delete d;

	return nullptr;
}

Font::Font(Common::String filename) {
	const uint32 headerFont = MKTAG('T', 'F', 'F', '\0');
	Common::File stream;

	stream.open(filename);

	uint32 header = stream.readUint32BE();

	if (header != headerFont)
		error("Invalid resource - %s", filename.c_str());

	stream.skip(4);	// total memory
	_count = stream.readUint16LE();
	_first = stream.readUint16LE();
	_last = stream.readUint16LE();
	_width = stream.readUint16LE();
	_height = stream.readUint16LE();

	_fontSurface.create(_width * _count, _height, ::Graphics::PixelFormat::createFormatCLUT8());

	byte cur;
	int bitIndex = 7;
	byte *p;

	cur = stream.readByte();

	for (uint n = 0; n < _count; n++) {
		for (uint y = 0; y < _height; y++) {
			p = (byte *)_fontSurface.getBasePtr(n * _width, y);

			for (uint x = n * _width; x < n * _width + _width; x++) {
				*p++ = (cur & (1 << bitIndex)) ? 0 : 0xFF;

				bitIndex--;
				if (bitIndex < 0) {
					bitIndex = 7;
					cur = stream.readByte();
				}
			}
		}
	}
}

Font::~Font() {
	_fontSurface.free();
}

::Graphics::Surface *Font::getLine(const Common::String &text) {
	::Graphics::Surface *line = new ::Graphics::Surface();
	line->create(text.size() * _width, _height, ::Graphics::PixelFormat::createFormatCLUT8());

	for (uint i = 0; i < text.size(); i++) {
		uint x = (text[i] - _first) * _width;
		line->copyRectToSurface(_fontSurface, i * _width, 0, Common::Rect(x, 0, x + _width, _height));
	}

	return line;
}

Common::String ErrorMessage::getErrorMessage(uint num) {
	assert(num < _chunkList.size());

	Chunk *chunk = &_chunkList[num];
	Common::String str;
	byte *data = new byte[chunk->size];

	_stream.seek(chunk->pos, SEEK_SET);
	_stream.read(data, chunk->size);
	if (_encrypted)
		decrypt(data, chunk->size);

	str = (char *)data;
	delete[] data;

	return str;
}

} // End of namespace Chewy