libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
Window.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <filesystem>
5#include <string>
6#include <vector>
7#include <unordered_set>
8#include <spdlog/spdlog.h>
11#include "ship/core/Component.h"
12
13namespace Ship {
14class Gui;
15class GuiWindow;
16
26struct Coords {
27 int32_t x;
28 int32_t y;
29};
30
32struct CoordsF {
33 float x;
34 float y;
35};
36
38struct WindowRect {
39 int32_t Left;
40 int32_t Top;
41 int32_t Right;
42 int32_t Bottom;
43};
44
45class Config;
46
62class Window : public Component {
63 friend class Context;
64
65 public:
66 explicit Window(std::shared_ptr<Config> config = nullptr);
71 Window(const std::vector<std::shared_ptr<GuiWindow>>& guiWindows, std::shared_ptr<Config> config = nullptr);
76 Window(std::shared_ptr<Gui> gui, std::shared_ptr<Config> config = nullptr);
82 Window(std::shared_ptr<Gui> gui, std::shared_ptr<MouseStateManager> mouseStateManager,
83 std::shared_ptr<Config> config = nullptr);
84 virtual ~Window();
85
87 virtual void Close() = 0;
89 virtual void RunGuiOnly() = 0;
91 virtual void StartFrame() = 0;
93 virtual void EndFrame() = 0;
95 virtual bool IsFrameReady() = 0;
97 virtual void HandleEvents() = 0;
102 virtual void SetCursorVisibility(bool visible) = 0;
104 virtual uint32_t GetWidth() = 0;
106 virtual uint32_t GetHeight() = 0;
108 virtual float GetAspectRatio() = 0;
110 virtual int32_t GetPosX() = 0;
112 virtual int32_t GetPosY() = 0;
117 virtual void SetMousePos(Coords pos) = 0;
119 virtual Coords GetMousePos() = 0;
121 virtual Coords GetMouseDelta() = 0;
123 virtual CoordsF GetMouseWheel() = 0;
128 virtual bool GetMouseState(MouseBtn btn) = 0;
133 virtual void SetMouseCapture(bool capture) = 0;
135 virtual bool IsMouseCaptured() = 0;
137 virtual uint32_t GetCurrentRefreshRate() = 0;
139 virtual bool SupportsWindowedFullscreen() = 0;
141 virtual bool CanDisableVerticalSync() = 0;
146 virtual void SetResolutionMultiplier(float multiplier) = 0;
151 virtual void SetMsaaLevel(uint32_t value) = 0;
156 virtual void SetFullscreen(bool isFullscreen) = 0;
158 virtual bool IsFullscreen() = 0;
160 virtual bool IsRunning() = 0;
166 virtual const char* GetKeyName(int32_t scancode) = 0;
168 virtual uintptr_t GetGfxFrameBuffer() = 0;
169
175 virtual void SetCurrentDimensions(uint32_t width, uint32_t height) = 0;
183 virtual void SetCurrentDimensions(uint32_t width, uint32_t height, int32_t posX, int32_t posY) = 0;
190 virtual void SetCurrentDimensions(bool isFullscreen, uint32_t width, uint32_t height) = 0;
199 virtual void SetCurrentDimensions(bool isFullscreen, uint32_t width, uint32_t height, int32_t posX,
200 int32_t posY) = 0;
206
219 virtual std::string GetWindowBackendName();
221 std::shared_ptr<std::vector<int32_t>> GetAvailableWindowBackends();
226 bool IsAvailableWindowBackend(int32_t backendId);
233 void SetLastScancode(int32_t scanCode);
241 std::shared_ptr<Gui> GetGui();
248 void SetAutoCaptureMouse(bool capture);
255 void SetForceCursorVisibility(bool visible);
264 void SetFullscreenScancode(int32_t scancode);
269 void SetMouseCaptureScancode(int32_t scancode);
271 std::shared_ptr<MouseStateManager> GetMouseStateManager();
272
273 protected:
275 void OnInit(const nlohmann::json& initArgs) override;
280 void SetWindowBackend(int32_t backend);
285 void AddAvailableWindowBackend(int32_t backend);
293
295 std::shared_ptr<Config> GetConfig() const;
296
297 private:
298 std::shared_ptr<Gui> mGui;
299 int32_t mLastScancode = -1;
300 int32_t mWindowBackend;
301 std::shared_ptr<MouseStateManager> mMouseStateManager;
302 std::shared_ptr<std::vector<int32_t>> mAvailableWindowBackends;
303 // Hold a reference to Config because Window has a Save function called on Context destructor, where the singleton
304 // is no longer available.
305 std::shared_ptr<Config> mConfig;
306 int32_t mFullscreenScancode;
307 int32_t mMouseCaptureScancode;
308};
309} // namespace Ship
A named Part with a parent/child hierarchy and optional thread safety.
Definition Component.h:34
Persistent JSON-backed key-value configuration store.
Definition Config.h:62
Central singleton context for the libultraship engine.
Definition Context.h:63
Abstract base class for the application window and rendering surface.
Definition Window.h:62
virtual void SetCurrentDimensions(uint32_t width, uint32_t height)=0
Sets the window dimensions (size and position) and applies them immediately.
float GetCurrentAspectRatio()
Returns the current aspect ratio, recomputed from GetWidth() / GetHeight().
virtual bool IsRunning()=0
Returns true while the window / render loop is running.
virtual void HandleEvents()=0
Processes pending OS/input events.
void SetFullscreenScancode(int32_t scancode)
Binds a scancode to the fullscreen toggle action.
void SaveWindowToConfig()
Persists window geometry and backend settings to the Config.
bool ShouldAutoCaptureMouse()
Returns true if the window should automatically capture the mouse when focused.
void AddAvailableWindowBackend(int32_t backend)
Adds a backend to the list of backends available on this platform.
virtual void SetMsaaLevel(uint32_t value)=0
Sets the MSAA sample count.
virtual int32_t GetPosY()=0
Returns the Y position of the window's top-left corner on the desktop.
virtual void SetFullscreen(bool isFullscreen)=0
Switches between fullscreen and windowed mode.
virtual bool SupportsWindowedFullscreen()=0
Returns true if the backend supports a windowed-fullscreen (borderless) mode.
virtual bool IsFrameReady()=0
Returns true if the renderer is ready to begin a new frame.
virtual void SetCurrentDimensions(bool isFullscreen, uint32_t width, uint32_t height)=0
Switches fullscreen state and sets the window dimensions.
virtual float GetAspectRatio()=0
Returns the current aspect ratio (width / height) of the window.
virtual void SetMouseCapture(bool capture)=0
Enables or disables mouse capture (relative/locked mouse mode).
int32_t GetLastScancode()
Returns the scancode of the last key event received.
void SetForceCursorVisibility(bool visible)
Forces the cursor to remain visible even when captured.
virtual void SetMousePos(Coords pos)=0
Moves the mouse cursor to the given window-relative coordinates.
virtual void StartFrame()=0
Begins a new render frame; clears the back buffer and sets up state.
virtual bool IsMouseCaptured()=0
Returns true if the mouse is currently captured.
virtual bool CanDisableVerticalSync()=0
Returns true if vertical sync can be disabled on this backend.
bool IsAvailableWindowBackend(int32_t backendId)
Returns true if the backend with the given ID is available on this platform.
virtual void SetCurrentDimensions(uint32_t width, uint32_t height, int32_t posX, int32_t posY)=0
Sets the window dimensions and position and applies them immediately.
virtual uint32_t GetCurrentRefreshRate()=0
Returns the display's current refresh rate in Hz.
virtual void RunGuiOnly()=0
Runs a single GUI-only tick without executing game logic.
Window(const std::vector< std::shared_ptr< GuiWindow > > &guiWindows, std::shared_ptr< Config > config=nullptr)
Constructs a Window and pre-registers a list of GUI windows.
void SetAutoCaptureMouse(bool capture)
Enables or disables automatic mouse capture on window focus.
virtual bool IsFullscreen()=0
Returns true if the window is currently in fullscreen mode.
virtual void EndFrame()=0
Ends the current render frame and presents it to the display.
virtual Coords GetMousePos()=0
Returns the current mouse cursor position relative to the window.
void SetWindowBackend(int32_t backend)
Records the active graphics backend. Called by subclass constructors.
std::shared_ptr< Gui > GetGui()
Returns the GUI overlay layer.
std::shared_ptr< std::vector< int32_t > > GetAvailableWindowBackends()
Returns the list of backends available on this platform.
virtual Coords GetMouseDelta()=0
Returns the mouse movement delta since the last frame.
virtual int32_t GetPosX()=0
Returns the X position of the window's top-left corner on the desktop.
int32_t GetFullscreenScancode()
Returns the scancode bound to the fullscreen toggle action.
virtual uintptr_t GetGfxFrameBuffer()=0
Returns a handle to the graphics API framebuffer object.
Window(std::shared_ptr< Gui > gui, std::shared_ptr< MouseStateManager > mouseStateManager, std::shared_ptr< Config > config=nullptr)
Constructs a Window with both a Gui and a MouseStateManager.
void ToggleFullscreen()
Toggles between fullscreen and windowed mode.
void SetLastScancode(int32_t scanCode)
Records the most recently received keyboard scancode.
virtual const char * GetKeyName(int32_t scancode)=0
Returns the human-readable name for a keyboard scancode.
std::shared_ptr< MouseStateManager > GetMouseStateManager()
Returns the MouseStateManager used to track mouse button and position state.
Window(std::shared_ptr< Gui > gui, std::shared_ptr< Config > config=nullptr)
Constructs a Window backed by a pre-constructed Gui instance.
void SetMouseCaptureScancode(int32_t scancode)
Binds a scancode to the mouse-capture toggle action.
int32_t GetSavedWindowBackend()
Reads the saved window backend identifier from the config.
virtual std::string GetWindowBackendName()
Returns a human-readable name for the current window backend.
virtual void SetResolutionMultiplier(float multiplier)=0
Sets the internal rendering resolution multiplier.
std::shared_ptr< Config > GetConfig() const
Returns the cached Config component after validating it is ready for use.
bool ShouldForceCursorVisibility()
Returns true if the cursor should always be visible regardless of capture state.
int32_t GetMouseCaptureScancode()
Returns the scancode bound to the mouse-capture toggle action.
virtual bool GetMouseState(MouseBtn btn)=0
Returns whether the given mouse button is currently pressed.
virtual uint32_t GetWidth()=0
Returns the current width of the window client area in pixels.
virtual CoordsF GetMouseWheel()=0
Returns the mouse wheel scroll delta for the current frame.
void OnInit(const nlohmann::json &initArgs) override
Caches this Window on the MouseStateManager.
virtual void Close()=0
Requests that the window and application close.
virtual void SetCursorVisibility(bool visible)=0
Shows or hides the OS mouse cursor over the window.
int32_t GetWindowBackend()
Returns the current graphics backend identifier.
virtual WindowRect GetPrimaryMonitorRect()=0
Returns the bounding rectangle of the primary monitor.
virtual void SetCurrentDimensions(bool isFullscreen, uint32_t width, uint32_t height, int32_t posX, int32_t posY)=0
Switches fullscreen state and sets the window dimensions and position.
Window(std::shared_ptr< Config > config=nullptr)
virtual uint32_t GetHeight()=0
Returns the current height of the window client area in pixels.
virtual ~Window()
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14
MouseBtn
Identifies a mouse button delivered to the input mapping system.
Definition KeyboardScancodes.h:136
Floating-point pixel coordinates.
Definition Window.h:32
float y
Definition Window.h:34
float x
Definition Window.h:33
Identifies the graphics/windowing backend in use.
Definition Window.h:26
int32_t y
Definition Window.h:28
int32_t x
Definition Window.h:27
Screen-space rectangle in integer pixels.
Definition Window.h:38
int32_t Bottom
Definition Window.h:42
int32_t Left
Definition Window.h:39
int32_t Right
Definition Window.h:41
int32_t Top
Definition Window.h:40