This repository was archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathvid_d3d.cpp
More file actions
2470 lines (1995 loc) · 77.9 KB
/
Copy pathvid_d3d.cpp
File metadata and controls
2470 lines (1995 loc) · 77.9 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
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
997
998
999
1000
// DirectD3D blitter
// #define USE_D3D_REFERENCE_DEVICE
#include "burner.h"
#if !defined BUILD_X64_EXE
// #include "vid_directx_support.h"
#include <guiddef.h>
#include "vid_softfx.h"
// #define ENABLE_PROFILING FBA_DEBUG
#define DIRECT3D_VERSION 0x0700 // Use this Direct3D version
#define D3D_OVERLOADS
#include <d3d.h>
#include "rgb_pattern.h"
#include "ddraw_core.h"
const float PI = 3.14159265358979323846f;
typedef struct _D3DTLVERTEX2 {
union { D3DVALUE sx; D3DVALUE dvSX; };
union { D3DVALUE sy; D3DVALUE dvSY; };
union { D3DVALUE sz; D3DVALUE dvSZ; };
union { D3DVALUE rhw; D3DVALUE dvRHW; };
union { D3DCOLOR color; D3DCOLOR dcColor; };
union { D3DCOLOR specular; D3DCOLOR dcSpecular; };
union { D3DVALUE tu; D3DVALUE dvTU; };
union { D3DVALUE tv; D3DVALUE dvTV; };
union { D3DVALUE tu1; D3DVALUE dvTU1; };
union { D3DVALUE tv1; D3DVALUE dvTV1; };
#if(DIRECT3D_VERSION >= 0x0500)
#if (defined __cplusplus) && (defined D3D_OVERLOADS)
_D3DTLVERTEX2() { }
_D3DTLVERTEX2(const D3DVECTOR& v, float _rhw, D3DCOLOR _color, D3DCOLOR _specular, float _tu, float _tv, float _tu1, float _tv1)
{
sx = v.x; sy = v.y; sz = v.z; rhw = _rhw;
color = _color; specular = _specular;
tu = _tu; tv = _tv;
tu1 = _tu1; tv1 = _tv1;
}
#endif
#endif
} D3DTLVERTEX2, *LPD3DTLVERTEX2;
#define D3DFVF_TLVERTEX2 ( D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX2 | D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1) )
typedef struct _D3DLVERTEX2 {
union { D3DVALUE x; D3DVALUE dvX; };
union { D3DVALUE y; D3DVALUE dvY; };
union { D3DVALUE z; D3DVALUE dvZ; };
DWORD dwReserved;
union { D3DCOLOR color; D3DCOLOR dcColor; };
union { D3DCOLOR specular; D3DCOLOR dcSpecular; };
union { D3DVALUE tu; D3DVALUE dvTU; };
union { D3DVALUE tv; D3DVALUE dvTV; };
union { D3DVALUE tu1; D3DVALUE dvTU1; };
union { D3DVALUE tv1; D3DVALUE dvTV1; };
#if(DIRECT3D_VERSION >= 0x0500)
#if (defined __cplusplus) && (defined D3D_OVERLOADS)
_D3DLVERTEX2() { }
_D3DLVERTEX2(const D3DVECTOR& v, D3DCOLOR _color, D3DCOLOR _specular, float _tu, float _tv, float _tu1, float _tv1)
{
x = v.x; y = v.y; z = v.z; dwReserved = 0;
color = _color; specular = _specular;
tu = _tu; tv = _tv;
tu1 = _tu1; tv1 = _tv1;
}
#endif
#endif
} D3DLVERTEX2, *LPD3DLVERTEX2;
#define D3DFVF_LVERTEX2 ( D3DFVF_XYZ | D3DFVF_RESERVED1 | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX2 | D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1) )
typedef struct _D3DVERTEX2 {
union { D3DVALUE x; D3DVALUE dvX; };
union { D3DVALUE y; D3DVALUE dvY; };
union { D3DVALUE z; D3DVALUE dvZ; };
union { D3DVALUE nx; D3DVALUE dvNX; };
union { D3DVALUE ny; D3DVALUE dvNY; };
union { D3DVALUE nz; D3DVALUE dvNZ; };
union { D3DVALUE tu; D3DVALUE dvTU; };
union { D3DVALUE tv; D3DVALUE dvTV; };
union { D3DVALUE tu1; D3DVALUE dvTU1; };
union { D3DVALUE tv1; D3DVALUE dvTV1; };
#if(DIRECT3D_VERSION >= 0x0500)
#if (defined __cplusplus) && (defined D3D_OVERLOADS)
_D3DVERTEX2() { }
_D3DVERTEX2(const D3DVECTOR& v, const D3DVECTOR& n, float _tu, float _tv, float _tu1, float _tv1)
{
x = v.x; y = v.y; z = v.z;
nx = n.x; ny = n.y; nz = n.z;
tu = _tu; tv = _tv;
tu1 = _tu1; tv1 = _tv1;
}
#endif
#endif
} D3DVERTEX2, *LPD3DVERTEX2;
#define D3DFVF_VERTEX2 ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX2 | D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1) )
static bool bLostControl;
static bool bUsePageflip; // Use a flipping surface when in fullscreen mode
static bool bUseTriplebuffer;
static bool bMultiTexturing;
static bool bRenderToTexture;
struct EffectPreset {
int nUseFilter; // -1: disable, 1: enable, use specified settings, 0: don't change
int nUseEffects; //
int nUseScanlines; //
int nUseFeedback; //
int nUsePrescale; //
int nIntensity;
int nFloor;
int nSaturation;
int nPatternMode;
TCHAR* pPatternName;
int nAttenuation;
int nBlendMode;
int nScanIntensity;
int nAmount;
int nOverSaturation;
};
static const EffectPreset PresetInfo[] = {
{ 0, -1, -1, 0, -1, 0, 0, 0, 0, NULL, 0, 0, 0, 0, 0 }, // 1x zoom
{ 1, 1, 1, 0, 0, 0x0080, 0x20, 0x00, 0, _T("4x4_rgb_pattern.i.rgb"), 0x00, 0, 0x008F8F8F, 0, 0 }, // 2x zoom
{ 1, 1, 1, 0, 0, 0x00A0, 0x30, 0x00, 2, _T("4x6_rgb_pattern.i.rgb"), 0x00, 0, 0x005F5F5F, 0, 0 }, // 3x zoom
{ 1, 1, 1, 0, 1, 0x0080, 0x20, 0x00, 3, _T("6x8_rgb_pattern.i.rgb"), 0x20, 0, 0x004F4F4F, 0, 0 }, // 4x zoom
{ 1, 1, 1, 0, 1, 0x0060, 0x10, 0x48, 3, _T("9x10_ellipsoid.i.rgb"), 0x20, 0, 0x005F5F5F, 0, 0 }, // 5x zoom
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, 0, 0, 0, 0, 0 }, // 6x zoom
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, 0, 0, 0, 0, 0 }, // 7x zoom
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, 0, 0, 0, 0, 0 }, // 8x zoom
// Selectable presets
{ 0, 1, -1, 0, 0, 0x0100, 0x80, 0x40, 2, _T("4x6_rgb_pattern.i.rgb"), 0, 1, 0, 0, 0 }, // Small Faint RGB mask
{ 0, 1, -1, 0, 0, 0x0100, 0x80, 0x40, 2, _T("10x6_large_dot.i.rgb"), 0, 1, 0, 0, 0 }, // Large RGB Dot mask
{ 0, 1, -1, 0, 0, 0x0100, 0x90, 0x80, 2, _T("12x10_large_ellipsoid.i.rgb"), 0, 1, 0, 0, 0 }, // Large Faint RGB mask
{ 0, 1, -1, 0, 0, 0x0100, 0xC0, 0x00, 0, _T("4x4_mame_rgbtiny.i.rgb"), 0, 1, 0, 0, 0 }, // MAME rgbtiny small
{ 0, 1, -1, 0, 0, 0x0100, 0xD0, 0x00, 0, _T("8x8_mame_rgbtiny.i.rgb"), 0, 1, 0, 0, 0 }, // MAME rgbtiny large
{ 0, 1, 1, 0, 0, 0x0030, 0x00, 0x00, 0, _T("3x1_aperture_grille.i.rgb"), 0x20, 0, 0x00BFBFBF, 0, 0 }, // Aperture Grille
{ 0, 1, -1, 0, 0, 0x0040, 0x00, 0x00, 0, _T("10x6_large_dot.i.rgb"), 0x08, 0, 0, 0, 0 }, // Large Oversaturated RGB Dot
{ 0, 1, -1, 0, 0, 0x00E0, 0x00, 0x40, 3, _T("18x10_large_round.i.rgb"), 0x10, 0, 0, 0, 0 }, // Huge Oversaturated pattern
};
static bool bCreateImage = false;
static bool bUseRGBEffects = false; // Use RGB effects
static int nRGBScanlineIntensity = 0; // Scanline intensity when RGB effects are used
static int nRGBEffectIntensity = 0x0100; // Multiply pattern RGB values with this
static int nRGBEffectFloor = 0x00; // Minimum pattern colour grey-value
static int nRGBEffectSaturation = 0x00; // Minimum saturation of the pattern colours
static int nRGBEffectPatternmode = 0; // How to use the Floor/Saturation values
static int nRGBEffectAttenuation = 0; // How much to attenuate the image before applying RGB effects
static TCHAR* pRGBEffectPatternName = _T("4x6 RGB Pattern.rgb"); // Pattern to use
static int nRGBEffectBlendmode = 1; // Set of D3D blendmodes
static int nRotateGame;
static bool bRotateEffects; // Use rotated scanlines/RGB patterns for rotated games
static bool bUseLighting;
static bool bUse3DProjection;
static float fPreviousScreenAngle;
static float fPreviousScreenCurvature;
static int nPreScale = 0;
static int nPreScaleZoom = 0;
static int nPreScaleEffect = 0;
static IDirectDraw7* pDD = NULL; // DirectDraw interface
static IDirect3D7* pD3D = NULL; // Direct3D interface
static IDirect3DDevice7* pD3DDevice = NULL;
static D3DDEVICEDESC7 d3dDeviceDesc; // holds capabilities of the 3D hardware
static IDirectDrawSurface7* pPrimarySurf = NULL; // Primary surface
static IDirectDrawSurface7* pBackbuffer = NULL; // Back buffer surface
static D3DVIEWPORT7 Viewport = {0, 0, 0, 0, 0.0f, 10.0f};
static RECT Dest = { 0, 0, 0, 0 };
static RECT Render;
static int nZoomFactor;
static int nGameWidth = 0, nGameHeight = 0; // Game screen size
static int nGameImageWidth, nGameImageHeight;
static IDirectDrawSurface7* pEmuImage[4] = {NULL, }; // The emulator screen surfaces/textures
static int nTextureWidth, nTextureHeight;
static int nPreScaleTextureWidth, nPreScaleTextureHeight;
static IDirectDrawSurface7* pScanlineTexture[2] = {NULL, }; // The scanline textures
static IDirectDrawSurface7* pRGBEffectTexture = NULL; // The RGB effects texture
static DWORD nScanlineOp;
static int n3DScreenGridSize;
static D3DTLVERTEX2 vScreen[4]; // Vectors for emulator image, screen coordinates
static D3DVERTEX2* v3DScreen = NULL; // Vectors for emulator image, 3D coordinates
static D3DTLVERTEX vFeedbackImage[4]; // Vectors for feedback
static D3DMATRIX matWorld;
static D3DMATRIX matView;
static D3DMATRIX matProjection;
// static D3DMATRIX matIdentity = D3DMATRIX(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1);
static void SetWorldMatrix()
{
/*
matView._12 = matView._13 = matView._14 = 0.0f;
matView._21 = matView._23 = matView._24 = 0.0f;
matView._31 = matView._32 = matView._34 = 0.0f;
matView._41 = 0.0f;
matView._11 = matView._22 = matView._33 = matView._44 = 1.0f;
matView._42 = -1.0f;
matView._43 = fNearPlane;
pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &matView);
*/
matWorld._12 = matWorld._13 = matWorld._14 = matWorld._21 = matWorld._24 = 0.0f;
matWorld._31 = matWorld._34 = matWorld._41 = matWorld._42 = matWorld._43 = 0.0f;
matWorld._11 = matWorld._44 = 1.0f;
matWorld._22 = (float)cos(fVidScreenAngle);
matWorld._23 = (float)sin(fVidScreenAngle);
matWorld._32 = -(float)sin(fVidScreenAngle);
matWorld._33 = (float)cos(fVidScreenAngle);
pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
// pD3DDevice->MultiplyTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
}
static void SetViewMatrix(float fNearPlane)
{
matView._12 = matView._13 = matView._14 = 0.0f;
matView._21 = matView._23 = matView._24 = 0.0f;
matView._31 = matView._32 = matView._34 = 0.0f;
matView._41 = 0.0f;
matView._11 = matView._22 = matView._33 = matView._44 = 1.0f;
matView._42 = -1.0f;
matView._43 = fNearPlane;
pD3DDevice->SetTransform(D3DTRANSFORMSTATE_VIEW, &matView);
}
static void SetProjectionMatrix(float fNearPlane)
{
// Set projection matrix for perspective
memset(&matProjection, 0, sizeof(D3DMATRIX));
float fFarPlane = 1000.0f;
float Q = fFarPlane / (fFarPlane - fNearPlane);
matProjection._11 = fNearPlane;
matProjection._22 = fNearPlane;
matProjection._33 = Q;
matProjection._34 = 1.0f;
matProjection._43 = -Q * fNearPlane;
pD3DDevice->SetTransform(D3DTRANSFORMSTATE_PROJECTION, &matProjection);
}
static int GetTextureSize(int nSize)
{
int nTextureSize = 128;
while (nTextureSize < nSize) {
nTextureSize <<= 1;
}
return nTextureSize;
}
static int Update3DScreen()
{
if (bUse3DProjection) {
if (v3DScreen == NULL) {
v3DScreen = (D3DVERTEX2*)malloc(6 * n3DScreenGridSize * n3DScreenGridSize * sizeof(D3DVERTEX2));
if (v3DScreen == NULL) {
return 1;
}
}
// Use a 3D world and tilt the image backward
if (fVidScreenCurvature == 0.0f && !bUseLighting) {
v3DScreen[nRotateGame & 2 ? 3 : 0] = D3DVERTEX2(D3DVECTOR(-1.0f, 2.0f, 0.0f), D3DVECTOR(0, 0, 1), vScreen[0].tu, vScreen[0].tv, vScreen[0].tu1, vScreen[0].tv1);
v3DScreen[nRotateGame & 2 ? 2 : 1] = D3DVERTEX2(D3DVECTOR( 1.0f, 2.0f, 0.0f), D3DVECTOR(0, 0, 1), vScreen[1].tu, vScreen[1].tv, vScreen[1].tu1, vScreen[1].tv1);
v3DScreen[nRotateGame & 2 ? 1 : 2] = D3DVERTEX2(D3DVECTOR(-1.0f, 0.0f, 0.0f), D3DVECTOR(0, 0, 1), vScreen[2].tu, vScreen[2].tv, vScreen[2].tu1, vScreen[2].tv1);
v3DScreen[nRotateGame & 2 ? 0 : 3] = D3DVERTEX2(D3DVECTOR( 1.0f, 0.0f, 0.0f), D3DVECTOR(0, 0, 1), vScreen[3].tu, vScreen[3].tv, vScreen[3].tu1, vScreen[3].tv1);
} else {
int nGameAspectX = 4, nGameAspectY = 3;
double dXAngle, dYAngle;
if (bDrvOkay) {
if ((BurnDrvGetFlags() & (BDF_ORIENTATION_VERTICAL | BDF_ORIENTATION_FLIPPED)) && nVidRotationAdjust) {
BurnDrvGetAspect(&nGameAspectY, &nGameAspectX);
} else {
BurnDrvGetAspect(&nGameAspectX, &nGameAspectY);
}
}
if (nGameAspectX > nGameAspectY) {
dXAngle = (double)fVidScreenCurvature;
dYAngle = (double)fVidScreenCurvature * nGameAspectY / nGameAspectX;
} else {
dXAngle = (double)fVidScreenCurvature * nGameAspectX / nGameAspectY;
dYAngle = (double)fVidScreenCurvature;
}
double dXFactor = 1.0 / sin(dXAngle / 2);
double dYFactor = 1.0 / sin(dYAngle / 2);
for (int y = 0; y < n3DScreenGridSize; y++) {
double dTop = dYFactor * sin(-dYAngle / 2 + dYAngle * y / n3DScreenGridSize);
double dBottom = dYFactor * sin(-dYAngle / 2 + dYAngle * (y + 1) / n3DScreenGridSize);
double dTopFactor = cos(-dYAngle / 2 + dYAngle * y / n3DScreenGridSize);
double dBottomFactor = cos(-dYAngle / 2 + dYAngle * (y + 1) / n3DScreenGridSize);
for (int x = 0; x < n3DScreenGridSize; x++) {
double dLeft = dXFactor * sin(-dXAngle / 2 + dXAngle * x / n3DScreenGridSize);
double dRight = dXFactor * sin(-dXAngle / 2 + dXAngle * (x + 1) / n3DScreenGridSize);
double dLeftFactor = cos(-dXAngle / 2 + dXAngle * x / n3DScreenGridSize);
double dRightFactor = cos(-dXAngle / 2 + dXAngle * (x + 1) / n3DScreenGridSize);
if (nRotateGame & 1) {
v3DScreen[6 * y + ((n3DScreenGridSize - 1 - x) * 6 * n3DScreenGridSize) + 0] = D3DVERTEX2(D3DVECTOR((float)(dTopFactor * dLeft), (float)(1.0 - dLeftFactor * dTop), (float)(1.0 - dTopFactor * dLeftFactor) ), D3DVECTOR(0, 0, 0), 0, 0, 0, 0);
v3DScreen[6 * y + ((n3DScreenGridSize - 1 - x) * 6 * n3DScreenGridSize) + 1] = D3DVERTEX2(D3DVECTOR((float)(dTopFactor * dRight), (float)(1.0 - dRightFactor * dTop), (float)(1.0 - dTopFactor * dRightFactor)), D3DVECTOR(0, 0, 0), 0, 0, 0, 0);
v3DScreen[6 * y + ((n3DScreenGridSize - 1 - x) * 6 * n3DScreenGridSize) + 2] = D3DVERTEX2(D3DVECTOR((float)(dBottomFactor * dLeft), (float)(1.0 - dLeftFactor * dBottom), (float)(1.0 - dBottomFactor * dLeftFactor) ), D3DVECTOR(0, 0, 0), 0, 0, 0, 0);
v3DScreen[6 * y + ((n3DScreenGridSize - 1 - x) * 6 * n3DScreenGridSize) + 3] = D3DVERTEX2(D3DVECTOR((float)(dTopFactor * dRight), (float)(1.0 - dRightFactor * dTop), (float)(1.0 - dTopFactor * dRightFactor)), D3DVECTOR(0, 0, 0), 0, 0, 0, 0);
v3DScreen[6 * y + ((n3DScreenGridSize - 1 - x) * 6 * n3DScreenGridSize) + 4] = D3DVERTEX2(D3DVECTOR((float)(dBottomFactor * dLeft), (float)(1.0 - dLeftFactor * dBottom), (float)(1.0 - dBottomFactor * dLeftFactor) ), D3DVECTOR(0, 0, 0), 0, 0, 0, 0);
v3DScreen[6 * y + ((n3DScreenGridSize - 1 - x) * 6 * n3DScreenGridSize) + 5] = D3DVERTEX2(D3DVECTOR((float)(dBottomFactor * dRight), (float)(1.0 - dRightFactor * dBottom), (float)(1.0 - dBottomFactor * dRightFactor)), D3DVECTOR(0, 0, 0), 0, 0, 0, 0);
} else {
v3DScreen[6 * x + (y * 6 * n3DScreenGridSize) + 0] = D3DVERTEX2(D3DVECTOR((float)(dTopFactor * dLeft), (float)(1.0 - dLeftFactor * dTop), (float)(1.0 - dTopFactor * dLeftFactor) ), D3DVECTOR(0, 0, 0), 0, 0, 0, 0);
v3DScreen[6 * x + (y * 6 * n3DScreenGridSize) + 1] = D3DVERTEX2(D3DVECTOR((float)(dTopFactor * dRight), (float)(1.0 - dRightFactor * dTop), (float)(1.0 - dTopFactor * dRightFactor)), D3DVECTOR(0, 0, 0), 0, 0, 0, 0);
v3DScreen[6 * x + (y * 6 * n3DScreenGridSize) + 2] = D3DVERTEX2(D3DVECTOR((float)(dBottomFactor * dLeft), (float)(1.0 - dLeftFactor * dBottom), (float)(1.0 - dBottomFactor * dLeftFactor) ), D3DVECTOR(0, 0, 0), 0, 0, 0, 0);
v3DScreen[6 * x + (y * 6 * n3DScreenGridSize) + 3] = D3DVERTEX2(D3DVECTOR((float)(dTopFactor * dRight), (float)(1.0 - dRightFactor * dTop), (float)(1.0 - dTopFactor * dRightFactor)), D3DVECTOR(0, 0, 0), 0, 0, 0, 0);
v3DScreen[6 * x + (y * 6 * n3DScreenGridSize) + 4] = D3DVERTEX2(D3DVECTOR((float)(dBottomFactor * dLeft), (float)(1.0 - dLeftFactor * dBottom), (float)(1.0 - dBottomFactor * dLeftFactor) ), D3DVECTOR(0, 0, 0), 0, 0, 0, 0);
v3DScreen[6 * x + (y * 6 * n3DScreenGridSize) + 5] = D3DVERTEX2(D3DVECTOR((float)(dBottomFactor * dRight), (float)(1.0 - dRightFactor * dBottom), (float)(1.0 - dBottomFactor * dRightFactor)), D3DVECTOR(0, 0, 0), 0, 0, 0, 0);
}
}
}
// Compute the vertex normals
for (int x = 0; x < n3DScreenGridSize; x++) {
for (int y = 0; y < n3DScreenGridSize; y++) {
int i = 6 * x + (y * 6 * n3DScreenGridSize);
for (int j = 0; j < 6; j++) {
v3DScreen[i + j].nx = v3DScreen[i + j].x; v3DScreen[i + j].ny = v3DScreen[i + j].y - 1.0f; v3DScreen[i + j].nz = (1.0f - v3DScreen[i + j].z) / fVidScreenCurvature;
}
}
}
// Texture mapping coordinates for the emulator image
for (int y = 0; y < n3DScreenGridSize; y++) {
float tuLeft, tuRight;
float tvTop, tvBottom;
int ry = nRotateGame & 2 ? n3DScreenGridSize - 1 - y : y;
tvTop = ((float)nGameImageHeight / nTextureHeight) * y / n3DScreenGridSize;
tvBottom = ((float)nGameImageHeight / nTextureHeight) * (y + 1) / n3DScreenGridSize;
for (int x = 0; x < n3DScreenGridSize; x++) {
int rx = nRotateGame & 2 ? n3DScreenGridSize - 1 - x : x;
tuLeft = (float(nGameImageWidth) / nTextureWidth) * x / n3DScreenGridSize;
tuRight = (float(nGameImageWidth) / nTextureWidth) * (x + 1) / n3DScreenGridSize;
if (nRotateGame & 1) {
int i = 6 * (n3DScreenGridSize - 1 - rx) + ((n3DScreenGridSize - 1 - ry) * 6 * n3DScreenGridSize);
v3DScreen[i + (nRotateGame & 2 ? 5 : 0)].tu = tuRight; v3DScreen[i + (nRotateGame & 2 ? 5 : 0)].tv = tvTop;
v3DScreen[i + (nRotateGame & 2 ? 4 : 1)].tu = tuRight; v3DScreen[i + (nRotateGame & 2 ? 4 : 1)].tv = tvBottom;
v3DScreen[i + (nRotateGame & 2 ? 3 : 2)].tu = tuLeft; v3DScreen[i + (nRotateGame & 2 ? 3 : 2)].tv = tvTop;
v3DScreen[i + (nRotateGame & 2 ? 2 : 3)].tu = tuRight; v3DScreen[i + (nRotateGame & 2 ? 2 : 3)].tv = tvBottom;
v3DScreen[i + (nRotateGame & 2 ? 1 : 4)].tu = tuLeft; v3DScreen[i + (nRotateGame & 2 ? 1 : 4)].tv = tvTop;
v3DScreen[i + (nRotateGame & 2 ? 0 : 5)].tu = tuLeft; v3DScreen[i + (nRotateGame & 2 ? 0 : 5)].tv = tvBottom;
} else {
int i = 6 * rx + (ry * 6 * n3DScreenGridSize);
v3DScreen[i + (nRotateGame & 2 ? 5 : 0)].tu = tuLeft; v3DScreen[i + (nRotateGame & 2 ? 5 : 0)].tv = tvTop;
v3DScreen[i + (nRotateGame & 2 ? 4 : 1)].tu = tuRight; v3DScreen[i + (nRotateGame & 2 ? 4 : 1)].tv = tvTop;
v3DScreen[i + (nRotateGame & 2 ? 3 : 2)].tu = tuLeft; v3DScreen[i + (nRotateGame & 2 ? 3 : 2)].tv = tvBottom;
v3DScreen[i + (nRotateGame & 2 ? 2 : 3)].tu = tuRight; v3DScreen[i + (nRotateGame & 2 ? 2 : 3)].tv = tvTop;
v3DScreen[i + (nRotateGame & 2 ? 1 : 4)].tu = tuLeft; v3DScreen[i + (nRotateGame & 2 ? 1 : 4)].tv = tvBottom;
v3DScreen[i + (nRotateGame & 2 ? 0 : 5)].tu = tuRight; v3DScreen[i + (nRotateGame & 2 ? 0 : 5)].tv = tvBottom;
}
}
}
// Texture mapping coordinates for the scanlines
for (int y = 0; y < n3DScreenGridSize; y++) {
float tuLeft, tuRight;
float tvTop, tvBottom;
int ry = nRotateGame & 2 ? n3DScreenGridSize - 1 - y : y;
tvTop = float(nGameImageHeight) * y / n3DScreenGridSize;
tvBottom = float(nGameImageHeight) * (y + 1) / n3DScreenGridSize;
for (int x = 0; x < n3DScreenGridSize; x++) {
int rx = nRotateGame & 2 ? n3DScreenGridSize - 1 - x : x;
tuLeft = float(nGameImageWidth) * x / n3DScreenGridSize;
tuRight = float(nGameImageWidth) * (x + 1) / n3DScreenGridSize;
if ((!(nRotateGame & 1) && bRotateEffects) || ((nRotateGame & 1) && !bRotateEffects)) {
int i = 6 * (n3DScreenGridSize - 1 - rx) + ((n3DScreenGridSize - 1 - ry) * 6 * n3DScreenGridSize);
v3DScreen[i + (nRotateGame & 2 ? 5 : 0)].tu1 = tuRight; v3DScreen[i + (nRotateGame & 2 ? 5 : 0)].tv1 = tvTop;
v3DScreen[i + (nRotateGame & 2 ? 4 : 1)].tu1 = tuRight; v3DScreen[i + (nRotateGame & 2 ? 4 : 1)].tv1 = tvBottom;
v3DScreen[i + (nRotateGame & 2 ? 3 : 2)].tu1 = tuLeft; v3DScreen[i + (nRotateGame & 2 ? 3 : 2)].tv1 = tvTop;
v3DScreen[i + (nRotateGame & 2 ? 2 : 3)].tu1 = tuRight; v3DScreen[i + (nRotateGame & 2 ? 2 : 3)].tv1 = tvBottom;
v3DScreen[i + (nRotateGame & 2 ? 1 : 4)].tu1 = tuLeft; v3DScreen[i + (nRotateGame & 2 ? 1 : 4)].tv1 = tvTop;
v3DScreen[i + (nRotateGame & 2 ? 0 : 5)].tu1 = tuLeft; v3DScreen[i + (nRotateGame & 2 ? 0 : 5)].tv1 = tvBottom;
} else {
int i = 6 * rx + (ry * 6 * n3DScreenGridSize);
v3DScreen[i + (nRotateGame & 2 ? 5 : 0)].tu1 = tuLeft; v3DScreen[i + (nRotateGame & 2 ? 5 : 0)].tv1 = tvTop;
v3DScreen[i + (nRotateGame & 2 ? 4 : 1)].tu1 = tuRight; v3DScreen[i + (nRotateGame & 2 ? 4 : 1)].tv1 = tvTop;
v3DScreen[i + (nRotateGame & 2 ? 3 : 2)].tu1 = tuLeft; v3DScreen[i + (nRotateGame & 2 ? 3 : 2)].tv1 = tvBottom;
v3DScreen[i + (nRotateGame & 2 ? 2 : 3)].tu1 = tuRight; v3DScreen[i + (nRotateGame & 2 ? 2 : 3)].tv1 = tvTop;
v3DScreen[i + (nRotateGame & 2 ? 1 : 4)].tu1 = tuLeft; v3DScreen[i + (nRotateGame & 2 ? 1 : 4)].tv1 = tvBottom;
v3DScreen[i + (nRotateGame & 2 ? 0 : 5)].tu1 = tuRight; v3DScreen[i + (nRotateGame & 2 ? 0 : 5)].tv1 = tvBottom;
}
}
}
}
}
return 0;
}
static void PutPixel(unsigned char** ppSurface, unsigned int nColour)
{
switch (nVidScrnDepth) {
case 15:
*((unsigned short*)(*ppSurface)) = ((nColour >> 9) & 0x7C00) | ((nColour >> 6) & 0x03E0) | ((nColour >> 3) & 0x001F);
*ppSurface += 2;
break;
case 16:
*((unsigned short*)(*ppSurface)) = ((nColour >> 8) & 0xF800) | ((nColour >> 5) & 0x07E0) | ((nColour >> 3) & 0x001F);
*ppSurface += 2;
break;
case 24:
(*ppSurface)[0] = (nColour >> 16) & 0xFF;
(*ppSurface)[1] = (nColour >> 8) & 0xFF;
(*ppSurface)[2] = (nColour >> 0) & 0xFF;
*ppSurface += 3;
break;
case 32:
*((unsigned int*)(*ppSurface)) = nColour;
*ppSurface += 4;
break;
}
}
static int ReleaseSurfaces()
{
// Release effects surfaces
RELEASE(pRGBEffectTexture);
RELEASE(pScanlineTexture[0]);
RELEASE(pScanlineTexture[1]);
// Release game-image surfaces
RELEASE(pEmuImage[0]);
RELEASE(pEmuImage[1]);
RELEASE(pEmuImage[2]);
RELEASE(pEmuImage[3]);
VidSFreeVidImage();
return 0;
}
static int vidExit()
{
VidSExitOSD();
if (v3DScreen) {
free(v3DScreen);
v3DScreen = NULL;
}
VidSoftFXExit();
ReleaseSurfaces();
VidSRestoreGamma();
RELEASE(pD3DDevice);
RELEASE(pD3D);
VidSRestoreScreenMode();
if (bUsePageflip == false) {
RELEASE(pBackbuffer); // explicitly free the off-screen surface when not usign pageflipping
}
RELEASE(pPrimarySurf);
pBackbuffer = NULL;
VidSExit();
RELEASE(pDD);
return 0;
}
static HRESULT CALLBACK myEnumTexturesCallback(DDPIXELFORMAT* pddpf, void* pPreferredddpf)
{
if (pddpf->dwFlags == DDPF_RGB && pddpf->dwRGBBitCount == 16) {
if (VidSoftFXCheckDepth(nPreScaleEffect, 15) == 15) {
// Use 15-bit format if supported by the 3D hardware and the effects blitter
if(pddpf->dwRBitMask == 0x7C00 && pddpf->dwGBitMask == 0x03E0 && pddpf->dwBBitMask == 0x001F) {
memcpy(pPreferredddpf, pddpf, sizeof(DDPIXELFORMAT) );
return D3DENUMRET_CANCEL;
}
}
// Use 16-bit format otherwise
if(pddpf->dwRBitMask == 0xF800 && pddpf->dwGBitMask == 0x07E0 && pddpf->dwBBitMask == 0x001F) {
memcpy(pPreferredddpf, pddpf, sizeof(DDPIXELFORMAT) );
}
}
return D3DENUMRET_OK;
}
static int vidCreateGameSurfaces()
{
bool bForceTextureFormat;
DDSURFACEDESC2 ddsd;
DDPIXELFORMAT ddpf;
// Determine if we should use a texture format different from the screen format
bForceTextureFormat = false;
if ((VidSoftFXCheckDepth(nPreScaleEffect, 16) != 32 && nVidScrnDepth > 16 &&
(/*bDrvOkay &&*/ VidSoftFXCheckDepth(nPreScaleEffect, 32) != 32)) ||
(/*bDrvOkay &&*/ (bVidForce16bit ||
(bDoGamma && nVidFullscreen && bVidUseHardwareGamma) ||
(bDoGamma && !nVidFullscreen && bHardwareGammaOnly))))
{
memset(&ddpf, 0, sizeof(DDPIXELFORMAT));
pD3DDevice->EnumTextureFormats(&myEnumTexturesCallback, (void*)&ddpf);
if (ddpf.dwSize) {
bForceTextureFormat = true; // NOTE: this makes emulation=16bpp
}
}
/*
{ // debug code for the above if-statement *keep!* :)
int ii;
ii = VidSoftFXCheckDepth(nPreScaleEffect, 16) != 32 && nVidScrnDepth > 16;
bprintf(0, _T("Part1 %d "), ii);
ii = (bDrvOkay || VidSoftFXCheckDepth(nPreScaleEffect, 32) != 32);
bprintf(0, _T("Part2 %d \n"), ii);
ii = (bDoGamma && nVidFullscreen && bVidUseHardwareGamma);
bprintf(0, _T("(bDoGamma && nVidFullscreen && bVidUseHardwareGamma) == [%d]\n"), ii);
ii = (bDoGamma && !nVidFullscreen && bHardwareGammaOnly);
bprintf(0, _T("(bDoGamma && !nVidFullscreen && bHardwareGammaOnly) == [%d]\n"), ii);
bprintf(0, _T("nPreScaleEffect [%X]\n"), nPreScaleEffect);
bprintf(0, _T("VidSoftFXCheckDepth(nPreScaleEffect, 16) [%d]\n"), VidSoftFXCheckDepth(nPreScaleEffect, 16));
bprintf(0, _T("VidSoftFXCheckDepth(nPreScaleEffect, 32) [%d]\n"), VidSoftFXCheckDepth(nPreScaleEffect, 32));
bprintf(0, _T("nVidScrnDepth [%X] bDrvOkay [%X]\n"), nVidScrnDepth, bDrvOkay);
bprintf(0, _T("bVidForce16bit[%X] forcetexture[%X]\n"), bVidForce16bit, bForceTextureFormat);
bprintf(0, _T("bDoGamma [%X] nVidFullscreen [%X] bVidUseHardwareGamma [%X] bHardwareGammaOnly [%X]\n"), bDoGamma, nVidFullscreen, bVidUseHardwareGamma, bHardwareGammaOnly);
}
*/
// Create a secondary surface to render the game image onto for the feedback effect
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE;
ddsd.dwWidth = (nPreScale & 1) ? nPreScaleTextureWidth : nTextureWidth;
ddsd.dwHeight = (nPreScale & 2) ? nPreScaleTextureHeight : nTextureHeight;
memset(&ddsd.ddpfPixelFormat, 0, sizeof(DDPIXELFORMAT));
if (FAILED(pDD->CreateSurface(&ddsd, &pEmuImage[1], NULL))) {
bRenderToTexture = false;
} else {
IDirectDrawSurface7* pPreviousTarget;
if (FAILED(pD3DDevice->GetRenderTarget(&pPreviousTarget))) {
bRenderToTexture = false;
} else {
if (FAILED(pD3DDevice->SetRenderTarget(pEmuImage[1], 0))) {
bRenderToTexture = false;
} else {
bRenderToTexture = true;
VidSClearSurface(pEmuImage[1], 0, NULL);
}
pD3DDevice->SetRenderTarget(pPreviousTarget, 0);
pPreviousTarget->Release();
}
}
if (!bRenderToTexture) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Warning: Device can't render to textures. Using fall-back method for pre-scale/feedback.\n"));
#endif
RELEASE(pEmuImage[1]);
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
ddsd.dwWidth = (nPreScale & 1) ? nPreScaleTextureWidth : nTextureWidth;
ddsd.dwHeight = (nPreScale & 2) ? nPreScaleTextureHeight : nTextureHeight;
memset(&ddsd.ddpfPixelFormat, 0, sizeof(DDPIXELFORMAT));
if (FAILED(pDD->CreateSurface(&ddsd, &pEmuImage[1], NULL))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create secondary game surface(s) for pre-scale/feedback.\n"));
#endif
return 1;
}
VidSClearSurface(pEmuImage[1], 0, NULL);
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY;
ddsd.dwWidth = (nPreScale & 1) ? nPreScaleTextureWidth : nTextureWidth;
ddsd.dwHeight = (nPreScale & 2) ? nPreScaleTextureHeight : nTextureHeight;
if (FAILED(pDD->CreateSurface(&ddsd, &pEmuImage[3], NULL))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create secondary game surface(s) for pre-scale/feedback.\n"));
#endif
return 1;
}
VidSClearSurface(pEmuImage[3], 0, NULL);
}
if (nVidTransferMethod <= 0) {
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY;
ddsd.dwWidth = nTextureWidth;
ddsd.dwHeight = nTextureHeight;
if (nPreScaleEffect) {
ddsd.dwWidth = nPreScaleTextureWidth;
ddsd.dwHeight = nPreScaleTextureHeight;
}
if (bForceTextureFormat) {
ddsd.dwFlags |= DDSD_PIXELFORMAT;
memcpy(&ddsd.ddpfPixelFormat, &ddpf, sizeof(DDPIXELFORMAT));
}
if (FAILED(pDD->CreateSurface(&ddsd, &pEmuImage[2], NULL))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create secondary game surface for transfer.\n"));
#endif
return 1;
}
VidSClearSurface(pEmuImage[2], 0, NULL);
}
// Create a surface for the texture that will contain the game image
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
if (nVidTransferMethod > 0) {
ddsd.ddsCaps.dwCaps2 = DDSCAPS2_HINTDYNAMIC | DDSCAPS2_TEXTUREMANAGE;
}
ddsd.dwWidth = nTextureWidth;
ddsd.dwHeight = nTextureHeight;
if (nPreScaleEffect) {
ddsd.dwWidth = nPreScaleTextureWidth;
ddsd.dwHeight = nPreScaleTextureHeight;
}
if (bForceTextureFormat) {
ddsd.dwFlags |= DDSD_PIXELFORMAT;
memcpy(&ddsd.ddpfPixelFormat, &ddpf, sizeof(DDPIXELFORMAT));
}
if (FAILED(pDD->CreateSurface(&ddsd, &pEmuImage[0], NULL))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create game texture surface.\n"));
#endif
return 1;
}
VidSClearSurface(pEmuImage[0], 0, NULL);
return 0;
}
static int InitEffectsSurfaces()
{
DDSURFACEDESC2 ddsd;
for (int i = 0, nSize = 2; i < 2; i++, nSize <<= 1) {
int Scanlines[][8] = { { 0xFFFFFF, 0x000000 }, { 0x9F9F9F, 0xFFFFFF, 0x9F9F9F, 0x000000 } };
if (pScanlineTexture[i]) {
if (FAILED(pScanlineTexture[i]->IsLost())) {
if (FAILED(pScanlineTexture[i]->Restore())) {
return 1;
}
}
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
if (FAILED(pScanlineTexture[i]->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WRITEONLY | DDLOCK_DISCARDCONTENTS | DDLOCK_WAIT, NULL))) {
return 1;
}
for (int y = 0; y < nSize; y++) {
unsigned char* pSurface = (unsigned char*)ddsd.lpSurface + y * ddsd.lPitch;
int nColour = Scanlines[i][y];
for (int x = 0; x < nSize; x++) {
PutPixel(&pSurface, nColour);
}
}
pScanlineTexture[i]->Unlock(NULL);
}
}
if (pRGBEffectTexture) {
TCHAR pFilename[MAX_PATH] = _T("");
int nPatternSize, nPatternXSize, nPatternYSize;
unsigned char* RGBPattern = NULL;
if (FAILED(pRGBEffectTexture->IsLost())) {
if (FAILED(pRGBEffectTexture->Restore())) {
return 1;
}
}
// Get the filename and size of a pattern
_tcscpy(pFilename, _T("pattern/"));
_tcscat(pFilename, pRGBEffectPatternName);
_stscanf(pRGBEffectPatternName, _T("%i x %i"), &nPatternXSize, &nPatternYSize);
nPatternSize = nPatternXSize * nPatternYSize * 4;
for (int i = 0; BuiltinPatternInfo[i].szName; i++) {
if (!_tcscmp(BuiltinPatternInfo[i].szName, pRGBEffectPatternName)) {
RGBPattern = (unsigned char*)malloc(nPatternSize);
memcpy(RGBPattern, BuiltinPatternInfo[i].pData, nPatternSize);
break;
}
}
if (RGBPattern == NULL) {
FILE* fp = _tfopen(pFilename, _T("rb"));
if (fp) {
// Read the file containing the pattern data
fseek(fp, 0, SEEK_END);
// Check if the file is the correct size.
if (ftell(fp) != nPatternSize) {
fclose(fp);
FBAPopupAddText(PUF_TEXT_NO_TRANSLATE, _T("RGB pattern \'%s\' has an incorrect size.\nIt should be %i bytes long."), pRGBEffectPatternName, nPatternSize);
FBAPopupDisplay(PUF_TYPE_WARNING);
return 1;
}
RGBPattern = (unsigned char*)malloc(nPatternSize);
fseek(fp, 0, SEEK_SET);
fread(RGBPattern, 1, nPatternSize, fp);
fclose(fp);
}
}
if (RGBPattern) {
// Pre-process the pattern
for (int y = 0; y < nPatternYSize; y++) {
unsigned char* pPattern = &RGBPattern[y * (nPatternXSize << 2)];
for (int x = 0; x < nPatternXSize; x++) {
switch (nRGBEffectPatternmode) {
case 0:
case 1: {
if (nRGBEffectPatternmode == 0 || pPattern[(x << 2) + 3]) {
int n;
for (int z = 0; z < 3; z++) {
n = nRGBEffectIntensity * pPattern[(x << 2) + z] / 256;
pPattern[(x << 2) + z] = (n > 255) ? 255 : nRGBEffectFloor + (n * (256 - nRGBEffectFloor) / 256);
}
}
break;
}
case 2:
case 3: {
int nSaturate = 0;
int nMax = 0;
int n;
if (pPattern[(x << 2) + 0] != pPattern[(x << 2) + 1] || pPattern[(x << 2) + 0] != pPattern[(x << 2) + 2] || pPattern[(x << 2) + 1] != pPattern[(x << 2) + 2]) {
if (pPattern[(x << 2) + 0] >= pPattern[(x << 2) + 1] && pPattern[(x << 2) + 0] >= pPattern[(x << 2) + 2]) {
nMax = pPattern[(x << 2) + 0];
nSaturate |= 1;
}
if (pPattern[(x << 2) + 1] >= pPattern[(x << 2) + 0] && pPattern[(x << 2) + 1] >= pPattern[(x << 2) + 2]) {
nMax = pPattern[(x << 2) + 1];
nSaturate |= 2;
}
if (pPattern[(x << 2) + 2] >= pPattern[(x << 2) + 0] && pPattern[(x << 2) + 2] >= pPattern[(x << 2) + 1]) {
nMax = pPattern[(x << 2) + 2];
nSaturate |= 4;
}
nSaturate ^= 7;
}
for (int z = 0; z < 3; z++) {
if (nRGBEffectPatternmode == 2 || pPattern[(x << 2) + 3]) {
n = pPattern[(x << 2) + z];
if (nSaturate & (1 << z)) {
n += (nMax - n) * nRGBEffectSaturation / 256;
}
n = n * nRGBEffectIntensity / 256;
pPattern[(x << 2) + z] = (n > 255) ? 255 : nRGBEffectFloor + (n * (256 - nRGBEffectFloor) / 256);
}
}
break;
}
}
}
}
// Replicate the pattern over the needed size
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
if (FAILED(pRGBEffectTexture->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WRITEONLY | DDLOCK_DISCARDCONTENTS | DDLOCK_WAIT, NULL))) {
return 1;
} else {
if ((!(nRotateGame & 1) && bRotateEffects) || ((nRotateGame & 1) && !bRotateEffects)) {
for (unsigned int y = 0; y < ddsd.dwHeight; y++) {
int ry = nRotateGame & 2 ? ddsd.dwHeight - 1 - y : y;
unsigned char* pSurface = (unsigned char*)ddsd.lpSurface + ddsd.lPitch * y;
unsigned char* pPattern = &RGBPattern[(ry % nPatternXSize) << 2];
for (unsigned int x = 0; x < ddsd.dwWidth; x++) {
int rx = nRotateGame & 2 ? ddsd.dwWidth - 1 - x : x;
int nColour = pPattern[((rx % nPatternYSize) * (nPatternXSize << 2)) + 0] << 16;
nColour |= pPattern[((rx % nPatternYSize) * (nPatternXSize << 2)) + 1] << 8;
nColour |= pPattern[((rx % nPatternYSize) * (nPatternXSize << 2)) + 2];
PutPixel(&pSurface, nColour);
}
}
} else {
for (unsigned int y = 0; y < ddsd.dwHeight; y++) {
int ry = nRotateGame & 2 ? ddsd.dwHeight - 1 - y : y;
unsigned char* pSurface = (unsigned char*)ddsd.lpSurface + ddsd.lPitch * y;
unsigned char* pPattern = &RGBPattern[(ry % nPatternYSize) * nPatternXSize << 2];
for (unsigned int x = 0; x < ddsd.dwWidth; x++) {
int rx = nRotateGame & 2 ? ddsd.dwWidth - 1 - x : x;
int nColour = pPattern[((rx % nPatternXSize) << 2) + 0] << 16;
nColour |= pPattern[((rx % nPatternXSize) << 2) + 1] << 8;
nColour |= pPattern[((rx % nPatternXSize) << 2) + 2];
PutPixel(&pSurface, nColour);
}
}
}
pRGBEffectTexture->Unlock(NULL);
}
if (RGBPattern) {
free(RGBPattern);
RGBPattern = NULL;
}
} else {
FBAPopupAddText(PUF_TEXT_NO_TRANSLATE, _T("Couldn't find RGB pattern."));
FBAPopupDisplay(PUF_TYPE_WARNING);
return 1;
}
}
return 0;
}
// Create extra surfaces for textures and effects
static int vidCreateEffectsSurfaces()
{
DDSURFACEDESC2 ddsd;
// Create the scanline textures
for (int i = 0, nSize = 2; i < 2; i++, nSize <<= 1) {
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
ddsd.dwWidth = nSize;
ddsd.dwHeight = nSize;
if (FAILED(pDD->CreateSurface(&ddsd, &pScanlineTexture[i], NULL))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create scanline texture.\n"));
#endif
return 1;
}
}
if (bUseRGBEffects) {
// Create the RGB effect texture
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
// ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
if (!nVidFullscreen || (nVidFullscreen && bVidFullStretch)) {
ddsd.dwWidth = nVidScrnWidth;
ddsd.dwHeight = nVidScrnHeight;
} else {
if (bVidArcaderes) {
ddsd.dwWidth = nGameWidth;
ddsd.dwHeight = nGameHeight;
} else {
RECT rect = { 0, 0, nVidScrnWidth, nVidScrnHeight };
VidImageSize(&rect, nGameWidth, nGameHeight);
ddsd.dwWidth = rect.right - rect.left;
ddsd.dwHeight = rect.bottom - rect.top;
}
}
if (FAILED(pDD->CreateSurface(&ddsd, &pRGBEffectTexture, NULL))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create RGB effects surface.\n"));
#endif
return 1;
}
}
InitEffectsSurfaces();
return 0;