forked from dawaga/ErectusExperimental
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErectusD3D9.cpp
302 lines (261 loc) · 7.09 KB
/
ErectusD3D9.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
#include "ErectusInclude.h"
D3DPRESENT_PARAMETERS D3D9Parameters;
LPDIRECT3D9 D3D9Interface = NULL;
LPDIRECT3DDEVICE9 D3D9Device = NULL;
LPD3DXSPRITE D3DXSprite = NULL;
LPD3DXFONT D3DXFont = NULL;
bool D3D9InterfaceCreated = false;
bool D3D9DeviceCreated = false;
bool D3DXSpriteCreated = false;
bool D3DXFontCreated = false;
bool D3D9Initialized = false;
bool DeviceResetQueued = false;
bool DeviceResetState = false;
int DeviceResetCounter = 0;
bool D3D9DrawText(const char *Text, bool Shadowed, bool Centered, float *Position, float *Color, float Alpha)
{
if (Text == nullptr)
{
return false;
}
if (Alpha <= 0.0f)
{
return false;
}
DWORD Format = DT_NOCLIP;
if (Centered) Format |= DT_CENTER;
LONG LongPosition[2] = { LONG(Position[0]), LONG(Position[1]) };
if (Shadowed)
{
RECT Background = { LongPosition[0] + 1L, LongPosition[1] + 1L, LongPosition[0] + 1L, LongPosition[1] + 1L };
D3DXFont->DrawText(D3DXSprite, Text, -1, &Background, Format, D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.0f, Alpha));
}
RECT Foreground = { LongPosition[0], LongPosition[1], LongPosition[0], LongPosition[1] };
D3DXFont->DrawText(D3DXSprite, Text, -1, &Foreground, Format, D3DCOLOR_COLORVALUE(Color[0], Color[1], Color[2], Alpha));
return true;
}
void D3D9Reset(HRESULT DeviceState)
{
if (DeviceState == D3DERR_DEVICELOST)
{
DeviceResetCounter++;
if (DeviceResetCounter > 600)
{
DeviceResetCounter = 0;
Close();
}
}
else
{
MoveWindow(WindowHwnd, WindowPosition[0], WindowPosition[1], WindowSize[0], WindowSize[1], FALSE);
D3D9Parameters.BackBufferWidth = WindowSize[0];
D3D9Parameters.BackBufferHeight = WindowSize[1];
if (DeviceResetState == false)
{
DeviceResetState = true;
DeviceResetCounter = 0;
D3DXSprite->OnLostDevice();
D3DXFont->OnLostDevice();
ImGui_ImplDX9_InvalidateDeviceObjects();
}
if (D3D9Device->Reset(&D3D9Parameters) == D3D_OK)
{
DeviceResetState = false;
DeviceResetQueued = false;
D3DXSprite->OnResetDevice();
D3DXFont->OnResetDevice();
ImGui_ImplDX9_CreateDeviceObjects();
}
MoveWindow(WindowHwnd, WindowPosition[0], WindowPosition[1], WindowSize[0], WindowSize[1], FALSE);
}
}
void D3D9Render()
{
auto Preframe = std::chrono::system_clock::now();
if (D3D9Initialized && ImGuiInitialized)
{
HRESULT DeviceState = D3D9Device->TestCooperativeLevel();
switch (DeviceState)
{
case D3D_OK:
D3D9Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.0f, 0.0f), 1.0f, 0);
D3D9Device->BeginScene();
if (ProcessMenuActive)
{
ProcessMenu();
}
else
{
if (OverlayMenuActive)
{
OverlayMenu();
}
else if (OverlayActive && OverlayForeground)
{
D3DXSprite->Begin(D3DXSPRITE_ALPHABLEND);
TargetLockingValid = false;
TargetLockingClosestDegrees = CustomTargetSettings.LockingFOV;
TargetLockingClosestPtr = 0;
RenderCustomEntityList();
RenderCustomNPCList();
RenderCustomPlayerList();
if (!TargetLockingValid)
{
if (TargetLockingPtr)
{
if (CustomTargetSettings.Retargeting)
{
TargetLockingCooldown = CustomTargetSettings.Cooldown;
}
else
{
TargetLockingCooldown = -1;
}
TargetLockingPtr = 0;
}
else if (TargetLockingClosestDegrees < CustomTargetSettings.LockingFOV)
{
TargetLockingCooldown = 0;
TargetLockingPtr = TargetLockingClosestPtr;
}
}
RenderData();
D3DXSprite->End();
}
}
D3D9Device->EndScene();
D3D9Device->Present(NULL, NULL, NULL, NULL);
break;
case D3DERR_DEVICENOTRESET:
DeviceResetQueued = true;
break;
case D3DERR_DEVICELOST:
DeviceResetQueued = true;
break;
default:
Close();
break;
}
if (ProcessSelected)
{
ProcessValidCounter++;
if (ProcessValidCounter > 20)
{
ProcessValidCounter = 0;
if (WaitForSingleObject(Handle, 0) != WAIT_TIMEOUT)
{
ResetProcessData(true, 1);
}
if (OverlayActive)
{
if (Hwnd == GetForegroundWindow())
{
OverlayForeground = true;
if (!SetOverlayPosition(true, true))
{
SetProcessMenu();
}
}
else
{
OverlayForeground = false;
if (!SetOverlayPosition(false, false))
{
SetProcessMenu();
}
}
}
}
if (!ThreadCreationState)
{
ThreadCreationState = CreateProcessThreads();
}
if (DoubleKeyPress(VK_CONTROL, VK_RETURN, &OverlayMenuPress))
{
if (OverlayMenuActive)
{
SetOverlayPosition(false, true);
}
else
{
SetOverlayMenu();
}
}
}
if (DeviceResetQueued)
{
D3D9Reset(DeviceState);
}
if (ThreadDestructionQueued)
{
if (!ThreadDestruction())
{
ThreadDestructionCounter++;
if (ThreadDestructionCounter > 900)
{
ThreadDestructionCounter = 0;
Close();
}
}
}
}
std::this_thread::sleep_until(Preframe + std::chrono::milliseconds(16));
}
bool D3D9Initialize()
{
ZeroMemory(&D3D9Parameters, sizeof(D3D9Parameters));
D3D9Parameters.BackBufferWidth = WindowSize[0];
D3D9Parameters.BackBufferHeight = WindowSize[1];
D3D9Parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
D3D9Parameters.BackBufferCount = 1;
D3D9Parameters.MultiSampleType = D3DMULTISAMPLE_NONE;
D3D9Parameters.MultiSampleQuality = 0;
D3D9Parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3D9Parameters.hDeviceWindow = WindowHwnd;
D3D9Parameters.Windowed = TRUE;
D3D9Parameters.EnableAutoDepthStencil = TRUE;
D3D9Parameters.AutoDepthStencilFormat = D3DFMT_D16;
D3D9Parameters.Flags = 0;
D3D9Parameters.FullScreen_RefreshRateInHz = 0;
D3D9Parameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
D3D9Interface = Direct3DCreate9(D3D_SDK_VERSION);
if (D3D9Interface == NULL) return false;
else D3D9InterfaceCreated = true;
HRESULT DeviceResult = D3D9Interface->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, 0, D3DCREATE_HARDWARE_VERTEXPROCESSING, &D3D9Parameters, &D3D9Device);
if (DeviceResult != D3D_OK) return false;
else D3D9DeviceCreated = true;
HRESULT SpriteResult = D3DXCreateSprite(D3D9Device, &D3DXSprite);
if (SpriteResult != D3D_OK) return false;
else D3DXSpriteCreated = true;
HRESULT FontResult = D3DXCreateFont(D3D9Device, 0, 0, 0, 0, FALSE, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "System", &D3DXFont);
if (FontResult != D3D_OK) return false;
else D3DXFontCreated = true;
return true;
}
void D3D9Cleanup()
{
if (D3D9InterfaceCreated)
{
D3D9InterfaceCreated = false;
D3D9Interface->Release();
D3D9Interface = NULL;
}
if (D3D9DeviceCreated)
{
D3D9DeviceCreated = false;
D3D9Device->Release();
D3D9Device = NULL;
}
if (D3DXSpriteCreated)
{
D3DXSpriteCreated = false;
D3DXSprite->Release();
D3DXSprite = NULL;
}
if (D3DXFontCreated)
{
D3DXFontCreated = false;
D3DXFont->Release();
D3DXFont = NULL;
}
}