Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "draco.h"
- #include "gui.h"
- int __stdcall wWinMain(HINSTANCE instance, HINSTANCE previousInstance, PWSTR arguments, int commandShow)
- {
- gui::createWindow("menu", "menu class");
- gui::createDevice();
- gui::createImGui();
- while (gui::exit)
- {
- gui::beginRender();
- gui::render();
- gui::endRender();
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- }
- gui::destroyImGui();
- gui::destroyDevice();
- gui::destroyWindow();
- return EXIT_SUCCESS;
- }
- #pragma once
- #include <d3d9.h>
- namespace gui
- {
- constexpr int WIDTH = 500;
- constexpr int HEIGHT = 300;
- inline bool exit = true;
- inline HWND window = nullptr;
- inline WNDCLASSEXA windowClass = {};
- // Position of window
- inline POINTS position = {};
- inline PDIRECT3D9 d3d = nullptr;
- inline LPDIRECT3DDEVICE9 device = nullptr;
- inline D3DPRESENT_PARAMETERS presentParameters = {};
- auto createWindow(
- const char* windowName,
- const char* className) noexcept -> void;
- auto destroyWindow() noexcept -> void;
- auto createDevice() noexcept -> bool;
- auto resetDevice() noexcept -> void;
- auto destroyDevice() noexcept -> void;
- auto createImGui() noexcept -> void;
- auto destroyImGui() noexcept -> void;
- auto beginRender() noexcept -> void;
- auto endRender() noexcept -> void;
- auto render() noexcept -> void;
- }
- #include "gui.h"
- #include <windows.h>
- #include "imgui/imgui.h"
- #include "imgui/imgui_impl_dx9.h"
- #include "imgui/imgui_impl_win32.h"
- extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND window, UINT message, WPARAM wparam, LPARAM lparam);
- LRESULT WINAPI WindowProcess(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
- {
- if (ImGui_ImplWin32_WndProcHandler(window, message, wparam, lparam))
- {
- return true;
- }
- switch (message)
- {
- // Called when window is resized
- case WM_SIZE:
- if (gui::device && wparam != SIZE_MINIMIZED)
- {
- gui::presentParameters.BackBufferWidth = LOWORD(lparam);
- gui::presentParameters.BackBufferHeight = HIWORD(lparam);
- gui::resetDevice();
- }
- return 0;
- case WM_SYSCOMMAND:
- if ((wparam & 0xfff0) == SC_KEYMENU)
- {
- return 0;
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- case WM_LBUTTONDOWN:
- gui::position = MAKEPOINTS(lparam);
- return 0;
- case WM_MOUSEMOVE:
- if (wparam == MK_LBUTTON)
- {
- const auto points = MAKEPOINTS(lparam);
- auto rect = ::RECT{};
- GetWindowRect(gui::window, &rect);
- rect.left += points.x - gui::position.x;
- rect.left += points.y - gui::position.y;
- if (gui::position.x >= 0 &&
- gui::position.x <= gui::WIDTH &&
- gui::position.y >= 0 && gui::position.y <= 19)
- {
- SetWindowPos(
- gui::window,
- HWND_TOPMOST,
- rect.left,
- rect.top,
- 0, 0,
- SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER
- );
- }
- }
- return 0;
- }
- }
- auto gui::createWindow(const char* windowName, const char* className) noexcept -> void
- {
- windowClass.cbSize = sizeof(WNDCLASSEXA);
- windowClass.style = CS_CLASSDC;
- windowClass.lpfnWndProc = WindowProcess;
- windowClass.cbClsExtra = 0;
- windowClass.cbWndExtra = 0;
- windowClass.hInstance = GetModuleHandleA(0);
- windowClass.hIcon = 0;
- windowClass.hbrBackground = 0;
- windowClass.lpszMenuName = 0;
- windowClass.lpszClassName = className;
- windowClass.hIconSm = 0;
- RegisterClassEx(&windowClass);
- window = CreateWindowEx(
- 0,
- className,
- windowName,
- WS_POPUP,
- 100,
- 100,
- WIDTH,
- HEIGHT,
- 0,
- 0,
- windowClass.hInstance,
- 0
- );
- ShowWindow(window, SW_SHOWDEFAULT);
- UpdateWindow(window);
- }
- auto gui::destroyWindow() noexcept -> void
- {
- DestroyWindow(window);
- UnregisterClass(windowClass.lpszClassName, windowClass.hInstance);
- }
- auto gui::createDevice() noexcept -> bool
- {
- d3d = Direct3DCreate9(D3D_SDK_VERSION);
- if (!d3d)
- {
- return false;
- }
- // Create space in memory
- ZeroMemory(&presentParameters, sizeof(presentParameters));
- presentParameters.Windowed = TRUE;
- presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
- presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
- presentParameters.EnableAutoDepthStencil = TRUE;
- presentParameters.AutoDepthStencilFormat = D3DFMT_D16;
- presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
- if (d3d->CreateDevice(
- D3DADAPTER_DEFAULT,
- D3DDEVTYPE_HAL,
- window,
- D3DCREATE_HARDWARE_VERTEXPROCESSING,
- &presentParameters,
- &device) < 0)
- {
- return false;
- }
- return true;
- }
- auto gui::resetDevice() noexcept -> void
- {
- ImGui_ImplDX9_InvalidateDeviceObjects();
- const auto result = device->Reset(&presentParameters);
- if (result == D3DERR_INVALIDCALL)
- {
- IM_ASSERT(0);
- }
- ImGui_ImplDX9_CreateDeviceObjects();
- }
- auto gui::destroyDevice() noexcept -> void
- {
- if (device)
- {
- device->Release();
- device = nullptr;
- }
- if (d3d)
- {
- d3d->Release();
- d3d = nullptr;
- }
- }
- auto gui::createImGui() noexcept -> void
- {
- IMGUI_CHECKVERSION();
- ImGui::CreateContext();
- ImGuiIO& io = ::ImGui::GetIO();
- io.IniFilename = NULL;
- ImGui::StyleColorsDark();
- ImGui_ImplWin32_Init(window);
- ImGui_ImplDX9_Init(device);
- }
- auto gui::destroyImGui() noexcept -> void
- {
- ImGui_ImplDX9_Shutdown();
- ImGui_ImplWin32_Shutdown();
- ImGui::DestroyContext();
- }
- auto gui::beginRender() noexcept -> void
- {
- MSG message;
- while (PeekMessage(&message, 0, 0, 0, PM_REMOVE))
- {
- TranslateMessage(&message);
- DispatchMessage(&message);
- }
- ImGui_ImplDX9_NewFrame();
- ImGui_ImplWin32_NewFrame();
- ImGui::NewFrame();
- }
- auto gui::endRender() noexcept -> void
- {
- ImGui::EndFrame();
- device->SetRenderState(D3DRS_ZENABLE, FALSE);
- device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
- device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
- device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 0, 255), 1.0f, 0);
- if (device->BeginScene() >= 0)
- {
- ImGui::Render();
- ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
- device->EndScene();
- }
- const auto result = device->Present(0, 0, 0, 0);
- if (result == D3DERR_DEVICELOST && device->TestCooperativeLevel() == D3DERR_DEVICENOTRESET)
- {
- resetDevice();
- }
- }
- auto gui::render() noexcept -> void
- {
- ImGui::SetNextWindowPos({ 0, 0 });
- ImGui::SetNextWindowSize({ WIDTH, HEIGHT });
- ImGui::Begin(
- "lol",
- &exit,
- ImGuiWindowFlags_NoResize |
- ImGuiWindowFlags_NoSavedSettings |
- ImGuiWindowFlags_NoCollapse |
- ImGuiWindowFlags_NoMove
- );
- ImGui::Button("enable");
- ImGui::End();
- }
Advertisement
Add Comment
Please, Sign In to add comment