-
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.
feat: wrap win32 application properly into native::Application class (#…
…16) Signed-off-by: Tony Gorez <[email protected]>
- Loading branch information
Showing
14 changed files
with
221 additions
and
68 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
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
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
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
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
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,2 +1,5 @@ | ||
add_example(NAME hello_world TYPE desktop) | ||
add_example(NAME cli TYPE cli) | ||
|
||
if(APPLE) | ||
add_example(NAME cli TYPE cli) | ||
endif() |
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
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
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
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
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,67 +1,95 @@ | ||
#include <windows.h> | ||
#include <Windows.h> | ||
|
||
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); | ||
#include <sourcemeta/native/application.h> | ||
|
||
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, | ||
LPSTR lpCmdLine, int nCmdShow) { | ||
const char CLASS_NAME[] = "NativeApplication"; | ||
#include "delegate_win32.h" | ||
|
||
#include <cassert> | ||
#include <exception> | ||
#include <iostream> | ||
|
||
namespace { | ||
sourcemeta::native::Application *instance_{nullptr}; | ||
} | ||
|
||
namespace sourcemeta::native { | ||
|
||
Application::Application() { | ||
assert(!instance_); | ||
instance_ = this; | ||
} | ||
|
||
Application::~Application() { | ||
if (internal_) { | ||
// DestroyWindow(static_cast<HWND>(internal_)); | ||
} | ||
instance_ = nullptr; | ||
} | ||
|
||
Application &Application::instance() { | ||
assert(instance_); | ||
return *instance_; | ||
} | ||
|
||
auto Application::run() noexcept -> int { | ||
assert(!running_); | ||
running_ = true; | ||
|
||
on_start(); | ||
|
||
const char CLASS_NAME[] = "NativeWin32ApplicationClass"; | ||
|
||
WNDCLASS wc = {}; | ||
wc.lpfnWndProc = WindowProc; | ||
wc.hInstance = hInstance; | ||
wc.lpfnWndProc = AppDelegate::WindowProc; | ||
wc.hInstance = GetModuleHandle(NULL); | ||
wc.lpszClassName = CLASS_NAME; | ||
|
||
RegisterClass(&wc); | ||
if (!RegisterClass(&wc)) { | ||
on_error(std::make_exception_ptr( | ||
std::runtime_error("Failed to register window class"))); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// Create the window | ||
HWND hwnd = | ||
CreateWindowEx(0, // Optional window styles | ||
CLASS_NAME, // Window class | ||
"Your App Title", // Window text | ||
WS_OVERLAPPEDWINDOW, // Window style | ||
|
||
// Size and position | ||
CreateWindowEx(0, CLASS_NAME, "Win32 Application", WS_OVERLAPPEDWINDOW, | ||
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, | ||
|
||
NULL, // Parent window | ||
NULL, // Menu | ||
hInstance, // Instance handle | ||
NULL // Additional application data | ||
); | ||
NULL, NULL, GetModuleHandle(NULL), NULL); | ||
|
||
if (hwnd == NULL) { | ||
return 0; | ||
on_error( | ||
std::make_exception_ptr(std::runtime_error("Failed to create window"))); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
ShowWindow(hwnd, nCmdShow); | ||
internal_ = hwnd; | ||
ShowWindow(hwnd, SW_SHOW); | ||
|
||
// Run the message loop | ||
MSG msg = {}; | ||
while (GetMessage(&msg, NULL, 0, 0)) { | ||
TranslateMessage(&msg); | ||
DispatchMessage(&msg); | ||
} | ||
|
||
return 0; | ||
return static_cast<int>(msg.wParam); | ||
} | ||
|
||
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, | ||
LPARAM lParam) { | ||
switch (uMsg) { | ||
case WM_DESTROY: | ||
PostQuitMessage(0); | ||
return 0; | ||
|
||
case WM_PAINT: { | ||
PAINTSTRUCT ps; | ||
HDC hdc = BeginPaint(hwnd, &ps); | ||
|
||
// All painting occurs here, between BeginPaint and EndPaint. | ||
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1)); | ||
|
||
EndPaint(hwnd, &ps); | ||
} | ||
return 0; | ||
auto Application::on_error(std::exception_ptr error) -> void { | ||
try { | ||
if (error) | ||
std::rethrow_exception(error); | ||
} catch (const std::exception &error) { | ||
std::cerr << "Error: " << error.what() << std::endl; | ||
} | ||
return DefWindowProc(hwnd, uMsg, wParam, lParam); | ||
#ifndef NDEBUG | ||
std::abort(); | ||
#else | ||
throw error; | ||
#endif | ||
} | ||
|
||
auto Application::exit(const int code) const noexcept -> void { | ||
assert(running_); | ||
PostQuitMessage(code); | ||
} | ||
|
||
} // namespace sourcemeta::native |
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,41 @@ | ||
#include <Windows.h> | ||
|
||
#include <sourcemeta/native/application.h> | ||
|
||
#include "delegate_win32.h" | ||
|
||
namespace sourcemeta::native { | ||
|
||
LRESULT CALLBACK AppDelegate::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, | ||
LPARAM lParam) { | ||
switch (uMsg) { | ||
case WM_CREATE: | ||
// try { | ||
ApplicationInternals::on_ready(); | ||
// } catch (...) { | ||
// ApplicationInternals::on_error(std::current_exception()); | ||
// } | ||
return 0; | ||
case WM_DESTROY: | ||
PostQuitMessage(0); | ||
return 0; | ||
case WM_PAINT: { | ||
PAINTSTRUCT ps; | ||
HDC hdc = BeginPaint(hwnd, &ps); | ||
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1)); | ||
EndPaint(hwnd, &ps); | ||
} | ||
return 0; | ||
} | ||
return DefWindowProc(hwnd, uMsg, wParam, lParam); | ||
} | ||
|
||
auto ApplicationInternals::on_ready() -> void { | ||
sourcemeta::native::Application::instance().on_ready(); | ||
} | ||
|
||
auto ApplicationInternals::on_error(std::exception_ptr error) noexcept -> void { | ||
sourcemeta::native::Application::instance().on_error(error); | ||
} | ||
|
||
} // namespace sourcemeta::native |
Oops, something went wrong.