Nameless Engine
Loading...
Searching...
No Matches
Window.h
1#pragma once
2
3// Standard.
4#include <variant>
5#include <memory>
6#include <optional>
7#include <utility>
8
9// Custom.
10#include "misc/Error.h"
11#include "game/GameManager.h"
12#include "render/Renderer.h"
13#include "window/GLFW.hpp"
14#include "game/GameInstance.h"
15#include "input/KeyboardKey.hpp"
16#include "input/MouseButton.hpp"
17
18namespace ne {
19 class Error;
20 class GameInstance;
21
26 // Only Window can directly create instances of this class.
27 // For users we provide a separate function.
28 friend class Window;
29
30 public:
31 WindowCursor() = delete;
32 WindowCursor(const WindowCursor&) = delete;
33 WindowCursor& operator=(const WindowCursor&) = delete;
34
37
38 protected:
52 static std::variant<std::unique_ptr<WindowCursor>, Error>
53 create(const std::filesystem::path& pathToIcon);
54
60 void releaseCursor();
61
67 GLFWcursor* getCursor() const;
68
69 private:
75 WindowCursor(GLFWcursor* pCursor);
76
78 GLFWcursor* pCursor = nullptr;
79 };
80
86 int iWindowWidth = 800;
88 int iWindowHeight = 600;
90 std::string_view sWindowTitle;
92 std::filesystem::path pathToWindowIcon;
94 bool bShowWindow = true;
96 bool bMaximized = false;
98 bool bFullscreen = false;
100 bool bIsSplashScreen = false;
101 };
102
103 class Window;
104
109 public:
110 WindowBuilder() = default;
111
120 WindowBuilder& withSize(int iWidth, int iHeight);
121
129 WindowBuilder& withTitle(std::string_view sWindowTitle);
130
138 WindowBuilder& withIcon(const std::filesystem::path& pathToIcon);
139
148 WindowBuilder& withVisibility(bool bShow);
149
158 WindowBuilder& withMaximizedState(bool bMaximized);
159
169 WindowBuilder& withSplashScreenMode(bool bIsSplashScreen);
170
181 WindowBuilder& withFullscreenMode(bool bEnableFullscreen);
182
190 std::variant<std::unique_ptr<Window>, Error> build();
191
192 private:
195 };
196
198 class Window {
199 // Builder will create new instances.
200 friend class WindowBuilder;
201
202 public:
203 Window() = delete;
204 Window(const Window&) = delete;
205 Window& operator=(const Window&) = delete;
206
207 virtual ~Window();
208
216 static WindowBuilder getBuilder();
217
237 void setPreferredRenderer(const std::optional<RendererType>& preferredRenderer);
238
246 template <typename MyGameInstance>
247 requires std::derived_from<MyGameInstance, GameInstance>
248 void processEvents();
249
256 void setOpacity(float opacity) const;
257
263 void setTitle(const std::string& sNewTitle);
264
277 [[nodiscard]] std::optional<Error> setIcon(const std::filesystem::path& pathToIcon) const;
278
294 std::variant<WindowCursor*, Error> createCursor(const std::filesystem::path& pathToIcon);
295
306 void setCursor(WindowCursor* pCursor);
307
313 void setDefaultCursor();
314
321 void setCursorVisibility(bool bIsVisible) const;
322
329 void minimize() const;
330
337 void maximize() const;
338
346 void restore() const;
347
355 void hide() const;
356
363 void show() const;
364
369 void close() const;
370
379 std::pair<int, int> getSize() const;
380
390 std::pair<double, double> getCursorPosition() const;
391
397 std::string getTitle() const;
398
404 float getOpacity() const;
405
411 Renderer* getRenderer() const;
412
413#if defined(WIN32)
419 HWND getWindowHandle() const;
420#endif
421
429 GLFWwindow* getGlfwWindow() const;
430
440 void onKeyboardInput(KeyboardKey key, KeyboardModifiers modifiers, bool bIsPressedDown) const;
441
451 void onMouseInput(MouseButton button, KeyboardModifiers modifiers, bool bIsPressedDown) const;
452
460 void onMouseScrollMove(int iOffset) const;
461
462 private:
472 static void
473 glfwWindowKeyboardCallback(GLFWwindow* pGlfwWindow, int iKey, int iScancode, int iAction, int iMods);
474
483 static void glfwWindowMouseCallback(GLFWwindow* pGlfwWindow, int iButton, int iAction, int iMods);
484
491 static void glfwWindowFocusCallback(GLFWwindow* pGlfwWindow, int iFocused);
492
500 static void glfwWindowMouseCursorPosCallback(GLFWwindow* pGlfwWindow, double xPos, double yPos);
501
509 static void glfwWindowMouseScrollCallback(GLFWwindow* pGlfwWindow, double xOffset, double yOffset);
510
518 static void glfwFramebufferResizeCallback(GLFWwindow* pGlfwWindow, int iWidth, int iHeight);
519
525 void bindToWindowEvents();
526
533
538 void showErrorIfNotOnMainThread() const;
539
546 void onMouseMove(double xPos, double yPos);
547
553 void onWindowFocusChanged(bool bIsFocused) const;
554
561 void onFramebufferSizeChanged(int iWidth, int iHeight) const;
562
570 static std::variant<std::unique_ptr<Window>, Error> create(WindowBuilderParameters& params);
571
578 Window(GLFWwindow* pGlfwWindow, const std::string& sWindowTitle);
579
581 std::unique_ptr<GameManager> pGameManager;
582
584 GLFWwindow* pGlfwWindow = nullptr;
585
587 std::vector<std::unique_ptr<WindowCursor>> vCreatedCursors;
588
590 std::thread::id mainThreadId;
591
593 std::string sWindowTitle;
594
596 std::optional<RendererType> preferredRenderer;
597
599 double lastMouseXPos = 0.0;
600
602 double lastMouseYPos = 0.0;
603 };
604
605 template <typename MyGameInstance>
606 requires std::derived_from<MyGameInstance, GameInstance>
608 // Create game manager.
609 pGameManager = std::unique_ptr<GameManager>(new GameManager(this));
610 auto optionalError = pGameManager->initialize(preferredRenderer);
611 if (optionalError.has_value()) [[unlikely]] {
612 optionalError->addCurrentLocationToErrorStack();
613 optionalError->showError();
614 throw std::runtime_error(optionalError->getFullErrorMessage());
615 }
616
617 // ... initialize other GameManager fields here ...
618
619 // Finally create Game Instance when engine (GameManager) is fully initialized.
620 // So that the user can call engine functions in Game Instance constructor.
621 pGameManager->setGameInstance<MyGameInstance>();
622
623 // Now bind to window events because game manager/instance is created.
625
626 // After enabling window events notify game instance about game being ready to start.
627 pGameManager->onGameStarted();
628
629 // Prepare reference to renderer.
630 const auto pRenderer = getRenderer();
631
632 // Used for tick.
633 float currentTimeInSec = 0.0f;
634 float prevTimeInSec = static_cast<float>(glfwGetTime());
635
636 while (!glfwWindowShouldClose(pGlfwWindow)) {
637 // Execute deferred tasks before processing input and ticking
638 // because some deferred tasks are used to remove despawned nodes from world,
639 // this way we won't trigger input events/tick on despawned nodes.
640 pGameManager->executeDeferredTasks();
641
642 // Process window events.
643 glfwPollEvents();
644
645 // Tick.
646 currentTimeInSec = static_cast<float>(glfwGetTime());
647 pGameManager->onBeforeNewFrame(currentTimeInSec - prevTimeInSec);
648 prevTimeInSec = currentTimeInSec;
649
650 // Draw next frame.
651 pRenderer->drawNextFrame();
652
653 // Notify game manager about tick finish.
654 pGameManager->onTickFinished();
655 }
656
657 // Notify game manager about window closed.
658 pGameManager->onWindowClose();
659
660 // Unbind from callbacks before destroying the game manager/game instance.
662
663 // Destroy game manager.
664 pGameManager->destroy(); // explicitly destroy here to run GC for the last time (before everything
665 pGameManager = nullptr; // else is destroyed)
666 Logger::get().info("game manager is destroyed");
667 }
668} // namespace ne
Definition: Error.h:27
Definition: GameManager.h:34
Definition: KeyboardKey.hpp:10
void info(std::string_view sText, const std::source_location location=std::source_location::current()) const
Definition: Logger.cpp:50
static Logger & get()
Definition: Logger.cpp:41
Definition: Renderer.h:43
Definition: Window.h:108
WindowBuilder & withTitle(std::string_view sWindowTitle)
Definition: Window.cpp:27
WindowBuilder & withFullscreenMode(bool bEnableFullscreen)
Definition: Window.cpp:57
WindowBuilderParameters params
Definition: Window.h:194
WindowBuilder & withVisibility(bool bShow)
Definition: Window.cpp:39
std::variant< std::unique_ptr< Window >, Error > build()
Definition: Window.cpp:63
WindowBuilder & withMaximizedState(bool bMaximized)
Definition: Window.cpp:45
WindowBuilder & withIcon(const std::filesystem::path &pathToIcon)
Definition: Window.cpp:33
WindowBuilder & withSize(int iWidth, int iHeight)
Definition: Window.cpp:20
WindowBuilder & withSplashScreenMode(bool bIsSplashScreen)
Definition: Window.cpp:51
Definition: Window.h:25
void releaseCursor()
Definition: Window.cpp:526
static std::variant< std::unique_ptr< WindowCursor >, Error > create(const std::filesystem::path &pathToIcon)
Definition: Window.cpp:503
GLFWcursor * pCursor
Definition: Window.h:78
GLFWcursor * getCursor() const
Definition: Window.cpp:531
~WindowCursor()
Definition: Window.cpp:494
Definition: Window.h:198
std::unique_ptr< GameManager > pGameManager
Definition: Window.h:581
Renderer * getRenderer() const
Definition: Window.cpp:104
void bindToWindowEvents()
Definition: Window.cpp:207
std::string getTitle() const
Definition: Window.cpp:100
std::vector< std::unique_ptr< WindowCursor > > vCreatedCursors
Definition: Window.h:587
void setPreferredRenderer(const std::optional< RendererType > &preferredRenderer)
Definition: Window.cpp:394
std::pair< int, int > getSize() const
Definition: Window.cpp:72
void onMouseMove(double xPos, double yPos)
Definition: Window.cpp:126
std::variant< WindowCursor *, Error > createCursor(const std::filesystem::path &pathToIcon)
Definition: Window.cpp:428
void unbindFromWindowEvents()
Definition: Window.cpp:251
double lastMouseXPos
Definition: Window.h:599
static void glfwWindowFocusCallback(GLFWwindow *pGlfwWindow, int iFocused)
Definition: Window.cpp:171
void restore() const
Definition: Window.cpp:481
static void glfwWindowMouseScrollCallback(GLFWwindow *pGlfwWindow, double xOffset, double yOffset)
Definition: Window.cpp:189
void show() const
Definition: Window.cpp:398
void setDefaultCursor()
Definition: Window.cpp:453
void hide() const
Definition: Window.cpp:65
void setCursor(WindowCursor *pCursor)
Definition: Window.cpp:447
GLFWwindow * getGlfwWindow() const
Definition: Window.cpp:116
std::pair< double, double > getCursorPosition() const
Definition: Window.cpp:83
void setOpacity(float opacity) const
Definition: Window.cpp:400
static WindowBuilder getBuilder()
Definition: Window.cpp:392
void setCursorVisibility(bool bIsVisible) const
Definition: Window.cpp:455
float getOpacity() const
Definition: Window.cpp:102
void onFramebufferSizeChanged(int iWidth, int iHeight) const
Definition: Window.cpp:141
void showErrorIfNotOnMainThread() const
Definition: Window.cpp:292
void onWindowFocusChanged(bool bIsFocused) const
Definition: Window.cpp:137
std::string sWindowTitle
Definition: Window.h:593
void onMouseScrollMove(int iOffset) const
Definition: Window.cpp:135
static std::variant< std::unique_ptr< Window >, Error > create(WindowBuilderParameters &params)
Definition: Window.cpp:305
std::optional< RendererType > preferredRenderer
Definition: Window.h:596
void processEvents()
Definition: Window.h:607
static void glfwWindowMouseCallback(GLFWwindow *pGlfwWindow, int iButton, int iAction, int iMods)
Definition: Window.cpp:161
void close() const
Definition: Window.cpp:70
void onKeyboardInput(KeyboardKey key, KeyboardModifiers modifiers, bool bIsPressedDown) const
Definition: Window.cpp:118
static void glfwWindowMouseCursorPosCallback(GLFWwindow *pGlfwWindow, double xPos, double yPos)
Definition: Window.cpp:180
std::thread::id mainThreadId
Definition: Window.h:590
void minimize() const
Definition: Window.cpp:471
static void glfwFramebufferResizeCallback(GLFWwindow *pGlfwWindow, int iWidth, int iHeight)
Definition: Window.cpp:198
void onMouseInput(MouseButton button, KeyboardModifiers modifiers, bool bIsPressedDown) const
Definition: Window.cpp:122
GLFWwindow * pGlfwWindow
Definition: Window.h:584
static void glfwWindowKeyboardCallback(GLFWwindow *pGlfwWindow, int iKey, int iScancode, int iAction, int iMods)
Definition: Window.cpp:146
void setTitle(const std::string &sNewTitle)
Definition: Window.cpp:402
void maximize() const
Definition: Window.cpp:476
double lastMouseYPos
Definition: Window.h:602
std::optional< Error > setIcon(const std::filesystem::path &pathToIcon) const
Definition: Window.cpp:407
Definition: Window.h:84
bool bShowWindow
Definition: Window.h:94
std::filesystem::path pathToWindowIcon
Definition: Window.h:92
int iWindowWidth
Definition: Window.h:86
bool bIsSplashScreen
Definition: Window.h:100
std::string_view sWindowTitle
Definition: Window.h:90
int iWindowHeight
Definition: Window.h:88
bool bMaximized
Definition: Window.h:96
bool bFullscreen
Definition: Window.h:98