Advertisement
CorrM

Untitled

Dec 27th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.79 KB | None | 0 0
  1. #include "GameOverlay.h"
  2.  
  3. GameOverlay::GameOverlay()
  4. {
  5. }
  6. GameOverlay::~GameOverlay()
  7. {
  8. ImGui_ImplDX9_Shutdown();
  9. ImGui_ImplWin32_Shutdown();
  10. ImGui::DestroyContext();
  11.  
  12. if (g_pd3dDevice) g_pd3dDevice->Release();
  13. if (pD3D) pD3D->Release();
  14. DestroyWindow(OverWindow);
  15. UnregisterClass(ClassName.c_str(), wc.hInstance);
  16.  
  17. UnhookWindowsHookEx(hMouseHook);
  18. }
  19.  
  20. extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  21. LRESULT WINAPI GameOverlay::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  22. {
  23. if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  24. return true;
  25.  
  26. switch (msg)
  27. {
  28. case WM_SIZE:
  29. if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
  30. {
  31. ImGui_ImplDX9_InvalidateDeviceObjects();
  32. g_d3dpp.BackBufferWidth = LOWORD(lParam);
  33. g_d3dpp.BackBufferHeight = HIWORD(lParam);
  34. HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp);
  35. if (hr == D3DERR_INVALIDCALL)
  36. IM_ASSERT(0);
  37. ImGui_ImplDX9_CreateDeviceObjects();
  38. }
  39. return 0;
  40. case WM_SYSCOMMAND:
  41. if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
  42. return 0;
  43. break;
  44. case WM_DESTROY:
  45. PostQuitMessage(0);
  46. return 0;
  47. }
  48.  
  49. return DefWindowProc(hWnd, msg, wParam, lParam);
  50. }
  51. LRESULT CALLBACK GameOverlay::MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
  52. {
  53. GameOverlay *g = (GameOverlay*)GetWindowLongPtr(OverWindow, GWLP_USERDATA);
  54. if (g->IsOpen) // bool
  55. {
  56. ImGuiIO& io = ImGui::GetIO();
  57. MOUSEHOOKSTRUCT* pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
  58. if (pMouseStruct != NULL) {
  59. switch (wParam)
  60. {
  61. case WM_LBUTTONDOWN:
  62. io.MouseDown[0] = true;
  63. break;
  64. case WM_LBUTTONUP:
  65. io.MouseDown[0] = false;
  66. io.MouseReleased[0] = true;
  67. break;
  68. case WM_RBUTTONDOWN:
  69. io.MouseDown[1] = true;
  70. break;
  71. case WM_RBUTTONUP:
  72. io.MouseDown[1] = false;
  73. io.MouseReleased[1] = true;
  74. break;
  75. }
  76. }
  77. }
  78. return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
  79. }
  80.  
  81. bool GameOverlay::Inti(string ClassName, string WindowTitle)
  82. {
  83. if (_inti)
  84. return false;
  85.  
  86. this->ClassName = ClassName;
  87. this->WindowTitle = WindowTitle;
  88.  
  89. RECT rc;
  90. HWND newhwnd = FindWindowEx(FindWindow("TXGuiFoundation", NULL), NULL, "AEngineRenderWindowClass", NULL);
  91. if (newhwnd != NULL)
  92. {
  93. GetWindowRect(newhwnd, &rc);
  94. //s_width = rc.right - rc.left;
  95. //s_height = rc.bottom - rc.top;
  96. }
  97. else
  98. {
  99. //ExitProcess(0);
  100. }
  101.  
  102. // Create application window
  103. //wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, (HMODULE)GetModuleHandle(NULL), NULL, NULL, NULL, NULL, ClassName.c_str(), NULL };
  104. ZeroMemory(&wc, sizeof(WNDCLASSEX));
  105. wc.cbSize = sizeof(WNDCLASSEX);
  106. wc.style = CS_HREDRAW | CS_VREDRAW;
  107. wc.lpfnWndProc = WndProc;
  108. wc.hInstance = 0L;
  109. wc.hCursor = NULL;
  110. // wc.hbrBackground = (HBRUSH)RGB(0, 0, 0);
  111. wc.hbrBackground = NULL;
  112. wc.lpszClassName = ClassName.c_str();
  113. RegisterClassEx(&wc);
  114. OverWindow = CreateWindow(ClassName.c_str(), WindowTitle.c_str(), WS_EX_TOPMOST | WS_POPUP, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
  115.  
  116. SetWindowLong(OverWindow, GWL_EXSTYLE, (int)GetWindowLong(OverWindow, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT);
  117. SetLayeredWindowAttributes(OverWindow, RGB(0, 0, 0), 255, ULW_COLORKEY | LWA_ALPHA);
  118.  
  119. // Trick to get the class inside static funcs
  120. SetWindowLongPtr(OverWindow, GWLP_USERDATA, (LONG_PTR)this);
  121.  
  122. // Initialize Direct3D
  123. if ((pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
  124. {
  125. UnregisterClass(ClassName.c_str(), wc.hInstance);
  126. return false;
  127. }
  128. ZeroMemory(&g_d3dpp, sizeof(g_d3dpp));
  129. g_d3dpp.Windowed = TRUE;
  130. g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  131. g_d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
  132. g_d3dpp.EnableAutoDepthStencil = TRUE;
  133. g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  134. g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // Present with vsync
  135. //g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Present without vsync, maximum unthrottled framerate
  136.  
  137. // Create the D3DDevice
  138. if (pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, OverWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0)
  139. {
  140. pD3D->Release();
  141. UnregisterClass(ClassName.c_str(), wc.hInstance);
  142. return false;
  143. }
  144.  
  145. // Setup Dear ImGui context
  146. IMGUI_CHECKVERSION();
  147. ImGui::CreateContext();
  148. ImGuiIO& io = ImGui::GetIO();
  149. io.DeltaTime = 1.0f / 60.0f;
  150. //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
  151.  
  152. // Setup Dear ImGui style
  153. ImGui::StyleColorsDark();
  154. //ImGui::StyleColorsClassic();
  155.  
  156. // Setup Platform/Renderer bindings
  157. ImGui_ImplWin32_Init(OverWindow);
  158. ImGui_ImplDX9_Init(g_pd3dDevice);
  159.  
  160. // Load Fonts
  161. // - 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.
  162. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  163. // - 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).
  164. // - 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.
  165. // - Read 'misc/fonts/README.txt' for more instructions and details.
  166. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  167. //io.Fonts->AddFontDefault();
  168. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
  169. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
  170. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
  171. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
  172. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  173. //IM_ASSERT(font != NULL);
  174.  
  175. hMouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, 0, GetWindowThreadProcessId(OverWindow, NULL));
  176. return true;
  177. }
  178.  
  179. void GameOverlay::InternalRendering()
  180. {
  181. bool show_demo_window = true;
  182. bool show_another_window = false;
  183. ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 0.0f);
  184.  
  185. // Start the Dear ImGui frame
  186. ImGui_ImplDX9_NewFrame();
  187. ImGui_ImplWin32_NewFrame();
  188. ImGui::NewFrame();
  189.  
  190. // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
  191. if (show_demo_window)
  192. ImGui::ShowDemoWindow(&show_demo_window);
  193.  
  194. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
  195. {
  196. static float f = 0.0f;
  197. static int counter = 0;
  198.  
  199. ImGui::Begin(WindowTitle.c_str(), &IsOpen); // Create a window called "Hello, world!" and append into it.
  200.  
  201. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  202. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  203. ImGui::Checkbox("Another Window", &show_another_window);
  204.  
  205. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  206. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  207.  
  208. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  209. counter++;
  210. ImGui::SameLine();
  211. ImGui::Text("counter = %d", counter);
  212.  
  213. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  214. ImGui::End();
  215. }
  216.  
  217. // 3. Show another simple window.
  218. if (show_another_window)
  219. {
  220. ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
  221. ImGui::Text("Hello from another window!");
  222. if (ImGui::Button("Close Me"))
  223. show_another_window = false;
  224. ImGui::End();
  225. }
  226. }
  227.  
  228. void GameOverlay::Update()
  229. {
  230. ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 0.0f);
  231.  
  232. // Main loop
  233. MSG msg;
  234. ZeroMemory(&msg, sizeof(msg));
  235. while (msg.message != WM_QUIT)
  236. {
  237. // Poll and handle messages (inputs, window resize, etc.)
  238. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  239. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  240. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  241. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  242. if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  243. {
  244. TranslateMessage(&msg);
  245. DispatchMessage(&msg);
  246. continue;
  247. }
  248.  
  249. // Set Open State
  250. if (lastState != IsOpen)
  251. {
  252. lastState = IsOpen;
  253. UpdateOverlayState();
  254. }
  255.  
  256. InternalRendering();
  257.  
  258. // Rendering
  259. ImGui::EndFrame();
  260. g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
  261. g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
  262. g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false);
  263. 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));
  264. g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0);
  265. if (g_pd3dDevice->BeginScene() >= 0)
  266. {
  267. ImGui::Render();
  268. ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
  269. g_pd3dDevice->EndScene();
  270. }
  271. HRESULT result = g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
  272.  
  273. // Handle loss of D3D9 device
  274. if (result == D3DERR_DEVICELOST && g_pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET)
  275. {
  276. ImGui_ImplDX9_InvalidateDeviceObjects();
  277. g_pd3dDevice->Reset(&g_d3dpp);
  278. ImGui_ImplDX9_CreateDeviceObjects();
  279. }
  280. }
  281. }
  282.  
  283. void GameOverlay::Start()
  284. {
  285. if (_inti)
  286. return;
  287.  
  288. _inti = true;
  289.  
  290. ShowWindow(OverWindow, SW_SHOWDEFAULT);
  291. UpdateWindow(OverWindow);
  292.  
  293. // Start the updater
  294. auto UpdaterStart = [](LPVOID p) -> DWORD WINAPI { GameOverlay *t = (GameOverlay*)p; t->Update(); return 0; };
  295. CreateThread(NULL, NULL, UpdaterStart, this, NULL, NULL);
  296.  
  297. }
  298.  
  299. void GameOverlay::UpdateOverlayState()
  300. {
  301. //We call this function only when we open or close the menu
  302. long style = GetWindowLong(OverWindow, GWL_EXSTYLE);
  303. if (IsOpen)
  304. {
  305. style &= ~WS_EX_LAYERED;
  306. SetWindowLong(OverWindow, GWL_EXSTYLE, style);
  307. SetForegroundWindow(OverWindow);
  308. }
  309. else
  310. {
  311. style |= WS_EX_LAYERED;
  312. SetWindowLong(OverWindow, GWL_EXSTYLE, style);
  313. }
  314. //Credits go to someone from UC
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement