-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWinScreen.cpp
More file actions
708 lines (662 loc) · 18 KB
/
Copy pathWinScreen.cpp
File metadata and controls
708 lines (662 loc) · 18 KB
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
#include "stdafx.h"
#include "UI.h"
#include <stdio.h>
#include "Dispatch.h"
#include "CSB.h"
#include "Data.h"
#ifdef _MSVC_INTEL
#include <io.h>
#include <FCNTL.h>
#include <SYS\STAT.h>
#endif
extern CntPtrTo<ID2D1Factory> g_pID2D1Factory;
extern CntPtrTo<ID2D1HwndRenderTarget> g_pID2DRenderTarget;
extern CntPtrTo<ID2D1Bitmap> g_pID2DBitmap;
#ifdef _LINUX
#pragma pack(1)
#define DWORD unsigned int
#define LONG int
#define WORD unsigned short int
#define BYTE unsigned char
#define BI_RGB 0
#define DIB_RGB_COLORS 0
struct BITMAPINFOHEADER
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
};
struct RGBQUAD
{
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
};
struct BITMAPINFO
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
};
#pragma pack()
#endif
extern i32 VBLMultiplier;
extern PALETTE globalPalette;
extern bool fullscreenRequested;
bool virtualFullscreen = false;
extern bool overlayActive;
extern i32 xGraphicJitter;
extern i32 xOverlayJitter;
extern i32 yGraphicJitter;
extern bool jitterChanged;
extern ui32 dumpWindow;
ui32 STBLTCount = 0;
i32 screenSize = 1;
ui32 g_bitmap[320 * 200];
PALETTE palette1;
PALETTE palette2;
PALETTE oldPalette1;
PALETTE oldPalette2;
ui8 black[320]; // All zeros for one line of overlay
ui8 fourBitPixels[320 * 200];
bool screenAlreadyUnpacked;
RECT g_rcClient{0, 0, 1, 1}; // The rect that the DM screen is mapped to in the window
bool IsTextScrollArea(i32, i32)
{
return false;
}
void SwapTextZOrder()
{
return;
}
bool HasPaletteChanged(PALETTE &palette, PALETTE &oldpalette)
{
bool change = false;
for(i32 i = 0; i < 16; i++)
{
if(oldpalette.color[i] != palette.color[i])
{
oldpalette.color[i] = palette.color[i];
change = true;
}
}
return change;
}
ui32 prePixelMasks[16] = {
0xffffffff,
0x00010001,
0x00030003,
0x00070007,
0x000f000f,
0x001f001f,
0x003f003f,
0x007f007f,
0x00ff00ff,
0x01ff01ff,
0x03ff03ff,
0x07ff07ff,
0x0fff0fff,
0x1fff1fff,
0x3fff3fff,
0x7fff7fff,
};
ui32 postPixelMasks[16] = {
0xffffffff,
0x80008000,
0xc000c000,
0xe000e000,
0xf000f000,
0xf800f800,
0xfc00fc00,
0xfe00fe00,
0xff00ff00,
0xff60ff80,
0xffc0ffc0,
0xffe0ffe0,
0xfff0fff0,
0xfff8fff8,
0xfffcfffc,
0xfffefffe};
bool HasAreaChanged(ui8 *STScreen,
int x0, int y0, int width, int height,
ui8 *prevSTScreen)
{
// Each line will consist of
// *** 0-15 pixels (prePixelCount)
// *** n quadwords of 16 pixels each (quadCount)
// *** 0-15 pixels (postPicelCount)
// We will do the comparison by processing the pre-pixels,
// then the quadwords, then the post-pixels.
int prePixelCount, postPixelCount, lastPixel;
int fullQuadCount, line;
ui8 *pFirstQuadA, *pFirstQuadB;
ui32 *pQuadA, *pQuadB;
ui32 prePixelMask, postPixelMask;
pFirstQuadA = STScreen + 160 * y0 + 8 * (x0 / 16);
pFirstQuadB = prevSTScreen + 160 * y0 + 8 * (x0 / 16);
lastPixel = x0 + width;
postPixelCount = lastPixel % 16;
postPixelMask = postPixelMasks[postPixelCount];
prePixelCount = (16 - x0) & 15;
fullQuadCount = (width - prePixelCount - postPixelCount) / 16;
if(prePixelCount != 0)
{
prePixelMask = prePixelMasks[prePixelCount];
if(prePixelCount >= width)
{
prePixelCount = width;
prePixelMask &= postPixelMask;
postPixelMask = 0;
}
pQuadA = (ui32 *)pFirstQuadA;
pQuadB = (ui32 *)pFirstQuadB;
for(line = 0; line < height; line++)
{
if(((pQuadA[0] ^ pQuadB[0]) & prePixelMask) != 0)
return true;
if(((pQuadA[1] ^ pQuadB[1]) & prePixelMask) != 0)
return true;
pQuadA += 40;
pQuadB += 40;
}
}
pQuadA = (ui32 *)pFirstQuadA;
pQuadB = (ui32 *)pFirstQuadB;
if(prePixelCount != 0)
{
pQuadA += 2;
pQuadB += 2;
}
for(line = 0; line < height; line++)
{
ui32 *pQA, *pQB;
int q;
pQA = pQuadA;
pQB = pQuadB;
for(q = 0; q < fullQuadCount; q++)
{
if((pQA[0] != pQB[0]) || (pQA[1] != pQB[1]))
{
return true;
}
pQA += 2;
pQB += 2;
}
pQuadA += 40;
pQuadB += 40;
}
if(postPixelCount != 0)
{
pQuadA = (ui32 *)pFirstQuadA + 2 * fullQuadCount;
pQuadB = (ui32 *)pFirstQuadB + 2 * fullQuadCount;
if(prePixelCount != 0)
{
pQuadA += 2;
pQuadB += 2;
}
for(line = 0; line < height; line++)
{
if(((pQuadA[0] ^ pQuadB[0]) & postPixelMask) != 0)
{
return true;
}
if(((pQuadA[1] ^ pQuadB[1]) & postPixelMask) != 0)
{
return true;
}
pQuadA += 40;
pQuadB += 40;
}
}
return false;
}
bool ForcedScreenDraw = false;
void ForceScreenDraw()
{
ForcedScreenDraw = true;
}
constexpr auto BuildUnpackTable()
{
std::array<ui32, 256> table{};
for(int i = 0; i < 256; i++)
{
ui32 m = 0;
if(i & 0x80)
m |= 0x00000001;
if(i & 0x40)
m |= 0x00000100;
if(i & 0x20)
m |= 0x00010000;
if(i & 0x10)
m |= 0x01000000;
if(i & 0x08)
m |= 0x00000010;
if(i & 0x04)
m |= 0x00001000;
if(i & 0x02)
m |= 0x00100000;
if(i & 0x01)
m |= 0x10000000;
table[i] = m;
}
return table;
}
constexpr std::array<ui32, 256> unpack = BuildUnpackTable();
// Unpack 64000 (320x200) pixels of ST graphic.
// Convert from bit-plane to single byte-per-pixel format
// Takes about a millisecond on Raspberry Pi.
void UnpackScreen(ui8 *src, ui8 *dst)
{
if(screenAlreadyUnpacked)
return;
screenAlreadyUnpacked = true;
ui32 *piDest = (ui32 *)dst;
for(int i = 0; i < 320 * 200 / 16; i++, src += 8, piDest += 4)
{
ui32 r = (unpack[src[0]] << 0) | (unpack[src[2]] << 1) | (unpack[src[4]] << 2) | (unpack[src[6]] << 3);
piDest[0] = r & 0x0f0f0f0f;
piDest[1] = (r >> 4) & 0x0f0f0f0f;
r = (unpack[src[1]] << 0) | (unpack[src[3]] << 1) | (unpack[src[5]] << 2) | (unpack[src[7]] << 3);
piDest[2] = r & 0x0f0f0f0f;
piDest[3] = (r >> 4) & 0x0f0f0f0f;
}
}
int updateScreenAreaEnterCount = 0;
int updateScreenAreaLeaveCount = 0;
// Returns 1 if screen area changed.
int UpdateScreenArea(ui8 *STScreen,
i32 x0, i32 y0,
i32 width, i32 height,
i32 dstX, i32 dstY,
const PALETTE &palette,
bool paletteChanged,
ui8 *pOldScreen,
bool useOverlay)
{
bool overlayChanged = false;
i32 firstNibble[7];
i32 firstOverlay[7];
i32 segWidth[7];
i32 xgj, xoj, ygj, currentGraphicLine, lastGraphicLine;
updateScreenAreaEnterCount++;
overlayChanged = useOverlay && currentOverlay.m_change;
currentOverlay.m_change = false;
if(!paletteChanged && !overlayChanged && !jitterChanged && !HasAreaChanged(STScreen, x0, y0, width, height,pOldScreen))
{
updateScreenAreaLeaveCount++;
return 0;
}
UnpackScreen(physbase(), fourBitPixels);
jitterChanged = false;
currentOverlay.CreateOverlayTable(palette, useOverlay);
STBLTCount++;
if(useOverlay)
{
xgj = xGraphicJitter;
xoj = xOverlayJitter;
ygj = yGraphicJitter;
}
else
{
xgj = 0;
xoj = 0;
ygj = 0;
}
if(ygj >= 0)
{
currentGraphicLine = -ygj;
lastGraphicLine = height - ygj - 1;
}
else
{
currentGraphicLine = -ygj;
lastGraphicLine = height;
}
/* There are 13 cases
* xGraphicJitter xOverlayJitter
* -10 -15 GO-G-B
* -10 -10 GO-B
* -10 -5 GO-O-B
* -10 0 GO-O
* -10 10 G-GO-O
* 0 -10 GO-G
* 0 0 GO
* 0 10 G-GO
* 10 -10 O-GO-G
* 10 5 B-O-GO
* 10 0 O-GO
* 10 10 B-GO
* 10 15 B-G-GO
*
* Altogether we have seven segments in each line
* n0 B
* n1 G
* n2 O
* n3 GO
* n4 G
* n5 O
* n6 B
*
*
* Now we will determine all the parameters for displaying a
* line for each of 13 possible cases.
*/
if(xgj < 0)
{
if(xoj < 0)
{
NotImplemented(0x777107);
}
else if(xoj == 0)
{
segWidth[0] = 0;
segWidth[1] = 0;
segWidth[2] = 0;
firstNibble[3] = -xgj;
firstOverlay[3] = 0;
segWidth[3] = width + xgj;
segWidth[4] = 0;
segWidth[5] = 0;
segWidth[6] = -xgj;
firstNibble[6] = -1;
firstOverlay[6] = width + xgj;
}
else
{
NotImplemented(0x777109);
}
}
else if(xgj == 0)
{
if(xoj < 0)
{
NotImplemented(0x777101);
}
else if(xoj == 0)
{
segWidth[0] = 0;
segWidth[1] = 0;
segWidth[2] = xgj;
firstNibble[2] = -1;
firstOverlay[2] = 0;
firstNibble[3] = 0;
firstOverlay[3] = firstOverlay[2] + xgj;
segWidth[3] = width - xgj;
segWidth[4] = 0;
segWidth[5] = 0;
segWidth[6] = 0;
}
else
{
NotImplemented(0x777104);
}
}
else
{
if(xoj == 0)
{
segWidth[0] = 0;
segWidth[1] = 0;
segWidth[2] = xgj;
firstOverlay[2] = 0;
firstNibble[2] = -1;
firstNibble[3] = 0;
firstOverlay[3] = xgj;
segWidth[3] = width - xgj;
segWidth[4] = 0;
segWidth[5] = 0;
segWidth[6] = 0;
}
else if(xoj < 0)
{
NotImplemented(0x777111);
}
else
{
NotImplemented(0x777112);
}
}
ui32 *pBitmap = g_bitmap + x0 + y0 * 320;
for(i32 line = 0; line < height; line++, currentGraphicLine++)
{
ui8 *pPixels;
if((currentGraphicLine < 0) || (currentGraphicLine > lastGraphicLine))
pPixels = black;
else
pPixels = fourBitPixels + 320 * (y0 + currentGraphicLine) + x0;
i32 currentPixel = 0;
for(i32 segment = 0; segment < 7; segment++)
{
if(segWidth[segment] == 0)
continue;
const ui8 *src = (firstNibble[segment] < 0) ? black : pPixels + firstNibble[segment];
ui8 *pOverlay = (useOverlay && overlayActive) ? currentOverlay.m_overlay + 224 * (135 - line) + firstOverlay[segment] : black;
ui32 *dst = pBitmap + 1 * (320 * line + currentPixel);
int num = segWidth[segment];
const auto *palette = currentOverlay.m_table;
for(; num > 0; num--)
*(dst++) = palette[(*(pOverlay++) << 4) + *(src++)];
currentPixel += segWidth[segment];
}
}
updateScreenAreaLeaveCount++;
return 1;
}
void MakeBMPPalette(RGBQUAD *colors, PALETTE *stpalette)
{
// Create RGB color palette from the ST 9-bit palette
ui32 i;
for(i = 0; i < 16; i++)
{
ASSERT(stpalette->color[i] <= 0x777, "palette");
colors[i].rgbRed = (ui8)((((stpalette->color[i] >> 8) & 7) * 0xff) / 0x7); // red
colors[i].rgbGreen = (ui8)((((stpalette->color[i] >> 4) & 7) * 0xff) / 0x7); // green
colors[i].rgbBlue = (ui8)((((stpalette->color[i] >> 0) & 7) * 0xff) / 0x7); // blue
}
}
void MakeBMPBitmap(ui16 *src, ui8 *dst)
{
ui32 k[16];
i32 i, j, line;
ui16 s[4];
src += 200 * 80;
for(line = 0; line < 200; line++)
{
src -= 80;
for(i = 0; i < 20; i++)
{
// Convert 16 pixels at a time
s[0] = LE16(src[0]);
s[1] = LE16(src[1]);
s[2] = LE16(src[2]);
s[3] = LE16(src[3]);
for(j = 0; j < 16; j++)
{
k[j] = (s[0] >> 15) & 1;
k[j] |= (s[1] >> 14) & 2;
k[j] |= (s[2] >> 13) & 4;
k[j] |= (s[3] >> 12) & 8;
s[0] <<= 1;
s[1] <<= 1;
s[2] <<= 1;
s[3] <<= 1;
}
for(j = 0; j < 8; j++)
{
dst[j] = (ui8)((k[j * 2] << 4) + k[j * 2 + 1]);
}
src += 4;
dst += 8;
}
src -= 80;
}
}
ui8 prevScreen[32000];
void display()
{
static i32 numDisplay = 0;
static bool initialized = false;
int areaChangedCount;
screenAlreadyUnpacked = false;
if(!initialized)
{
memset(black, 0, 320);
initialized = true;
}
numDisplay++;
if((VBLMultiplier != 1) && ((d.Time & 0xf) != 0) && (VBLMultiplier != 99))
return;
if(d.DynamicPaletteSwitching)
{
palette1=d.PalettePortraits;
palette2=d.PaletteViewport;
globalPalette=d.PalettePortraits;
}
else
{
palette1=globalPalette;
palette2=globalPalette;
}
bool pc1 = ForcedScreenDraw || HasPaletteChanged(palette1, oldPalette1);
bool pc2 = ForcedScreenDraw || HasPaletteChanged(palette2, oldPalette2);
ForcedScreenDraw = false;
{
areaChangedCount = 0;
if(!virtualFullscreen)
{
areaChangedCount +=
UpdateScreenArea(physbase(), // STScreen,
0, 0x21,
0xe0, 0xa9 - 0x21,
0, 0x21,
palette2,
pc2,
prevScreen,
videoMode == VM_ADVENTURE);
areaChangedCount +=
UpdateScreenArea(physbase(), // STScreen,
0, 0xa9,
320, 0xc8 - 0xa9,
0, 0xa9,
palette1,
pc1,
prevScreen,
false);
areaChangedCount +=
UpdateScreenArea(physbase(), // STScreen,
0, 0,
320, 0x21,
0, 0,
palette1,
pc1,
prevScreen,
false);
areaChangedCount +=
UpdateScreenArea(physbase(), // STScreen,
0xe0, 0x21,
0x140 - 0xe0, 0xa9 - 0x21,
0xe0, 0x21,
palette2,
pc2,
prevScreen,
false);
if(areaChangedCount)
{
g_pID2DRenderTarget->BeginDraw();
auto rect = D2D1_RECT_U{0, 0, uint32_t(g_rcAtari.right), uint32_t(g_rcAtari.bottom)};
g_pID2DBitmap->CopyFromMemory(&rect, g_bitmap, sizeof(DWORD) * g_rcAtari.right);
g_pID2DRenderTarget->DrawBitmap(g_pID2DBitmap, D2D1_RECT_F{float(g_rcClient.left), float(g_rcClient.top), float(g_rcClient.right), float(g_rcClient.bottom)}, 1.0f, D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR);
g_pID2DRenderTarget->EndDraw();
}
}
else
{
#if 0
if (videoSegSize[0] != 0)
{
areaChangedCount +=
UpdateScreenArea( //Viewport
physbase(),//STScreen,
videoSegSrcX[0],
videoSegSrcY[0],
videoSegWidth[0],
videoSegHeight[0],
videoSegX[0],
videoSegY[0],
palette2,
pc2,
prevScreen,
videoSegSize[0],
true);
}
if (videoSegSize[1] != 0)
{
areaChangedCount +=
UpdateScreenArea( //portrait area
physbase(),//STScreen,
videoSegSrcX[1],
videoSegSrcY[1],
videoSegWidth[1],
videoSegHeight[1],
videoSegX[1],
videoSegY[1],
palette1,
pc1,
prevScreen,
videoSegSize[1],
false);
}
if (videoSegSize[2] != 0)
{
areaChangedCount +=
UpdateScreenArea( //spells,weapons,moves
physbase(),//STScreen,
videoSegSrcX[2],
videoSegSrcY[2],
videoSegWidth[2],
videoSegHeight[2],
videoSegX[2],
videoSegY[2],
palette2,
pc2,
prevScreen,
videoSegSize[2],
false);
}
if (videoSegSize[3] != 0)
{
areaChangedCount +=
UpdateScreenArea( //Text scrolling area
physbase(),//STScreen,
videoSegSrcX[3],
videoSegSrcY[3],
videoSegWidth[3],
videoSegHeight[3],
videoSegX[3],
videoSegY[3],
palette1,
pc1,
prevScreen,
videoSegSize[3],
false);
}
#endif
}
if(areaChangedCount)
{
memcpy(prevScreen, physbase(), 32000);
}
}
#ifdef _MOVIE
UI_SetDIBitsToDevice(-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
#endif
}
// #endif