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
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2002-2005 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 "common/stdafx.h"
#include "scumm/scumm.h"
#include "scumm/nut_renderer.h"
#include "scumm/util.h"
namespace Scumm {
NutRenderer::NutRenderer(ScummEngine *vm) :
_vm(vm),
_loaded(false),
_numChars(0) {
memset(_chars, 0, sizeof(_chars));
}
NutRenderer::~NutRenderer() {
for (int i = 0; i < _numChars; i++) {
delete []_chars[i].src;
}
}
void smush_decode_codec1(byte *dst, const byte *src, int left, int top, int width, int height, int pitch);
static void smush_decode_codec21(byte *dst, const byte *src, int width, int height, int pitch) {
while (height--) {
uint8 *dstPtrNext = dst + pitch;
const uint8 *srcPtrNext = src + 2 + READ_LE_UINT16(src); src += 2;
int len = width;
do {
int offs = READ_LE_UINT16(src); src += 2;
dst += offs;
len -= offs;
if (len <= 0) {
break;
}
int w = READ_LE_UINT16(src) + 1; src += 2;
len -= w;
if (len < 0) {
w += len;
}
// the original codec44 handles this part slightly differently (this is the only difference with codec21) :
// src bytes equal to 255 are replaced by 0 in dst
// src bytes equal to 1 are replaced by a color passed as an argument in the original function
// other src bytes values are copied as-is
memcpy(dst, src, w); dst += w; src += w;
} while (len > 0);
dst = dstPtrNext;
src = srcPtrNext;
}
}
bool NutRenderer::loadFont(const char *filename) {
if (_loaded) {
debug(0, "NutRenderer::loadFont() Font already loaded, ok, loading...");
}
ScummFile file;
_vm->openFile(file, filename);
if (!file.isOpen()) {
error("NutRenderer::loadFont() Can't open font file: %s", filename);
return false;
}
uint32 tag = file.readUint32BE();
if (tag != 'ANIM') {
error("NutRenderer::loadFont() there is no ANIM chunk in font header");
return false;
}
uint32 length = file.readUint32BE();
byte *dataSrc = (byte *)malloc(length);
file.read(dataSrc, length);
file.close();
if (READ_BE_UINT32(dataSrc) != 'AHDR') {
error("NutRenderer::loadFont() there is no AHDR chunk in font header");
free(dataSrc);
return false;
}
_numChars = READ_LE_UINT16(dataSrc + 10);
assert(_numChars <= ARRAYSIZE(_chars));
uint32 offset = 0;
for (int l = 0; l < _numChars; l++) {
offset += READ_BE_UINT32(dataSrc + offset + 4) + 8;
if (READ_BE_UINT32(dataSrc + offset) != 'FRME') {
error("NutRenderer::loadFont(%s) there is no FRME chunk %d (offset %x)", filename, l, offset);
break;
}
offset += 8;
if (READ_BE_UINT32(dataSrc + offset) != 'FOBJ') {
error("NutRenderer::loadFont(%s) there is no FOBJ chunk in FRME chunk %d (offset %x)", filename, l, offset);
break;
}
int codec = READ_LE_UINT16(dataSrc + offset + 8);
_chars[l].xoffs = READ_LE_UINT16(dataSrc + offset + 10);
_chars[l].yoffs = READ_LE_UINT16(dataSrc + offset + 12);
_chars[l].width = READ_LE_UINT16(dataSrc + offset + 14);
_chars[l].height = READ_LE_UINT16(dataSrc + offset + 16);
const int srcSize = _chars[l].width * _chars[l].height;
_chars[l].src = new byte[srcSize];
// If characters have transparency, then bytes just get skipped and
// so there may appear some garbage. That's why we have to fill it
// with zeroes first.
memset(_chars[l].src, 0, srcSize);
const uint8 *fobjptr = dataSrc + offset + 22;
switch (codec) {
case 1:
smush_decode_codec1(_chars[l].src, fobjptr, 0, 0, _chars[l].width, _chars[l].height, _chars[l].width);
break;
case 21:
case 44:
smush_decode_codec21(_chars[l].src, fobjptr, _chars[l].width, _chars[l].height, _chars[l].width);
break;
default:
error("NutRenderer::loadFont: unknown codec: %d", codec);
}
}
free(dataSrc);
_loaded = true;
return true;
}
int NutRenderer::getCharWidth(byte c) const {
if (!_loaded) {
error("NutRenderer::getCharWidth() Font is not loaded");
return 0;
}
if (c >= 0x80 && _vm->_useCJKMode)
return _vm->_2byteWidth / 2;
if (c >= _numChars)
error("invalid character in NutRenderer::getCharWidth : %d (%d)", c, _numChars);
return _chars[c].width;
}
int NutRenderer::getCharHeight(byte c) const {
if (!_loaded) {
error("NutRenderer::getCharHeight() Font is not loaded");
return 0;
}
if (c >= 0x80 && _vm->_useCJKMode)
return _vm->_2byteHeight;
if (c >= _numChars)
error("invalid character in NutRenderer::getCharHeight : %d (%d)", c, _numChars);
return _chars[c].height;
}
void NutRenderer::drawShadowChar(const Graphics::Surface &s, int c, int x, int y, byte color, bool showShadow) {
if (!_loaded) {
error("NutRenderer::drawShadowChar() Font is not loaded");
return;
}
// HACK: we draw the character a total of 7 times: 6 times shifted
// and in black for the shadow, and once in the right color and position.
// This way we achieve the exact look as the original CMI had. However,
// the question remains whether they did it this way, too, or if there is
// some "font shadow" resource we don't know yet.
static const int offsetX[7] = { -1, 0, 1, 0, 1, 2, 0 };
static const int offsetY[7] = { 0, -1, 0, 1, 2, 1, 0 };
const int cTable[7] = { 0, 0, 0, 0, 0, 0, color };
int i = 0;
if (!showShadow)
i = 6;
for (; i < 7; i++) {
x += offsetX[i];
y += offsetY[i];
color = cTable[i];
if (c >= 256 && _vm->_useCJKMode)
draw2byte(s, c, x, y, color);
else
drawChar(s, (byte)c, x, y, color);
x -= offsetX[i];
y -= offsetY[i];
}
}
void NutRenderer::drawFrame(byte *dst, int c, int x, int y) {
const int width = MIN(_chars[c].width, _vm->_screenWidth - x);
const int height = MIN(_chars[c].height, _vm->_screenHeight - y);
const byte *src = _chars[c].src;
const int srcPitch = _chars[c].width;
byte bits = 0;
const int minX = x < 0 ? -x : 0;
const int minY = y < 0 ? -y : 0;
if (height <= 0 || width <= 0) {
return;
}
dst += _vm->_screenWidth * y + x;
if (minY) {
src += minY * srcPitch;
dst += minY * _vm->_screenWidth;
}
for (int ty = minY; ty < height; ty++) {
for (int tx = minX; tx < width; tx++) {
bits = src[tx];
if (bits != 231 && bits) {
dst[tx] = bits;
}
}
src += srcPitch;
dst += _vm->_screenWidth;
}
}
void NutRenderer::drawChar(const Graphics::Surface &s, byte c, int x, int y, byte color) {
byte *dst = (byte *)s.pixels + y * s.pitch + x;
const int width = MIN(_chars[c].width, s.w - x);
const int height = MIN(_chars[c].height, s.h - y);
const byte *src = _chars[c].src;
const int srcPitch = _chars[c].width;
const int minX = x < 0 ? -x : 0;
const int minY = y < 0 ? -y : 0;
if (height <= 0 || width <= 0) {
return;
}
if (minY) {
src += minY * srcPitch;
dst += minY * s.pitch;
}
for (int ty = minY; ty < height; ty++) {
for (int tx = minX; tx < width; tx++) {
if (src[tx] != 0) {
dst[tx] = color;
}
}
src += srcPitch;
dst += s.pitch;
}
}
void NutRenderer::draw2byte(const Graphics::Surface &s, int c, int x, int y, byte color) {
if (!_loaded) {
error("NutRenderer::draw2byte() Font is not loaded");
return;
}
byte *dst = (byte *)s.pixels + y * s.pitch + x;
const int width = _vm->_2byteWidth;
const int height = MIN(_vm->_2byteHeight, s.h - y);
const byte *src = _vm->get2byteCharPtr(c);
byte bits = 0;
if (height <= 0 || width <= 0) {
return;
}
for (int ty = 0; ty < height; ty++) {
for (int tx = 0; tx < width; tx++) {
if ((tx & 7) == 0)
bits = *src++;
if (x + tx < 0 || x + tx >= s.w || y + ty < 0)
continue;
if (bits & revBitMask(tx % 8)) {
dst[tx] = color;
}
}
dst += s.pitch;
}
}
} // End of namespace Scumm
|