aboutsummaryrefslogtreecommitdiff
path: root/scumm/nut_renderer.cpp
blob: 17c0385db7284149208c7bac2f56df7d7a644a1a (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001/2002 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 */

#include "stdafx.h"
#include "scumm.h"
#include "nut_renderer.h"

NutRenderer::NutRenderer(Scumm *vm) {
	_vm = vm;
	_initialized = false;
	_loaded = false;
	_dataSrc = NULL;
}

NutRenderer::~NutRenderer() {
	if (_dataSrc != NULL)
		free(_dataSrc);
}

void NutRenderer::decodeCodec44(byte *dst, byte *src, uint32 length) {
	byte val;
	uint16 size_line;
	uint16 num;

	do {
		size_line = READ_LE_UINT16(src);
		src += 2;
		length -= 2;

		while (size_line != 0) {
			num = *src++;
			val = *src++;
			memset(dst, val, num);
			dst += num;
			length -= 2;
			size_line -= 2;
			if (size_line == 0) break;

			num = READ_LE_UINT16(src) + 1;
			src += 2;
			memcpy(dst, src, num);
			dst += num;
			src += num;
			length -= num + 2;
			size_line -= num + 2;

		}
		dst--;

	} while (length > 1);
}

bool NutRenderer::loadFont(const char *filename, const char *dir) {
	debug(2,  "NutRenderer::loadFont() called");
	if (_loaded == true) {
		debug(2, "NutRenderer::loadFont() Font already loaded, ok, loading...");
	}
	
	File file;
	file.open(filename, dir);
	if (file.isOpen() == false) {
		error("NutRenderer::loadFont() Can't open font file: %s/%s", dir, filename);
		return false;
	}

	uint32 tag = file.readUint32BE();
	if (tag != 'ANIM') {
		debug(2, "NutRenderer::loadFont() there is no ANIM chunk in font header");
		return false;
	}

	if (_dataSrc != NULL)
		free(_dataSrc);

	uint32 length = file.readUint32BE();
	_dataSrc = (byte *)malloc(length);
	file.read(_dataSrc, length);
	file.close();

	if (READ_BE_UINT32(_dataSrc) != 'AHDR') {
		debug(2, "NutRenderer::loadFont() there is no AHDR chunk in font header");
		free(_dataSrc);
		return false;
	}
	
	int32 l;
	uint32 offset = READ_BE_UINT32(_dataSrc + 4) + 8;
	for (l = 0; l < 256; l++) {
		if (READ_BE_UINT32(_dataSrc + offset) == 'FRME') {
			offset += 8;
			if (READ_BE_UINT32(_dataSrc + offset) == 'FOBJ') {
				_offsets[l] = offset + 8;
				offset += READ_BE_UINT32(_dataSrc + offset + 4) + 8;
			}
			else {
				debug(2, "NutRenderer::loadFont() there is no FRME chunk");
				free(_dataSrc);
				return false;
			}
		}
		else {
			debug(2, "NutRenderer::loadFont() there is no FOBJ chunk in FRME chunk");
			free(_dataSrc);
			return false;
		}
	}

	_loaded = true;
	return true;
}

int32 NutRenderer::getCharWidth(char c) {
	debug(2,  "NutRenderer::getCharWidth() called");
	if (_loaded == false) {
		debug(2, "NutRenderer::getCharWidth() Font is not loaded");
		return 0;
	}

	return READ_LE_UINT16(_dataSrc + _offsets[c] + 6);
}

int32 NutRenderer::getCharHeight(char c) {
	debug(2,  "NutRenderer::getCharHeight() called");
	if (_loaded == false) {
		debug(2, "NutRenderer::getCharHeight() Font is not loaded");
		return 0;
	}

	return READ_LE_UINT16(_dataSrc + _offsets[c] + 8);
}

int32 NutRenderer::getStringWidth(char *string) {
	debug(2,  "NutRenderer::getStringWidth() called");
	if (_loaded == false) {
		debug(2, "NutRenderer::getStringWidth() Font is not loaded");
		return 0;
	}
	int32 length = 0;
	int32 l = 0;
	
	do {
		length += getCharWidth(string[l]);
		l++;
	} while (string[l] != 0);

	return length;
}

void NutRenderer::drawString(const char *string, int32 x, int32 y, byte color, int32 mode) {
	debug(2,  "NutRenderer::drawString() called");
	if (_loaded == false) {
		debug(2, "NutRenderer::drawString() Font is not loaded");
		return;
	}

	int l = 0;
	int left = x;
	int height = 0, tmp;
	do {
		if ((x < 0) || (y < 0) || (x > _vm->_realWidth) || (y > _vm->_realHeight)) {
			debug(2, "NutRenderer::drawString() position x, y out of range");
			return;
		}

		drawChar(string[l], x, y, color);
		x += getCharWidth(string[l]);
		tmp = getCharHeight(string[l]);
		if (height < tmp)
			height = tmp;
		l++;
	} while (string[l] != 0);

	_vm->updateDirtyRect(0, left, x, y, y + height, 0);
}

void NutRenderer::drawChar(char c, int32 x, int32 y, byte color) {
	debug(2,  "NutRenderer::drawChar('%c', %d, %d, %d) called", c, x, y, (int)color);
	if (_loaded == false) {
		debug(2, "NutRenderer::drawChar() Font is not loaded");
		return;
	}

	byte * src = (byte*)(_dataSrc + _offsets[c] + 14);
	byte * dst = _vm->virtscr[0].screenPtr + y * _vm->_realWidth + x + _vm->virtscr[0].xstart;
	byte *mask = _vm->getResourceAddress(rtBuffer, 9)
					+ (y * _vm->_realWidth + x) / 8 + _vm->_screenStartStrip;
	byte maskmask;
	int maskpos;

	uint32 length = READ_BE_UINT32(_dataSrc + _offsets[c] - 4) - 14;

	decodeCodec44(_tmpCodecBuffer, src, length);
	src = _tmpCodecBuffer;

	int32 width = READ_LE_UINT16(_dataSrc + _offsets[c] + 6);
	int32 height = READ_LE_UINT16(_dataSrc + _offsets[c] + 8);
	
	for (int32 ty = 0; ty < height; ty++) {
		maskmask = revBitMask[x & 7];
		maskpos = 0;
		for (int32 tx = 0; tx < width; tx++) {
			byte pixel = *src++;
#if 1
			// FIXME: This way, at least the actor speech colors are done right.
			// However, we still don't draw any "shadows" behind the text, and indeed
			// the character data doesn't seem to contain it. So how does CMI draw the
			// font shadows? Either they are stored seperatly and we have to render them
			// in addition to the normal font. Or maybe the created the shadows dynamically
			// on the fly: if we draw the character several times in black, moved a bit
			// each time, that would mostly create the shadow effect. But why would they
			// do this, as it would increase the char drawing time by factor 5-6 ? And in
			// fact, even then the shadow would not be 100% right.
			if (pixel != 0) {
				mask[maskpos] |= maskmask;
				dst[tx] = color;
			}
#else
			if (pixel != 0) {
				if (pixel == 0x01)
					pixel = (color == 0) ? 0xf : color;
				if (pixel == 0xff)
					pixel = 0x0;
				dst[tx] = pixel;
			}
#endif
			maskmask >>= 1;
			if (maskmask == 0) {
				maskmask = 0x80;
				maskpos++;
			}
		}
		dst += _vm->_realWidth;
		mask += _vm->gdi._numStrips;
	}
}