sandbox
Loading...
Searching...
No Matches
viewport.hpp
1#ifndef LIBSBX_GRAPHICS_VIEWPORT_HPP_
2#define LIBSBX_GRAPHICS_VIEWPORT_HPP_
3
4#include <optional>
5
6#include <libsbx/utility/enum.hpp>
7
8#include <libsbx/math/vector2.hpp>
9
10namespace sbx::graphics {
11
12class viewport {
13
14public:
15
16 enum class type : std::uint8_t {
17 fixed = utility::bit_v<0>,
18 window = utility::bit_v<1>,
19 dynamic = utility::bit_v<2>,
20 all = fixed | window | dynamic
21 }; // enum class type
22
23 static auto fixed(const math::vector2u& size) -> viewport {
24 return viewport{type::fixed, math::vector2f{1.0f, 1.0f}, math::vector2i{0, 0}, size};
25 }
26
27 static auto fixed(const std::uint32_t width, const std::uint32_t height) -> viewport {
28 return viewport{type::fixed, math::vector2f{1.0f, 1.0f}, math::vector2i{0, 0}, math::vector2u{width, height}};
29 }
30
31 static auto window() -> viewport {
32 return viewport{type::window, math::vector2f{1.0f, 1.0f}, math::vector2i{0, 0}, std::nullopt};
33 }
34
35 static auto dynamic() -> viewport {
36 return viewport{type::dynamic, math::vector2f{1.0f, 1.0f}, math::vector2i{0, 0}, std::nullopt};
37 }
38
39 auto scale() const noexcept -> const math::vector2f& {
40 return _scale;
41 }
42
43 auto set_scale(const math::vector2f& scale) noexcept -> void {
44 _scale = scale;
45 }
46
47 auto offset() const noexcept -> const math::vector2i& {
48 return _offset;
49 }
50
51 auto set_offset(const math::vector2i& offset) noexcept -> void {
52 _offset = offset;
53 }
54
55 auto size() const noexcept -> const std::optional<math::vector2u>& {
56 return _size;
57 }
58
59 auto set_size(const math::vector2u& size) noexcept -> void {
60 _size = size;
61 }
62
63 auto is_fixed() const noexcept -> bool {
64 return _type == type::fixed;
65 }
66
67 auto is_window() const noexcept -> bool {
68 return _type == type::window;
69 }
70
71 auto is_dynamic() const noexcept -> bool {
72 return _type == type::dynamic;
73 }
74
75 auto is_type(const type flags) const noexcept -> bool {
76 return utility::to_underlying(flags) & utility::to_underlying(_type);
77 }
78
79private:
80
81 viewport(const type type, const math::vector2f& scale, const math::vector2i& offset, const std::optional<math::vector2u>& size = std::nullopt) noexcept
82 : _type{type},
83 _scale{scale},
84 _offset{offset},
85 _size{size} { }
86
87 type _type;
88 math::vector2f _scale;
89 math::vector2i _offset;
90 std::optional<math::vector2u> _size;
91
92}; // class viewport
93
94inline constexpr auto operator|(const viewport::type lhs, const viewport::type rhs) noexcept -> viewport::type {
95 return utility::from_underlying<viewport::type>(utility::to_underlying(lhs) | utility::to_underlying(rhs));
96}
97
99
100public:
101
102 render_area(const math::vector2u& extent = math::vector2u{}, const math::vector2i& offset = math::vector2i{}) noexcept
103 : _extent{extent},
104 _offset{offset},
105 _aspect_ratio{static_cast<std::float_t>(extent.x()) / static_cast<std::float_t>(extent.y())} { }
106
107 auto operator==(const render_area& other) const noexcept -> bool {
108 return _extent == other._extent && _offset == other._offset;
109 }
110
111 auto extent() const noexcept -> const math::vector2u& {
112 return _extent;
113 }
114
115 auto set_extent(const math::vector2u& extent) noexcept -> void {
116 _extent = extent;
117 }
118
119 auto offset() const noexcept -> const math::vector2i& {
120 return _offset;
121 }
122
123 auto set_offset(const math::vector2i& offset) noexcept -> void {
124 _offset = offset;
125 }
126
127 auto aspect_ratio() const noexcept -> std::float_t {
128 return _aspect_ratio;
129 }
130
131 auto set_aspect_ratio(std::float_t aspect_ratio) noexcept -> void {
132 _aspect_ratio = aspect_ratio;
133 }
134
135private:
136
137 math::vector2u _extent;
138 math::vector2i _offset;
139 std::float_t _aspect_ratio;
140
141}; // class render_area
142
143}; // namespace sbx::graphics
144
145#endif // LIBSBX_GRAPHICS_VIEWPORT_HPP_
Definition: viewport.hpp:98
Definition: viewport.hpp:12
A vector in two-dimensional space.
Definition: vector2.hpp:27