Window.hpp
1// Copyright 2019 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5#ifndef SMK_WINDOW_HPP
6#define SMK_WINDOW_HPP
7
8#include <functional>
9#include <glm/glm.hpp>
10#include <memory>
11#include <smk/RenderTarget.hpp>
12#include <string>
13
14struct GLFWwindow;
15
16namespace smk {
17
18class View;
19class VertexArray;
20class Sprite;
21class Input;
22class InputImpl;
23
24/// @brief A window. You can draw objects on the window.
25///
26/// Example:
27/// --------
28/// ~~~cpp
29/// auto window = smk::Window(640, 480, "Window title");
30/// ~~~
31class Window : public RenderTarget {
32 public:
33 Window();
34 Window(int width, int height, const std::string& title);
35 ~Window();
36
37 GLFWwindow* window() const;
38 float time() const;
39 Input& input();
40
41 void ExecuteMainLoop(const std::function<void(void)>& loop);
42 void ExecuteMainLoopUntil(const std::function<bool(void)>& loop);
43
44 // Pool new events. This update the |input()| element.
45 void PoolEvents();
46
47 // Notify the current frame is ready to be displayed. The current and next one
48 // are swapped.
49 void Display();
50
51 // Wait until the end of the frame to maintain a targetted frame per seconds.
52 // (optional).
53 void LimitFrameRate(float fps);
54
55 // Returns true when the user wants to close the window.
56 bool ShouldClose();
57
58 // Move-only ressource.
59 Window(Window&&) noexcept;
60 Window(const Window&) = delete;
61 Window& operator=(Window&&) noexcept;
62 Window& operator=(const Window&) = delete;
63
64 private:
65 GLFWwindow* window_ = nullptr;
66
67 // Time:
68 float time_ = 0.f;
69 float time_last_sleep_ = 0.f;
70
71 void UpdateDimensions();
72
73 std::unique_ptr<InputImpl> input_;
74 int id_ = 0;
75
76 std::string module_canvas_selector_;
77};
78
79} // namespace smk
80
81#endif /* end of include guard: SMK_WINDOW_HPP */
A class for querying input state (keyboard, mouse, touch). The Input class is tied to a smk::Window....
Definition: Input.hpp:29
int height() const
the height of the surface.
int width() const
the width of the surface.
A window. You can draw objects on the window.
Definition: Window.hpp:31
void ExecuteMainLoop(const std::function< void(void)> &loop)
Helper function. Execute the main loop of the application. On web based application: registers the lo...
Definition: Window.cpp:297
void PoolEvents()
Handle all the new input events. This update the input() object.
Definition: Window.cpp:238
bool ShouldClose()
Returns true when the user wants to close the window.
Definition: Window.cpp:340
void Display()
Present what has been draw to the screen.
Definition: Window.cpp:244
void ExecuteMainLoopUntil(const std::function< bool(void)> &loop)
Helper function. Execute the main loop of the application. On web based application: registers the lo...
Definition: Window.cpp:280
float time() const
The last time Window::PoolEvent() was called.
Definition: Window.cpp:265
Window()
A null window.
Definition: Window.cpp:117
GLFWwindow * window() const
The window handle.
Definition: Window.cpp:260
Input & input()
Return an object for querying the input state.
Definition: Window.cpp:270
void LimitFrameRate(float fps)
Definition: Window.cpp:327