-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
217 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
#include <stdexcept> | ||
#include <dwmapi.h> | ||
#include <iostream> | ||
#include "WindowManager.hpp" | ||
#include "backends/imgui_impl_win32.h" | ||
|
||
extern IMGUI_IMPL_API LRESULT | ||
ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); | ||
|
||
namespace Xash::GUI | ||
{ | ||
WindowManager::WindowManager(HINSTANCE instance) : hInstance(instance) | ||
{ | ||
try | ||
{ | ||
CreateWindowClass(); | ||
InitWindow(); | ||
InitIcon(); | ||
mDx11.Init(hWindow); | ||
ApplyDarkMode(); | ||
} | ||
catch (const std::exception &e) | ||
{ | ||
std::cerr << e.what() << std::endl; | ||
bIsRunning = false; | ||
} | ||
} | ||
|
||
WindowManager::~WindowManager() | ||
{ | ||
if (hIcon) | ||
{ | ||
DestroyIcon(hIcon); | ||
} | ||
if (hWindow) | ||
{ | ||
DestroyWindow(hWindow); | ||
} | ||
if (bIsWindowClassRegistered) | ||
{ | ||
UnregisterClassW(mWindowClass.lpszClassName, mWindowClass.hInstance); | ||
} | ||
} | ||
|
||
// PUBLIC METHODS | ||
|
||
bool WindowManager::IsRunning() const | ||
{ | ||
return bIsRunning; | ||
} | ||
|
||
void WindowManager::Display() | ||
{ | ||
mDx11.Clear(); | ||
mDx11.Display(); | ||
} | ||
|
||
void WindowManager::Update() | ||
{ | ||
HandleWindowMessages(); | ||
} | ||
|
||
// PRIVATE METHODS | ||
|
||
void WindowManager::HandleWindowMessages() | ||
{ | ||
MSG message = {0}; | ||
constexpr UINT msgFilterMin = 0; | ||
constexpr UINT msgFilterMax = 0; | ||
|
||
while (PeekMessageW(&message, nullptr, msgFilterMin, msgFilterMax, PM_REMOVE)) | ||
{ | ||
TranslateMessage(&message); | ||
DispatchMessageW(&message); | ||
if (message.message == WM_QUIT) | ||
{ | ||
bIsRunning = FALSE; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
LRESULT CALLBACK | ||
WindowManager::WindowProcedure(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) | ||
{ | ||
if (ImGui_ImplWin32_WndProcHandler(hwnd, uMsg, wParam, lParam)) | ||
{ | ||
return true; | ||
} | ||
if (uMsg == WM_DESTROY) | ||
{ | ||
PostQuitMessage(0); | ||
return 0; | ||
} | ||
return DefWindowProcW(hwnd, uMsg, wParam, lParam); | ||
} | ||
|
||
// INITIALIZATION METHODS | ||
|
||
bool WindowManager::InitWindow() | ||
{ | ||
hWindow = CreateWindowExW( | ||
WS_EX_ACCEPTFILES, mWindowClass.lpszClassName, WindowName, | ||
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, | ||
WindowWidth, WindowHeight, nullptr, nullptr, mWindowClass.hInstance, nullptr | ||
); | ||
if (!hWindow) | ||
{ | ||
throw std::runtime_error("Failed to create window"); | ||
} | ||
ShowWindow(hWindow, SW_SHOWDEFAULT); | ||
return true; | ||
} | ||
|
||
void WindowManager::InitIcon() | ||
{ | ||
constexpr int iconSize = 0; | ||
|
||
hIcon = (HICON)LoadImageW( | ||
hInstance, IconPath, IMAGE_ICON, iconSize, iconSize, | ||
LR_LOADFROMFILE | LR_DEFAULTSIZE | ||
); | ||
if (!hIcon) | ||
{ | ||
throw std::runtime_error("Failed to load icon"); | ||
} | ||
SendMessageW(hWindow, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(hIcon)); | ||
SendMessageW(hWindow, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(hIcon)); | ||
} | ||
|
||
void WindowManager::ApplyDarkMode() | ||
{ | ||
DwmSetWindowAttribute( | ||
hWindow, DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE, &bIsDarkMode, | ||
sizeof(bIsDarkMode) | ||
); | ||
} | ||
|
||
void WindowManager::CreateWindowClass() | ||
{ | ||
constexpr UINT windowClassStyle = 0; | ||
|
||
mWindowClass.cbSize = sizeof(WNDCLASSEXW); | ||
mWindowClass.style = windowClassStyle; | ||
mWindowClass.lpfnWndProc = WindowProcedure; | ||
mWindowClass.hInstance = hInstance; | ||
mWindowClass.hIcon = hIcon; | ||
mWindowClass.hIconSm = hIcon; | ||
mWindowClass.lpszClassName = WindowName; | ||
if (!RegisterClassExW(&mWindowClass)) | ||
{ | ||
throw std::runtime_error("Failed to register window class"); | ||
} | ||
bIsWindowClassRegistered = true; | ||
} | ||
|
||
} // namespace Xash::GUI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#pragma once | ||
|
||
#include <windows.h> | ||
#include "DirectX11.hpp" | ||
|
||
namespace Xash | ||
{ | ||
namespace GUI | ||
{ | ||
class WindowManager | ||
{ | ||
static constexpr LPCWSTR WindowName = L"Xash"; | ||
static constexpr UINT WindowWidth = 700; | ||
static constexpr UINT WindowHeight = 400; | ||
static constexpr LPCWSTR IconPath = L"assets\\icon.ico"; | ||
|
||
public: | ||
WindowManager(HINSTANCE hInstance); | ||
~WindowManager(); | ||
|
||
bool IsRunning() const; | ||
void Update(); | ||
void Display(); | ||
|
||
private: | ||
bool InitWindow(); | ||
void CreateWindowClass(); | ||
void InitIcon(); | ||
void ApplyDarkMode(); | ||
void HandleWindowMessages(); | ||
|
||
static LRESULT CALLBACK | ||
WindowProcedure(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); | ||
|
||
HWND hWindow = nullptr; | ||
HICON hIcon = nullptr; | ||
HINSTANCE hInstance = nullptr; | ||
WNDCLASSEXW mWindowClass = {0}; | ||
|
||
DirectX11 mDx11; | ||
|
||
BOOL bIsDarkMode = TRUE; | ||
bool bIsRunning = true; | ||
bool bIsWindowClassRegistered = false; | ||
}; | ||
} // namespace GUI | ||
} // namespace Xash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
#include <windows.h> | ||
#include <iostream> | ||
#include "WindowManager.hpp" | ||
|
||
int WINAPI wWinMain( | ||
HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) | ||
int WINAPI | ||
wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) | ||
{ | ||
#ifndef NDEBUG | ||
std::wcout << L"Debug mode" << std::endl; | ||
#else | ||
std::wcout << L"Release mode" << std::endl; | ||
#endif | ||
UNREFERENCED_PARAMETER(hPrevInstance); | ||
UNREFERENCED_PARAMETER(pCmdLine); | ||
UNREFERENCED_PARAMETER(nCmdShow); | ||
|
||
Xash::GUI::WindowManager windowManager(hInstance); | ||
while (windowManager.IsRunning()) | ||
{ | ||
windowManager.Update(); | ||
windowManager.Display(); | ||
} | ||
return 0; | ||
} |