This repository has been archived by the owner on Sep 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_dx9.cpp
457 lines (418 loc) · 15.9 KB
/
main_dx9.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
// ImGui - standalone example application for DirectX 9
// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
#include <imgui.h>
#include "imgui_impl_dx9.h"
#include "imgui_impl_dx9.cpp"
#include <d3d9.h>
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#include <tchar.h>
/////////////////////////////////////////////////////////////////////////////////
#include <boost/filesystem.hpp>
#include <Windows.h>
#include "../mod.h"
typedef ModInterface* (*TModCreate)();
typedef int(*TModFree)(ModInterface* context);
//typedef int(*TModUnInit)(ImGuiContext* context);
//typedef int(*TModRender)(ImGuiContext* context);
//typedef int(*TModInit)(ImGuiContext* context);
//typedef void(*TModTextureData)(int index, unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel);
//typedef bool(*TModGetTextureDirtyRect)(int index, int dindex, RECT* rect);
//typedef void(*TModSetTexture)(int index, void* texture);
//typedef int(*TModTextureBegin)();
//typedef void(*TModTextureEnd)();
//typedef bool(*TModUpdateFont)(ImGuiContext* context);
//typedef bool(*TModMenu)(bool* show);
TModCreate modCreate = nullptr;
TModFree modFree = nullptr;
//TModUnInit modUnInit = nullptr;
//TModRender modRender = nullptr;
//TModInit modInit = nullptr;
//TModTextureData modTextureData = nullptr;
//TModGetTextureDirtyRect modGetTextureDirtyRect = nullptr;
//TModSetTexture modSetTexture = nullptr;
//TModTextureBegin modTextureBegin = nullptr;
//TModTextureEnd modTextureEnd = nullptr;
//TModUpdateFont modUpdateFont = nullptr;
//TModMenu modMenu = nullptr;
HMODULE mod = nullptr;
ModInterface* modInterface = nullptr;
/////////////////////////////////////////////////////////////////////////////////
// Data
//static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
static D3DPRESENT_PARAMETERS g_d3dpp;
static std::vector<LPDIRECT3DTEXTURE9> g_ModTextures;
static bool ImGui_ImplDX9_CreateModTexture(int texindex)
{
if (!modInterface)
return false;
g_ModTextures.resize(std::max<size_t>(g_ModTextures.size(), texindex + 1));
auto& g_ModTexture = g_ModTextures.at(texindex);
if (LPDIRECT3DTEXTURE9 tex = g_ModTexture)
{
tex->Release();
if (modInterface)
modInterface->SetTexture(texindex, nullptr);
g_ModTexture = nullptr;
}
// Build texture atlas
ImGuiIO& io = ImGui::GetIO();
unsigned char* pixels;
int width, height, bytes_per_pixel;
modInterface->TextureData(texindex, &pixels, &width, &height, &bytes_per_pixel);
// Upload texture to graphics system
if (g_pd3dDevice->CreateTexture(width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_ModTexture, NULL) < 0)
return false;
D3DLOCKED_RECT tex_locked_rect;
if (g_ModTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK)
return false;
for (int y = 0; y < height; y++)
memcpy((unsigned char *)tex_locked_rect.pBits + tex_locked_rect.Pitch * y, pixels + (width * bytes_per_pixel) * y, (width * bytes_per_pixel));
g_ModTexture->UnlockRect(0);
if (modInterface)
modInterface->SetTexture(texindex, g_ModTexture);
return true;
}
bool ImGui_ImplDX9_CreateDeviceObjects_()
{
ImGui_ImplDX9_InvalidateDeviceObjects();
if (!g_pd3dDevice)
return false;
if (!ImGui_ImplDX9_CreateFontsTexture())
return false;
return true;
}
extern LRESULT ImGui_ImplDX9_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
IMGUI_API LRESULT ImGui_ImplDX9_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (ImGui::GetCurrentContext() == NULL)
return 0;
ImGuiIO& io = ImGui::GetIO();
switch (msg)
{
case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
{
int button = 0;
if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) button = 0;
if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) button = 1;
if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) button = 2;
if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL)
::SetCapture(hwnd);
io.MouseDown[button] = true;
return 0;
}
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
{
int button = 0;
if (msg == WM_LBUTTONUP) button = 0;
if (msg == WM_RBUTTONUP) button = 1;
if (msg == WM_MBUTTONUP) button = 2;
io.MouseDown[button] = false;
if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd)
::ReleaseCapture();
return 0;
}
case WM_MOUSEWHEEL:
io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
return 0;
case WM_MOUSEHWHEEL:
io.MouseWheelH += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
return 0;
case WM_MOUSEMOVE:
io.MousePos.x = (signed short)(lParam);
io.MousePos.y = (signed short)(lParam >> 16);
return 0;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (wParam < 256)
io.KeysDown[wParam] = 1;
return 0;
case WM_KEYUP:
case WM_SYSKEYUP:
if (wParam < 256)
io.KeysDown[wParam] = 0;
return 0;
case WM_CHAR:
// You can also use ToAscii()+GetKeyboardState() to retrieve characters.
if (wParam > 0 && wParam < 0x10000)
io.AddInputCharacter((unsigned short)wParam);
return 0;
case WM_SETCURSOR:
if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor())
return 1;
return 0;
}
return 0;
}
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (ImGui_ImplDX9_WndProcHandler(hWnd, msg, wParam, lParam))
return true;
switch (msg)
{
case WM_SIZE:
if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
{
ImGui_ImplDX9_InvalidateDeviceObjects();
int idx = 0;
for (auto& tex : g_ModTextures)
{
if (tex)
{
tex->Release();
if(modInterface)
modInterface->SetTexture(idx, nullptr);
++idx;
}
}
g_ModTextures.clear();
g_d3dpp.BackBufferWidth = LOWORD(lParam);
g_d3dpp.BackBufferHeight = HIWORD(lParam);
HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp);
if (hr == D3DERR_INVALIDCALL)
IM_ASSERT(0);
ImGui_ImplDX9_CreateDeviceObjects_();
}
return 0;
case WM_SYSCOMMAND:
if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
return 0;
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int main(int argc, char** argv)
{
/////////////////////////////////////////////////////////////////////////////////
wchar_t szPath[1024] = { 0, };
GetModuleFileNameW(NULL, szPath, 1023);
boost::filesystem::path p(szPath);
if (argc > 1)
{
if(boost::filesystem::exists(p.parent_path() / argv[1]))
p = p.parent_path() / argv[1];
else
p = argv[1];
}
else
{
p = p.parent_path() / "ActWebSocketImguiOverlay.dll";
}
mod = LoadLibraryW(p.wstring().c_str());
if (mod)
{
modCreate = (TModCreate)GetProcAddress(mod, "ModCreate");
modFree = (TModFree)GetProcAddress(mod, "ModFree");
if (modCreate && modFree) {
modInterface = modCreate();
}
//modInit = (TModInit)GetProcAddress(mod, "ModInit");
//modRender = (TModRender)GetProcAddress(mod, "ModRender");
//modUnInit = (TModUnInit)GetProcAddress(mod, "ModUnInit");
//modTextureData = (TModTextureData)GetProcAddress(mod, "ModTextureData");
//modSetTexture = (TModSetTexture)GetProcAddress(mod, "ModSetTexture");
//modGetTextureDirtyRect = (TModGetTextureDirtyRect)GetProcAddress(mod, "ModGetTextureDirtyRect");
//modTextureBegin = (TModTextureBegin)GetProcAddress(mod, "ModTextureBegin");
//modTextureEnd = (TModTextureEnd)GetProcAddress(mod, "ModTextureEnd");
//modUpdateFont = (TModUpdateFont)GetProcAddress(mod, "ModUpdateFont");
//modMenu = (TModMenu)GetProcAddress(mod, "ModMenu");
}
/////////////////////////////////////////////////////////////////////////////////
// Create application window
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, _T("ImGui Example"), NULL };
RegisterClassEx(&wc);
HWND hwnd = CreateWindow(_T("ImGui Example"), _T("ImGui DirectX9 Example"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
// Initialize Direct3D
LPDIRECT3D9 pD3D;
if ((pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{
UnregisterClass(_T("ImGui Example"), wc.hInstance);
return 0;
}
ZeroMemory(&g_d3dpp, sizeof(g_d3dpp));
g_d3dpp.Windowed = TRUE;
g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
g_d3dpp.EnableAutoDepthStencil = TRUE;
g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // Present with vsync
//g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Present without vsync, maximum unthrottled framerate
/////////////////////////////////////////////////////////////////////////////////
if (modInterface)
{
modInterface->Init(ImGui::GetCurrentContext());
}
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
if (modInterface)
{
modInterface->UnInit(ImGui::GetCurrentContext());
}
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
if (modInterface)
{
modInterface->Init(ImGui::GetCurrentContext());
}
/////////////////////////////////////////////////////////////////////////////////
// Create the D3DDevice
if (pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0)
{
pD3D->Release();
UnregisterClass(_T("ImGui Example"), wc.hInstance);
return 0;
}
// Load Fonts
// (there is a default font, this is only if you want to change it. see extra_fonts/README.txt for more details)
//ImGuiIO& io = ImGui::GetIO();
//io.Fonts->AddFontDefault();
//io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f);
//io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f);
//io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f);
//io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
// Setup Dear ImGui binding
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
ImGui_ImplDX9_Init(hwnd, g_pd3dDevice);
// Setup style
ImGui::StyleColorsDark();
//ImGui::StyleColorsClassic();
// Load Fonts
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Read 'misc/fonts/README.txt' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
//io.Fonts->AddFontDefault();
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != NULL);
bool show_demo_window = true;
bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
// Main loop
MSG msg;
ZeroMemory(&msg, sizeof(msg));
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
//io.IniFilename = nullptr;
//ImGui_ImplDX9_CreateDeviceObjects_();
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
continue;
}
if (modInterface)
{
if (modInterface->UpdateFont(ImGui::GetCurrentContext()))
{
if (g_FontTexture)
g_FontTexture->Release();
g_FontTexture = nullptr;
}
}
if (modInterface)
{
int textures = modInterface->TextureBegin();
for (int texidx = 0; texidx < textures; ++texidx)
{
if (texidx >= g_ModTextures.size() || !g_ModTextures[texidx]) {
ImGui_ImplDX9_CreateModTexture(texidx);
}
else
{
RECT rect;
int rectidx = 0;
int width, height, bits_per_pixel;
unsigned char *pixels;
if (modInterface->GetTextureDirtyRect(texidx, 0, &rect))
{
modInterface->TextureData(texidx, &pixels, &width, &height, &bits_per_pixel);
int rectidx = 0;
D3DLOCKED_RECT mod_atlas_rect;
HRESULT hr = S_OK;
auto tex = g_ModTextures[texidx];
if (SUCCEEDED(tex->LockRect(0, &mod_atlas_rect, nullptr, 0)))
{
if (mod_atlas_rect.pBits)
{
while (modInterface->GetTextureDirtyRect(texidx, rectidx++, &rect))
{
int bx = std::min<int>(rect.left, std::min<int>(rect.right, width - 1));
int by = std::min<int>(rect.top, std::min<int>(rect.bottom, height - 1));
int ex = std::max<int>(rect.left, std::min<int>(rect.right, width - 1));
int ey = std::max<int>(rect.top, std::min<int>(rect.bottom, height - 1));
for (int y = by; y <= ey; y++)
{
BYTE* mapped_data = static_cast<BYTE *>(mod_atlas_rect.pBits) + mod_atlas_rect.Pitch * y + bx *bits_per_pixel;
BYTE* data = pixels + (width * bits_per_pixel) * y + bx *bits_per_pixel;
const int size = (ex - bx + 1) * bits_per_pixel;
memcpy(mapped_data, data, size);
}
}
}
tex->UnlockRect(0);
}
}
}
}
modInterface->TextureEnd();
}
ImGui_ImplDX9_NewFrame();
// Rendering
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false);
D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_color.x*255.0f), (int)(clear_color.y*255.0f), (int)(clear_color.z*255.0f), (int)(clear_color.w*255.0f));
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0);
if (g_pd3dDevice->BeginScene() >= 0)
{
/////////////////////////////////////////////////////////////////////////////////
if (modInterface)
{
modInterface->Render(ImGui::GetCurrentContext());
}
/////////////////////////////////////////////////////////////////////////////////
ImGui::Render();
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
g_pd3dDevice->EndScene();
}
HRESULT result = g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
// Handle loss of D3D9 device
if (result == D3DERR_DEVICELOST && g_pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET)
{
ImGui_ImplDX9_InvalidateDeviceObjects();
g_pd3dDevice->Reset(&g_d3dpp);
ImGui_ImplDX9_CreateDeviceObjects();
}
}
ImGui_ImplDX9_Shutdown();
if (g_pd3dDevice) g_pd3dDevice->Release();
if (pD3D) pD3D->Release();
UnregisterClass(_T("ImGui Example"), wc.hInstance);
/////////////////////////////////////////////////////////////////////////////////
if (modInterface)
{
modInterface->UnInit(ImGui::GetCurrentContext());
}
/////////////////////////////////////////////////////////////////////////////////
ImGui::DestroyContext();
return 0;
}