Initial commit

This commit is contained in:
William McVicar
2023-05-04 22:33:04 +02:00
committed by GitHub
commit 34126c21a2
22 changed files with 849 additions and 0 deletions

47
src/CMakeLists.txt Normal file
View File

@@ -0,0 +1,47 @@
set(current_target starter_window)
set(SOURCE_FILES)
set(STARTER_WINDOW_SRC
main.cpp
window.h
)
source_group("" FILES ${STARTER_WINDOW_SRC})
if(MSVC)
set(PLATFORM_SRC
platform/win32_window.cpp
)
else()
set(PLATFORM_SRC
platform/null_window.cpp
)
endif()
source_group(platform FILES ${PLATFORM_SRC})
list(APPEND SOURCE_FILES ${PLATFORM_SRC})
list(APPEND SOURCE_FILES ${STARTER_WINDOW_SRC})
add_executable(
${current_target}
${SOURCE_FILES}
)
if( ENABLE_ALL_REASONABLE_WARNINGS )
MESSAGE("-- Additional Warnings Enabled")
target_enable_warnings(${current_target})
endif()
if( ENABLE_WARNINGS_AS_ERRORS )
MESSAGE("-- Warnings as Errors")
target_warnings_as_errors(${current_target})
endif()
if( ENABLE_SANITIZERS )
MESSAGE("-- Sanitizers Enabled")
target_enable_sanitizers(${current_target})
endif()
if( ENABLE_STATIC_ANALYSIS )
MESSAGE("-- Static Analysis Enabled")
target_enable_static_analysis(${current_target})
endif()

20
src/main.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include "window.h"
constexpr int CW_USEDEFAULT = 0x80000000;
auto main() -> int
{
auto window = swCreateWindow({
.x = CW_USEDEFAULT,
.y = CW_USEDEFAULT,
.width = CW_USEDEFAULT,
.height = CW_USEDEFAULT,
.name = "Starter Window"
});
while (!window->ShouldClose())
{
window->PumpMessages();
}
}

View File

@@ -0,0 +1,28 @@
#include "../window.h"
namespace starter_window
{
class NullWindowImpl final : public Window
{
public:
NullWindowImpl() = default;
~NullWindowImpl() override = default;
NullWindowImpl(NullWindowImpl&&) = delete;
NullWindowImpl& operator=(NullWindowImpl&&) = delete;
NullWindowImpl(const NullWindowImpl&) = delete;
NullWindowImpl& operator=(const NullWindowImpl&) = delete;
void PumpMessages() override {}
bool ShouldClose() override { return false; }
};
} // namespace starter_window
std::unique_ptr<starter_window::Window> swCreateWindow(starter_window::WindowCreateParams)
{
auto result = std::make_unique<starter_window::NullWindowImpl>();
return result;
}

View File

@@ -0,0 +1,132 @@
#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 final : public Window
{
public:
Win32WindowImpl();
~Win32WindowImpl() override = default;
Win32WindowImpl(const Win32WindowImpl&) = delete;
Win32WindowImpl& operator=(const Win32WindowImpl&) = delete;
bool init(WindowCreateParams params);
void PumpMessages() override;
bool ShouldClose() override;
HINSTANCE hInstance;
HWND hWnd;
bool m_close;
};
Win32WindowImpl::Win32WindowImpl()
: hInstance(GetModuleHandle(NULL))
, hWnd(nullptr)
, m_close(false)
{
}
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;
}
void Win32WindowImpl::PumpMessages()
{
MSG message = {};
if (GetMessage(&message, NULL, 0, 0) != 0)
{
TranslateMessage(&message);
DispatchMessage(&message);
}
else
{
// GetMessage returned WM_QUIT
m_close = true;
}
}
bool Win32WindowImpl::ShouldClose()
{
return m_close;
}
} // 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;
}

27
src/window.h Normal file
View File

@@ -0,0 +1,27 @@
#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 void PumpMessages() = 0;
virtual bool ShouldClose() = 0;
};
}
std::unique_ptr<starter_window::Window> swCreateWindow(starter_window::WindowCreateParams params);