From a8a609907da6499fc33c44042f79f378d304d10b Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 23 Dec 2018 13:17:08 +0100 Subject: [PATCH] Add launcher --- premake5.lua | 2 +- src/launcher/launcher.cpp | 12 ++++++ src/launcher/launcher.hpp | 13 ++++++ src/launcher/window.cpp | 84 +++++++++++++++++++++++++++++++++++++++ src/launcher/window.hpp | 22 ++++++++++ src/main.cpp | 28 ++----------- src/resource.rc | 2 +- src/std_include.hpp | 5 ++- 8 files changed, 141 insertions(+), 27 deletions(-) create mode 100644 src/launcher/launcher.cpp create mode 100644 src/launcher/launcher.hpp create mode 100644 src/launcher/window.cpp create mode 100644 src/launcher/window.hpp diff --git a/premake5.lua b/premake5.lua index 4635b57..8793cb9 100644 --- a/premake5.lua +++ b/premake5.lua @@ -51,7 +51,7 @@ workspace "open-mw3" configuration {} project "open-mw3" - kind "SharedLib" + kind "WindowedApp" language "C++" pchheader "std_include.hpp" diff --git a/src/launcher/launcher.cpp b/src/launcher/launcher.cpp new file mode 100644 index 0000000..a32a083 --- /dev/null +++ b/src/launcher/launcher.cpp @@ -0,0 +1,12 @@ +#include +#include "launcher.hpp" + +launcher::launcher() : window_("Open-IW5", 300, 400) +{ + +} + +void launcher::run() const +{ + this->window_.run(); +} diff --git a/src/launcher/launcher.hpp b/src/launcher/launcher.hpp new file mode 100644 index 0000000..6dce2af --- /dev/null +++ b/src/launcher/launcher.hpp @@ -0,0 +1,13 @@ +#pragma once +#include "window.hpp" + +class launcher +{ +public: + launcher(); + + void run() const; + +private: + window window_; +}; diff --git a/src/launcher/window.cpp b/src/launcher/window.cpp new file mode 100644 index 0000000..23ded9a --- /dev/null +++ b/src/launcher/window.cpp @@ -0,0 +1,84 @@ +#include +#include "window.hpp" + +window::window(const std::string& title, int width, int height) +{ + const auto handle = GetModuleHandle(nullptr); + + ZeroMemory(&this->wc_, sizeof(this->wc_)); + + this->wc_.cbSize = sizeof(this->wc_); + this->wc_.style = CS_HREDRAW | CS_VREDRAW; + this->wc_.lpfnWndProc = window::static_processor; + this->wc_.hInstance = handle; + this->wc_.hCursor = LoadCursor(nullptr, IDC_ARROW); + this->wc_.hIcon = LoadIcon(handle, MAKEINTRESOURCE(102)); + this->wc_.hIconSm = this->wc_.hIcon; + this->wc_.hbrBackground = CreateSolidBrush(RGB(35, 35, 35));//HBRUSH(COLOR_WINDOW); + this->wc_.lpszClassName = L"omw3_window"; + RegisterClassEx(&this->wc_); + + const auto x = (GetSystemMetrics(SM_CXSCREEN) - width) / 2; + const auto y = (GetSystemMetrics(SM_CYSCREEN) - height) / 2; + + this->handle_ = CreateWindowExA(NULL, "omw3_window", title.data(), (WS_OVERLAPPEDWINDOW | WS_VISIBLE) & ~(WS_THICKFRAME | WS_MAXIMIZEBOX), x, y, width, + height, nullptr, nullptr, handle, nullptr); + + SetWindowLongPtrA(*this, GWLP_USERDATA, LONG_PTR(this)); +} + +window::~window() +{ + this->close(); + UnregisterClass(this->wc_.lpszClassName, this->wc_.hInstance); + DeleteObject(this->wc_.hbrBackground); +} + +void window::close() +{ + if (!this->handle_) return; + + SendMessageA(this->handle_, WM_KILL_WINDOW, NULL, NULL); + this->handle_ = nullptr; +} + +void window::run() const +{ + MSG msg; + while (this->handle_ && IsWindow(*this)) + { + if (PeekMessageA(&msg, nullptr, NULL, NULL, PM_REMOVE)) + { + TranslateMessage(&msg); + DispatchMessageA(&msg); + } + else + { + std::this_thread::sleep_for(1ms); + } + } +} + +LRESULT CALLBACK window::processor(UINT message, WPARAM w_param, LPARAM l_param) const +{ + if (message == WM_KILL_WINDOW) + { + DestroyWindow(*this); + return 0; + } + + return DefWindowProc(*this, message, w_param, l_param); +} + +LRESULT CALLBACK window::static_processor(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param) +{ + window* self = reinterpret_cast(GetWindowLongPtr(hwnd, GWLP_USERDATA)); + if (self) return self->processor(message, w_param, l_param); + return DefWindowProc(hwnd, message, w_param, l_param); +} + + +window::operator HWND() const +{ + return this->handle_; +} diff --git a/src/launcher/window.hpp b/src/launcher/window.hpp new file mode 100644 index 0000000..0b7cbc5 --- /dev/null +++ b/src/launcher/window.hpp @@ -0,0 +1,22 @@ +#pragma once + +#define WM_KILL_WINDOW (WM_USER+0) + +class window final +{ +public: + window(const std::string& title, int width, int height); + ~window(); + + void close(); + void run() const; + + operator HWND() const; + +private: + WNDCLASSEX wc_{}; + HWND handle_ = nullptr; + + LRESULT CALLBACK processor(UINT message, WPARAM w_param, LPARAM l_param) const; + static LRESULT CALLBACK static_processor(HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param); +}; diff --git a/src/main.cpp b/src/main.cpp index 65b5bd9..d07cc17 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,28 +1,8 @@ #include +#include "launcher/launcher.hpp" -namespace main +int CALLBACK WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nCmdShow*/) { - void initialize() - { - - } - - void uninitialize() - { - - } -} - -BOOL APIENTRY DllMain(HMODULE, DWORD call_reason, LPVOID) -{ - if (call_reason == DLL_PROCESS_ATTACH) - { - main::initialize(); - } - else if (call_reason == DLL_PROCESS_DETACH) - { - main::uninitialize(); - } - - return TRUE; + launcher launcher; + launcher.run(); } diff --git a/src/resource.rc b/src/resource.rc index 817486b..0119ffe 100644 --- a/src/resource.rc +++ b/src/resource.rc @@ -69,7 +69,7 @@ BEGIN VALUE "FileVersion", "1.0.0.0" VALUE "InternalName", "Open-IW5" VALUE "LegalCopyright", "All rights reserved." - VALUE "OriginalFilename", "oiw5.dll" + VALUE "OriginalFilename", "oiw5.exe" VALUE "ProductName", "oiw5" VALUE "ProductVersion", "1.0.0.0" END diff --git a/src/std_include.hpp b/src/std_include.hpp index 4438c81..91da9c3 100644 --- a/src/std_include.hpp +++ b/src/std_include.hpp @@ -7,7 +7,10 @@ #include #include #include +#include + +using namespace std::literals; #pragma warning(disable: 4100) -#include "steam\steam.hpp" +#include "steam/steam.hpp"