Abstracted Window
+ Pure Virtual Window class + Win32 Impl * TODO: do something about the while(window->PumpMessages())
This commit is contained in:
@@ -1,29 +1,41 @@
|
||||
set(current_target blank_slate)
|
||||
set(current_target starter_window)
|
||||
|
||||
set(SOURCE_FILES)
|
||||
set(STARTER_WINDOW_SRC
|
||||
main.cpp
|
||||
window.h
|
||||
)
|
||||
source_group("" FILES ${STARTER_WINDOW_SRC})
|
||||
|
||||
set(PLATFORM_SRC
|
||||
platform/win32_window.cpp
|
||||
)
|
||||
source_group(platform FILES ${PLATFORM_SRC})
|
||||
|
||||
list(APPEND SOURCE_FILES ${PLATFORM_SRC})
|
||||
list(APPEND SOURCE_FILES ${STARTER_WINDOW_SRC})
|
||||
|
||||
add_executable(
|
||||
${current_target}
|
||||
main.cpp
|
||||
${current_target}
|
||||
${SOURCE_FILES}
|
||||
)
|
||||
|
||||
find_package(spdlog CONFIG REQUIRED)
|
||||
target_link_libraries(${current_target} PRIVATE spdlog::spdlog spdlog::spdlog_header_only)
|
||||
|
||||
if( ENABLE_ALL_REASONABLE_WARNINGS )
|
||||
MESSAGE("Additional Warnings Enabled")
|
||||
MESSAGE("-- Additional Warnings Enabled")
|
||||
target_enable_warnings(${current_target})
|
||||
endif()
|
||||
|
||||
if( ENABLE_WARNINGS_AS_ERRORS )
|
||||
MESSAGE("Warnings as Errors")
|
||||
MESSAGE("-- Warnings as Errors")
|
||||
target_warnings_as_errors(${current_target})
|
||||
endif()
|
||||
|
||||
if( ENABLE_SANITIZERS )
|
||||
MESSAGE("Sanitizers Enabled")
|
||||
MESSAGE("-- Sanitizers Enabled")
|
||||
target_enable_sanitizers(${current_target})
|
||||
endif()
|
||||
|
||||
if( ENABLE_STATIC_ANALYSIS )
|
||||
MESSAGE("Static Analysis Enabled")
|
||||
MESSAGE("-- Static Analysis Enabled")
|
||||
target_enable_static_analysis(${current_target})
|
||||
endif()
|
||||
|
||||
18
src/main.cpp
18
src/main.cpp
@@ -1,8 +1,20 @@
|
||||
#include <spdlog/spdlog.h>
|
||||
#include "window.h"
|
||||
|
||||
constexpr int CW_USEDEFAULT = 0x80000000;
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
spdlog::trace("Hello, World!");
|
||||
return 0;
|
||||
auto window = swCreateWindow({
|
||||
.x = CW_USEDEFAULT,
|
||||
.y = CW_USEDEFAULT,
|
||||
.width = CW_USEDEFAULT,
|
||||
.height = CW_USEDEFAULT,
|
||||
.name = "Starter Window"
|
||||
});
|
||||
|
||||
while (window->PumpMessages())
|
||||
{
|
||||
//do something
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
123
src/platform/win32_window.cpp
Normal file
123
src/platform/win32_window.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#include "../window.h"
|
||||
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
static LRESULT CALLBACK WindowProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
switch (message)
|
||||
{
|
||||
case WM_DESTROY:
|
||||
{
|
||||
PostQuitMessage(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_PAINT:
|
||||
{
|
||||
PAINTSTRUCT ps = {};
|
||||
HDC hdc = BeginPaint(window, &ps);
|
||||
FillRect(hdc, &ps.rcPaint, reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1));
|
||||
EndPaint(window, &ps);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return DefWindowProc(window, message, wParam, lParam);
|
||||
}
|
||||
|
||||
namespace starter_window
|
||||
{
|
||||
|
||||
class Win32WindowImpl : public Window
|
||||
{
|
||||
public:
|
||||
Win32WindowImpl();
|
||||
~Win32WindowImpl() override = default;
|
||||
|
||||
Win32WindowImpl(const Win32WindowImpl&) = delete;
|
||||
Win32WindowImpl& operator=(const Win32WindowImpl&) = delete;
|
||||
|
||||
bool init(WindowCreateParams params);
|
||||
bool PumpMessages() override;
|
||||
|
||||
HINSTANCE hInstance;
|
||||
HWND hWnd;
|
||||
};
|
||||
|
||||
Win32WindowImpl::Win32WindowImpl()
|
||||
: hInstance(GetModuleHandle(NULL))
|
||||
, hWnd(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
bool Win32WindowImpl::init(WindowCreateParams params)
|
||||
{
|
||||
const char className[] = "Win32WindowImpl";
|
||||
|
||||
WNDCLASSEX wc = {};
|
||||
|
||||
wc.cbSize = sizeof(WNDCLASSEX);
|
||||
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
|
||||
wc.lpfnWndProc = WindowProc;
|
||||
wc.hInstance = hInstance;
|
||||
wc.lpszClassName = className;
|
||||
|
||||
if (RegisterClassEx(&wc) == NULL)
|
||||
{
|
||||
MessageBox(nullptr, "Call to RegisterClass failed", NULL, MB_OK);
|
||||
return false;
|
||||
}
|
||||
|
||||
HWND window = CreateWindowEx(
|
||||
0,
|
||||
className,
|
||||
params.name,
|
||||
WS_OVERLAPPEDWINDOW,
|
||||
params.x, params.y, params.width, params.height,
|
||||
NULL,
|
||||
NULL,
|
||||
hInstance,
|
||||
NULL);
|
||||
|
||||
if (window == NULL)
|
||||
{
|
||||
MessageBox(nullptr, "Call to CreateWindow failed", NULL, MB_OK);
|
||||
return false;
|
||||
}
|
||||
|
||||
ShowWindow(window, SW_SHOW);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Win32WindowImpl::PumpMessages()
|
||||
{
|
||||
MSG message = {};
|
||||
if (GetMessage(&message, NULL, 0, 0) != 0)
|
||||
{
|
||||
TranslateMessage(&message);
|
||||
DispatchMessage(&message);
|
||||
return true;
|
||||
}
|
||||
|
||||
// GetMessage returned WM_QUIT
|
||||
return false;
|
||||
}
|
||||
} // namespace starter_window
|
||||
|
||||
std::unique_ptr<starter_window::Window> swCreateWindow(starter_window::WindowCreateParams params)
|
||||
{
|
||||
auto result = std::make_unique<starter_window::Win32WindowImpl>();
|
||||
if (result->init(params) == false)
|
||||
{
|
||||
result = nullptr;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
26
src/window.h
Normal file
26
src/window.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace starter_window
|
||||
{
|
||||
|
||||
struct WindowCreateParams
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
int width;
|
||||
int height;
|
||||
const char* name;
|
||||
};
|
||||
|
||||
class Window
|
||||
{
|
||||
public:
|
||||
virtual ~Window() = default;
|
||||
virtual bool PumpMessages() = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<starter_window::Window> swCreateWindow(starter_window::WindowCreateParams params);
|
||||
Reference in New Issue
Block a user