-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDXC_ddraw.cpp
More file actions
838 lines (701 loc) · 23.8 KB
/
DXC_ddraw.cpp
File metadata and controls
838 lines (701 loc) · 23.8 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
// DXC_ddraw.cpp: implementation of the DXC_ddraw class.
//
//////////////////////////////////////////////////////////////////////
#include <string.h>
#include <objbase.h>
#include "DXC_ddraw.h"
extern HWND G_hEditWnd;
extern HWND G_hWnd;
#ifdef DEF_HTMLCOMMOM // Html 다이얼로그 부분..
extern HWND G_hInternetWnd;
#endif
extern long G_lTransG100[64][64], G_lTransRB100[64][64];
extern long G_lTransG70[64][64], G_lTransRB70[64][64];
extern long G_lTransG50[64][64], G_lTransRB50[64][64];
extern long G_lTransG25[64][64], G_lTransRB25[64][64];
extern long G_lTransG2[64][64], G_lTransRB2[64][64];
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
DXC_ddraw::DXC_ddraw()
{
m_lpFrontB4 = NULL;
m_lpDD4 = NULL;
m_lpPDBGS = NULL;
m_lpBackB4flip = NULL;
m_cPixelFormat = 0;
#ifdef _DEBUG
m_bFullMode = FALSE;
#else
m_bFullMode = TRUE;
#endif
}
DXC_ddraw::~DXC_ddraw()
{
if (m_hFontInUse != NULL) DeleteObject(m_hFontInUse);
if (m_lpBackB4flip != NULL) m_lpBackB4flip->Release();
if (m_lpBackB4 != NULL) m_lpBackB4->Release();
if (m_lpFrontB4 != NULL) m_lpFrontB4->Release();
if (m_bFullMode == TRUE)
{
if (m_lpDD4 != NULL) m_lpDD4->RestoreDisplayMode();
}
if (m_lpDD4 != NULL) m_lpDD4->Release();
}
BOOL DXC_ddraw::bInit(HWND hWnd)
{
HRESULT ddVal;
DDSURFACEDESC2 ddsd;
int iS, iD;
SetRect(&m_rcClipArea, 0,0, 640, 480);
ddVal = DirectDrawCreateEx(NULL, (VOID**)&m_lpDD4, IID_IDirectDraw7, NULL);
if (ddVal != DD_OK) return FALSE;
if( m_bFullMode == TRUE )
{
DDSCAPS2 ddscaps;
// 전체화면 모드이다.
ddVal = m_lpDD4->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
if (ddVal != DD_OK) return FALSE;
ddVal = m_lpDD4->SetDisplayMode(640, 480, 16,0,0);
if (ddVal != DD_OK) return FALSE;
// 프론트 버퍼 생성
memset( (VOID *)&ddsd, 0, sizeof(ddsd) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.dwBackBufferCount = 1;//2; //v1.3
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddVal = m_lpDD4->CreateSurface(&ddsd, &m_lpFrontB4, NULL);
if (ddVal != DD_OK) return FALSE;
ZeroMemory(&ddscaps, sizeof(ddscaps));
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
ddVal = m_lpFrontB4->GetAttachedSurface(&ddscaps, &m_lpBackB4flip);
if (ddVal != DD_OK) return FALSE;
SetRect(&m_rcFlipping, 0, 0, 640, 480); // our fictitious sprite bitmap is
}
else
{
// 윈도우 모드이다.
int cx = GetSystemMetrics(SM_CXFULLSCREEN);
int cy = GetSystemMetrics(SM_CYFULLSCREEN);
ddVal = m_lpDD4->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
if (ddVal != DD_OK) return FALSE;
cx = cx/2;
cy = cy/2;
if(cy>280) cy -= 40;
SetWindowPos( hWnd, HWND_TOP, cx-320, cy-240, 640, 480, SWP_SHOWWINDOW);
// 프론트 버퍼 생성
memset( (VOID *)&ddsd, 0, sizeof(ddsd) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS;
//ddsd.dwBackBufferCount = 0;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
ddVal = m_lpDD4->CreateSurface(&ddsd, &m_lpFrontB4, NULL);
if (ddVal != DD_OK) return FALSE;
SetRect(&m_rcFlipping, cx-320, cy-240, cx+320, cy+240); // our fictitious sprite bitmap is
}
InitFlipToGDI(hWnd);
// 백버퍼 생성
m_lpBackB4 = pCreateOffScreenSurface(640, 480);
if (m_lpBackB4 == NULL) return FALSE;
// Pre-draw background surface 버퍼 생성
m_lpPDBGS = pCreateOffScreenSurface(640+32, 480+32);
if (m_lpPDBGS == NULL) return FALSE;
// 백버퍼의 주소와 피치를 얻어온다.
ddsd.dwSize = sizeof(ddsd);
if (m_lpBackB4->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL) != DD_OK) return FALSE;
m_pBackB4Addr = (WORD *)ddsd.lpSurface;
m_sBackB4Pitch = (short)ddsd.lPitch >> 1;
m_lpBackB4->Unlock(NULL);
// 픽셀구조를 알아내 컬러 테이블을 생성한다.
_TestPixelFormat();
for (iS = 0; iS < 64; iS++)
for (iD = 0; iD < 64; iD++) {
m_lTransRB100[iD][iS] = _CalcMaxValue(iS, iD, 'R', 1, 1.0f);
m_lTransG100[iD][iS] = _CalcMaxValue(iS, iD, 'G', 1, 1.0f);
m_lTransRB70[iD][iS] = _CalcMaxValue(iS, iD, 'R', 1, 0.7f);
m_lTransG70[iD][iS] = _CalcMaxValue(iS, iD, 'G', 1, 0.7f);
m_lTransRB50[iD][iS] = _CalcMaxValue(iS, iD, 'R', 1, 0.5f);
m_lTransG50[iD][iS] = _CalcMaxValue(iS, iD, 'G', 1, 0.5f);
m_lTransRB25[iD][iS] = _CalcMaxValue(iS, iD, 'R', 1, 0.25f);
m_lTransG25[iD][iS] = _CalcMaxValue(iS, iD, 'G', 1, 0.25f);
m_lTransRB2[iD][iS] = _CalcMaxValue(iS, iD, 'R', 2, 1.0f);
m_lTransG2[iD][iS] = _CalcMaxValue(iS, iD, 'G', 2, 1.0f);
m_lFadeRB[iD][iS] = _CalcMinValue(iS, iD, 'R');
m_lFadeG[iD][iS] = _CalcMinValue(iS, iD, 'G');
G_lTransRB100[iD][iS] = _CalcMaxValue(iS, iD, 'R', 1, 1.0f);
G_lTransG100[iD][iS] = _CalcMaxValue(iS, iD, 'G', 1, 1.0f);
G_lTransRB70[iD][iS] = _CalcMaxValue(iS, iD, 'R', 1, 0.7f);
G_lTransG70[iD][iS] = _CalcMaxValue(iS, iD, 'G', 1, 0.7f);
G_lTransRB50[iD][iS] = _CalcMaxValue(iS, iD, 'R', 1, 0.5f);
G_lTransG50[iD][iS] = _CalcMaxValue(iS, iD, 'G', 1, 0.5f);
G_lTransRB25[iD][iS] = _CalcMaxValue(iS, iD, 'R', 1, 0.25f);
G_lTransG25[iD][iS] = _CalcMaxValue(iS, iD, 'G', 1, 0.25f);
G_lTransRB2[iD][iS] = _CalcMaxValue(iS, iD, 'R', 2, 1.0f);
G_lTransG2[iD][iS] = _CalcMaxValue(iS, iD, 'G', 2, 1.0f);
}
m_hFontInUse = NULL;
#if DEF_LANGUAGE == 1 // 언어:Taiwan
m_hFontInUse = CreateFont(12,0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, NONANTIALIASED_QUALITY, VARIABLE_PITCH, "MingLiu") ;
#endif
#if DEF_LANGUAGE == 2 // 언어:Chinese
m_hFontInUse = CreateFont(12,0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, NONANTIALIASED_QUALITY, VARIABLE_PITCH, "MingLiu") ;
#endif
#if DEF_LANGUAGE == 3 // 언어:Korean
m_hFontInUse = CreateFont(12,0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGUL_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, NONANTIALIASED_QUALITY, VARIABLE_PITCH, "바탕") ;
#endif
#if DEF_LANGUAGE == 4 // 언어:English
m_hFontInUse = CreateFont(16,0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, NONANTIALIASED_QUALITY, VARIABLE_PITCH, "Comic Sans MS") ;
#endif
#if DEF_LANGUAGE == 5 // 언어:Japanese
m_hFontInUse = CreateFont(12,0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, NONANTIALIASED_QUALITY, VARIABLE_PITCH, "MS PGothic") ;
#endif
m_hDC = NULL;
return TRUE;
}
HRESULT DXC_ddraw::iFlip()
{
HRESULT ddVal;
if( m_bFullMode )
{
#ifdef DEF_USING_WIN_IME
ddVal = m_lpFrontB4->Blt( NULL, m_lpBackB4, NULL, DDBLT_WAIT, NULL );
if (G_hEditWnd != NULL) {
if (ddVal != DDERR_SURFACELOST) m_lpDD4->FlipToGDISurface();
}
#else
#ifdef DEF_HTMLCOMMOM
if( G_hInternetWnd != NULL ) {
ddVal = m_lpFrontB4->Blt( NULL, m_lpBackB4, NULL, DDBLT_WAIT, NULL );
m_lpDD4->FlipToGDISurface();
}
else {
ddVal = m_lpBackB4flip->BltFast( 0, 0, m_lpBackB4, &m_rcFlipping, DDBLTFAST_NOCOLORKEY); // | DDBLTFAST_WAIT);
ddVal = m_lpFrontB4->Flip(m_lpBackB4flip, DDFLIP_WAIT);
}
#else
ddVal = m_lpBackB4flip->BltFast( 0, 0, m_lpBackB4, &m_rcFlipping, DDBLTFAST_NOCOLORKEY); // | DDBLTFAST_WAIT);
ddVal = m_lpFrontB4->Flip(m_lpBackB4flip, DDFLIP_WAIT);
#endif
#endif
}
else
{
//SetRect( &m_rcFlipping, 0, 0, 1152, 864 );
ddVal = m_lpFrontB4->Blt(&m_rcFlipping, m_lpBackB4, NULL, DDBLT_WAIT, NULL);
}
if (ddVal == DDERR_SURFACELOST) {
DDSURFACEDESC2 ddsd2;
// 서페이스가 손실되었다. 복구한다.
m_lpFrontB4->Restore();
m_lpBackB4->Restore();
// 백버퍼의 주소를 얻어온다.
ddsd2.dwSize = sizeof(ddsd2);
if (m_lpBackB4->Lock(NULL, &ddsd2, DDLOCK_WAIT, NULL) != DD_OK) return FALSE;
m_pBackB4Addr = (WORD *)ddsd2.lpSurface;
m_lpBackB4->Unlock(NULL);
return DDERR_SURFACELOST;
}
return DD_OK;
}
void DXC_ddraw::ChangeDisplayMode(HWND hWnd)
{
HRESULT ddVal;
DDSURFACEDESC2 ddsd;
if (m_lpBackB4flip != NULL)
{
m_lpBackB4flip->Release();
m_lpBackB4flip = NULL;
}
if (m_lpBackB4 != NULL) m_lpBackB4->Release();
if (m_lpFrontB4 != NULL) m_lpFrontB4->Release();
if (m_lpDD4 != NULL)
{
if (m_bFullMode == TRUE) m_lpDD4->RestoreDisplayMode();
}
if( m_bFullMode == TRUE )
{
int cx = GetSystemMetrics(SM_CXFULLSCREEN);
int cy = GetSystemMetrics(SM_CYFULLSCREEN);
ddVal = m_lpDD4->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
if (ddVal != DD_OK) return;
cx = cx/2;
cy = cy/2;
if(cy>280) cy -= 40;
SetWindowPos( hWnd, NULL, cx-320, cy-240, 640, 480, SWP_SHOWWINDOW);
// 프론트 버퍼 생성
memset( (VOID *)&ddsd, 0, sizeof(ddsd) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS;
//ddsd.dwBackBufferCount = 0;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
ddVal = m_lpDD4->CreateSurface(&ddsd, &m_lpFrontB4, NULL);
if (ddVal != DD_OK) return;
SetRect(&m_rcFlipping, cx-320, cy-240, cx+320, cy+240); // our fictitious sprite bitmap is
m_bFullMode = FALSE;
}
else
{
DDSCAPS2 ddscaps;
// 전체화면 모드이다.
ddVal = m_lpDD4->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
if (ddVal != DD_OK) return;
ddVal = m_lpDD4->SetDisplayMode(640, 480, 16,0,0);
if (ddVal != DD_OK) return;
// 프론트 버퍼 생성
memset( (VOID *)&ddsd, 0, sizeof(ddsd) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.dwBackBufferCount = 1;//2; //v1.3
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddVal = m_lpDD4->CreateSurface(&ddsd, &m_lpFrontB4, NULL);
if (ddVal != DD_OK) return;
ZeroMemory(&ddscaps, sizeof(ddscaps));
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
ddVal = m_lpFrontB4->GetAttachedSurface(&ddscaps, &m_lpBackB4flip);
if (ddVal != DD_OK) return;
SetRect(&m_rcFlipping, 0, 0, 640, 480); // our fictitious sprite bitmap is
m_bFullMode = TRUE;
}
InitFlipToGDI(hWnd);
// 백버퍼 생성
m_lpBackB4 = pCreateOffScreenSurface(640, 480);
if (m_lpBackB4 == NULL) return;
// Pre-draw background surface 버퍼 생성
m_lpPDBGS = pCreateOffScreenSurface(640+32, 480+32);
if (m_lpPDBGS == NULL) return;
// 백버퍼의 주소와 피치를 얻어온다.
ddsd.dwSize = sizeof(ddsd);
if (m_lpBackB4->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL) != DD_OK) return;
m_pBackB4Addr = (WORD *)ddsd.lpSurface;
m_sBackB4Pitch = (short)ddsd.lPitch >> 1;
m_lpBackB4->Unlock(NULL);
}
IDirectDrawSurface7 * DXC_ddraw::pCreateOffScreenSurface(WORD wSzX, WORD wSzY)
{
DDSURFACEDESC2 ddsd;
IDirectDrawSurface7 * pdds4;
ZeroMemory(&ddsd, sizeof(ddsd));
if ((wSzX % 4) != 0) wSzX += 4 - (wSzX % 4);
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT |DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
ddsd.dwWidth = (DWORD)wSzX;
ddsd.dwHeight = (DWORD)wSzY;
if (m_lpDD4->CreateSurface(&ddsd, &pdds4, NULL) != DD_OK) return NULL;
return pdds4;
}
HRESULT DXC_ddraw::iSetColorKey(IDirectDrawSurface7 * pdds4, COLORREF rgb)
{
DDCOLORKEY ddck;
ddck.dwColorSpaceLowValue = _dwColorMatch(pdds4, rgb);
ddck.dwColorSpaceHighValue = ddck.dwColorSpaceLowValue;
return pdds4->SetColorKey(DDCKEY_SRCBLT, &ddck);
}
HRESULT DXC_ddraw::iSetColorKey(IDirectDrawSurface7 * pdds4, WORD wColorKey)
{
DDCOLORKEY ddck;
ddck.dwColorSpaceLowValue = _dwColorMatch(pdds4, wColorKey);
ddck.dwColorSpaceHighValue = ddck.dwColorSpaceLowValue;
return pdds4->SetColorKey(DDCKEY_SRCBLT, &ddck);
}
DWORD DXC_ddraw::_dwColorMatch(IDirectDrawSurface7 * pdds4, COLORREF rgb)
{
COLORREF rgbT;
HDC hdc;
DWORD dw = CLR_INVALID;
DDSURFACEDESC2 ddsd2;
HRESULT hres;
if (rgb != CLR_INVALID && pdds4->GetDC(&hdc) == DD_OK)
{
rgbT = GetPixel(hdc, 0, 0);
SetPixel(hdc, 0, 0, rgb);
pdds4->ReleaseDC(hdc);
}
ddsd2.dwSize = sizeof(ddsd2);
while ((hres = pdds4->Lock(NULL, &ddsd2, 0, NULL)) == DDERR_WASSTILLDRAWING);
if (hres == DD_OK)
{
dw = *(DWORD *)ddsd2.lpSurface;
dw &= (1 << ddsd2.ddpfPixelFormat.dwRGBBitCount)-1;
pdds4->Unlock(NULL);
}
if (rgb != CLR_INVALID && pdds4->GetDC(&hdc) == DD_OK)
{
SetPixel(hdc, 0, 0, rgbT);
pdds4->ReleaseDC(hdc);
}
return dw;
}
DWORD DXC_ddraw::_dwColorMatch(IDirectDrawSurface7 * pdds4, WORD wColorKey)
{
DWORD dw = CLR_INVALID, * dwp;
DDSURFACEDESC2 ddsd2;
HRESULT hres;
ddsd2.dwSize = sizeof(ddsd2);
while ((hres = pdds4->Lock(NULL, &ddsd2, 0, NULL)) == DDERR_WASSTILLDRAWING);
if (hres == DD_OK)
{
dwp = (DWORD *)ddsd2.lpSurface;
*dwp = (DWORD)wColorKey;
dw = *(DWORD *)ddsd2.lpSurface;
dw &= (1 << ddsd2.ddpfPixelFormat.dwRGBBitCount)-1;
pdds4->Unlock(NULL);
}
return dw;
}
void DXC_ddraw::TextOut(int x, int y, char * cStr, COLORREF rgb)
{
SetTextColor(m_hDC, rgb );
::TextOut(m_hDC, x, y, cStr, strlen(cStr));
}
void DXC_ddraw::_TestPixelFormat()
{
DDSURFACEDESC2 ddSurfaceDesc2;
HRESULT hResult;
ZeroMemory(&ddSurfaceDesc2, sizeof(DDSURFACEDESC2));
ddSurfaceDesc2.dwSize = sizeof(ddSurfaceDesc2);
ddSurfaceDesc2.dwFlags = DDSD_PIXELFORMAT;
hResult = m_lpBackB4->GetSurfaceDesc(&ddSurfaceDesc2);
if (hResult == DD_OK)
{
if (ddSurfaceDesc2.ddpfPixelFormat.dwRBitMask == 0x0000F800) {
m_cPixelFormat = 1;
// RGB 5:6:5
}
if (ddSurfaceDesc2.ddpfPixelFormat.dwRBitMask == 0x00007C00) {
m_cPixelFormat = 2;
// RGB 5:5:5
}
if (ddSurfaceDesc2.ddpfPixelFormat.dwRBitMask == 0x0000001F) {
m_cPixelFormat = 3;
// BGR 5:6:5
}
}
}
long DXC_ddraw::_CalcMaxValue(int iS, int iD, char cMode, char cMethod, double dAlpha)
{
long Sum;
double dTmp;
switch (cMethod) {
case 1:
dTmp = (double)iS;
dTmp = dTmp * dAlpha;
iS = (int)dTmp;
Sum = (iS) + (iD);
if (Sum < iD) Sum = iD;
break;
case 2:
Sum = (iS + iD) / 2;
break;
}
switch (cMode) {
case 'G':
switch (m_cPixelFormat) {
case 1:
if (Sum >= 64) Sum = 63; //v1.3
break;
case 2:
if (Sum >= 32) Sum = 31;
break;
}
break;
default:
if (Sum >= 32) Sum = 31;
break;
}
return Sum;
}
long DXC_ddraw::_CalcMinValue(int iS, int iD, char cMode)
{
long Sum;
Sum = iD - iS;
if (Sum < 0) Sum = 0;
switch (cMode) {
case 'G':
switch (m_cPixelFormat) {
case 1:
if (Sum >= 64) Sum = 63; //v1.3
break;
case 2:
if (Sum >= 32) Sum = 31;
break;
}
break;
default:
if (Sum >= 32) Sum = 31;
break;
}
return Sum;
}
void DXC_ddraw::ClearBackB4()
{
DDSURFACEDESC2 ddsd2;
ddsd2.dwSize = sizeof(ddsd2);
if (m_lpBackB4->Lock(NULL, &ddsd2, DDLOCK_WAIT, NULL) != DD_OK) return;
memset((char *)ddsd2.lpSurface, 0, ddsd2.lPitch * 480);
m_lpBackB4->Unlock(NULL);
}
void DXC_ddraw::DrawShadowBox(short sX, short sY, short dX, short dY, int iType)
{
WORD * pDst, wValue;
int ix, iy;
pDst = (WORD *)m_pBackB4Addr + sX + ((sY)*m_sBackB4Pitch);
if (iType == 0) {
switch (m_cPixelFormat) {
case 1:
for (iy = 0; iy <= (dY - sY); iy++) {
for (ix = 0; ix <= (dX - sX); ix++)
pDst[ix] = (pDst[ix] & 0xf7de) >> 1;
pDst += m_sBackB4Pitch;
}
break;
case 2:
for (iy = 0; iy <= (dY - sY); iy++) {
for (ix = 0; ix <= (dX - sX); ix++)
pDst[ix] = (pDst[ix] & 0x7bde) >> 1;
pDst += m_sBackB4Pitch;
}
break;
}
}
else
{
switch (iType) {
case 1:
if (m_cPixelFormat == 1)
wValue = 0x38e7;
else wValue = 0x1ce7;
break;
case 2:
if (m_cPixelFormat == 1)
wValue = 0x1863;
else wValue = 0xc63;
break;
}
for (iy = 0; iy <= (dY - sY); iy++) {
for (ix = 0; ix <= (dX - sX); ix++)
pDst[ix] = wValue;
pDst += m_sBackB4Pitch;
}
}
}
void DXC_ddraw::PutPixel(short sX, short sY, WORD wR, WORD wG, WORD wB)
{
WORD * pDst;
if ((sX < 0) || (sY < 0) || (sX > 639) || (sY > 479)) return;
pDst = (WORD *)m_pBackB4Addr + sX + ((sY)*m_sBackB4Pitch);
switch (m_cPixelFormat) {
case 1:
*pDst = (WORD)( ((wR>>3)<<11) | ((wG>>2)<<5) | (wB>>3) );
//*pDst = (WORD)((wR<<11) | (wG<<5) | wB);
break;
case 2:
*pDst = (WORD)( ((wR>>3)<<10) | ((wG>>3)<<5) | (wB>>3) );
//*pDst = (WORD)((wR<<10) | (wG<<5) | wB);
break;
}
}
void DXC_ddraw::_GetBackBufferDC()
{
m_lpBackB4->GetDC(&m_hDC);
SelectObject(m_hDC, m_hFontInUse);
SetBkMode(m_hDC, TRANSPARENT);
SetBkColor(m_hDC, RGB(0,0,0));
}
void DXC_ddraw::_ReleaseBackBufferDC()
{
m_lpBackB4->ReleaseDC(m_hDC);
}
void DXC_ddraw::DrawText(LPRECT pRect, char *pString, COLORREF rgb)
{
SetTextColor(m_hDC, rgb);
::DrawText(m_hDC, pString, strlen(pString), pRect, DT_CENTER | DT_NOCLIP | DT_WORDBREAK | DT_NOPREFIX);//v2.15
}
HRESULT DXC_ddraw::InitFlipToGDI(HWND hWnd)
{
LPDIRECTDRAWCLIPPER pClipper;
HRESULT hr;
DDCAPS ddcaps;
ZeroMemory( &ddcaps, sizeof(ddcaps) );
ddcaps.dwSize = sizeof(ddcaps);
m_lpDD4->GetCaps( &ddcaps, NULL );
if( (ddcaps.dwCaps2 & DDCAPS2_CANRENDERWINDOWED) == 0 )
{
// This means FlipToGDISurface() is not supported, so to display GDI
// on these cards, you you must create a bitmap of the GDI window
// and BitBlt the bitmap to the backbuffer then flip as normal. However,
// this specific sample does not show this.
return E_FAIL;
}
// Create a clipper when using GDI to draw on the primary surface
if( FAILED( hr = m_lpDD4->CreateClipper( 0, &pClipper, NULL ) ) )
return hr;
pClipper->SetHWnd( 0, hWnd );
if( FAILED( hr = m_lpFrontB4->SetClipper( pClipper ) ) ) return hr;
// We can release the clipper now since g_pDDSPrimary
// now maintains a ref count on the clipper
if( pClipper )
{
pClipper->Release();
pClipper = NULL;
}
return S_OK;
}
void DXC_ddraw::ColorTransferRGB(COLORREF fcolor, int * iR, int * iG, int * iB)
{
WORD wR, wG, wB;
switch(m_cPixelFormat)
{
case 1:
// R
wR = (WORD)((fcolor&0x000000f8)>>3);
// G
wG = (WORD)((fcolor&0x0000fc00)>>10);
// B
wB = (WORD)((fcolor&0x00f80000)>>19);
*iR = (int)wR;
*iG = (int)wG;
*iB = (int)wB;
break;
case 2:
// R
wR = (WORD)((fcolor&0x000000f8)>>3);
// G
wG = (WORD)((fcolor&0x0000f800)>>11);
// B
wB = (WORD)((fcolor&0x00f80000)>>19);
*iR = (int)wR;
*iG = (int)wG;
*iB = (int)wB;
break;
}
}
//---------------------------------------------------------------------------
bool DXC_ddraw::Screenshot(LPCTSTR FileName, LPDIRECTDRAWSURFACE7 lpDDS)
{
if (!FileName || !lpDDS) return false;
bool Success=false;
HDC SurfDC=NULL; // GDI-compatible device context for the surface
HBITMAP OffscrBmp=NULL; // bitmap that is converted to a DIB
HDC OffscrDC=NULL; // offscreen DC that we can select OffscrBmp into
LPBITMAPINFO lpbi=NULL; // bitmap format info; used by GetDIBits
LPVOID lpvBits=NULL; // pointer to bitmap bits array
HANDLE BmpFile=INVALID_HANDLE_VALUE; // destination .bmp file
BITMAPFILEHEADER bmfh; // .bmp file header
try
{
// Get dimensions of Surface:
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
if (FAILED(lpDDS->GetSurfaceDesc(&ddsd))) throw 0;
int Width = ddsd.dwWidth;
int Height = ddsd.dwHeight;
// Create a GDI-compatible device context for the surface:
if (FAILED(lpDDS->GetDC(&SurfDC))) throw 1;
// We need an HBITMAP to convert it to a DIB:
if ((OffscrBmp = CreateCompatibleBitmap(SurfDC, Width, Height)) == NULL)
throw 2;
// The bitmap is empty, so let's copy the contents of the surface to it.
// For that we need to select it into a device context. We create one.
if ((OffscrDC = CreateCompatibleDC(SurfDC)) == NULL) throw 3;
// Select OffscrBmp into OffscrDC:
HBITMAP OldBmp = (HBITMAP)SelectObject(OffscrDC, OffscrBmp);
// Now we can copy the contents of the surface to the offscreen bitmap:
BitBlt(OffscrDC, 0, 0, Width, Height, SurfDC, 0, 0, SRCCOPY);
// We don't need SurfDC anymore. Free it:
lpDDS->ReleaseDC(SurfDC); SurfDC = NULL;
// GetDIBits requires format info about the bitmap. We can have GetDIBits
// fill a structure with that info if we pass a NULL pointer for lpvBits:
// Reserve memory for bitmap info (BITMAPINFOHEADER + largest possible
// palette):
if ((lpbi = (LPBITMAPINFO)(new char[sizeof(BITMAPINFOHEADER) +
256 * sizeof(RGBQUAD)])) == NULL) throw 4;
ZeroMemory(&lpbi->bmiHeader, sizeof(BITMAPINFOHEADER));
lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
// Get info but first de-select OffscrBmp because GetDIBits requires it:
SelectObject(OffscrDC, OldBmp);
if (!GetDIBits(OffscrDC, OffscrBmp, 0, Height, NULL, lpbi, DIB_RGB_COLORS))
throw 5;
// Reserve memory for bitmap bits:
if ((lpvBits = new char[lpbi->bmiHeader.biSizeImage]) == NULL)
throw 6;
// Have GetDIBits convert OffscrBmp to a DIB (device-independent bitmap):
if (!GetDIBits(OffscrDC, OffscrBmp, 0, Height, lpvBits, lpbi,
DIB_RGB_COLORS)) throw 7;
// Create a file to save the DIB to:
if ((BmpFile = CreateFile(FileName,
GENERIC_WRITE,
0, NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE) throw 8;
DWORD Written; // number of bytes written by WriteFile
// Write a file header to the file:
bmfh.bfType = 19778; // 'BM'
// bmfh.bfSize = ??? // we'll write that later
bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
// bmfh.bfOffBits = ??? // we'll write that later
if (!WriteFile(BmpFile, &bmfh, sizeof(bmfh), &Written, NULL))
throw 9;
if (Written < sizeof(bmfh)) throw 9;
// Write BITMAPINFOHEADER to the file:
if (!WriteFile(BmpFile, &lpbi->bmiHeader, sizeof(BITMAPINFOHEADER),
&Written, NULL)) throw 10;
if (Written < sizeof(BITMAPINFOHEADER)) throw 10;
// Calculate size of palette:
int PalEntries;
// 16-bit or 32-bit bitmaps require bit masks:
if (lpbi->bmiHeader.biCompression == BI_BITFIELDS) PalEntries = 3;
else
// bitmap is palettized?
PalEntries = (lpbi->bmiHeader.biBitCount <= 8) ?
// 2^biBitCount palette entries max.:
(int)(1 << lpbi->bmiHeader.biBitCount)
// bitmap is TrueColor -> no palette:
: 0;
// If biClrUsed use only biClrUsed palette entries:
if (lpbi->bmiHeader.biClrUsed) PalEntries = lpbi->bmiHeader.biClrUsed;
// Write palette to the file:
if (PalEntries)
{
if (!WriteFile(BmpFile, &lpbi->bmiColors, PalEntries * sizeof(RGBQUAD),
&Written, NULL)) throw 11;
if (Written < PalEntries * sizeof(RGBQUAD)) throw 11;
}
// The current position in the file (at the beginning of the bitmap bits)
// will be saved to the BITMAPFILEHEADER:
bmfh.bfOffBits = SetFilePointer(BmpFile, 0, 0, FILE_CURRENT);
// Write bitmap bits to the file:
if (!WriteFile(BmpFile, lpvBits, lpbi->bmiHeader.biSizeImage,
&Written, NULL)) throw 12;
if (Written < lpbi->bmiHeader.biSizeImage) throw 12;
// The current pos. in the file is the final file size and will be saved:
bmfh.bfSize = SetFilePointer(BmpFile, 0, 0, FILE_CURRENT);
// We have all the info for the file header. Save the updated version:
SetFilePointer(BmpFile, 0, 0, FILE_BEGIN);
if (!WriteFile(BmpFile, &bmfh, sizeof(bmfh), &Written, NULL))
throw 13;
if (Written < sizeof(bmfh)) throw 13;
OutputDebugString("Screenshot Success\r\n");
Success = true;
}
catch (int &errorcode)
{
char Buf[100];
wsprintf(Buf, "Screenshot error #%i\r\n", errorcode);
OutputDebugString(Buf);
}
catch (...)
{
OutputDebugString("Screenshot error\r\n");
}
if (SurfDC) lpDDS->ReleaseDC(SurfDC);
if (OffscrDC) DeleteDC(OffscrDC);
if (OffscrBmp) DeleteObject(OffscrBmp);
if (lpbi) delete[] lpbi;
if (lpvBits) delete[] lpvBits;
if (BmpFile != INVALID_HANDLE_VALUE) CloseHandle(BmpFile);
return Success;
}
//---------------------------------------------------------------------------