-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOmniScalerSystem.cpp
1344 lines (1185 loc) · 34.7 KB
/
OmniScalerSystem.cpp
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
////////////////////////////////////////////////////////////////////////////////
// Filename: OmniScalerSystem.cpp
////////////////////////////////////////////////////////////////////////////////
#include "OmniScalerSystem.h"
//global variables
int gAllowResize = 0;
int gIsActive = 0;
int isactive = 0;
int gModeWidth = 824;
int gScreenWidth = 824;
int gScreenHeight = 618;
int gRealScreenWidth;
int gRealScreenHeight;
int gRealRefreshRate;
uint32_t frameStart;
int frameTime;
int frameDelay;
int gScreenBits;
WNDPROC origfunc = NULL;
int gLastUpdate = -1;
int gActiveCnt = 0;
int gSmooth = 0;
int gAltWinPos = 0;
int gGDI = 0;
STARTUPINFO si;
PROCESS_INFORMATION pi;
HWND gHwnd = NULL;
TCHAR appClassName[MAX_PATH];
WNDCLASSEX wc;
HWND d3dHwnd = NULL;
HANDLE appHandle;
int tex_wq = 5274;
int tex_hq = 2637;
size_t bitmap_buffer_size = tex_wq * tex_hq;
BYTE* lpPixels = new BYTE[bitmap_buffer_size];
BYTE *bitmap_buffer;
UINT_PTR IDT_TIMER1;
DWORD lpExitCode = 0;
DWORD currExitCode = 0;
HHOOK kb_hhook = NULL;
int glinitvar = 0;
BOOL gHandleKbExit = FALSE;
LPCWSTR g_szClassName[] = { L"d3dWindowClass" };
//RESHADE INIT
static int reshadeinit = -1;
WORD reshadeInitKey = VK_HOME;
int reshadeInitDur = 1000;
//MOUSE AND MOUSE LIMIT
HCURSOR ttc;
int* mouseCursorBmpData;
int mcursor_w = 32;
int mcursor_h = 32;
float mcursor_u;
float mcursor_v;
int mcursor_wq = 256;
int mcursor_hq = 256;
RECT r;
RECT windowRect;
POINT mousePos;
float mousePosRemap_xscale;
float mousePosRemap_yscale;
float mousePosRemap_xoffset;
float mousePosRemap_yoffset;
int mousePointer_size = 32;
float initIntegerScale;
float mouseCursorSize;
float gScreenDynWidth;
float gScreenDynHeight;
float rDynOffset;
float rDynYOffset;
float rScreenWnd;
float rScreenReal;
float rScreenWndScale;
float gHwnd_winwidth;
float gHwnd_winheight;
RECT mouse_area;
BOOL gScreenResized = FALSE;
int gWndTick = 0;
int fullscr_delay = 0;
int alttab_delay = 0;
BOOL isMinimised = FALSE;
//INI VALS
BOOL integerScaling = FALSE;
float integerScalingOverride = 1.0f;
BOOL IntegerScalingOverscan = FALSE;
BOOL linearInterpolation = FALSE;
BOOL modeLauncher = TRUE;
BOOL isFullscreen = TRUE;
BOOL isWindowed = FALSE;
BOOL isStretched = FALSE;
BOOL MousePointerEnable = FALSE;
BOOL MousePointerLinear = FALSE;
BOOL MousePointerBig = FALSE;
wchar_t argv[10000];
float topOffset = 0.0f;
float bottomOffset = 0.0f;
float leftOffset = 0.0f;
float rightOffset = 0.0f;
//INI VALS
//GAME PROCESS
HANDLE hProcess_Game;
DWORD processId_Game;
OmniScalerSystem::OmniScalerSystem()
{
m_Input = 0;
m_Graphics = 0;
}
OmniScalerSystem::OmniScalerSystem(const OmniScalerSystem& other)
{
}
OmniScalerSystem::~OmniScalerSystem()
{
}
bool OmniScalerSystem::InitScaler(HINSTANCE& m_hInstance, int& argCount, LPWSTR& szArgList, wchar_t *CommandLineW)
{
bool result;
TCHAR currDir[1000];
TCHAR GamePath[1000];
boost::filesystem::path GameDir;
boost::filesystem::path LauncherExe;
boost::filesystem::path GameExe;
ZeroMemory(&GameDir, sizeof(GameDir));
ZeroMemory(&GameExe, sizeof(GameExe));
ZeroMemory(&GamePath, sizeof(GamePath));
// mouse area and window area
RECT gsr;
RECT gsr_win;
POINT lefttop;
POINT rightbottom;
GetCurrentDirectory(1000, currDir);
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USEPOSITION;
si.dwX = 0;
si.dwY = 0;
ZeroMemory(&pi, sizeof(pi));
MSG tmsg = { 0 };
TranslateMessage(&tmsg);
ptree pt;
srand(time(NULL));
try
{
std:string str;
ZeroMemory(&str, sizeof(str));
ini_parser::read_ini("omniscaler.ini", pt);
if ((pt.get<std::uint16_t>("Fullscreen.Enabled") != 0))
{
topOffset = (pt.get<std::float_t>("Fullscreen.offset_top"));
bottomOffset = (pt.get<std::float_t>("Fullscreen.offset_bottom"));
//leftOffset = (pt.get<std::float_t>("Fullscreen.offset_left"));
//rightOffset = (pt.get<std::float_t>("Fullscreen.offset_right"));
integerScaling = bool(pt.get<std::uint16_t>("Fullscreen.IntegerScaling"));
integerScalingOverride = (pt.get<std::float_t>("Fullscreen.integerScalingOverride"));
IntegerScalingOverscan = bool(pt.get<std::uint16_t>("Fullscreen.IntegerScalingOverscan"));
linearInterpolation = bool(pt.get<std::uint16_t>("Fullscreen.LinearFilter"));
MousePointerEnable = bool(pt.get<std::uint16_t>("Fullscreen.MousePointerEnable"));
MousePointerLinear = bool(pt.get<std::uint16_t>("Fullscreen.MousePointerLinear"));
MousePointerBig = bool(pt.get<std::uint16_t>("Fullscreen.MousePointerBig"));
isFullscreen = bool(1 - pt.get<std::uint16_t>("Fullscreen.StartWindowed"));
isWindowed = bool(pt.get<std::uint16_t>("Fullscreen.StartWindowed"));
isStretched = bool(pt.get<std::uint16_t>("Fullscreen.Stretched"));
}
modeLauncher = bool( pt.get<std::string>("Fullscreen.LauncherExe").size() > 0 ? 1 : 0);
str = pt.get<std::string>("Fullscreen.GameDir");
if (str.size() > 0)
{
std::wstring widestr = std::wstring(str.begin(), str.end());
GameDir = { widestr.c_str() };
ZeroMemory(&str, sizeof(str));
}
else
{
GameDir = { L"." };
}
/*
check if game starts with launcher
if not, set game exe path directly:
*/
if (!modeLauncher)
{
str = pt.get<std::string>("Fullscreen.GameExe");
if (str.size() > 0)
{
std::wstring widestr = std::wstring(str.begin(), str.end());
GameExe = { widestr.c_str() };
ZeroMemory(&str, sizeof(str));
}
if (GameExe.size() > 0 && GameDir.size() > 0)
{
wcscat_s(GamePath, L"\"");
wcscat_s(GamePath, GameDir.c_str());
wcscat_s(GamePath, L"\\");
wcscat_s(GamePath, GameExe.c_str());
wcscat_s(GamePath, L"\"");
//SetCurrentDirectory(GameDir.c_str());
//Exe Arguments Parse: Use the Custom Command Line Parser
wcscpy_s(argv, GamePath);
}
}
/*
check if game starts with launcher
if so, set launcher exe in game path:
*/
else
{
str = pt.get<std::string>("Fullscreen.GameExe");
if (str.size() > 0)
{
std::wstring widestr = std::wstring(str.begin(), str.end());
GameExe = { widestr.c_str() };
ZeroMemory(&str, sizeof(str));
}
str = pt.get<std::string>("Fullscreen.LauncherExe");
if (str.size() > 0)
{
std::wstring widestr = std::wstring(str.begin(), str.end());
LauncherExe = { widestr.c_str() };
ZeroMemory(&str, sizeof(str));
}
if (LauncherExe.size() > 0 && GameDir.size() > 0)
{
wcscat_s(GamePath, L"\"");
wcscat_s(GamePath, GameDir.c_str());
wcscat_s(GamePath, L"\\");
wcscat_s(GamePath, LauncherExe.c_str());
wcscat_s(GamePath, L"\"");
//SetCurrentDirectory(GameDir.c_str());
//Exe Arguments Parse: Use the Custom Command Line Parser
wcscpy_s(argv, GamePath);
}
}
}
catch (std::exception &ex)
{
std::cerr << ex.what() << std::endl;
}
//CommandLineW
wcscat_s(argv, L" ");
wcscat_s(argv, CommandLineW);
//for (int i = 0; i < argCount; i++)
//{
// wcscat_s(argv, L" ");
// wcscat_s(argv, L"\"");
// wcscat_s(argv, &szArgList[i]);
// wcscat_s(argv, L"\"");
//}
SetEnvironmentVariable(L"__COMPAT_LAYER", L"HighDpiAware");
//Exe Arguments Parse: Add the Command Line Arguments
//to List controls.
if (!CreateProcess(
NULL, // No module name (use command line)
argv, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
GameDir.c_str(), // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
return 1;
}
CloseHandle(pi.hThread);
//SetCurrentDirectory(currDir);
Sleep(2000);
//BlockInput(TRUE);
if (!modeLauncher)
{
gHwnd = FindTargetWindow(pi.dwProcessId);
}
else
{
while (!gHwnd)
{
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
//convert wchar_t to const char*
_bstr_t b(entry.szExeFile);
const char* c = b;
//convert wchar_t to const char*
_bstr_t gec(GameExe.c_str());
const char* GameExeChar = gec;
if (stricmp(c, GameExeChar) == 0)
{
gHwnd = find_main_window(entry.th32ProcessID);
hProcess_Game = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
}
}
}
}
}
if (gHwnd != NULL)
{
if (!modeLauncher)
{
SetFocus(gHwnd);
SetForegroundWindow(gHwnd);
SetActiveWindow(gHwnd);
PostMessage(gHwnd, WM_ACTIVATE, 1, 0);
appHandle = OpenProcess(PROCESS_ALL_ACCESS, false, pi.dwProcessId);
}
else
{
SetFocus(gHwnd);
SetForegroundWindow(gHwnd);
SetActiveWindow(gHwnd);
PostMessage(gHwnd, WM_ACTIVATE, 1, 0);
appHandle = hProcess_Game;
}
GetClientRect(gHwnd, &gsr);
GetWindowRect(gHwnd, &gsr_win);
lefttop = { gsr.left, gsr.top }; // Practicaly both are 0
ClientToScreen(gHwnd, &lefttop);
rightbottom = { gsr.right, gsr.bottom };
ClientToScreen(gHwnd, &rightbottom);
// Limit mouse to client area on game window
mouse_area.top = lefttop.y;// +(lefttop.y - gsr_win.top);
mouse_area.bottom = rightbottom.y;// -(gsr_win.bottom - rightbottom.y);
mouse_area.left = lefttop.x;
mouse_area.right = rightbottom.x;
InitWindow(m_hInstance, d3dHwnd, gHwnd);
// Create the input object. This object will be used to handle reading the keyboard input from the user.
kb_hhook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)LowLevelKeyboardProc, 0, 0);
//m_Input = new OmniScalerInput;
//if (!m_Input)
//{
// return false;
//}
//// Initialize the input object.
//m_Input->Initialize();
//custom mouse pointer
//mouseCursorBmpData array buffer size based on texel dimensions
gHwnd_winwidth = (float)mouse_area.right - (float)mouse_area.left;
gHwnd_winheight = (float)mouse_area.bottom - (float)mouse_area.top;
gScreenWidth = gsr.right;
gScreenHeight = gsr.bottom;
mcursor_wq = mcursor_w * 4;
mcursor_hq = mcursor_h * 4;
int mouseCursorBmpData_size = (mcursor_wq) * (mcursor_hq);
mouseCursorBmpData = new int[mouseCursorBmpData_size];
memset(mouseCursorBmpData, 0, sizeof(int) * mcursor_wq * mcursor_hq);
// u, v of mouse pointer
mcursor_u = (float)gScreenWidth / (float)mcursor_w;
mcursor_v = (float)gScreenHeight / (float)mcursor_h;
//scale mouse co-ordinates from game res to screen res
rScreenWnd = ((gHwnd_winwidth) / gHwnd_winheight);// / ((float)gRealScreenWidth / (float)gRealScreenHeight);
rScreenReal = ((float)gRealScreenWidth / (float)gRealScreenHeight);// / ((gHwnd_winwidth) / gHwnd_winheight);
rScreenWndScale = ((float)gRealScreenHeight / (float)gScreenHeight);
//mousePointer_size = mcursor_w*mousePosRemap_xscale;
////Fill mouseCursorBmpData array with bitmap data
//for (int x = 0; x < mousePointer_size; x++) {
// for (int y = 0; y < mousePointer_size; y++) {
// mouseCursorBmpData[x + y * mousePointer_size] = (0) | (255 << 8) | (0 << 16) | (1 << 24); //R | G | B | A
// }
//}
getMouseCursorBitmap(mouseCursorBmpData);
if (!MousePointerBig)
{
mousePosRemap_xscale = 1.0f;
mousePosRemap_yscale = 1.0f;
mousePosRemap_xoffset = floor((float)gRealScreenWidth / (float)gScreenWidth);
mousePosRemap_yoffset = floor((float)gRealScreenHeight / (float)gScreenHeight);
}
else
{
if (((float)gRealScreenWidth / (float)gScreenWidth) > 2.1)
{
mousePosRemap_xscale = ceil(((float)gRealScreenWidth / (float)gScreenWidth) / 2.0f);
mousePosRemap_yscale = ceil(((float)gRealScreenHeight / (float)gScreenHeight) / 2.0f);
}
else
{
mousePosRemap_xscale = ceil(((float)gRealScreenWidth / (float)gScreenWidth));
mousePosRemap_yscale = ceil(((float)gRealScreenHeight / (float)gScreenHeight));
}
mousePosRemap_xoffset = 1.0f;
mousePosRemap_yoffset = 1.0f;
}
// size based on scaling mouse pointer
mouseCursorSize = ((float)(mousePointer_size) * (mousePosRemap_xscale));
// scaled width value of window
gScreenDynWidth = rScreenWnd * (float)gRealScreenHeight;
// x-axis offset float value for centering window if aspect ratio is narrower than monitor
rDynOffset = (gScreenDynWidth / (float)gRealScreenWidth) * -1.0f;
// init integerscale val
initIntegerScale = 1.0f;
if (integerScaling)
{
initIntegerScale = floor((float)gRealScreenHeight / (float)gScreenHeight) * integerScalingOverride;
if (IntegerScalingOverscan)
initIntegerScale = ceil((float)gRealScreenHeight / (float)gScreenHeight) * integerScalingOverride;
gScreenDynWidth = (float)gScreenWidth * initIntegerScale;
gScreenDynHeight = (float)gScreenHeight * initIntegerScale;
rDynOffset = (gScreenDynWidth / (float)gRealScreenWidth) * -1.0f;
rDynYOffset = (gScreenDynHeight / (float)gRealScreenHeight);
}
// Create the graphics object. This object will handle rendering all the graphics for this application.
m_Graphics = new OmniScalerGraphics;
if (!m_Graphics)
{
return false;
}
// Initialize the graphics object.
result = m_Graphics->Initialize(d3dHwnd, gHwnd, appHandle, gRealScreenWidth, gRealScreenHeight, gRealRefreshRate, gScreenWidth, gScreenHeight, topOffset, bottomOffset, leftOffset, rightOffset, linearInterpolation, MousePointerLinear, integerScaling, integerScalingOverride, IntegerScalingOverscan, mouseCursorBmpData, mousePosRemap_xscale, mousePosRemap_yscale, mousePointer_size, mouse_area, MousePointerEnable);
if (!result)
{
return false;
}
SetActiveWindow(gHwnd);
if (!d3dHwnd)
{
return false;
}
// set framerate delay
//frameDelay = 1000 / (gRealRefreshRate * 1.5);
//if (gRealRefreshRate > 119)
//{
frameDelay = 1000 / (gRealRefreshRate * 1.5);
//}
if (modeLauncher)
{
/*
Workaround for Teknoparrot
*/
_bstr_t geTest(LauncherExe.c_str());
const char* GameExeTest = geTest;
if (stricmp("TeknoParrotUi.exe", GameExeTest) == 0)
{
PROCESSENTRY32 TeknoParrotEntry;
TeknoParrotEntry.dwSize = sizeof(PROCESSENTRY32);
HANDLE TeknoParrotSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(TeknoParrotSnapshot, &TeknoParrotEntry) == TRUE)
{
while (Process32Next(TeknoParrotSnapshot, &TeknoParrotEntry) == TRUE)
{
//convert wchar_t to const char*
_bstr_t b(TeknoParrotEntry.szExeFile);
const char* c = b;
//convert wchar_t to const char*
_bstr_t gec(GameExe.c_str());
const char* GameExeChar = gec;
if (stricmp(c, "OpenParrotLoader.exe") == 0)
{
HWND plHwnd = find_main_window(TeknoParrotEntry.th32ProcessID);
PostMessage(gHwnd, WM_ACTIVATE, 0, 0);
Sleep(100);
PostMessage(plHwnd, WM_ACTIVATE, 1, 0);
Sleep(100);
PostMessage(plHwnd, WM_ACTIVATE, 0, 0);
Sleep(100);
PostMessage(gHwnd, WM_ACTIVATE, 1, 0);
}
}
}
}
/*
END of Workaround for Teknoparrot
*/
else
{
PostMessage(gHwnd, WM_ACTIVATE, 1, 0);
}
}
}
return true;
}
void OmniScalerSystem::MainLoop()
{
MSG msg;
bool done, result;
// Initialize the message structure.
ZeroMemory(&msg, sizeof(MSG));
// Loop until there is a quit message from the window or the user.
done = false;
while (!done)
{
// Framerate start
frameStart = GetTickCount();
// Handle the windows messages.
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// If windows signals to end the application then exit out.
if (msg.message == WM_QUIT)
{
done = true;
}
else
{
// Otherwise do the frame processing.
result = UpdateScreen();
if (!result)
{
done = true;
}
}
// Framerate delay
frameTime = GetTickCount() - frameStart;
if (frameDelay > frameTime)
{
Sleep(frameDelay - frameTime);
}
}
return;
}
bool OmniScalerSystem::UpdateScreen()
{
bool result;
//// Check if the user pressed escape and wants to exit the application.
//if (m_Input->IsKeyDown(VK_ESCAPE))
//{
// return false;
//}
// Check if game has closed
// exit if so
//if game exited by menu and not alt-f4, handle shutdown.
if (!gHandleKbExit)
{
if(!modeLauncher)
GetExitCodeProcess(pi.hProcess, &lpExitCode);
else
GetExitCodeProcess(hProcess_Game, &lpExitCode);
//TCHAR szBuffer[16] = { 0 };
//wsprintf(szBuffer, TEXT("%lu"), lpExitCode);
//MessageBox(0, szBuffer, L"d3dHwnd alpha value", MB_OK);
if (lpExitCode != STILL_ACTIVE)
{
ClipCursor(NULL);
ShowCursor(1);
// Close process and thread handles.
if (!modeLauncher)
CloseHandle(pi.hProcess);
else
CloseHandle(hProcess_Game);
//UnhookWindowsHookEx(kb_hhook);
PostMessage(d3dHwnd, WM_CLOSE, 0, 0);
return false;
}
}
/* Check if window was resized */
gWndTick++;
if (gWndTick > (83))
{
RECT gsr;
POINT rightbottom;
GetClientRect(gHwnd, &gsr);
rightbottom = { gsr.right, gsr.bottom };
if (gScreenWidth != rightbottom.x || gScreenHeight != (rightbottom.y - (int)topOffset - (int)bottomOffset))
{
gScreenResized = TRUE;
//TCHAR szBuffer[16] = { 0 };
//wsprintf(szBuffer, TEXT("%lu"), (int)gScreenResized);
//MessageBox(0, szBuffer, L"d3dHwnd alpha value", MB_OK);
}
gWndTick = 0;
}
GetCursorPos(&mousePos);
ScreenToClient(gHwnd, &mousePos);
// Client coordinates to screen coordinates
RECT gsr;
GetClientRect(gHwnd, &gsr);
float mouseX = mousePos.x;// *mousePosRemap_xscale;
float mouseY = mousePos.y;// *mousePosRemap_yscale;
/* re-init scaling if window resized */
if (gScreenResized)
{
m_Graphics->Initialize(d3dHwnd, gHwnd, appHandle, gRealScreenWidth, gRealScreenHeight, gRealRefreshRate, gScreenWidth, gScreenHeight, topOffset, bottomOffset, leftOffset, rightOffset, linearInterpolation, MousePointerLinear, integerScaling, integerScalingOverride, IntegerScalingOverscan, mouseCursorBmpData, mousePosRemap_xscale, mousePosRemap_yscale, mousePointer_size, mouse_area, MousePointerEnable);
gScreenResized = FALSE;
if (modeLauncher)
{
HWND prevHwnd = GetNextWindow(gHwnd, GW_HWNDNEXT);
SetFocus(prevHwnd);
SetForegroundWindow(prevHwnd);
SetActiveWindow(prevHwnd);
Sleep(50);
SetFocus(gHwnd);
SetForegroundWindow(gHwnd);
SetActiveWindow(gHwnd);
}
else
{
SetFocus(gHwnd);
SetForegroundWindow(gHwnd);
SetActiveWindow(gHwnd);
PostMessage(gHwnd, WM_ACTIVATE, 1, 0);
}
}
if (integerScaling)
{
// aspect offset for centering + mouse cursor + (mouse position multiplied by integer scale and 2) / window width
mouseX = rDynOffset + (mouseCursorSize / (float)gRealScreenWidth) + ((((float)(mousePos.x) * (2.0f)) * (initIntegerScale)) / (float)gRealScreenWidth);
mouseY = rDynYOffset - (mouseCursorSize / (float)gRealScreenHeight) - ((((float)(mousePos.y) * (2.0f)) * (initIntegerScale)) / (float)gRealScreenHeight);
}
else
{
// Calculate the X and Y pixel position on the screen to start drawing to.
if (rScreenWnd < rScreenReal)
{
// aspect offset for centering + mouse cursor + (mouse position multiplied by monitor to window scale ratio and 2) / window width
mouseX = rDynOffset + (mouseCursorSize / (float)gRealScreenWidth) + (((float)(mousePos.x * (2.0f)) * rScreenWndScale) / (float)gRealScreenWidth);
mouseY = 1.0f - (mouseCursorSize / (float)gRealScreenHeight) - (((float)(mousePos.y) * (2.0f)) / (float)gScreenHeight);
}
else
{
mouseX = -1.0f + (mouseCursorSize / (float)gRealScreenWidth) + (((float)(mousePos.x) * (2.0f)) / (float)gScreenWidth);
mouseY = 1.0f - (mouseCursorSize / (float)gRealScreenHeight) - (((float)(mousePos.y) * (2.0f)) / (float)gScreenHeight);
}
}
// Do the frame processing for the graphics object.
result = m_Graphics->UpdateScreen(
integerScaling,
linearInterpolation,
mouseX,
mouseY,
mousePosRemap_xscale,
mousePosRemap_yscale,
gsr,
gHwnd,
gRealScreenWidth,
gRealScreenHeight,
gScreenWidth,
gScreenHeight,
mcursor_w,
MousePointerEnable
);
if (!result)
{
return false;
}
return true;
}
LRESULT CALLBACK OmniScalerSystem::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
{
switch (umsg)
{
// // Check if a key has been pressed on the keyboard.
//case WM_KEYDOWN:
//{
// // If a key is pressed send it to the input object so it can record that state.
// m_Input->KeyDown((unsigned int)wparam);
// return 0;
//}
//// Check if a key has been released on the keyboard.
//case WM_KEYUP:
//{
// // If a key is released then send it to the input object so it can unset the state for that key.
// m_Input->KeyUp((unsigned int)wparam);
// return 0;
//}
// Any other messages send to the default message handler as our application won't make use of them.
default:
{
return DefWindowProc(hwnd, umsg, wparam, lparam);
}
}
}
void OmniScalerSystem::InitWindow(HINSTANCE d_hInstance, HWND& d3dHwnd, HWND gHwnd)
{
//LONG hInstanceL = GetWindowLongA(
// NULL, GWL_HINSTANCE);
//HINSTANCE hInstance = (HINSTANCE)hInstanceL;
// Get an external pointer to this object.
ApplicationHandle = this;
// Give the application a name.
m_applicationName = L"OMNISCALER";
//LONG hInstanceL = GetWindowLongA(
// NULL, GWL_HINSTANCE);
//m_hinstance = GetModuleHandle(NULL);
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = oscWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = d_hInstance;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = CreateSolidBrush(0x00000000);
wc.lpszMenuName = NULL;
wc.lpszClassName = *g_szClassName;
wc.hIconSm = NULL;
RegisterClassEx(&wc);
// next up: work area for primary display
SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
// best: full screen
MONITORINFO mon;
mon.cbSize = sizeof(mon);
if (GetMonitorInfo(MonitorFromWindow(gHwnd, MONITOR_DEFAULTTOPRIMARY), &mon))
{
r = mon.rcMonitor;
}
gRealScreenWidth = r.right;
gRealScreenHeight = r.bottom;
gAllowResize = 1;
d3dHwnd = CreateWindowEx(
WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
*g_szClassName,
m_applicationName,
WS_POPUP | WS_VISIBLE | WS_SYSMENU,
//NULL,
0,
0,
gRealScreenWidth,
gRealScreenHeight,
gHwnd,
NULL,
d_hInstance,
NULL);
//keep layered window opaque
SetLayeredWindowAttributes(d3dHwnd, 0, 255, LWA_ALPHA);
SetWindowPos(d3dHwnd, HWND_NOTOPMOST, 0, 0, 0,
0, WS_EX_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
UpdateWindow(d3dHwnd);
ShowWindow(d3dHwnd, SW_SHOW);
SetForegroundWindow(gHwnd);
SetActiveWindow(gHwnd);
ClipCursor(&mouse_area);
ShowCursor(0);
return;
}
void toggleFullscreen()
{
if (isWindowed)
{
//ShowWindow(d3dHwnd, SW_MINIMIZE);
isWindowed = bool(1);
isFullscreen = bool(0);
//isMinimised = bool(0);
SetLayeredWindowAttributes(d3dHwnd, 0, 0, LWA_ALPHA);
++fullscr_delay;
HWND desktopHwnd = GetDesktopWindow();
SetFocus(desktopHwnd);
SetForegroundWindow(desktopHwnd);
SetActiveWindow(desktopHwnd);
Sleep(50);
SetFocus(gHwnd);
SetForegroundWindow(gHwnd);
SetActiveWindow(gHwnd);
ClipCursor(NULL);
ShowCursor(1);
return;
}
{
//ShowWindow(d3dHwnd, SW_RESTORE);
isWindowed = bool(0);
isFullscreen = bool(1);
//isMinimised = bool(0);
SetLayeredWindowAttributes(d3dHwnd, 0, 255, LWA_ALPHA);
fullscr_delay = 0;
//make sure d3d window is at top
SetWindowPos(d3dHwnd, NULL, 0, 0, 0,
0, WS_EX_NOACTIVATE | WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST);
//SetWindowPos(d3dHwnd, HWND_NOTOPMOST, 0, 0, 0,
// 0, SWP_NOMOVE | SWP_NOSIZE);
SetActiveWindow(gHwnd);
//Sleep(50);
//make game window active
//SetFocus(d3dHwnd);
//SetForegroundWindow(d3dHwnd);
//SetActiveWindow(d3dHwnd);
ClipCursor(&mouse_area);
ShowCursor(0);
return;
}
}
LRESULT CALLBACK oscWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
int tick = GetTickCount();
HWND ActiveHwnd;
if (gLastUpdate == -1)
{
gLastUpdate = tick;
}
if ((tick - gLastUpdate) > (30) && reshadeinit == 1)
{
BYTE hwndAlpa;
GetLayeredWindowAttributes(hwnd, NULL, &hwndAlpa, NULL);
ActiveHwnd = GetActiveWindow();
//TCHAR szBuffer[16] = { 0 };
//wsprintf(szBuffer, TEXT("%lu"), (int)hwndAlpa);
//MessageBox(0, szBuffer, L"d3dHwnd alpha value", MB_OK);
if ((int)hwndAlpa == 255 && !isFullscreen && isWindowed && !isMinimised)
{
//PostMessage(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
SetLayeredWindowAttributes(hwnd, 0, 0, LWA_ALPHA);
ClipCursor(NULL);
ShowCursor(1);
isactive = 1;
}
else
//minimise ogl view when game minimised, restore when game active in view, hwnd is d3dHwnd
if (ActiveHwnd == gHwnd && (int)hwndAlpa == 0 && isFullscreen && !isWindowed )
{
//PostMessage(hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
ClipCursor(&mouse_area);
ShowCursor(0);
isactive = 1;
SetActiveWindow(gHwnd);
}
else
if ((int)hwndAlpa == 255 && ActiveHwnd != gHwnd && isFullscreen && !isWindowed)
{
//PostMessage(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
SetLayeredWindowAttributes(hwnd, 0, 0, LWA_ALPHA);
ClipCursor(NULL);
ShowCursor(1);
isactive = 0;
}
else
if ((int)hwndAlpa == 255)
{
ClipCursor(&mouse_area);
//ShowCursor(0);
SetActiveWindow(gHwnd);
}
else
if (ActiveHwnd != gHwnd && isFullscreen)
{
{
SetFocus(gHwnd);
SetForegroundWindow(gHwnd);
SetActiveWindow(gHwnd);
PostMessage(gHwnd, WM_ACTIVATE, 1, 0);
}
}
}
//Pause processing so that reshade does not crash
if (reshadeinit == -1)
{
BlockInput(FALSE);
//ShowCursor(1);
{
//make d3d window topmost and unable to take focus
SetWindowPos(hwnd, NULL, 0, 0, 0,
0, WS_EX_NOACTIVATE | WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST);
reshadeinit = 1;
//make game window active
SetFocus(gHwnd);
SetForegroundWindow(gHwnd);
SetActiveWindow(gHwnd);
PostMessage(gHwnd, WM_ACTIVATE, 1, 0);
}
//ClipCursor(&mouse_area);
//ShowCursor(0);
}
switch (msg)
{
case WM_CREATE:
{
ttc = LoadCursor(NULL, IDC_ARROW);
SetCursor(ttc);
}
case WM_SIZE:
switch (wParam)
{
case SIZE_MINIMIZED:
{
gIsActive = 0;
}
case SIZE_RESTORED:
{
gIsActive = 1;
}
}
break;
case WM_MOUSEMOVE:
case WM_MOUSEACTIVATE:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_RBUTTONDOWN: