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