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#include <string>
7#include <string_view>
8
9#include <libsbx/math/vector2.hpp>
10
11namespace sbx::graphics {
12
13class viewport {
14
15public:
16
17 enum class kind : std::uint8_t {
18 fixed,
19 named
20 }; // enum class kind
21
22 inline static constexpr auto window_name = std::string_view{"window"};
23
24 static auto fixed(const math::vector2u& size) -> viewport {
25 return viewport{kind::fixed, std::string{}, 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 fixed(math::vector2u{width, height});
30 }
31
32 static auto window(const math::vector2f& scale = math::vector2f{1.0f, 1.0f}) -> viewport {
33 return viewport{kind::named, std::string{window_name}, scale, math::vector2i{0, 0}, std::nullopt};
34 }
35
36 static auto named(std::string name, const math::vector2f& scale = math::vector2f{1.0f, 1.0f}) -> viewport {
37 return viewport{kind::named, std::move(name), scale, math::vector2i{0, 0}, std::nullopt};
38 }
39
40 auto scale() const noexcept -> const math::vector2f& {
41 return _scale;
42 }
43
44 auto offset() const noexcept -> const math::vector2i& {
45 return _offset;
46 }
47
48 auto size() const noexcept -> const std::optional<math::vector2u>& {
49 return _size;
50 }
51
52 auto is_fixed() const noexcept -> bool {
53 return _kind == kind::fixed;
54 }
55
56 auto is_named() const noexcept -> bool {
57 return _kind == kind::named;
58 }
59
60 auto name() const noexcept -> const std::string& {
61 return _name;
62 }
63
64private:
65
66 viewport(const kind kind, std::string name, const math::vector2f& scale, const math::vector2i& offset, const std::optional<math::vector2u>& size) noexcept
67 : _kind{kind},
68 _name{std::move(name)},
69 _scale{scale},
70 _offset{offset},
71 _size{size} { }
72
73 kind _kind;
74 std::string _name;
75 math::vector2f _scale;
76 math::vector2i _offset;
77 std::optional<math::vector2u> _size;
78
79}; // class viewport
80
82
83public:
84
85 render_area(const math::vector2u& extent = math::vector2u{}, const math::vector2i& offset = math::vector2i{}) noexcept
86 : _extent{extent},
87 _offset{offset},
88 _aspect_ratio{extent.y() == 0u ? 1.0f : static_cast<std::float_t>(extent.x()) / static_cast<std::float_t>(extent.y())} { }
89
90 auto operator==(const render_area& other) const noexcept -> bool {
91 return _extent == other._extent && _offset == other._offset;
92 }
93
94 auto extent() const noexcept -> const math::vector2u& {
95 return _extent;
96 }
97
98 auto set_extent(const math::vector2u& extent) noexcept -> void {
99 _extent = extent;
100 }
101
102 auto offset() const noexcept -> const math::vector2i& {
103 return _offset;
104 }
105
106 auto set_offset(const math::vector2i& offset) noexcept -> void {
107 _offset = offset;
108 }
109
110 auto aspect_ratio() const noexcept -> std::float_t {
111 return _aspect_ratio;
112 }
113
114 auto set_aspect_ratio(std::float_t aspect_ratio) noexcept -> void {
115 _aspect_ratio = aspect_ratio;
116 }
117
118private:
119
120 math::vector2u _extent;
121 math::vector2i _offset;
122 std::float_t _aspect_ratio;
123
124}; // class render_area
125
126} // namespace sbx::graphics
127
128#endif // LIBSBX_GRAPHICS_VIEWPORT_HPP_
Definition: viewport.hpp:81
Definition: viewport.hpp:13
A vector in two-dimensional space.
Definition: vector2.hpp:28