forked from mvdhout1992/cnc-ddraw
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathmain.c
1913 lines (1614 loc) · 60.5 KB
/
main.c
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
/*
* Copyright (c) 2010 Toni Spets <toni.spets@iki.fi>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
#include <stdio.h>
#include <ctype.h>
#include <d3d9.h>
#include <initguid.h>
#include "ddraw.h"
#include "main.h"
#include "opengl.h"
#include "palette.h"
#include "surface.h"
#include "clipper.h"
#include "hook.h"
#include "mouse.h"
#include "render_d3d9.h"
#define IDR_MYMENU 93
BOOL screenshot(struct IDirectDrawSurfaceImpl *);
void Settings_Load();
void Settings_Save(RECT *lpRect, int windowState);
void DInput_Hook();
void DInput_UnHook();
IDirectDrawImpl *ddraw = NULL;
RECT WindowRect = { .left = -32000, .top = -32000, .right = 0, .bottom = 0 };
int WindowState = -1;
BOOL Direct3D9Active;
BOOL GameHandlesClose;
BOOL ChildWindowExists;
DWORD NvOptimusEnablement = 1;
DWORD AmdPowerXpressRequestHighPerformance = 1;
//BOOL WINAPI DllMainCRTStartup(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved)
BOOL WINAPI DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
{
char buf[1024];
if (GetEnvironmentVariable("__COMPAT_LAYER", buf, sizeof(buf)))
{
char *s = strtok(buf, " ");
while (s)
{
if (strcmpi(s, "WIN95") == 0 || strcmpi(s, "WIN98") == 0 || strcmpi(s, "NT4SP5") == 0)
{
char mes[128] = { 0 };
_snprintf(
mes,
sizeof(mes),
"Please disable the '%s' compatibility mode for all game executables and "
"then try to start the game again.",
s);
MessageBoxA(NULL, mes, "Compatibility modes detected - cnc-ddraw", MB_OK);
//return FALSE;
break;
}
s = strtok(NULL, " ");
}
}
printf("cnc-ddraw DLL_PROCESS_ATTACH\n");
//SetProcessPriorityBoost(GetCurrentProcess(), TRUE);
BOOL setDpiAware = FALSE;
HMODULE hShcore = GetModuleHandle("shcore.dll");
typedef HRESULT (__stdcall* SetProcessDpiAwareness_)(PROCESS_DPI_AWARENESS value);
if(hShcore)
{
SetProcessDpiAwareness_ setProcessDpiAwareness =
(SetProcessDpiAwareness_)GetProcAddress(hShcore, "SetProcessDpiAwareness");
if (setProcessDpiAwareness)
{
HRESULT result = setProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
setDpiAware = result == S_OK || result == E_ACCESSDENIED;
}
}
if (!setDpiAware)
{
HMODULE hUser32 = GetModuleHandle("user32.dll");
typedef BOOL (__stdcall* SetProcessDPIAware_)();
if(hUser32)
{
SetProcessDPIAware_ setProcessDPIAware =
(SetProcessDPIAware_)GetProcAddress(hUser32, "SetProcessDPIAware");
if (setProcessDPIAware)
setProcessDPIAware();
}
}
timeBeginPeriod(1);
DInput_Hook();
break;
}
case DLL_PROCESS_DETACH:
{
printf("cnc-ddraw DLL_PROCESS_DETACH\n");
Settings_Save(&WindowRect, WindowState);
timeEndPeriod(1);
Hook_Exit();
DInput_UnHook();
break;
}
}
return TRUE;
}
BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam)
{
IDirectDrawSurfaceImpl *this = (IDirectDrawSurfaceImpl *)lParam;
RECT size;
RECT pos;
if (real_GetClientRect(hWnd, &size) && real_GetWindowRect(hWnd, &pos) && size.right > 1 && size.bottom > 1)
{
ChildWindowExists = TRUE;
HDC hDC = GetDC(hWnd);
MapWindowPoints(HWND_DESKTOP, ddraw->hWnd, (LPPOINT)&pos, 2);
BitBlt(hDC, 0, 0, size.right, size.bottom, this->hDC, pos.left, pos.top, SRCCOPY);
ReleaseDC(hWnd, hDC);
}
return FALSE;
}
static unsigned char getPixel(int x, int y)
{
return ((unsigned char *)ddraw->primary->surface)[y*ddraw->primary->lPitch + x*ddraw->primary->lXPitch];
}
int* InMovie = (int*)0x00665F58;
int* IsVQA640 = (int*)0x0065D7BC;
BYTE* ShouldStretch = (BYTE*)0x00607D78;
BOOL detect_cutscene()
{
if (ddraw->width <= CUTSCENE_WIDTH || ddraw->height <= CUTSCENE_HEIGHT)
return FALSE;
if (ddraw->isredalert)
{
if ((*InMovie && !*IsVQA640) || *ShouldStretch)
{
return TRUE;
}
return FALSE;
}
else if (ddraw->iscnc1)
{
return getPixel(CUTSCENE_WIDTH + 1, 0) == 0 || getPixel(CUTSCENE_WIDTH + 5, 1) == 0 ? TRUE : FALSE;
}
return FALSE;
}
void LimitGameTicks()
{
if (ddraw->ticksLimiter.hTimer)
{
FILETIME ft = { 0 };
GetSystemTimeAsFileTime(&ft);
if (CompareFileTime((FILETIME *)&ddraw->ticksLimiter.dueTime, &ft) == -1)
{
memcpy(&ddraw->ticksLimiter.dueTime, &ft, sizeof(LARGE_INTEGER));
}
else
{
WaitForSingleObject(ddraw->ticksLimiter.hTimer, ddraw->ticksLimiter.ticklength * 2);
}
ddraw->ticksLimiter.dueTime.QuadPart += ddraw->ticksLimiter.tickLengthNs;
SetWaitableTimer(ddraw->ticksLimiter.hTimer, &ddraw->ticksLimiter.dueTime, 0, NULL, NULL, FALSE);
}
else
{
static DWORD nextGameTick;
if (!nextGameTick)
{
nextGameTick = timeGetTime();
return;
}
nextGameTick += ddraw->ticksLimiter.ticklength;
DWORD tickCount = timeGetTime();
int sleepTime = nextGameTick - tickCount;
if (sleepTime <= 0 || sleepTime > ddraw->ticksLimiter.ticklength)
nextGameTick = tickCount;
else
Sleep(sleepTime);
}
}
void UpdateBnetPos(int newX, int newY)
{
static int oldX = -32000;
static int oldY = -32000;
if (oldX == -32000 || oldY == -32000 || !ddraw->bnetActive)
{
oldX = newX;
oldY = newY;
return;
}
POINT pt = { 0, 0 };
real_ClientToScreen(ddraw->hWnd, &pt);
RECT mainrc;
SetRect(&mainrc, pt.x, pt.y, pt.x + ddraw->width, pt.y + ddraw->height);
int adjY = 0;
int adjX = 0;
HWND hWnd = FindWindowEx(HWND_DESKTOP, NULL, "SDlgDialog", NULL);
while (hWnd != NULL)
{
RECT rc;
real_GetWindowRect(hWnd, &rc);
OffsetRect(&rc, newX - oldX, newY - oldY);
real_SetWindowPos(
hWnd,
0,
rc.left,
rc.top,
0,
0,
SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
if (rc.bottom - rc.top <= ddraw->height)
{
if (rc.bottom > mainrc.bottom && abs(mainrc.bottom - rc.bottom) > abs(adjY))
adjY = mainrc.bottom - rc.bottom;
else if (rc.top < mainrc.top && abs(mainrc.top - rc.top) > abs(adjY))
adjY = mainrc.top - rc.top;
}
if (rc.right - rc.left <= ddraw->width)
{
if (rc.right > mainrc.right && abs(mainrc.right - rc.right) > abs(adjX))
adjX = mainrc.right - rc.right;
else if (rc.left < mainrc.left && abs(mainrc.left - rc.left) > abs(adjX))
adjX = mainrc.left - rc.left;
}
hWnd = FindWindowEx(HWND_DESKTOP, hWnd, "SDlgDialog", NULL);
}
if (adjX || adjY)
{
HWND hWnd = FindWindowEx(HWND_DESKTOP, NULL, "SDlgDialog", NULL);
while (hWnd != NULL)
{
RECT rc;
real_GetWindowRect(hWnd, &rc);
OffsetRect(&rc, adjX, adjY);
real_SetWindowPos(
hWnd,
0,
rc.left,
rc.top,
0,
0,
SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
hWnd = FindWindowEx(HWND_DESKTOP, hWnd, "SDlgDialog", NULL);
}
}
oldX = newX;
oldY = newY;
}
HRESULT __stdcall ddraw_Compact(IDirectDrawImpl *This)
{
printf("??? DirectDraw::Compact(This=%p)\n", This);
return DD_OK;
}
HRESULT __stdcall ddraw_DuplicateSurface(IDirectDrawImpl *This, LPDIRECTDRAWSURFACE src, LPDIRECTDRAWSURFACE *dest)
{
printf("??? DirectDraw::DuplicateSurface(This=%p, ...)\n", This);
return DD_OK;
}
HRESULT __stdcall ddraw_EnumDisplayModes(IDirectDrawImpl *This, DWORD dwFlags, LPDDSURFACEDESC lpDDSurfaceDesc, LPVOID lpContext, LPDDENUMMODESCALLBACK lpEnumModesCallback)
{
DWORD i = 0;
DDSURFACEDESC s;
printf("DirectDraw::EnumDisplayModes(This=%p, dwFlags=%08X, lpDDSurfaceDesc=%p, lpContext=%p, lpEnumModesCallback=%p)\n", This, (unsigned int)dwFlags, lpDDSurfaceDesc, lpContext, lpEnumModesCallback);
if (lpDDSurfaceDesc != NULL)
{
//return DDERR_UNSUPPORTED;
}
// Some games crash when you feed them with too many resolutions...
if (This->bpp)
{
printf(" This->bpp=%d\n", This->bpp);
//set up some filters to keep the list short
DWORD refreshRate = 0;
DWORD bpp = 0;
DWORD flags = 99998;
DWORD fixedOutput = 99998;
DEVMODE m;
memset(&m, 0, sizeof(DEVMODE));
m.dmSize = sizeof(DEVMODE);
while (EnumDisplaySettings(NULL, i, &m))
{
if (refreshRate != 60 && m.dmDisplayFrequency >= 50)
refreshRate = m.dmDisplayFrequency;
if (bpp != 32 && m.dmBitsPerPel >= 16)
bpp = m.dmBitsPerPel;
if (flags != 0)
flags = m.dmDisplayFlags;
if (fixedOutput != DMDFO_DEFAULT)
fixedOutput = m.dmDisplayFixedOutput;
memset(&m, 0, sizeof(DEVMODE));
m.dmSize = sizeof(DEVMODE);
i++;
}
memset(&m, 0, sizeof(DEVMODE));
m.dmSize = sizeof(DEVMODE);
i = 0;
while (EnumDisplaySettings(NULL, i, &m))
{
if (refreshRate == m.dmDisplayFrequency &&
bpp == m.dmBitsPerPel &&
flags == m.dmDisplayFlags &&
fixedOutput == m.dmDisplayFixedOutput)
{
#if _DEBUG_X
printf(" %d: %dx%d@%d %d bpp\n", (int)i, (int)m.dmPelsWidth, (int)m.dmPelsHeight, (int)m.dmDisplayFrequency, (int)m.dmBitsPerPel);
#endif
memset(&s, 0, sizeof(DDSURFACEDESC));
s.dwSize = sizeof(DDSURFACEDESC);
s.dwFlags = DDSD_HEIGHT | DDSD_REFRESHRATE | DDSD_WIDTH | DDSD_PIXELFORMAT;
s.dwHeight = m.dmPelsHeight;
s.dwWidth = m.dmPelsWidth;
s.dwRefreshRate = 60;
s.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
s.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
s.ddpfPixelFormat.dwRGBBitCount = 8;
if (This->bpp == 16)
{
s.ddpfPixelFormat.dwFlags = DDPF_RGB;
s.ddpfPixelFormat.dwRGBBitCount = 16;
s.ddpfPixelFormat.dwRBitMask = 0xF800;
s.ddpfPixelFormat.dwGBitMask = 0x07E0;
s.ddpfPixelFormat.dwBBitMask = 0x001F;
}
if (lpEnumModesCallback(&s, lpContext) == DDENUMRET_CANCEL)
{
printf(" DDENUMRET_CANCEL returned, stopping\n");
break;
}
}
memset(&m, 0, sizeof(DEVMODE));
m.dmSize = sizeof(DEVMODE);
i++;
}
}
else
{
SIZE resolutions[] =
{
{ 320, 200 },
{ 640, 400 },
{ 640, 480 },
{ 800, 600 },
{ 1024, 768 },
{ 1280, 1024 },
{ 1280, 720 },
{ 1920, 1080 },
};
for (i = 0; i < sizeof(resolutions) / sizeof(resolutions[0]); i++)
{
memset(&s, 0, sizeof(DDSURFACEDESC));
s.dwSize = sizeof(DDSURFACEDESC);
s.dwFlags = DDSD_HEIGHT | DDSD_REFRESHRATE | DDSD_WIDTH | DDSD_PIXELFORMAT;
s.dwHeight = resolutions[i].cy;
s.dwWidth = resolutions[i].cx;
s.dwRefreshRate = 60;
s.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
s.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
s.ddpfPixelFormat.dwRGBBitCount = 8;
if (lpEnumModesCallback(&s, lpContext) == DDENUMRET_CANCEL)
{
printf(" DDENUMRET_CANCEL returned, stopping\n");
break;
}
s.ddpfPixelFormat.dwFlags = DDPF_RGB;
s.ddpfPixelFormat.dwRGBBitCount = 16;
s.ddpfPixelFormat.dwRBitMask = 0xF800;
s.ddpfPixelFormat.dwGBitMask = 0x07E0;
s.ddpfPixelFormat.dwBBitMask = 0x001F;
if (lpEnumModesCallback(&s, lpContext) == DDENUMRET_CANCEL)
{
printf(" DDENUMRET_CANCEL returned, stopping\n");
break;
}
}
}
return DD_OK;
}
HRESULT __stdcall ddraw_EnumSurfaces(IDirectDrawImpl *This, DWORD a, LPDDSURFACEDESC b, LPVOID c, LPDDENUMSURFACESCALLBACK d)
{
printf("??? DirectDraw::EnumSurfaces(This=%p, ...)\n", This);
return DD_OK;
}
HRESULT __stdcall ddraw_FlipToGDISurface(IDirectDrawImpl *This)
{
printf("??? DirectDraw::FlipToGDISurface(This=%p)\n", This);
return DD_OK;
}
HRESULT __stdcall ddraw_GetCaps(IDirectDrawImpl *This, LPDDCAPS lpDDDriverCaps, LPDDCAPS lpDDEmulCaps)
{
printf("DirectDraw::GetCaps(This=%p, lpDDDriverCaps=%p, lpDDEmulCaps=%p)\n", This, lpDDDriverCaps, lpDDEmulCaps);
if(lpDDDriverCaps)
{
lpDDDriverCaps->dwSize = sizeof(DDCAPS);
lpDDDriverCaps->dwCaps = DDCAPS_BLT | DDCAPS_PALETTE | DDCAPS_BLTCOLORFILL | DDCAPS_BLTSTRETCH | DDCAPS_CANCLIP;
lpDDDriverCaps->dwCKeyCaps = 0;
lpDDDriverCaps->dwPalCaps = DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE;
lpDDDriverCaps->dwVidMemTotal = 16777216;
lpDDDriverCaps->dwVidMemFree = 16777216;
lpDDDriverCaps->dwMaxVisibleOverlays = 0;
lpDDDriverCaps->dwCurrVisibleOverlays = 0;
lpDDDriverCaps->dwNumFourCCCodes = 0;
lpDDDriverCaps->dwAlignBoundarySrc = 0;
lpDDDriverCaps->dwAlignSizeSrc = 0;
lpDDDriverCaps->dwAlignBoundaryDest = 0;
lpDDDriverCaps->dwAlignSizeDest = 0;
lpDDDriverCaps->ddsCaps.dwCaps = DDSCAPS_FLIP;
}
if(lpDDEmulCaps)
{
lpDDEmulCaps->dwSize = 0;
}
return DD_OK;
}
HRESULT __stdcall ddraw_GetDisplayMode(IDirectDrawImpl *This, LPDDSURFACEDESC a)
{
printf("??? DirectDraw::GetDisplayMode(This=%p, ...)\n", This);
return DD_OK;
}
HRESULT __stdcall ddraw_GetFourCCCodes(IDirectDrawImpl *This, LPDWORD a, LPDWORD b)
{
printf("??? DirectDraw::GetFourCCCodes(This=%p, ...)\n", This);
return DD_OK;
}
HRESULT __stdcall ddraw_GetGDISurface(IDirectDrawImpl *This, LPDIRECTDRAWSURFACE *a)
{
printf("??? DirectDraw::GetGDISurface(This=%p, ...)\n", This);
return DD_OK;
}
HRESULT __stdcall ddraw_GetMonitorFrequency(IDirectDrawImpl *This, LPDWORD a)
{
printf("??? DirectDraw::GetMonitorFrequency(This=%p, ...)\n", This);
return DD_OK;
}
HRESULT __stdcall ddraw_GetScanLine(IDirectDrawImpl *This, LPDWORD a)
{
printf("??? DirectDraw::GetScanLine(This=%p, ...)\n", This);
return DD_OK;
}
HRESULT __stdcall ddraw_GetVerticalBlankStatus(IDirectDrawImpl *This, LPBOOL lpbIsInVB)
{
printf("??? DirectDraw::GetVerticalBlankStatus(This=%p, ...)\n", This);
*lpbIsInVB = TRUE;
return DD_OK;
}
HRESULT __stdcall ddraw_Initialize(IDirectDrawImpl *This, GUID *a)
{
printf("??? DirectDraw::Initialize(This=%p, ...)\n", This);
return DD_OK;
}
HRESULT __stdcall ddraw_RestoreDisplayMode(IDirectDrawImpl *This)
{
printf("DirectDraw::RestoreDisplayMode(This=%p)\n", This);
if(!This->render.run)
{
return DD_OK;
}
/* only stop drawing in GL mode when minimized */
if (This->renderer != render_soft_main)
{
EnterCriticalSection(&This->cs);
This->render.run = FALSE;
ReleaseSemaphore(ddraw->render.sem, 1, NULL);
LeaveCriticalSection(&This->cs);
if (This->render.thread)
{
WaitForSingleObject(This->render.thread, INFINITE);
This->render.thread = NULL;
}
if (This->renderer == render_d3d9_main)
Direct3D9_Release();
}
if(!ddraw->windowed)
{
if (!Direct3D9Active)
ChangeDisplaySettings(&This->mode, 0);
}
return DD_OK;
}
BOOL GetLowestResolution(float ratio, SIZE *outRes, DWORD minWidth, DWORD minHeight, DWORD maxWidth, DWORD maxHeight)
{
BOOL result = FALSE;
int orgRatio = (int)(ratio * 100);
SIZE lowest = { .cx = maxWidth + 1, .cy = maxHeight + 1 };
DWORD i = 0;
DEVMODE m;
memset(&m, 0, sizeof(DEVMODE));
m.dmSize = sizeof(DEVMODE);
while (EnumDisplaySettings(NULL, i, &m))
{
if (m.dmPelsWidth >= minWidth &&
m.dmPelsHeight >= minHeight &&
m.dmPelsWidth <= maxWidth &&
m.dmPelsHeight <= maxHeight &&
m.dmPelsWidth < lowest.cx &&
m.dmPelsHeight < lowest.cy)
{
int resRatio = (int)(((float)m.dmPelsWidth / m.dmPelsHeight) * 100);
if (resRatio == orgRatio)
{
result = TRUE;
outRes->cx = lowest.cx = m.dmPelsWidth;
outRes->cy = lowest.cy = m.dmPelsHeight;
}
}
memset(&m, 0, sizeof(DEVMODE));
m.dmSize = sizeof(DEVMODE);
i++;
}
return result;
}
void InitDirect3D9()
{
Direct3D9Active = Direct3D9_Create();
if (!Direct3D9Active)
{
Direct3D9_Release();
ShowDriverWarning = TRUE;
ddraw->renderer = render_soft_main;
}
}
// LastSetWindowPosTick = Workaround for a wine+gnome bug where each SetWindowPos call triggers a WA_INACTIVE message
DWORD LastSetWindowPosTick;
HRESULT __stdcall ddraw_SetDisplayMode(IDirectDrawImpl *This, DWORD width, DWORD height, DWORD bpp)
{
printf("DirectDraw::SetDisplayMode(This=%p, width=%d, height=%d, bpp=%d)\n", This, (unsigned int)width, (unsigned int)height, (unsigned int)bpp);
if (bpp != 8 && bpp != 16)
return DDERR_INVALIDMODE;
if (This->render.thread)
{
EnterCriticalSection(&This->cs);
This->render.run = FALSE;
ReleaseSemaphore(ddraw->render.sem, 1, NULL);
LeaveCriticalSection(&This->cs);
WaitForSingleObject(This->render.thread, INFINITE);
This->render.thread = NULL;
}
if (!This->mode.dmPelsWidth)
{
This->mode.dmSize = sizeof(DEVMODE);
This->mode.dmDriverExtra = 0;
if (EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &This->mode) == FALSE)
{
/* not expected */
return DDERR_UNSUPPORTED;
}
const HANDLE hbicon = LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDR_MYMENU), IMAGE_ICON, real_GetSystemMetrics(SM_CXICON), real_GetSystemMetrics(SM_CYICON), 0);
if (hbicon)
real_SendMessageA(This->hWnd, WM_SETICON, ICON_BIG, (LPARAM)hbicon);
const HANDLE hsicon = LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDR_MYMENU), IMAGE_ICON, real_GetSystemMetrics(SM_CXSMICON), real_GetSystemMetrics(SM_CYSMICON), 0);
if (hsicon)
real_SendMessageA(This->hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hsicon);
}
if (ddraw->altenter)
{
ddraw->altenter = FALSE;
This->render.width = ddraw->width;
This->render.height = ddraw->height;
}
else
{
This->render.width = WindowRect.right;
This->render.height = WindowRect.bottom;
}
//temporary fix: center window for games that keep changing their resolution
if (This->width && This->bpp == bpp && (This->width != width || This->height != height))
{
WindowRect.left = -32000;
WindowRect.top = -32000;
}
This->width = width;
This->height = height;
This->bpp = bpp;
ddraw->cursor.x = width / 2;
ddraw->cursor.y = height / 2;
BOOL border = This->border;
if(This->fullscreen)
{
This->render.width = This->mode.dmPelsWidth;
This->render.height = This->mode.dmPelsHeight;
if (This->windowed) //windowed-fullscreen aka borderless
{
border = FALSE;
WindowRect.left = -32000;
WindowRect.top = -32000;
// prevent OpenGL from going automatically into fullscreen exclusive mode
if (This->renderer == render_main)
This->render.height++;
}
}
if(This->render.width < This->width)
{
This->render.width = This->width;
}
if(This->render.height < This->height)
{
This->render.height = This->height;
}
This->render.run = TRUE;
BOOL lockMouse = ddraw->locked || This->fullscreen;
mouse_unlock();
memset(&This->render.mode, 0, sizeof(DEVMODE));
This->render.mode.dmSize = sizeof(DEVMODE);
This->render.mode.dmFields = DM_PELSWIDTH|DM_PELSHEIGHT;
This->render.mode.dmPelsWidth = This->render.width;
This->render.mode.dmPelsHeight = This->render.height;
if(This->render.bpp)
{
This->render.mode.dmFields |= DM_BITSPERPEL;
This->render.mode.dmBitsPerPel = This->render.bpp;
}
BOOL maintas = ddraw->maintas;
if (!This->windowed)
{
// Making sure the chosen resolution is valid
int oldWidth = This->render.width;
int oldHeight = This->render.height;
if (ChangeDisplaySettings(&This->render.mode, CDS_TEST) != DISP_CHANGE_SUCCESSFUL)
{
// fail... compare resolutions
if (This->render.width > This->mode.dmPelsWidth || This->render.height > This->mode.dmPelsHeight)
{
// chosen game resolution higher than current resolution, use windowed mode for this case
This->windowed = TRUE;
}
else
{
// Try 2x scaling
This->render.width *= 2;
This->render.height *= 2;
This->render.mode.dmPelsWidth = This->render.width;
This->render.mode.dmPelsHeight = This->render.height;
if ((This->render.width > This->mode.dmPelsWidth || This->render.height > This->mode.dmPelsHeight) ||
ChangeDisplaySettings(&This->render.mode, CDS_TEST) != DISP_CHANGE_SUCCESSFUL)
{
SIZE res = {0};
//try to get a resolution with the same aspect ratio as the requested resolution
BOOL foundRes = GetLowestResolution(
(float)oldWidth / oldHeight,
&res,
oldWidth + 1, //don't return the original resolution since we tested that one already
oldHeight + 1,
This->mode.dmPelsWidth,
This->mode.dmPelsHeight);
if (!foundRes)
{
//try to get a resolution with the same aspect ratio as the current display mode
foundRes = GetLowestResolution(
(float)This->mode.dmPelsWidth / This->mode.dmPelsHeight,
&res,
oldWidth,
oldHeight,
This->mode.dmPelsWidth,
This->mode.dmPelsHeight);
if (foundRes)
maintas = TRUE;
}
This->render.width = res.cx;
This->render.height = res.cy;
This->render.mode.dmPelsWidth = This->render.width;
This->render.mode.dmPelsHeight = This->render.height;
if (!foundRes || ChangeDisplaySettings(&This->render.mode, CDS_TEST) != DISP_CHANGE_SUCCESSFUL)
{
// try current display settings
This->render.width = This->mode.dmPelsWidth;
This->render.height = This->mode.dmPelsHeight;
This->render.mode.dmPelsWidth = This->render.width;
This->render.mode.dmPelsHeight = This->render.height;
if (ChangeDisplaySettings(&This->render.mode, CDS_TEST) != DISP_CHANGE_SUCCESSFUL)
{
// everything failed, use windowed mode instead
This->render.width = oldWidth;
This->render.height = oldHeight;
This->render.mode.dmPelsWidth = This->render.width;
This->render.mode.dmPelsHeight = This->render.height;
This->windowed = TRUE;
}
else
maintas = TRUE;
}
}
}
}
}
if (!ddraw->handlemouse)
This->boxing = maintas = FALSE;
This->render.viewport.width = This->render.width;
This->render.viewport.height = This->render.height;
This->render.viewport.x = 0;
This->render.viewport.y = 0;
if (This->boxing)
{
This->render.viewport.width = This->width;
This->render.viewport.height = This->height;
int i;
for (i = 20; i-- > 1;)
{
if (This->width * i <= This->render.width && This->height * i <= This->render.height)
{
This->render.viewport.width *= i;
This->render.viewport.height *= i;
break;
}
}
This->render.viewport.y = This->render.height / 2 - This->render.viewport.height / 2;
This->render.viewport.x = This->render.width / 2 - This->render.viewport.width / 2;
}
else if (maintas)
{
This->render.viewport.width = This->render.width;
This->render.viewport.height = ((float)This->height / This->width) * This->render.viewport.width;
if (This->render.viewport.height > This->render.height)
{
This->render.viewport.width =
((float)This->render.viewport.width / This->render.viewport.height) * This->render.height;
This->render.viewport.height = This->render.height;
}
This->render.viewport.y = This->render.height / 2 - This->render.viewport.height / 2;
This->render.viewport.x = This->render.width / 2 - This->render.viewport.width / 2;
}
This->render.scaleW = ((float)This->render.viewport.width / This->width);
This->render.scaleH = ((float)This->render.viewport.height / This->height);
This->render.unScaleW = ((float)This->width / This->render.viewport.width);
This->render.unScaleH = ((float)This->height / This->render.viewport.height);
if (This->windowed)
{
MSG msg; // workaround for "Not Responding" window problem in cnc games
PeekMessage(&msg, ddraw->hWnd, 0, 0, PM_NOREMOVE);
if (!border)
{
real_SetWindowLongA(This->hWnd, GWL_STYLE, GetWindowLong(This->hWnd, GWL_STYLE) & ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU));
}
else
{
real_SetWindowLongA(This->hWnd, GWL_STYLE, (GetWindowLong(This->hWnd, GWL_STYLE) | WS_OVERLAPPEDWINDOW) & ~WS_MAXIMIZEBOX);
}
if (ddraw->wine)
real_SetWindowLongA(This->hWnd, GWL_STYLE, (GetWindowLong(This->hWnd, GWL_STYLE) | WS_MINIMIZEBOX) & ~(WS_MAXIMIZEBOX | WS_THICKFRAME));
/* center the window with correct dimensions */
int x = (WindowRect.left != -32000) ? WindowRect.left : (This->mode.dmPelsWidth / 2) - (This->render.width / 2);
int y = (WindowRect.top != -32000) ? WindowRect.top : (This->mode.dmPelsHeight / 2) - (This->render.height / 2);
RECT dst = { x, y, This->render.width + x, This->render.height + y };
AdjustWindowRect(&dst, GetWindowLong(This->hWnd, GWL_STYLE), FALSE);
real_SetWindowPos(ddraw->hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
real_MoveWindow(This->hWnd, dst.left, dst.top, (dst.right - dst.left), (dst.bottom - dst.top), TRUE);
if (This->renderer == render_d3d9_main)
InitDirect3D9();
if (lockMouse)
mouse_lock();
}
else
{
if (This->renderer == render_d3d9_main)
InitDirect3D9();
if (!Direct3D9Active && ChangeDisplaySettings(&This->render.mode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
This->render.run = FALSE;
return DDERR_INVALIDMODE;
}
if (ddraw->wine)
real_SetWindowLongA(This->hWnd, GWL_STYLE, GetWindowLong(This->hWnd, GWL_STYLE) | WS_MINIMIZEBOX);
real_SetWindowPos(This->hWnd, HWND_TOPMOST, 0, 0, This->render.width, This->render.height, SWP_SHOWWINDOW);
LastSetWindowPosTick = timeGetTime();
mouse_lock();
}
if(This->render.viewport.x != 0 || This->render.viewport.y != 0)
{
RedrawWindow(This->hWnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
}
if(This->render.thread == NULL)
{
InterlockedExchange(&ddraw->render.paletteUpdated, TRUE);
InterlockedExchange(&ddraw->render.surfaceUpdated, TRUE);
ReleaseSemaphore(ddraw->render.sem, 1, NULL);
This->render.thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)This->renderer, NULL, 0, NULL);
}
return DD_OK;
}
HRESULT __stdcall ddraw_SetDisplayMode2(IDirectDrawImpl *This, DWORD width, DWORD height, DWORD bpp, DWORD refreshRate, DWORD flags)
{
printf("DirectDraw::SetDisplayMode2(refreshRate=%d, flags=%d)\n", (unsigned int)refreshRate, (unsigned int)flags);
return ddraw_SetDisplayMode(This, width, height, bpp);
}
void ToggleFullscreen()
{
if (ddraw->bnetActive)
return;
if (ddraw->windowed)
{
mouse_unlock();
WindowState = ddraw->windowed = FALSE;
real_SetWindowLongA(ddraw->hWnd, GWL_STYLE, GetWindowLong(ddraw->hWnd, GWL_STYLE) & ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU));
ddraw->altenter = TRUE;
ddraw_SetDisplayMode(ddraw, ddraw->width, ddraw->height, ddraw->bpp);
UpdateBnetPos(0, 0);
mouse_lock();
}
else
{
mouse_unlock();
WindowState = ddraw->windowed = TRUE;
if (Direct3D9Active)
Direct3D9_Reset();
else
ChangeDisplaySettings(&ddraw->mode, ddraw->bnetActive ? CDS_FULLSCREEN : 0);
ddraw_SetDisplayMode(ddraw, ddraw->width, ddraw->height, ddraw->bpp);
mouse_lock();
}
}
BOOL UnadjustWindowRectEx(LPRECT prc, DWORD dwStyle, BOOL fMenu, DWORD dwExStyle)
{
RECT rc;
SetRectEmpty(&rc);
BOOL fRc = AdjustWindowRectEx(&rc, dwStyle, fMenu, dwExStyle);
if (fRc)
{
prc->left -= rc.left;
prc->top -= rc.top;
prc->right -= rc.right;