metaforce/visigen/MainWin.cpp

152 lines
4.7 KiB
C++
Raw Permalink Normal View History

2017-02-25 07:59:37 +00:00
#include "VISIRenderer.hpp"
#include <Windows.h>
#include <WinUser.h>
#include <Shlwapi.h>
#include <strsafe.h>
#include "athena/Global.hpp"
#include "logvisor/logvisor.hpp"
#include <nowide/args.hpp>
#include <nowide/convert.hpp>
#include "../version.h"
2017-02-25 07:59:37 +00:00
#include <thread>
static logvisor::Module AthenaLog("Athena");
2019-07-28 01:21:31 +00:00
static void AthenaExc(athena::error::Level level, const char* /*file*/, const char*, int /*line*/,
fmt::string_view fmt, fmt::format_args args) {
AthenaLog.vreport(logvisor::Level(level), fmt, args);
2017-02-25 07:59:37 +00:00
}
static float s_Percent = 0.f;
static DWORD s_mainThreadId;
2018-12-08 05:30:43 +00:00
static void UpdatePercent(float percent) {
s_Percent = percent;
PostThreadMessage(s_mainThreadId, WM_USER + 1, 0, 0);
2017-02-25 07:59:37 +00:00
}
2017-02-26 02:43:27 +00:00
static const DWORD dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
static VISIRenderer* s_Renderer;
2017-02-25 07:59:37 +00:00
2018-12-08 05:30:43 +00:00
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
if (uMsg == WM_SIZING) {
RECT& dragRect = reinterpret_cast<RECT&>(lParam);
RECT tmpRect = dragRect;
tmpRect.bottom = tmpRect.top + 512;
tmpRect.right = tmpRect.left + 768;
AdjustWindowRect(&tmpRect, dwStyle, FALSE);
dragRect = tmpRect;
return TRUE;
} else if (uMsg == WM_CLOSE) {
s_Renderer->Terminate();
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
2017-02-25 07:59:37 +00:00
}
int main(int argc, char** argv) {
nowide::args _(argc, argv);
if (argc > 1 && !strcmp(argv[1], "--dlpackage")) {
2021-04-06 22:58:11 +00:00
fmt::print(FMT_STRING("{}\n"), METAFORCE_DLPACKAGE);
2018-12-08 05:30:43 +00:00
return 100;
}
logvisor::RegisterStandardExceptions();
logvisor::RegisterConsoleLogger();
atSetExceptionHandler(AthenaExc);
VISIRenderer renderer(argc, argv);
s_Renderer = &renderer;
int instIdx = -1;
if (argc > 3)
instIdx = atoi(argv[3]);
2018-12-08 05:30:43 +00:00
WNDCLASS wndClass = {CS_NOCLOSE, WindowProc, 0, 0, GetModuleHandle(nullptr), 0, 0, 0, 0, L"VISIGenWindow"};
RegisterClassW(&wndClass);
RECT clientRect = {0, 0, 768, 512};
AdjustWindowRect(&clientRect, dwStyle, FALSE);
int x = 0;
int y = 0;
if (instIdx != -1) {
x = (instIdx & 1) != 0;
y = (instIdx & 2) != 0;
}
HWND window = CreateWindowW(L"VISIGenWindow", L"VISIGen", dwStyle, x, y, clientRect.right - clientRect.left,
clientRect.bottom - clientRect.top, NULL, NULL, NULL, NULL);
PIXELFORMATDESCRIPTOR pfd = {sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL, // Flags
PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette.
32, // Colordepth of the framebuffer.
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
24, // Number of bits for the depthbuffer
8, // Number of bits for the stencilbuffer
0, // Number of Aux buffers in the framebuffer.
PFD_MAIN_PLANE,
0,
0,
0,
0};
HDC deviceContext = GetDC(window);
int pf = ChoosePixelFormat(deviceContext, &pfd);
SetPixelFormat(deviceContext, pf, &pfd);
HGLRC glContext = wglCreateContext(deviceContext);
ShowWindow(window, SW_SHOW);
s_mainThreadId = GetCurrentThreadId();
/* Spawn client thread */
std::thread clientThread([&]() {
wglMakeCurrent(deviceContext, glContext);
renderer.Run(UpdatePercent);
PostThreadMessage(s_mainThreadId, WM_USER, 0, 0);
});
/* Pump messages */
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0)) {
if (!msg.hwnd) {
/* PostThreadMessage events */
switch (msg.message) {
case WM_USER:
/* Quit message from client thread */
PostQuitMessage(0);
continue;
2019-07-28 01:21:31 +00:00
case WM_USER + 1: {
2018-12-08 05:30:43 +00:00
/* Update window title from client thread */
std::string title = fmt::format(FMT_STRING("VISIGen [{:g}%]"), s_Percent * 100.f);
SetWindowTextW(window, nowide::widen(title).c_str());
2018-12-08 05:30:43 +00:00
continue;
2019-07-28 01:21:31 +00:00
}
2018-12-08 05:30:43 +00:00
default:
break;
}
2017-02-25 07:59:37 +00:00
}
2018-12-08 05:30:43 +00:00
TranslateMessage(&msg);
DispatchMessage(&msg);
}
2017-02-25 07:59:37 +00:00
2018-12-08 05:30:43 +00:00
renderer.Terminate();
if (clientThread.joinable())
clientThread.join();
2017-02-25 07:59:37 +00:00
2018-12-08 05:30:43 +00:00
wglDeleteContext(glContext);
2017-02-25 07:59:37 +00:00
2018-12-08 05:30:43 +00:00
return renderer.ReturnVal();
2017-02-25 07:59:37 +00:00
}