Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. // REMEMBER TO CHANGE CREATIVE FROM CREATIVEADDRESS TO GAMEMODE
  2. #include "GuiLoader.h"
  3.  
  4. #include <iostream>
  5. #include "./imgui/imgui.h"
  6. #include "./imgui/imgui_impl_dx9.h"
  7. #include "./imgui/imgui_impl_win32.h"
  8. #include <d3d9.h>
  9. #define DIRECTINPUT_VERSION 0x0800
  10. #include <dinput.h>
  11. #include <tchar.h>
  12.  
  13.  
  14. using namespace std;
  15.  
  16.  
  17. // Data
  18. static LPDIRECT3D9 g_pD3D = NULL;
  19. static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
  20. static D3DPRESENT_PARAMETERS g_d3dpp = {};
  21.  
  22. // Forward declarations of helper functions
  23. bool CreateDeviceD3D(HWND hWnd);
  24. void CleanupDeviceD3D();
  25. void ResetDevice();
  26.  
  27. //void ResetDevice();
  28. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  29.  
  30. // Main code
  31. GuiLoader::GuiLoader() {
  32.  
  33.  
  34. // Create application window
  35. WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("Minecraft.Windows.exe"), NULL };
  36. ::RegisterClassEx(&wc);
  37.  
  38. HWND hwnd = CreateWindowEx(WS_EX_TOPMOST | WS_EX_LAYERED, wc.lpszClassName, L"ImGui", WS_POPUP, 0, 0, 1920, 1080, NULL, NULL, wc.hInstance, NULL);
  39. SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 255, LWA_COLORKEY | LWA_ALPHA);
  40.  
  41. // Initialize Direct3D
  42. if (!CreateDeviceD3D(hwnd))
  43. {
  44. CleanupDeviceD3D();
  45. ::UnregisterClass(wc.lpszClassName, wc.hInstance);
  46.  
  47. }
  48.  
  49. // Show the window
  50. ::ShowWindow(hwnd, SW_SHOWDEFAULT);
  51. ::UpdateWindow(hwnd);
  52.  
  53. // Setup Dear ImGui context
  54. IMGUI_CHECKVERSION();
  55. ImGui::CreateContext();
  56. ImGuiIO& io = ImGui::GetIO(); (void)io;
  57.  
  58. // Setup Dear ImGui style
  59. ImGui::StyleColorsDark();
  60. //ImGui::StyleColorsClassic();
  61.  
  62. // Setup Platform/Renderer bindings
  63. ImGui_ImplWin32_Init(hwnd);
  64. ImGui_ImplDX9_Init(g_pd3dDevice);
  65.  
  66. // Our state
  67. bool show_demo_window = false;
  68. bool show_another_window = false;
  69. ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  70.  
  71. // Main loop
  72. MSG msg;
  73. ZeroMemory(&msg, sizeof(msg));
  74. while (msg.message != WM_QUIT)
  75. {
  76.  
  77. if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  78. {
  79. ::TranslateMessage(&msg);
  80. ::DispatchMessage(&msg);
  81. continue;
  82. }
  83.  
  84.  
  85. // Start the Dear ImGui frame
  86. ImGui_ImplDX9_NewFrame();
  87. ImGui_ImplWin32_NewFrame();
  88. ImGui::NewFrame();
  89.  
  90. // 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!).
  91.  
  92. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
  93. {
  94.  
  95. ImGui::Begin("test");
  96. // ImGui::Checkbox("Gamemode", &EnableGM);
  97.  
  98.  
  99.  
  100. }
  101. // Rendering
  102. ImGui::EndFrame();
  103. g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
  104. g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
  105. g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false);
  106. 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));
  107. g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0);
  108. if (g_pd3dDevice->BeginScene() >= 0)
  109. {
  110. ImGui::Render();
  111. ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
  112. g_pd3dDevice->EndScene();
  113. }
  114. HRESULT result = g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
  115.  
  116. // Handle loss of D3D9 device
  117. if (result == D3DERR_DEVICELOST && g_pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET)
  118. ResetDevice();
  119. }
  120.  
  121.  
  122. ImGui_ImplDX9_Shutdown();
  123. ImGui_ImplWin32_Shutdown();
  124. ImGui::DestroyContext();
  125.  
  126. CleanupDeviceD3D();
  127. ::DestroyWindow(hwnd);
  128. ::UnregisterClass(wc.lpszClassName, wc.hInstance);
  129.  
  130. }
  131.  
  132.  
  133.  
  134. // He.lper functions
  135.  
  136. bool CreateDeviceD3D(HWND hWnd)
  137. {
  138. if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
  139. return false;
  140.  
  141.  
  142. ZeroMemory(&g_d3dpp, sizeof(g_d3dpp));
  143. g_d3dpp.Windowed = TRUE;
  144. g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  145. g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  146. g_d3dpp.EnableAutoDepthStencil = TRUE;
  147. g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  148. g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  149. if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0)
  150. return false;
  151.  
  152. return true;
  153. }
  154.  
  155. void CleanupDeviceD3D()
  156. {
  157. if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
  158. if (g_pD3D) { g_pD3D->Release(); g_pD3D = NULL; }
  159. }
  160.  
  161.  
  162. void ResetDevice()
  163. {
  164. ImGui_ImplDX9_InvalidateDeviceObjects();
  165. HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp);
  166. if (hr == D3DERR_INVALIDCALL)
  167. IM_ASSERT(0);
  168. ImGui_ImplDX9_CreateDeviceObjects();
  169. }
  170.  
  171. // Win32 message handler
  172. extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  173. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  174. {
  175. if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  176. return true;
  177.  
  178. switch (msg)
  179. {
  180. case WM_SIZE:
  181. if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
  182. {
  183. g_d3dpp.BackBufferWidth = LOWORD(lParam);
  184. g_d3dpp.BackBufferHeight = HIWORD(lParam);
  185. ResetDevice();
  186. }
  187. return 0;
  188. case WM_SYSCOMMAND:
  189. if ((wParam & 0xfff0) == SC_KEYMENU)
  190. return 0;
  191. break;
  192. case WM_DESTROY:
  193. ::PostQuitMessage(0);
  194. return 0;
  195. }
  196. return ::DefWindowProc(hWnd, msg, wParam, lParam);
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement