Start CMake modernization scaffold

This commit is contained in:
2026-05-31 23:40:43 +02:00
parent ee027984b7
commit c38ff8209b
36 changed files with 2118 additions and 1556 deletions

View File

@@ -0,0 +1,142 @@
#include "foundation/binary_stream.h"
namespace pp::foundation {
ByteReader::ByteReader(std::span<const std::byte> bytes) noexcept
: bytes_(bytes)
{
}
std::size_t ByteReader::position() const noexcept
{
return position_;
}
std::size_t ByteReader::size() const noexcept
{
return bytes_.size();
}
std::size_t ByteReader::remaining() const noexcept
{
return bytes_.size() - position_;
}
bool ByteReader::empty() const noexcept
{
return remaining() == 0;
}
Status ByteReader::seek(std::size_t position) noexcept
{
if (position > bytes_.size()) {
return Status::out_of_range("seek position is outside the stream");
}
position_ = position;
return Status::success();
}
Result<std::uint8_t> ByteReader::read_u8() noexcept
{
const auto bytes = read_bytes(1);
if (!bytes) {
return Result<std::uint8_t>::failure(bytes.status());
}
return Result<std::uint8_t>::success(static_cast<std::uint8_t>(bytes.value()[0]));
}
Result<std::uint16_t> ByteReader::read_u16_le() noexcept
{
const auto bytes = read_bytes(2);
if (!bytes) {
return Result<std::uint16_t>::failure(bytes.status());
}
const auto b0 = static_cast<std::uint16_t>(bytes.value()[0]);
const auto b1 = static_cast<std::uint16_t>(bytes.value()[1]);
return Result<std::uint16_t>::success(static_cast<std::uint16_t>(b0 | (b1 << 8U)));
}
Result<std::uint32_t> ByteReader::read_u32_le() noexcept
{
const auto bytes = read_bytes(4);
if (!bytes) {
return Result<std::uint32_t>::failure(bytes.status());
}
const auto b0 = static_cast<std::uint32_t>(bytes.value()[0]);
const auto b1 = static_cast<std::uint32_t>(bytes.value()[1]);
const auto b2 = static_cast<std::uint32_t>(bytes.value()[2]);
const auto b3 = static_cast<std::uint32_t>(bytes.value()[3]);
return Result<std::uint32_t>::success(b0 | (b1 << 8U) | (b2 << 16U) | (b3 << 24U));
}
Result<std::span<const std::byte>> ByteReader::read_bytes(std::size_t count) noexcept
{
if (count > remaining()) {
return Result<std::span<const std::byte>>::failure(
Status::out_of_range("read would move beyond the end of the stream"));
}
const auto start = position_;
position_ += count;
return Result<std::span<const std::byte>>::success(bytes_.subspan(start, count));
}
ByteWriter::ByteWriter(std::vector<std::byte>& bytes) noexcept
: bytes_(&bytes)
{
}
std::size_t ByteWriter::size() const noexcept
{
return bytes_ == nullptr ? 0 : bytes_->size();
}
Status ByteWriter::write_u8(std::uint8_t value)
{
if (bytes_ == nullptr) {
return Status::invalid_argument("writer has no backing storage");
}
bytes_->push_back(static_cast<std::byte>(value));
return Status::success();
}
Status ByteWriter::write_u16_le(std::uint16_t value)
{
if (bytes_ == nullptr) {
return Status::invalid_argument("writer has no backing storage");
}
bytes_->push_back(static_cast<std::byte>(value & 0xffU));
bytes_->push_back(static_cast<std::byte>((value >> 8U) & 0xffU));
return Status::success();
}
Status ByteWriter::write_u32_le(std::uint32_t value)
{
if (bytes_ == nullptr) {
return Status::invalid_argument("writer has no backing storage");
}
bytes_->push_back(static_cast<std::byte>(value & 0xffU));
bytes_->push_back(static_cast<std::byte>((value >> 8U) & 0xffU));
bytes_->push_back(static_cast<std::byte>((value >> 16U) & 0xffU));
bytes_->push_back(static_cast<std::byte>((value >> 24U) & 0xffU));
return Status::success();
}
Status ByteWriter::write_bytes(std::span<const std::byte> bytes)
{
if (bytes_ == nullptr) {
return Status::invalid_argument("writer has no backing storage");
}
bytes_->insert(bytes_->end(), bytes.begin(), bytes.end());
return Status::success();
}
}

View File

@@ -0,0 +1,46 @@
#pragma once
#include "foundation/result.h"
#include <cstddef>
#include <cstdint>
#include <span>
#include <vector>
namespace pp::foundation {
class ByteReader {
public:
explicit ByteReader(std::span<const std::byte> bytes) noexcept;
[[nodiscard]] std::size_t position() const noexcept;
[[nodiscard]] std::size_t size() const noexcept;
[[nodiscard]] std::size_t remaining() const noexcept;
[[nodiscard]] bool empty() const noexcept;
[[nodiscard]] Status seek(std::size_t position) noexcept;
[[nodiscard]] Result<std::uint8_t> read_u8() noexcept;
[[nodiscard]] Result<std::uint16_t> read_u16_le() noexcept;
[[nodiscard]] Result<std::uint32_t> read_u32_le() noexcept;
[[nodiscard]] Result<std::span<const std::byte>> read_bytes(std::size_t count) noexcept;
private:
std::span<const std::byte> bytes_;
std::size_t position_ = 0;
};
class ByteWriter {
public:
explicit ByteWriter(std::vector<std::byte>& bytes) noexcept;
[[nodiscard]] std::size_t size() const noexcept;
[[nodiscard]] Status write_u8(std::uint8_t value);
[[nodiscard]] Status write_u16_le(std::uint16_t value);
[[nodiscard]] Status write_u32_le(std::uint32_t value);
[[nodiscard]] Status write_bytes(std::span<const std::byte> bytes);
private:
std::vector<std::byte>* bytes_ = nullptr;
};
}

80
src/foundation/result.h Normal file
View File

@@ -0,0 +1,80 @@
#pragma once
namespace pp::foundation {
enum class StatusCode {
ok,
invalid_argument,
out_of_range,
};
struct Status {
StatusCode code = StatusCode::ok;
const char* message = "ok";
[[nodiscard]] constexpr bool ok() const noexcept
{
return code == StatusCode::ok;
}
[[nodiscard]] static constexpr Status success() noexcept
{
return {};
}
[[nodiscard]] static constexpr Status invalid_argument(const char* message) noexcept
{
return { StatusCode::invalid_argument, message };
}
[[nodiscard]] static constexpr Status out_of_range(const char* message) noexcept
{
return { StatusCode::out_of_range, message };
}
};
template <typename T>
class Result {
public:
[[nodiscard]] static constexpr Result success(T value) noexcept
{
return Result(value, Status::success());
}
[[nodiscard]] static constexpr Result failure(Status status) noexcept
{
return Result(T{}, status);
}
[[nodiscard]] constexpr bool ok() const noexcept
{
return status_.ok();
}
[[nodiscard]] constexpr explicit operator bool() const noexcept
{
return ok();
}
[[nodiscard]] constexpr const T& value() const noexcept
{
return value_;
}
[[nodiscard]] constexpr Status status() const noexcept
{
return status_;
}
private:
constexpr Result(T value, Status status) noexcept
: value_(value)
, status_(status)
{
}
T value_{};
Status status_{};
};
}

View File

@@ -19,7 +19,10 @@
#include "abr.h"
#include "settings.h"
#if __has_include(<renderdoc_app.h>)
#include <renderdoc_app.h>
#define USE_RENDERDOC
#endif
#include <iomanip>
#include <ctime>
@@ -33,7 +36,7 @@ HINSTANCE hInst;
HWND hWnd;
HDC hDC;
HGLRC hRC;
wchar_t* className;
const wchar_t* className;
bool keys[256];
std::mutex gl_mutex;
std::mutex async_mutex;
@@ -54,6 +57,7 @@ float timer_ink_touch = 0;
float timer_ink_pen = 0;
bool sandboxed = false;
#ifdef USE_RENDERDOC
RENDERDOC_API_1_4_0* rdoc_api = NULL;
bool win32_renderdoc_init()
{
@@ -78,6 +82,10 @@ void win32_renderdoc_frame_end()
if (rdoc_api)
rdoc_api->EndFrameCapture(NULL, NULL);
}
#else
void win32_renderdoc_frame_start() { }
void win32_renderdoc_frame_end() { }
#endif
HRESULT(*GetDpiForMonitor_fn)(HMONITOR hmonitor, MONITOR_DPI_TYPE dpiType, UINT* dpiX, UINT* dpiY);
HRESULT(*SetProcessDpiAwareness_fn)(PROCESS_DPI_AWARENESS value);
@@ -367,7 +375,7 @@ int read_WMI_info()
}
IWbemServices* pService = NULL;
if (FAILED(hRes = pLocator->ConnectServer(L"root\\CIMV2", NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &pService)))
if (FAILED(hRes = pLocator->ConnectServer(BSTR(L"root\\CIMV2"), NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &pService)))
{
pLocator->Release();
LOG("Unable to connect to \"CIMV2\": %x", hRes);
@@ -411,7 +419,7 @@ int read_WMI_info()
// GET DEVICE INFO
{
IEnumWbemClassObject* pEnumerator = NULL;
if (FAILED(hRes = pService->ExecQuery(L"WQL", L"SELECT * FROM Win32_ComputerSystem", WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumerator)))
if (FAILED(hRes = pService->ExecQuery(BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_ComputerSystem"), WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumerator)))
{
pLocator->Release();
pService->Release();
@@ -438,7 +446,7 @@ int read_WMI_info()
// GET OS INFO
{
IEnumWbemClassObject* pEnumerator = NULL;
if (FAILED(hRes = pService->ExecQuery(L"WQL", L"SELECT * FROM Win32_OperatingSystem", WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumerator)))
if (FAILED(hRes = pService->ExecQuery(BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_OperatingSystem"), WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumerator)))
{
pLocator->Release();
pService->Release();
@@ -468,7 +476,7 @@ int read_WMI_info()
pService->Release();
pService = NULL;
if (FAILED(hRes = pLocator->ConnectServer(L"root\\Microsoft\\Windows\\DeviceGuard", NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &pService)))
if (FAILED(hRes = pLocator->ConnectServer(BSTR(L"root\\Microsoft\\Windows\\DeviceGuard"), NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &pService)))
{
pLocator->Release();
LOG("Unable to connect to \"DeviceGuard\": %x", hRes);
@@ -478,7 +486,7 @@ int read_WMI_info()
// GET DEVICE GUARD
{
IEnumWbemClassObject* pEnumerator = NULL;
if (FAILED(hRes = pService->ExecQuery(L"WQL", L"SELECT * FROM Win32_DeviceGuard", WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumerator)))
if (FAILED(hRes = pService->ExecQuery(BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_DeviceGuard"), WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumerator)))
{
pLocator->Release();
pService->Release();
@@ -954,8 +962,10 @@ int main(int argc, char** argv)
LOG("GL vendor: %s", glGetString(GL_VENDOR));
LOG("GL renderer: %s", glGetString(GL_RENDERER));
#ifdef USE_RENDERDOC
if (!win32_renderdoc_init())
LOG("Renderdoc not started");
#endif // USE_RENDERDOC
swprintf_s(window_title, L"PanoPainter %s (%s)", g_version_number_w,
str2wstr((char*)glGetString(GL_RENDERER)).c_str());

View File

@@ -64,8 +64,12 @@
#elif _WIN32
#define _USE_MATH_DEFINES
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _SCL_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#endif
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
@@ -138,6 +142,7 @@
#include <regex>
#include <mutex>
#include <queue>
#include <chrono>
#include <memory>
#include <string>
#include <vector>
@@ -181,4 +186,4 @@
#ifndef EMSCRIPTEN
#include <curl/curl.h>
#endif
#endif

View File

@@ -11,7 +11,9 @@ const int g_version_build = PP_VERSION_BUILD;
#ifdef _WIN32
#include <windows.h>
const wchar_t* g_version_w = TEXT(PP_VERSION_STRING);
const wchar_t* g_version_number_w = TEXT(PP_VERSION_NUMBER_STRING);
const wchar_t* g_window_title_w = L"PanoPainter " TEXT(PP_VERSION_NUMBER_STRING);
#define PP_WIDEN2(x) L##x
#define PP_WIDEN(x) PP_WIDEN2(x)
const wchar_t* g_version_w = PP_WIDEN(PP_VERSION_STRING);
const wchar_t* g_version_number_w = PP_WIDEN(PP_VERSION_NUMBER_STRING);
const wchar_t* g_window_title_w = L"PanoPainter " PP_WIDEN(PP_VERSION_NUMBER_STRING);
#endif