-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdxr_text.cpp
463 lines (432 loc) · 16.8 KB
/
dxr_text.cpp
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "std.h"
#include "emul.h"
#include "vars.h"
#include "draw.h"
#include "dxrframe.h"
#include "fontdata.h"
#include "font8.h"
#include "font14.h"
#include "font16.h"
#include "init.h"
#include "util.h"
static void *root_tab;
const unsigned char *fontdata = fontdata1;
static void recognize_text(const unsigned char *st, unsigned char *d[])
{
void **dst = (void **)d;
const u32 *start = (u32 *)st;
for(unsigned i = 0; i < 64; i++)
{
dst[i] = root_tab;
}
for(unsigned j = conf.fontsize; j != 0; j--, start += temp.scx >> 4)
{
for(unsigned i = 0; i < 64 / 4; i++)
{
dst[4*i+0] = ((void **)dst[4*i+0])[(start[i] >> 4) & 0xF];
dst[4*i+1] = ((void **)dst[4*i+1])[(start[i] & 0xF)];
dst[4*i+2] = ((void **)dst[4*i+2])[(start[i] >> 20) & 0xF];
dst[4*i+3] = ((void **)dst[4*i+3])[(start[i] >> 16) & 0xF];
}
}
}
#define line_a64_8_2 line_a64_8 // scanlines âûãëÿäèò î÷åíü ïëîõî ñ øðèôòîì 8x16
#define line_a64_8_1 line_a64_8 // ïîýòîìó scanlines íå ïîääåpæàí
#define line_a64_16_2 line_a64_16
#define line_a64_16_1 line_a64_16
#define line_a64_32_2 line_a64_32
#define line_a64_32_1 line_a64_32
void line_a64_8(unsigned char *dst, unsigned char *src, unsigned char *chars[], unsigned line)
{
for (unsigned x = 0; x < 512; x += 32) {
unsigned s = *(unsigned*)src;
unsigned attr = (s >> 6) & 0x3FC;
unsigned char *ptr = *chars++;
if (!ptr) {
*(unsigned*)(dst+x+ 0) = t.sctab8d[0][((s >> 6) & 3) + attr];
*(unsigned*)(dst+x+ 4) = t.sctab8d[0][((s >> 4) & 3) + attr];
} else {
unsigned attr = 0xF0*4;
*(unsigned*)(dst+x+ 0) = t.sctab8[0][((ptr[line] >> 4) & 0xF) + attr*4];
*(unsigned*)(dst+x+ 4) = t.sctab8[0][((ptr[line] ) & 0xF) + attr*4];
}
ptr = *chars++;
if (!ptr) {
*(unsigned*)(dst+x+ 8) = t.sctab8d[0][((s >> 2) & 3) + attr];
*(unsigned*)(dst+x+12) = t.sctab8d[0][((s >> 0) & 3) + attr];
} else {
unsigned attr = 0xF0*4;
*(unsigned*)(dst+x+ 8) = t.sctab8[0][((ptr[line] >> 4) & 0xF) + attr*4];
*(unsigned*)(dst+x+12) = t.sctab8[0][((ptr[line] ) & 0xF) + attr*4];
}
ptr = *chars++;
attr = (s >> 22) & 0x3FC;
if (!ptr) {
*(unsigned*)(dst+x+16) = t.sctab8d[0][((s >>22) & 3) + attr];
*(unsigned*)(dst+x+20) = t.sctab8d[0][((s >>20) & 3) + attr];
} else {
unsigned attr = 0xF0*4;
*(unsigned*)(dst+x+16) = t.sctab8[0][((ptr[line] >> 4) & 0xF) + attr*4];
*(unsigned*)(dst+x+20) = t.sctab8[0][((ptr[line] ) & 0xF) + attr*4];
}
ptr = *chars++;
if (!ptr) {
*(unsigned*)(dst+x+24) = t.sctab8d[0][((s >>18) & 3) + attr];
*(unsigned*)(dst+x+28) = t.sctab8d[0][((s >>16) & 3) + attr];
} else {
unsigned attr = 0xF0*4;
*(unsigned*)(dst+x+24) = t.sctab8[0][((ptr[line] >> 4) & 0xF) + attr*4];
*(unsigned*)(dst+x+28) = t.sctab8[0][((ptr[line] ) & 0xF) + attr*4];
}
src += 4;
}
}
void line_a64_16(unsigned char *dst, unsigned char *src, unsigned char *chars[], unsigned line)
{
for (unsigned x = 0; x < 1024; x += 32) {
unsigned char s = *src++;
unsigned attr = *src++;
unsigned *tab1 = t.sctab16d[0] + attr;
unsigned *tab2 = t.sctab16 [0] + attr*4;
unsigned char *ptr = *chars++;
if (!ptr) {
*(unsigned*)(dst+x+ 0) = tab1[(s << 1) & 0x100];
*(unsigned*)(dst+x+ 4) = tab1[(s << 2) & 0x100];
*(unsigned*)(dst+x+ 8) = tab1[(s << 3) & 0x100];
*(unsigned*)(dst+x+12) = tab1[(s << 4) & 0x100];
} else {
unsigned s = ptr[line];
*(unsigned*)(dst+x) = tab2[(s >> 6) & 3];
*(unsigned*)(dst+x+4) = tab2[(s >> 4) & 3];
*(unsigned*)(dst+x+8) = tab2[(s >> 2) & 3];
*(unsigned*)(dst+x+12)= tab2[(s >> 0) & 3];
}
ptr = *chars++;
if (!ptr) {
*(unsigned*)(dst+x+16) = tab1[(s << 5) & 0x100];
*(unsigned*)(dst+x+20) = tab1[(s << 6) & 0x100];
*(unsigned*)(dst+x+24) = tab1[(s << 7) & 0x100];
*(unsigned*)(dst+x+28) = tab1[(s << 8) & 0x100];
} else {
unsigned s = ptr[line];
*(unsigned*)(dst+x+16) = tab2[(s >> 6) & 3];
*(unsigned*)(dst+x+20) = tab2[(s >> 4) & 3];
*(unsigned*)(dst+x+24) = tab2[(s >> 2) & 3];
*(unsigned*)(dst+x+28) = tab2[(s >> 0) & 3];
}
}
}
void line_a64_32(unsigned char *dst, unsigned char *src, unsigned char *chars[], unsigned line)
{
for (unsigned x = 0; x < 2048; x += 64) {
unsigned char s = *src++;
unsigned attr = *src++;
unsigned *tab = t.sctab32[0] + attr;
unsigned char *ptr = *chars++;
if (!ptr) {
*(unsigned*)(dst+x+ 0) =
*(unsigned*)(dst+x+ 4) = tab[(s << 1) & 0x100];
*(unsigned*)(dst+x+ 8) =
*(unsigned*)(dst+x+12) = tab[(s << 2) & 0x100];
*(unsigned*)(dst+x+16) =
*(unsigned*)(dst+x+20) = tab[(s << 3) & 0x100];
*(unsigned*)(dst+x+24) =
*(unsigned*)(dst+x+28) = tab[(s << 4) & 0x100];
} else {
unsigned s = ptr[line];
*(unsigned*)(dst+x+ 0) = tab[(s << 1) & 0x100];
*(unsigned*)(dst+x+ 4) = tab[(s << 2) & 0x100];
*(unsigned*)(dst+x+ 8) = tab[(s << 3) & 0x100];
*(unsigned*)(dst+x+12) = tab[(s << 4) & 0x100];
*(unsigned*)(dst+x+16) = tab[(s << 5) & 0x100];
*(unsigned*)(dst+x+20) = tab[(s << 6) & 0x100];
*(unsigned*)(dst+x+24) = tab[(s << 7) & 0x100];
*(unsigned*)(dst+x+28) = tab[(s << 8) & 0x100];
}
ptr = *chars++;
if (!ptr) {
*(unsigned*)(dst+x+32) =
*(unsigned*)(dst+x+36) = tab[(s << 5) & 0x100];
*(unsigned*)(dst+x+40) =
*(unsigned*)(dst+x+44) = tab[(s << 6) & 0x100];
*(unsigned*)(dst+x+48) =
*(unsigned*)(dst+x+52) = tab[(s << 7) & 0x100];
*(unsigned*)(dst+x+56) =
*(unsigned*)(dst+x+60) = tab[(s << 8) & 0x100];
} else {
unsigned s = ptr[line];
*(unsigned*)(dst+x+32) = tab[(s << 1) & 0x100];
*(unsigned*)(dst+x+36) = tab[(s << 2) & 0x100];
*(unsigned*)(dst+x+40) = tab[(s << 3) & 0x100];
*(unsigned*)(dst+x+44) = tab[(s << 4) & 0x100];
*(unsigned*)(dst+x+48) = tab[(s << 5) & 0x100];
*(unsigned*)(dst+x+52) = tab[(s << 6) & 0x100];
*(unsigned*)(dst+x+56) = tab[(s << 7) & 0x100];
*(unsigned*)(dst+x+60) = tab[(s << 8) & 0x100];
}
}
}
static unsigned char *zero64[64] = { 0 };
void rend_anti64_8s(unsigned char *dst, unsigned pitch, unsigned char *src, unsigned scroll = 0)
{
ATTR_ALIGN(16) unsigned char *chars[64];
unsigned delta = temp.scx/4;
if (scroll) for (unsigned y = 0; y < 64; y++) chars[y] = 0;
else recognize_text(src, chars), scroll = conf.fontsize;
for (unsigned s = 0; s < scroll; s++) {
line_a64_8_1(dst, src, chars, s); dst += pitch;
src += delta;
}
for (unsigned y = conf.fontsize; y < 192; y+=conf.fontsize) {
recognize_text(src, chars);
for (unsigned line = 0; line < conf.fontsize; line++) {
line_a64_8_1(dst, src, chars, line); dst += pitch;
src += delta;
}
}
for (unsigned line = scroll; line < conf.fontsize; line++) {
line_a64_8_1(dst, src, zero64, 0); dst += pitch;
src += delta;
}
}
void rend_anti64_8d(unsigned char *dst, unsigned pitch, unsigned char *src, unsigned scroll = 0)
{
ATTR_ALIGN(16) unsigned char *chars[64];
unsigned delta = temp.scx/4;
if (scroll) for (unsigned y = 0; y < 64; y++) chars[y] = 0;
else recognize_text(src, chars), scroll = conf.fontsize;
for (unsigned s = 0; s < scroll; s++) {
line_a64_8_1(dst, src, chars, s*2); dst += pitch;
line_a64_8_2(dst, src, chars, s*2+1); dst += pitch;
src += delta;
}
for (unsigned y = conf.fontsize; y < 192; y+=conf.fontsize) {
recognize_text(src, chars);
for (unsigned line = 0; line < conf.fontsize; line++) {
line_a64_8_1(dst, src, chars, line*2); dst += pitch;
line_a64_8_2(dst, src, chars, line*2+1); dst += pitch;
src += delta;
}
}
for (unsigned line = scroll; line < conf.fontsize; line++) {
line_a64_8_1(dst, src, zero64, 0); dst += pitch;
line_a64_8_2(dst, src, zero64, 0); dst += pitch;
src += delta;
}
}
void rend_anti64_16s(unsigned char *dst, unsigned pitch, unsigned char *src, unsigned scroll = 0)
{
ATTR_ALIGN(16) unsigned char *chars[64];
unsigned delta = temp.scx/4;
if (scroll) for (unsigned y = 0; y < 64; y++) chars[y] = 0;
else recognize_text(src, chars), scroll = conf.fontsize;
for (unsigned s = 0; s < scroll; s++) {
line_a64_16_1(dst, src, chars, s); dst += pitch;
src += delta;
}
for (unsigned y = conf.fontsize; y < 192; y+=conf.fontsize) {
recognize_text(src, chars);
for (unsigned line = 0; line < conf.fontsize; line++) {
line_a64_16_1(dst, src, chars, line); dst += pitch;
src += delta;
}
}
for (unsigned line = scroll; line < conf.fontsize; line++) {
line_a64_16_1(dst, src, zero64, 0); dst += pitch;
src += delta;
}
}
void rend_anti64_16d(unsigned char *dst, unsigned pitch, unsigned char *src, unsigned scroll = 0)
{
ATTR_ALIGN(16) unsigned char *chars[64];
unsigned delta = temp.scx/4;
if (scroll) for (unsigned y = 0; y < 64; y++) chars[y] = 0;
else recognize_text(src, chars), scroll = conf.fontsize;
for (unsigned s = 0; s < scroll; s++) {
line_a64_16_1(dst, src, chars, s*2); dst += pitch;
line_a64_16_2(dst, src, chars, s*2+1); dst += pitch;
src += delta;
}
for (unsigned y = conf.fontsize; y < 192; y+=conf.fontsize) {
recognize_text(src, chars);
for (unsigned line = 0; line < conf.fontsize; line++) {
line_a64_16_1(dst, src, chars, line*2); dst += pitch;
line_a64_16_2(dst, src, chars, line*2+1); dst += pitch;
src += delta;
}
}
for (unsigned line = scroll; line < conf.fontsize; line++) {
line_a64_16_1(dst, src, zero64, 0); dst += pitch;
line_a64_16_2(dst, src, zero64, 0); dst += pitch;
src += delta;
}
}
void rend_anti64_32s(unsigned char *dst, unsigned pitch, unsigned char *src, unsigned scroll = 0)
{
ATTR_ALIGN(16) unsigned char *chars[64];
unsigned delta = temp.scx/4;
if (scroll) for (unsigned y = 0; y < 64; y++) chars[y] = 0;
else recognize_text(src, chars), scroll = conf.fontsize;
for (unsigned s = 0; s < scroll; s++) {
line_a64_32_1(dst, src, chars, s); dst += pitch;
src += delta;
}
for (unsigned y = conf.fontsize; y < 192; y+=conf.fontsize) {
recognize_text(src, chars);
for (unsigned line = 0; line < conf.fontsize; line++) {
line_a64_32_1(dst, src, chars, line); dst += pitch;
src += delta;
}
}
for (unsigned line = scroll; line < conf.fontsize; line++) {
line_a64_32_1(dst, src, zero64, 0); dst += pitch;
src += delta;
}
}
void rend_anti64_32d(unsigned char *dst, unsigned pitch, unsigned char *src, unsigned scroll = 0)
{
ATTR_ALIGN(16) unsigned char *chars[64];
unsigned delta = temp.scx/4;
if (scroll) for (unsigned y = 0; y < 64; y++) chars[y] = 0;
else recognize_text(src, chars), scroll = conf.fontsize;
for (unsigned s = 0; s < scroll; s++) {
line_a64_32_1(dst, src, chars, s*2); dst += pitch;
line_a64_32_2(dst, src, chars, s*2+1); dst += pitch;
src += delta;
}
for (unsigned y = conf.fontsize; y < 192; y+=conf.fontsize) {
recognize_text(src, chars);
for (unsigned line = 0; line < conf.fontsize; line++) {
line_a64_32_1(dst, src, chars, line*2); dst += pitch;
line_a64_32_2(dst, src, chars, line*2+1); dst += pitch;
src += delta;
}
}
for (unsigned line = scroll; line < conf.fontsize; line++) {
line_a64_32_1(dst, src, zero64, 0); dst += pitch;
line_a64_32_2(dst, src, zero64, 0); dst += pitch;
src += delta;
}
}
unsigned detect_scroll(unsigned char *src)
{
unsigned char *chars[64];
unsigned delta = temp.scx/4, delta2 = delta * conf.fontsize;
unsigned max = 0, scroll = 0;
for (unsigned line = 0; line < conf.fontsize; line++)
{
unsigned found = 0; unsigned char *src_pos = src; src += delta;
for (unsigned pos = line; pos < 192; pos += conf.fontsize)
{
recognize_text(src_pos, chars);
for (unsigned i = 0; i < 64; i++)
found += (unsigned)(chars[i]) >> 16;
src_pos += delta2;
}
if (found > max)
max = found, scroll = line;
}
return scroll;
}
void __fastcall render_text(unsigned char *dst, unsigned pitch)
{
unsigned char *dst2 = dst + temp.b_left*temp.obpp/4 +
temp.b_top*pitch * ((temp.oy > temp.scy)?2:1);
if (temp.oy > temp.scy && conf.fast_sl) pitch *= 2;
unsigned char *src = rbuf + (temp.b_top*temp.scx+temp.b_left)/4;
unsigned scroll = conf.pixelscroll? detect_scroll(src) : 0;
if (temp.obpp == 8) { if (conf.fast_sl) rend_frame_8d1(dst, pitch), rend_anti64_8s (dst2, pitch, src, scroll); else rend_frame_8d(dst, pitch), rend_anti64_8d (dst2, pitch, src, scroll); return; }
if (temp.obpp == 16) { if (conf.fast_sl) rend_frame_16d1(dst, pitch), rend_anti64_16s(dst2, pitch, src, scroll); else rend_frame_16d(dst, pitch), rend_anti64_16d(dst2, pitch, src, scroll); return; }
if (temp.obpp == 32) { if (conf.fast_sl) rend_frame_32d1(dst, pitch), rend_anti64_32s(dst2, pitch, src, scroll); else rend_frame_32d(dst, pitch), rend_anti64_32d(dst2, pitch, src, scroll); return; }
}
void *alloc_table(void **&tptr, void *startval)
{
void *res = (void *)tptr;
for (unsigned i = 0; i < 16; i++)
tptr[i] = startval;
tptr += 16;
return res;
}
//
// dt[i-1] -> dt[i] -> dt[i+1]
// | dt[i+1]
// |
// dt[i] -> dt[i+1]
// | dt[i+1]
// |
// dt[i] -> dt[i+1]
// dt[i+1]
// l1[] = { 0, 0, 0 };
// l2[] = { l1, l1, l1 };
// l3[] = { l2, l2, l2 };
void create_font_tables()
{
const unsigned char *fontbase = conf.fast_sl ? font8 : font16;
unsigned fonth = conf.fast_sl ? 8 : 16;
if (conf.fontsize < 8)
{
fontbase = conf.fast_sl ? font8 : font14;
fonth = conf.fast_sl ? 8 : 14;
}
void *dummy_tab[8];
void **ts = (void **)t.font_tables;
dummy_tab[conf.fontsize-1] = alloc_table(ts, 0);
for (unsigned i = conf.fontsize-1; i; i--)
dummy_tab[i-1] = alloc_table(ts, dummy_tab[i]);
root_tab = dummy_tab[0];
// todo: collapse tree to graph
// (last bytes of same char with
// different first bytes may be same)
for (unsigned invert = 0; invert < 0x100; invert += 0xFF)
{
for (const unsigned char *ptr = fontdata; *ptr; ptr += 8)
{
unsigned code = *ptr++;
void **tab = (void **)root_tab;
for (unsigned level = 0; ; level++)
{
unsigned bits = (ptr[level] ^ (unsigned char)invert) & 0x0F;
if (level == conf.fontsize-1)
{
if (tab[bits])
{
#if 0 // 1 - debug
color(CONSCLR_ERROR);
printf("duplicate char %d (%02X)! - font table may corrupt\n", (ptr-1-fontdata)/9, code);
exit();
#endif
}
else
{
unsigned *newchar;
unsigned *basechar = (unsigned*)(fontbase + fonth*code); // Øðèôò 8xh, h=8,14,16
if (invert)
{
newchar = (unsigned *)ts;
ts += 4;
newchar[0] = ~basechar[0];
newchar[1] = ~basechar[1];
newchar[2] = ~basechar[2];
newchar[3] = ~basechar[3];
}
else
newchar = basechar;
tab[bits] = newchar;
}
break;
}
void *next = tab[bits];
if (next == dummy_tab[level+1])
tab[bits] = next = alloc_table(ts, (level==conf.fontsize-2)? 0 : dummy_tab[level+2]);
tab = (void **)next;
}
}
}
size_t usedsize = ((u8 *)ts) - ((u8 *)t.font_tables);
if (usedsize > sizeof(t.font_tables))
{
color(CONSCLR_ERROR);
printf("font table overflow: size=%u (0x%X) of 0x%X\n", usedsize, usedsize, sizeof t.font_tables);
exit();
}
}