Bicameral Engine
GPU-resident engine for a magic-research open-world game
Loading...
Searching...
No Matches
window.h
Go to the documentation of this file.
1// window.h — ゲームの窓と、窓に届いた入力(T-0004)。
2//
3// データの流れ: OS のメッセージ → WndProc → 入力のイベント(platform/input.h)を溜める → フレームのループが TakeInputEvents() で受け取り、
4// 世界に届けるものはコマンドにする(06 §3)。窓は世界の状態を知らない(CPU は View と Controller。D-107)。
5// PumpMessages() は待たない(PeekMessage)。フレームの歩調はスワップチェインの待ち(gpu/swap_chain.h)が決める。
6// WndProc が this を使うので、Window は動かさない(Create は unique_ptr で返す)。
7#pragma once
8
9#include <cstdint>
10#include <expected>
11#include <functional>
12#include <memory>
13#include <string>
14#include <string_view>
15#include <vector>
16
17#include "platform/input.h"
18
19namespace bicameral {
20
21 class Window {
22 public:
23 [[nodiscard]] static std::expected<std::unique_ptr<Window>, std::string> Create(std::wstring_view title,
24 uint32_t clientWidth,
25 uint32_t clientHeight);
26
27 ~Window();
28 Window(const Window&) = delete;
29 Window& operator=(const Window&) = delete;
30 Window(Window&&) = delete;
31 Window& operator=(Window&&) = delete;
32
33 // 溜まっているメッセージを全部処理する(待たない)。窓が閉じられたら false
34 bool PumpMessages();
35
36 [[nodiscard]] HWND Handle() const { return m_handle; }
37 [[nodiscard]] uint32_t ClientWidth() const { return m_clientWidth; }
38 [[nodiscard]] uint32_t ClientHeight() const { return m_clientHeight; }
39 [[nodiscard]] bool IsMinimized() const { return m_clientWidth == 0 || m_clientHeight == 0; }
40
41 // 前に呼んでから大きさが変わったか(呼ぶと false に戻る)
42 [[nodiscard]] bool TakeResized();
43
44 // 前に呼んでからの入力(古い順)
45 [[nodiscard]] std::vector<InputEvent> TakeInputEvents();
46
47 // 窓のメッセージを先に見る関数(エディタの ImGui。T-0023)。true を返したメッセージは窓では扱わない。
48 // 入力のイベントはそれでも溜める(どれを世界やカメラに渡すかは、受け取る側が ImGui の WantCapture* で決める)
49 using MessageHook = std::function<bool(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)>;
50 void SetMessageHook(MessageHook hook) { m_messageHook = std::move(hook); }
51
52 private:
53 Window() = default;
54
55 // --- メッセージ ---
56 static LRESULT CALLBACK WindowProcedure(HWND handle, UINT message, WPARAM wParam, LPARAM lParam);
57 LRESULT HandleMessage(UINT message, WPARAM wParam, LPARAM lParam);
58
59 // --- 入力を溜める ---
60 void AddButton(InputKind kind, PointerButton button, LPARAM lParam);
61 void AddWheel(WPARAM wParam);
62
63 HWND m_handle = nullptr;
65
66 // --- 大きさと状態 ---
67 uint32_t m_clientWidth = 0;
68 uint32_t m_clientHeight = 0;
69 bool m_resized = false;
70 bool m_closed = false;
71
72 // --- 入力 ---
73 std::vector<InputEvent> m_inputEvents;
74 int32_t m_wheelRemainder = 0; // WHEEL_DELTA に満たないホイールの端数
75 uint32_t m_heldButtons = 0; // 押している PointerButton のビット(離すまで SetCapture する)
76 };
77
78} // namespace bicameral
uint32_t m_clientHeight
Definition window.h:68
static std::expected< std::unique_ptr< Window >, std::string > Create(std::wstring_view title, uint32_t clientWidth, uint32_t clientHeight)
Definition window.cpp:32
std::function< bool(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)> MessageHook
Definition window.h:49
Window(const Window &)=delete
std::vector< InputEvent > TakeInputEvents()
Definition window.cpp:81
bool IsMinimized() const
Definition window.h:39
uint32_t ClientHeight() const
Definition window.h:38
uint32_t m_heldButtons
Definition window.h:75
bool TakeResized()
Definition window.cpp:77
Window(Window &&)=delete
LRESULT HandleMessage(UINT message, WPARAM wParam, LPARAM lParam)
Definition window.cpp:143
uint32_t ClientWidth() const
Definition window.h:37
uint32_t m_clientWidth
Definition window.h:67
HWND Handle() const
Definition window.h:36
std::vector< InputEvent > m_inputEvents
Definition window.h:73
MessageHook m_messageHook
Definition window.h:64
void SetMessageHook(MessageHook hook)
Definition window.h:50
bool PumpMessages()
Definition window.cpp:64
void AddWheel(WPARAM wParam)
Definition window.cpp:113
Window & operator=(Window &&)=delete
static LRESULT CALLBACK WindowProcedure(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
Definition window.cpp:125
Window & operator=(const Window &)=delete
int32_t m_wheelRemainder
Definition window.h:74
void AddButton(InputKind kind, PointerButton button, LPARAM lParam)
Definition window.cpp:97
PointerButton
Definition input.h:12
InputKind
Definition input.h:14