Skip to content

Commit

Permalink
feat(ui): create window for win32 (#21)
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Gorez <[email protected]>
  • Loading branch information
tony-go authored Oct 29, 2024
1 parent 9993642 commit 3d16475
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 106 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ install(FILES
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
COMPONENT sourcemeta_native)

# TODO(tonygo): hide this in a native macro
install(FILES
# Install native functions like (native_add_app, native_set_profile)
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/native.cmake"
Expand Down
18 changes: 17 additions & 1 deletion cmake/native.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ endfunction()
#

function(_native_add_app_win32)
cmake_parse_arguments(NATIVE "" "TARGET;PLATFORM" "SOURCES" ${ARGN})
cmake_parse_arguments(NATIVE "" "TARGET;PLATFORM" "SOURCES;MODULES" ${ARGN})

add_executable(${NATIVE_TARGET} WIN32 ${NATIVE_SOURCES})

Expand All @@ -180,4 +180,20 @@ function(_native_set_profile_win32)
target_sources(${NATIVE_PROPERTIES_TARGET} PRIVATE
"${CMAKE_CURRENT_BINARY_DIR}/resource.rc"
)

# Iterate over the modules and link them
foreach(module IN LISTS NATIVE_PROPERTIES_MODULES)
_native_link_modules_win32(TARGET ${NATIVE_PROPERTIES_TARGET} MODULE ${module})
endforeach()
endfunction()

function(_native_link_modules_win32)
cmake_parse_arguments(NATIVE_MODULE "" "TARGET;MODULE" "" ${ARGN})

# Link the module to the target
if(${NATIVE_MODULE_MODULE} STREQUAL "ui/window")
target_link_libraries(${NATIVE_MODULE_TARGET} sourcemeta::native::window::win32)
else()
message(WARNING "Unknown module: ${NATIVE_MODULE_MODULE}")
endif()
endfunction()
3 changes: 2 additions & 1 deletion config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ list(APPEND COMPONENTS
"args_foundation")
elseif (WIN32)
list(APPEND COMPONENTS
"application_win32")
"application_win32"
"window_win32")
endif()

# Include the native.cmake file
Expand Down
7 changes: 0 additions & 7 deletions example/hello_world/hello_world.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include <sourcemeta/native/application.h>

#ifdef __APPLE__ // Temporary until window module is available for windows
#include <sourcemeta/native/window.h>
#endif

#include <exception>
#include <iostream>
Expand All @@ -14,20 +11,16 @@ class App : public sourcemeta::native::Application {
auto on_ready() -> void override {
std::cout << "Ready!" << std::endl;

#ifdef __APPLE__ // Temporary
window.size(800, 600);
window.show();
#endif

this->exit();
}

auto on_error(std::exception_ptr) noexcept -> void override {}

#ifdef __APPLE__ // Temporary
private:
sourcemeta::native::Window window;
#endif
};

NATIVE_RUN(App)
2 changes: 1 addition & 1 deletion src/application/win32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ noa_library(
VARIANT win32
FOLDER "Native/Application"
PRIVATE_HEADERS entry.h
SOURCES app_win32.cc delegate_win32.h delegate_win32.cc)
SOURCES app_win32.cc)

noa_library_install(
NAMESPACE sourcemeta
Expand Down
36 changes: 1 addition & 35 deletions src/application/win32/app_win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include <sourcemeta/native/application.h>

#include "delegate_win32.h"

#include <cassert>
#include <exception>
#include <iostream>
Expand All @@ -19,12 +17,7 @@ Application::Application() {
instance_ = this;
}

Application::~Application() {
if (internal_) {
// DestroyWindow(static_cast<HWND>(internal_));
}
instance_ = nullptr;
}
Application::~Application() { instance_ = nullptr; }

Application &Application::instance() {
assert(instance_);
Expand All @@ -44,33 +37,6 @@ auto Application::run() noexcept -> int {
this->on_error(std::current_exception());
}

const char CLASS_NAME[] = "NativeWin32ApplicationClass";

WNDCLASS wc = {};
wc.lpfnWndProc = AppDelegate::WindowProc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = CLASS_NAME;

if (!RegisterClass(&wc)) {
on_error(std::make_exception_ptr(
std::runtime_error("Failed to register window class")));
return EXIT_FAILURE;
}

HWND hwnd =
CreateWindowEx(0, CLASS_NAME, "Win32 Application", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, GetModuleHandle(NULL), NULL);

if (hwnd == NULL) {
on_error(
std::make_exception_ptr(std::runtime_error("Failed to create window")));
return EXIT_FAILURE;
}

internal_ = hwnd;
ShowWindow(hwnd, SW_SHOW);

MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
Expand Down
36 changes: 0 additions & 36 deletions src/application/win32/delegate_win32.cc

This file was deleted.

22 changes: 0 additions & 22 deletions src/application/win32/delegate_win32.h

This file was deleted.

4 changes: 1 addition & 3 deletions src/ui/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
if(APPLE)
add_subdirectory(window)
endif()
add_subdirectory(window)
2 changes: 2 additions & 0 deletions src/ui/window/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
if(APPLE)
add_subdirectory(appkit)
elseif(WIN32)
add_subdirectory(win32)
endif()
13 changes: 13 additions & 0 deletions src/ui/window/win32/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
noa_library(
NAMESPACE sourcemeta
PROJECT native
NAME window
VARIANT win32
FOLDER "Native/Ui/Window"
SOURCES window_win32.cc)

noa_library_install(
NAMESPACE sourcemeta
PROJECT native
NAME window
VARIANT win32)
41 changes: 41 additions & 0 deletions src/ui/window/win32/window_win32.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <Windows.h>
#include <sourcemeta/native/window.h>

namespace sourcemeta::native {

Window::Window() : internal_(nullptr) {
const char CLASS_NAME[] = "NativeWin32WindowClass";

WNDCLASS wc = {};
wc.lpfnWndProc = DefWindowProc; // Use default window procedure for now
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);

// TODO(tony): add parameter for title
HWND hwnd =
CreateWindowEx(0, CLASS_NAME, "Window Title", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
GetModuleHandle(NULL), NULL);

internal_ = hwnd;
}

Window::~Window() {
if (internal_) {
DestroyWindow(static_cast<HWND>(internal_));
}
}

auto Window::size(const unsigned int width, const unsigned int height) -> void {
SetWindowPos(static_cast<HWND>(internal_), NULL, 0, 0, // Ignore position
width, height, SWP_NOMOVE | SWP_NOZORDER);
}

auto Window::show() -> void {
ShowWindow(static_cast<HWND>(internal_), SW_SHOW);
}

} // namespace sourcemeta::native

0 comments on commit 3d16475

Please sign in to comment.