forked from redcode/SpecEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectDraw.asm
996 lines (782 loc) · 38.6 KB
/
DirectDraw.asm
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
.data?
align 4
lpDD LPDIRECTDRAW ? ; DDraw object
lpDDSPrimary LPDIRECTDRAWSURFACE ? ; DDraw primary surface
lpDDSBackBuffer LPDIRECTDRAWSURFACE ? ; DDraw backbuffer surface
lpDDClipper LPDIRECTDRAWCLIPPER ? ; DDraw clipper object
lpDDPalette LPDIRECTDRAWPALETTE ? ; DDraw palette object
ddsd_p DDSURFACEDESC <?> ; DDraw primary surface descriptor
ddsd_b DDSURFACEDESC <?> ; DDraw backbuffer surface descriptor
ddscaps DDSCAPS <?> ; DDraw surface capabilities
ddPixelFormat DDPIXELFORMAT <?> ; DDraw pixel format
ddcaps DDCAPS <?> ; DDraw capabilities
ddbltfx DDBLTFX <?> ; DDraw Blitter FX
SrcRect RECT <?>
DDRect RECT <?>
BackBuffRect RECT <?>
FullScreenMode DWORD ?
DXResult DWORD ?
DesktopBPP DWORD ?
RedComponent DWORD ?
GreenComponent DWORD ?
BlueComponent DWORD ?
ddCurrentPalette DWORD ?
DIBdrawaddr DWORD ?
DIB_paper BYTE ?
DIB_ink BYTE ?
.data
SurfacesReady db FALSE ; surfaces only drawn when TRUE
.code
DDLOG macro str:req, code:req
ifdef LOGGING
LOGHEX str, eax
endif
endm
InitDirectDraw proc
DDCREATE_EMULATIONONLY = 2
mov lpDD, NULL
mov lpDDSPrimary, NULL
mov lpDDSBackBuffer, NULL
mov lpDDClipper, NULL
memclr addr ddbltfx, sizeof ddbltfx
mov ddbltfx.dwSize, sizeof ddbltfx
LOG "Creating DirectDraw object"
ifc DirectDraw_Acceleration eq FALSE then mov ecx, DDCREATE_EMULATIONONLY else mov ecx, NULL
invoke DirectDrawCreate, ecx, addr lpDD, NULL
mov DXResult, eax
.if eax != DD_OK
FATAL "Error initialising DirectDraw", eax
.endif
LOG "Reading DirectDraw capabilities"
memclr addr ddcaps, sizeof ddcaps
mov [ddcaps.dwSize], sizeof ddcaps
DDINVOKE GetCaps, lpDD, ADDR ddcaps, NULL
mov DXResult, eax
.if eax != DD_OK
FATAL "Error reading DirectDraw capabilities", eax
.endif
ret
InitDirectDraw endp
GetSurfaceColours proc
LOG "Reading surface pixel format"
mov [ddPixelFormat.dwSize], sizeof DDPIXELFORMAT
DDSINVOKE GetPixelFormat, lpDDSPrimary, addr ddPixelFormat
.if eax != DD_OK
FATAL "GetPixel Format failed!", eax
.endif
mov eax, -1
and eax, [ddPixelFormat.dwRBitMask]
mov [RedComponent], eax
mov eax, -1
and eax, [ddPixelFormat.dwGBitMask]
mov [GreenComponent], eax
mov eax, -1
and eax, [ddPixelFormat.dwBBitMask]
mov [BlueComponent], eax
invoke SetSpectrumPalette, UserPalette
invoke BuildULAplusPalette
invoke SetDirtyLines
UPDATEWINDOW
ret
GetSurfaceColours endp
ShutdownDirectDraw proc
.if lpDD != NULL
invoke FreeSurfaces
LOG "Releasing DirectDraw object"
DDINVOKE Release, lpDD
mov [lpDD], NULL
.endif
ret
ShutdownDirectDraw endp
FlipDisplayMode proc
.if FullScreenMode == TRUE ; switching to windowed mode
mov SwitchingModes, TRUE
LOG "--> Switching to windowed mode"
LOG "Freeing fullscreen surfaces"
invoke FreeSurfaces
invoke ShutdownDirectDraw
invoke InitDirectDraw
LOG "SetWindowLong"
invoke SetWindowLong, hWnd, GWL_EXSTYLE, dwExStyle
invoke SetWindowLong, hWnd, GWL_STYLE, dwStyle
LOG "Resizing window"
invoke ShowWindow, mainwin_hToolBar, SW_SHOW
invoke ShowWindow, mainwin_hStatus, SW_SHOW
mov ecx, WindowRect.right
sub ecx, WindowRect.left
mov edx, WindowRect.bottom
sub edx, WindowRect.top
invoke MoveWindow, hWnd, WindowRect.left, WindowRect.top, ecx, edx, TRUE
mov FullScreenMode, FALSE
LOG "Initialising windowed mode surfaces"
invoke InitSurfaces, hWnd
LOG "Updating window"
mov SwitchingModes, FALSE
invoke ShowWindow, hWnd, SW_SHOW
invoke SetDirtyLines
UPDATEWINDOW
LOG "Mode switch complete"
ret
.else ; switching to fullscreen mode
invoke ShowWindow, mainwin_hToolBar, SW_HIDE
invoke ShowWindow, mainwin_hStatus, SW_HIDE
invoke HideTools1Dialog
mov SwitchingModes, TRUE
LOG "--> Switching to fullscreen mode"
LOG "Freeing windowed surfaces"
invoke FreeSurfaces
invoke ShutdownDirectDraw
invoke InitDirectDraw
invoke GetWindowRect, hWnd, ADDR WindowRect ; save the window position
LOG "SetWindowLong"
invoke SetWindowLong, hWnd, GWL_EXSTYLE, WS_EX_LEFT or WS_EX_TOPMOST
invoke SetWindowLong, hWnd, GWL_STYLE, WS_VISIBLE or WS_POPUP
LOG "Resizing window"
invoke MoveWindow, hWnd, 0, 0, DDWidth, DDHeight, TRUE
mov FullScreenMode, TRUE
LOG "Initialising fullscreen mode surfaces"
invoke InitSurfaces, hWnd
LOG "Updating window"
mov SwitchingModes, FALSE
invoke SetDirtyLines
UPDATEWINDOW
LOG "Mode switch complete"
ret
.endif
FlipDisplayMode endp
InitSurfaces proc uses ebx esi edi,
hWin: DWORD
LOCAL DDMemFlags: DWORD
.if FullScreenMode == TRUE
; setup fullscreen mode
LOG "SetCooperativeLevel = DDSCL_FULLSCREEN + DDSCL_EXCLUSIVE"
DDINVOKE SetCooperativeLevel, lpDD, hWin, DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE
.if eax != DD_OK
FATAL "Error setting DirectDraw cooperative level", eax
.endif
LOG "Setting fullscreen display mode"
DDINVOKE SetDisplayMode, lpDD, DDWidth, DDHeight, DDBpp
.if eax != DD_OK
FATAL "Error setting display mode", eax
.endif
.else
; setup windowed mode
LOG "SetCooperativeLevel = DDSCL_NORMAL"
DDINVOKE SetCooperativeLevel, lpDD, hWin, DDSCL_NORMAL
.if eax != DD_OK
FATAL "Failed setting DirectDraw cooperative level", eax
.endif
.endif
; the rest of surface setup is *almost* identical for windowed and fullscreen modes
; setup primary surface
memclr addr ddsd_p, sizeof ddsd_p
mov [ddsd_p.dwSize], sizeof DDSURFACEDESC
mov [ddsd_p.dwFlags], DDSD_CAPS
mov [ddsd_p.ddsCaps.dwCaps], DDSCAPS_PRIMARYSURFACE or DDSCAPS_VIDEOMEMORY
RecreateSurface: LOG "Creating primary surface"
DDINVOKE CreateSurface, lpDD, ADDR ddsd_p, ADDR lpDDSPrimary, NULL
mov DXResult, eax
.if eax != DD_OK
switch eax
case DDERR_NOEXCLUSIVEMODE
invoke Sleep, 100
jmp RecreateSurface
case DDERR_NODIRECTDRAWHW
mov [ddsd_p.ddsCaps.dwCaps], DDSCAPS_PRIMARYSURFACE or DDSCAPS_SYSTEMMEMORY
jmp RecreateSurface
.else
FATAL "Failed creating primary surface", eax
endsw
.endif
; setup backbuffer surface
memclr addr ddsd_b, sizeof ddsd_b
mov [ddsd_b.dwSize], sizeof DDSURFACEDESC
mov [ddsd_b.dwWidth], DIBWidth
mov [ddsd_b.dwHeight], DIBHeight
mov [ddsd_b.dwFlags], DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT
mov eax, [ddcaps.dwCaps]
and eax, DDCAPS_BLTSTRETCH
.if eax == DDCAPS_BLTSTRETCH
mov [DDMemFlags], DDSCAPS_VIDEOMEMORY
LOG "Attempting to allocate VIDEOMEM backbuffer"
.else
mov [DDMemFlags], DDSCAPS_SYSTEMMEMORY
LOG "Attempting to allocate SYSTEMMEM backbuffer"
.endif
mov eax, [DDMemFlags]
or eax, DDSCAPS_OFFSCREENPLAIN
mov [ddsd_b.ddsCaps.dwCaps], eax
LOG "Creating back buffer surface"
DDINVOKE CreateSurface, lpDD, ADDR ddsd_b, ADDR lpDDSBackBuffer, NULL
mov DXResult, eax
.if eax != DD_OK
.if [DDMemFlags] == DDSCAPS_VIDEOMEMORY
LOG "Falling back to a SYSTEMMEM backbuffer"
mov [DDMemFlags], DDSCAPS_SYSTEMMEMORY
mov eax, [DDMemFlags]
or eax, DDSCAPS_OFFSCREENPLAIN
mov [ddsd_b.ddsCaps.dwCaps], eax
DDINVOKE CreateSurface, lpDD, ADDR ddsd_b, ADDR lpDDSBackBuffer, NULL
mov DXResult, eax
.endif
.endif
.if eax != DD_OK
FATAL "Failed creating backbuffer surface", eax
.endif
LOG "Creating clipper"
mov lpDDClipper, 0
DDINVOKE CreateClipper, lpDD, 0, ADDR lpDDClipper, NULL
mov DXResult, eax
.if eax == DD_OK
LOG "Attaching clipper to primary surface"
DDCINVOKE SetHWnd, lpDDClipper, 0, hWin
DDSINVOKE SetClipper, lpDDSPrimary, lpDDClipper
LOG "Releasing clipper"
DDCINVOKE Release, lpDDClipper
.else
FATAL "Failed creating clipper object", eax
.endif
invoke GetSurfaceColours
mov SurfacesReady, TRUE
ret
InitSurfaces endp
FreeSurfaces proc
.if FullScreenMode == TRUE
LOG "Restoring Display mode"
DDINVOKE RestoreDisplayMode, lpDD
.if eax != DD_OK
LOG "Failed restoring display mode"
.endif
LOG "SetCooperativeLevel = Normal"
DDINVOKE SetCooperativeLevel, lpDD, hWnd, DDSCL_NORMAL
.if eax != DD_OK
LOG "Failed setting DirectDraw cooperative level"
.endif
.endif
.if lpDDSPrimary != NULL
LOG "Releasing primary surface"
DDSINVOKE Release, lpDDSPrimary
mov [lpDDSPrimary], NULL
.endif
.if lpDDSBackBuffer != NULL
LOG "Releasing back buffer surface"
DDSINVOKE Release, lpDDSBackBuffer
mov [lpDDSBackBuffer], NULL
.endif
mov SurfacesReady, FALSE
ret
FreeSurfaces endp
RestoreSurfaces proc
; returns DD_OK if surfaces not lost or successfully restored
DDSINVOKE IsLost, lpDDSPrimary
.if eax == DDERR_SURFACELOST
DDSINVOKE Restore, lpDDSPrimary
invoke SetDirtyLines
.endif
.if eax != DD_OK
ret
.endif
DDSINVOKE IsLost, lpDDSBackBuffer
.if eax == DDERR_SURFACELOST
DDSINVOKE Restore, lpDDSBackBuffer
invoke SetDirtyLines
.endif
ret
RestoreSurfaces endp
align 16
DIBToScreen proc hWin: DWORD
FS_OFFSET_X equ ((DIBWidth-320)/2)
FS_OFFSET_Y equ 24 ; only take lower 24 border lines from top border area
ZOOMOFFSET equ 15
invoke Draw_Icons
.if $fnc (DumptoDXSurface) == DD_OK
invoke BlitScreen, hWin
.endif
ret ; return with any error
DIBToScreen endp
align 16
BltFlip_Mirror_Horz proc
test [ddcaps.dwFXCaps], DDFXCAPS_BLTMIRRORLEFTRIGHT
.if !ZERO?
xor ddbltfx.dwDDFX, DDBLTFX_MIRRORLEFTRIGHT
.endif
ret
BltFlip_Mirror_Horz endp
align 16
BltFlip_Mirror_Vert proc
test [ddcaps.dwFXCaps], DDFXCAPS_BLTMIRRORUPDOWN
.if !ZERO?
xor ddbltfx.dwDDFX, DDBLTFX_MIRRORUPDOWN
.endif
ret
BltFlip_Mirror_Vert endp
DOBLIT MACRO
.if VSync_Enabled == TRUE
DDINVOKE WaitForVerticalBlank, lpDD, DDWAITVB_BLOCKBEGIN, NULL
.endif
DDSINVOKE Blt, lpDDSPrimary, addr DDRect, lpDDSBackBuffer, addr SrcRect, DDBLT_WAIT or DDBLT_DDFX, addr ddbltfx
.if eax != DD_OK
DDLOG "Blit fail: ", eax
.if eax == DDERR_INVALIDRECT
LOG "INVALIDRECT"
LOGRECT "Src rect", offset SrcRect
LOGRECT "Dest rect", offset DDRect
.endif
.endif
ENDM
align 16
GetSrcDIBRect proc lpRect: DWORD
mov ecx, lpRect
assume ecx: ptr RECT
mov [ecx].left, 0
m2m [ecx].right, MACHINE.DisplayWidth
mov [ecx].top, 0
m2m [ecx].bottom, MACHINE.DisplayHeight
ret
assume ecx: nothing
GetSrcDIBRect endp
align 16
SetDIBDrawPosn proc x, y: DWORD
imul eax, y, DIBWidth
add eax, x
add eax, lpDIBBits
mov DIBdrawaddr, eax
ret
SetDIBDrawPosn endp
align 16
SetDIBPaper proc colour: BYTE
mov al, colour
add al, CLR_SPECBASE
mov DIB_paper, al
ret
SetDIBPaper endp
align 16
SetDIBInk proc colour: BYTE
mov al, colour
add al, CLR_SPECBASE
mov DIB_ink, al
ret
SetDIBInk endp
align 16
DrawDIBText proc uses edi esi,
lpText: DWORD
mov esi, lpText
.while byte ptr [esi] != 0
invoke DrawDIBChar, byte ptr [esi]
inc esi
.endw
ret
DrawDIBText endp
align 16
DrawDIBChar proc uses esi edi,
char: BYTE
movzx eax, char
sub eax, 32
.if CARRY? || (eax >= DIBcharfontsizeof/8)
mov al, "?" - 32
.endif
mov edi, DIBdrawaddr
lea esi, [DIBcharfont+eax*8]
mov dl, DIB_paper
mov dh, DIB_ink
SETLOOP 8
mov al, [esi]
inc esi
mov ah, 8
@@: shl al, 1
.if CARRY?
mov [edi], dh
.else
mov [edi], dl
.endif
inc edi
dec ah
jnz @B
add edi, DIBWidth - 8
ENDLOOP
add DIBdrawaddr, 8
ret
DrawDIBChar endp
align 16
BlitScreen proc hWin: DWORD
local tPoint: POINT
invoke GetSrcDIBRect, addr SrcRect
.if FullScreenMode == TRUE
mov DDRect.left, 0
mov DDRect.top, 0
mov DDRect.right, DDWidth
mov DDRect.bottom, DDHeight
.else
; window routine
mov tPoint.x, 0
mov tPoint.y, 0
invoke ClientToScreen, hWin, addr tPoint
invoke GetClientRect, hWin, addr DDRect
invoke OffsetRect, addr DDRect, tPoint.x, tPoint.y
mov eax, ToolBarHeight
add DDRect.top, eax
mov eax, StatusHeight
sub DDRect.bottom, eax
.endif
DOBLIT
.if eax != DD_OK
.if eax == DDERR_SURFACELOST
invoke RestoreSurfaces
.if eax == DD_OK
DOBLIT
.endif
.else
LOG "Blit error"
.endif
.endif
ret
BlitScreen endp
GETPIXELPALETTE macro
ifc byte ptr [edx] lt 128 then mov ddCurrentPalette, offset ddSurfacePalette else mov ddCurrentPalette, offset ddULAplusPalette
endm
align 16
ColourDump proc uses esi edi ebx,
Dest: DWORD,
Pitch: DWORD,
BPPFunction: DWORD,
DisplayOffset: DWORD, ; offset from Dest in bytes
R_BorderOffset: DWORD ; offset from Dest in bytes
local pEndSurface: DWORD
mov eax, MACHINE.DisplayHeight
mov ecx, [Pitch]
mul ecx
add eax, [Dest]
mov pEndSurface, eax
mov eax, SPGfx.VerticalOffset
.if eax > 0
sub eax, 10
ifc CARRY? then xor eax, eax
mov SPGfx.VerticalOffset, eax
mul [Pitch]
add eax, [Dest]
mov edi, eax
invoke SetDirtyLines
.else
mov edi, [Dest]
.endif
mov esi, [lpDIBBits]
lea edx, DirtyLines
movzx eax, MACHINE.TopBorderLines
SETLOOP eax
.if byte ptr [edx] != 0
GETPIXELPALETTE
mov byte ptr [edx], 0
push edi
push esi
mov eax, MACHINE.DisplayWidth
call [BPPFunction]
pop esi
pop edi
.endif
add edx, 3
add esi, DIBWidth
add edi, [Pitch]
ifc edi ge pEndSurface then mov edi, Dest
ENDLOOP
SETLOOP DISPLAYLINES
; test left border update
.if byte ptr [edx] != 0
GETPIXELPALETTE
mov byte ptr [edx], 0
push edi
push esi
mov eax, MACHINE.BorderWidth
call [BPPFunction]
pop esi
pop edi
.endif
inc edx
; test display update
.if byte ptr [edx] != 0
GETPIXELPALETTE
mov byte ptr [edx], 0
push edi
push esi
add esi, MACHINE.BorderWidth
add edi, DisplayOffset
mov eax, MACHINE.PixelWidth
call [BPPFunction]
pop esi
pop edi
.endif
inc edx
; test right border update
.if byte ptr [edx] != 0
GETPIXELPALETTE
mov byte ptr [edx], 0
push edi
push esi
add esi, MACHINE.BorderWidth
add esi, MACHINE.PixelWidth
add edi, R_BorderOffset
mov eax, MACHINE.BorderWidth
call [BPPFunction]
pop esi
pop edi
.endif
inc edx
add esi, DIBWidth
add edi, [Pitch]
ifc edi ge pEndSurface then mov edi, Dest
ENDLOOP
movzx eax, MACHINE.BottomBorderLines
SETLOOP eax
.if byte ptr [edx] != 0
GETPIXELPALETTE
mov byte ptr [edx], 0
push edi
push esi
mov eax, MACHINE.DisplayWidth
call [BPPFunction]
pop esi
pop edi
.endif
add edx, 3
add esi, DIBWidth
add edi, [Pitch]
ifc edi ge pEndSurface then mov edi, Dest
ENDLOOP
ret
ColourDump endp
align 16
ColourDump8bit: mov ecx, eax
shr ecx, 2 ; bytes_to_convert/4
rep movsd
ret
align 16
ColourDump16bit: push edx
push ebp
mov ebp, ddCurrentPalette
shr eax, 3 ; bytes_to_convert/8
push eax
@@: movzx eax, byte ptr [esi]
movzx ebx, byte ptr [esi+1]
movzx ecx, byte ptr [esi+2]
movzx edx, byte ptr [esi+3]
add esi, 4
mov ax, word ptr [ebp+eax*4]
mov bx, word ptr [ebp+ebx*4]
mov cx, word ptr [ebp+ecx*4]
mov dx, word ptr [ebp+edx*4]
mov [edi], ax
mov [edi+2], bx
mov [edi+4], cx
mov [edi+6], dx
add edi, 8
movzx eax, byte ptr [esi]
movzx ebx, byte ptr [esi+1]
movzx ecx, byte ptr [esi+2]
movzx edx, byte ptr [esi+3]
add esi, 4
mov ax, word ptr [ebp+eax*4]
mov bx, word ptr [ebp+ebx*4]
mov cx, word ptr [ebp+ecx*4]
mov dx, word ptr [ebp+edx*4]
mov [edi], ax
mov [edi+2], bx
mov [edi+4], cx
mov [edi+6], dx
add edi, 8
dec dword ptr [esp]
jnz @B
add esp, 4
pop ebp
pop edx
ret
align 16
ColourDump24bit: push ebp
mov ebp, ddCurrentPalette
shr eax, 2 ; bytes_to_convert/4
push eax
@@: mov ebx, [esi]
add esi, 4
movzx eax, bl
shr ebx, 8
mov ecx, dword ptr [ebp+eax*4]
mov [edi], ecx
movzx eax, bl
shr ebx, 8
mov ecx, dword ptr [ebp+eax*4]
mov [edi+3], ecx
movzx eax, bl
shr ebx, 8
mov ecx, dword ptr [ebp+eax*4]
mov [edi+6], ecx
movzx eax, bl
shr ebx, 8
mov ecx, dword ptr [ebp+eax*4]
mov [edi+9], ecx
add edi, 12
dec dword ptr [esp]
jnz @B
add esp, 4
pop ebp
ret
align 16
ColourDump32bit: push edx
push ebp
mov ebp, ddCurrentPalette
shr eax, 2 ; bytes_to_convert/4
push eax
@@: movzx eax, byte ptr [esi]
movzx ebx, byte ptr [esi+1]
movzx ecx, byte ptr [esi+2]
movzx edx, byte ptr [esi+3]
add esi, 4
mov eax, dword ptr [ebp+eax*4]
mov ebx, dword ptr [ebp+ebx*4]
mov ecx, dword ptr [ebp+ecx*4]
mov edx, dword ptr [ebp+edx*4]
mov [edi], eax
mov [edi+4], ebx
mov [edi+8], ecx
mov [edi+12], edx
add edi, 16
dec dword ptr [esp]
jnz @B
add esp, 4
pop ebp
pop edx
ret
align 16
DumptoDXSurface proc
local Pitch : DWORD,
Dest : DWORD
invoke LockBackBufferSurface
.if eax != DD_OK ; failed to lock the surface
ret ; return with the error
.endif
m2m Pitch, [ddsd_b.lPitch]
m2m Dest, [ddsd_b.lpSurface]
.if FullScreenMode == TRUE
mov eax, DDBpp
.else
mov eax, DesktopBPP
.endif
mov edx, MACHINE.BorderWidth ; left border width
add edx, MACHINE.PixelWidth ; right border offset
mov ecx, MACHINE.BorderWidth
.if eax == 32
shl edx, 2 ; * 4
shl ecx, 2 ; * 4
invoke ColourDump, Dest, Pitch, addr ColourDump32bit, ecx, edx
.elseif eax == 16
shl edx, 1 ; * 2
shl ecx, 1 ; * 2
invoke ColourDump, Dest, Pitch, addr ColourDump16bit, ecx, edx
.elseif eax == 24
mov eax, edx
shl edx, 1
add edx, eax ; * 3
mov eax, ecx
shl ecx, 1
add ecx, eax ; * 3
invoke ColourDump, Dest, Pitch, addr ColourDump24bit, ecx, edx
.elseif eax == 8
invoke ColourDump, Dest, Pitch, addr ColourDump8bit, ecx, edx
.else
invoke UnlockBackBufferSurface
LOG "Unsupported desktop colour depth"
return -1 ; return with error
.endif
invoke UnlockBackBufferSurface
return DD_OK ; return no error
DumptoDXSurface endp
align 16
LockBackBufferSurface proc
LockBackSurface: mov [ddsd_b.dwSize], sizeof DDSURFACEDESC
mov [ddsd_b.dwFlags], DDSD_PITCH
; not using DDLOCK_WAIT causes deferred blits to prevent main window refreshes on WM_PAINT when emulator is paused
DDSINVOKE mLock, lpDDSBackBuffer, NULL, addr ddsd_b, DDLOCK_SURFACEMEMORYPTR or DDLOCK_WRITEONLY or DDLOCK_WAIT, NULL
.if eax == DD_OK
ret ; return with no error
.endif
.if eax == DDERR_SURFACELOST
invoke RestoreSurfaces
.if eax == DD_OK
jmp LockBackSurface ; relock if surfaces restored successfully
.endif
DDLOG "Restore fail: ", eax
ret ; else return the error
.elseif (eax == DDERR_SURFACEBUSY) || (eax == DDERR_WASSTILLDRAWING)
ret ; drop this frame and return the error
.else
DDLOG "Lock fail: ", eax
.endif
ret ; return the error
LockBackBufferSurface endp
align 16
UnlockBackBufferSurface proc
DDSINVOKE Unlock, lpDDSBackBuffer, NULL
.if eax != DD_OK
DDLOG "Unlock fail: ", eax
.endif
ret
UnlockBackBufferSurface endp
align 16
GetDesktopBPP proc
local tempDC: DWORD,
tempBPP: DWORD
mov tempDC, $fnc (GetWindowDC, $fnc (GetDesktopWindow))
mov tempBPP, $fnc (GetDeviceCaps, tempDC, BITSPIXEL)
invoke ReleaseDC, $fnc (GetDesktopWindow), tempDC
return tempBPP ; return bits per pixel
GetDesktopBPP endp
.data
align 8
; Original file C:\RadASM\Spectrum_Projects\SuperCrapInvaders\NewFont.font at 768 bytes
DIBcharfont db 0,0,0,0,0,0,0,0,48,48,48,48,48,0,48,48
db 54,108,0,0,0,0,0,0,0,36,126,36,36,126,36,0
db 0,8,62,40,62,10,62,8,113,82,100,8,19,37,71,0
db 0,16,40,16,42,68,58,0,12,12,24,0,0,0,0,0
db 12,24,24,24,24,24,24,12,24,12,12,12,12,12,12,24
db 0,24,126,60,126,24,0,0,0,0,8,8,62,8,8,0
db 0,0,0,0,0,24,24,48,0,0,0,62,62,0,0,0
db 0,0,0,0,0,48,48,0,1,2,4,8,16,32,64,0
db 62,99,103,107,115,99,62,0,56,24,24,24,24,24,126,0
db 60,102,6,12,24,48,127,0,127,3,6,31,3,99,62,0
db 6,14,22,38,70,127,6,6,127,96,96,62,3,99,62,0
db 12,24,48,126,99,99,62,0,127,99,3,6,12,24,48,0
db 62,99,99,62,99,99,62,0,62,99,99,63,3,99,62,0
db 0,0,24,24,0,24,24,0,0,0,24,24,0,24,24,48
db 0,0,4,8,16,8,4,0,0,0,0,62,0,62,0,0
db 255,231,129,195,129,231,255,255,62,99,3,30,24,0,24,24
db 0,60,74,86,94,64,60,0,28,54,99,127,99,99,102,0
db 94,119,99,126,99,119,94,0,30,51,96,96,96,51,30,0
db 124,102,99,99,99,102,124,0,63,115,96,126,96,115,63,0
db 127,99,96,124,96,96,96,0,62,99,96,111,99,103,61,0
db 99,99,99,127,115,99,99,0,126,90,24,24,24,90,126,0
db 63,6,3,3,99,54,28,0,99,102,108,124,110,103,99,0
db 112,96,96,96,96,99,127,0,118,127,107,107,107,99,102,0
db 110,63,51,51,51,51,115,2,28,54,99,99,99,54,28,0
db 126,51,51,126,48,48,120,0,28,54,99,99,109,54,27,1
db 126,99,99,126,108,102,99,0,62,99,96,62,3,99,62,0
db 255,153,24,24,24,24,60,0,115,51,99,99,99,103,61,0
db 99,99,99,99,119,62,28,8,99,99,107,107,107,127,54,0
db 99,119,62,28,62,119,99,0,115,51,99,63,3,99,62,0
db 127,71,14,28,56,115,127,0,0,14,8,8,8,8,14,0
db 0,0,64,32,16,8,4,0,0,112,16,16,16,16,112,0
db 0,16,56,84,16,16,16,0,0,0,0,0,0,0,0,255
db 0,28,34,120,32,32,126,0,0,0,62,3,63,99,63,0
db 48,96,110,115,99,99,126,0,0,0,62,99,96,99,62,0
db 7,3,59,103,99,99,63,0,0,0,62,99,126,96,62,0
db 14,24,127,24,24,24,24,48,0,0,62,99,99,63,3,126
db 96,96,126,99,99,99,102,0,24,0,56,24,24,24,126,0
db 12,0,14,7,3,3,102,60,96,96,99,102,124,102,99,0
db 56,24,48,48,48,60,24,0,0,0,118,127,107,107,99,2
db 0,0,110,59,51,51,115,2,0,0,62,99,99,99,62,0
db 0,0,126,51,51,126,48,120,0,0,62,102,102,110,54,7
db 0,0,110,51,48,48,120,0,0,0,62,96,62,3,126,0
db 0,48,126,48,48,51,30,0,0,0,115,51,99,99,63,0
db 0,0,99,99,54,28,8,0,0,0,99,107,107,127,54,0
db 0,0,99,54,28,54,99,0,0,0,115,51,99,63,3,62
db 0,0,127,70,28,49,127,0,0,14,8,48,8,8,14,0
db 0,8,8,8,8,8,8,0,0,112,16,12,16,16,112,0
db 0,20,40,0,0,0,0,0,60,66,153,161,161,153,66,60
DIBcharfontsizeof equ $-DIBcharfont
.code