PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
window.h
1#pragma once
2
3#include "pixelbullet/input/input.h"
4
5#include <glm/glm.hpp>
6#include <volk.h>
7
8#include <cstdint>
9#include <functional>
10#include <string>
11#include <utility>
12
13struct GLFWwindow;
14
15namespace pixelbullet
16{
17class Event;
18class Filesystem;
19class VirtualPath;
20struct ApplicationSpecification;
21
22class Window final
23{
24public:
25 enum class WindowState
26 {
27 Normal,
28 Minimized,
29 Maximized
30 };
31
32 explicit Window(const ApplicationSpecification& props, bool startHidden = false);
33 ~Window();
34
36 void OnUpdate() noexcept;
37
39 void SetIcon(const Filesystem& filesystem, const VirtualPath& iconPath);
40
42 unsigned int GetWidth() const noexcept
43 {
44 return width_;
45 }
46 unsigned int GetHeight() const noexcept
47 {
48 return height_;
49 }
50 glm::ivec2 GetSize() const noexcept
51 {
52 return { width_, height_ };
53 }
54 glm::ivec2 GetPosition() const noexcept
55 {
56 return position_;
57 }
58 unsigned int GetFramebufferWidth() const noexcept
59 {
60 return static_cast<unsigned int>(GetFramebufferSize().x);
61 }
62 unsigned int GetFramebufferHeight() const noexcept
63 {
64 return static_cast<unsigned int>(GetFramebufferSize().y);
65 }
66 glm::ivec2 GetFramebufferSize() const noexcept
67 {
68 const auto size = fullscreen_ ? fullscreen_size_ : size_;
69
70 if (size.x > 0 && size.y > 0)
71 {
72 return size;
73 }
74
75 return GetSize();
76 }
77
79 void SetEventCallback(const std::function<void(Event&)>& callback)
80 {
81 event_callback_ = callback;
82 }
83 void SetFramebufferResizeCallback(const std::function<void(uint32_t)>& callback)
84 {
85 framebuffer_resize_callback_ = callback;
86 }
87
88 [[nodiscard]] const input::InputState& GetInputState() const noexcept
89 {
90 return input_state_;
91 }
92
94 void* GetNativeWindow() const noexcept
95 {
96 return window_;
97 }
98
100 std::pair<const char**, uint32_t> GetInstanceExtensions() const;
101
103 VkResult CreateSurface(VkInstance instance, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface) const;
104
106 void Minimize() noexcept;
107
109 void Maximize() noexcept;
110
112 void Restore() noexcept;
113
115 bool IsMinimized() const noexcept;
116
118 bool IsFocused() const noexcept
119 {
120 return focused_;
121 }
122
124 bool IsMaximized() const noexcept;
125
127 WindowState GetWindowState() const noexcept;
128
129private:
130 void Init(const ApplicationSpecification& props, bool startHidden);
131 void Shutdown();
132
133private:
134 GLFWwindow* window_ = nullptr;
135 std::string title_;
136 unsigned int width_ = 0, height_ = 0;
137 std::function<void(Event&)> event_callback_;
138 std::function<void(uint32_t)> framebuffer_resize_callback_;
139
140 bool fullscreen_ = false;
141 bool focused_ = false;
142 glm::ivec2 size_{ 0, 0 };
143 glm::ivec2 fullscreen_size_{ 0, 0 };
144 glm::ivec2 position_{ 0, 0 };
145 input::InputState input_state_;
146 uint32_t id_ = 0;
147};
148} // namespace pixelbullet
Definition event.h:72
Definition filesystem.h:16
Definition virtual_path.h:10
Definition window.h:23
WindowState GetWindowState() const noexcept
Returns the current window state.
Definition window.cc:312
void * GetNativeWindow() const noexcept
Returns the underlying native window pointer.
Definition window.h:94
void Maximize() noexcept
Maximizes the window.
Definition window.cc:286
bool IsFocused() const noexcept
Returns whether the window currently has focus.
Definition window.h:118
VkResult CreateSurface(VkInstance instance, const VkAllocationCallbacks *allocator, VkSurfaceKHR *surface) const
Creates a Vulkan surface for this window.
Definition window.cc:273
void Restore() noexcept
Restores the window from a minimized or maximized state.
Definition window.cc:294
void SetEventCallback(const std::function< void(Event &)> &callback)
Sets the callback that will be invoked when events occur.
Definition window.h:79
void SetIcon(const Filesystem &filesystem, const VirtualPath &iconPath)
Sets the window icon from an image file.
Definition window.cc:235
unsigned int GetWidth() const noexcept
Accessors for window size.
Definition window.h:42
bool IsMaximized() const noexcept
Returns whether the window is currently maximized.
Definition window.cc:307
void OnUpdate() noexcept
Polls for and processes events.
Definition window.cc:230
void Minimize() noexcept
Minimizes the window.
Definition window.cc:278
bool IsMinimized() const noexcept
Returns whether the window is currently minimized.
Definition window.cc:302
std::pair< const char **, uint32_t > GetInstanceExtensions() const
Retrieves the Vulkan instance extensions required by GLFW.
Definition window.cc:260
Contains configuration options for both the game and the engine.
Definition specification.h:145