Plutonium framework API 0.3.0
UI framework libraries for libnx
Loading...
Searching...
No Matches
render_Renderer.hpp
Go to the documentation of this file.
1
2/*
3
4 Plutonium library
5
6 @file render_Renderer.hpp
7 @brief A Renderer is the object performing basic rendering. (simply, a SDL2 wrapper)
8 @author XorTroll
9
10 @copyright Plutonium project - an easy-to-use UI framework for Nintendo Switch homebrew
11
12*/
13
14#pragma once
15#include <pu/ui/ui_Types.hpp>
16#include <pu/ui/render/render_SDL2.hpp>
17#include <pu/ttf/ttf_Font.hpp>
18#include <vector>
19
20namespace pu::ui::render {
21
22 constexpr u32 BaseScreenWidth = 1280;
23 constexpr u32 BaseScreenHeight = 720;
24
25 constexpr u32 ScreenWidth = 1920;
26 constexpr u32 ScreenHeight = 1080;
27
28 constexpr double ScreenFactor = (double)ScreenWidth / (double)BaseScreenWidth;
29
46
47 RendererInitOptions(const u32 sdl_flags, const u32 sdl_render_flags, const u32 w = ScreenWidth, const u32 h = ScreenHeight) : sdl_flags(sdl_flags), sdl_render_flags(sdl_render_flags), width(w), height(h), default_shared_fonts(), default_font_paths(), extra_default_font_sizes(), init_mixer(false), audio_mixer_flags(0), init_img(false), sdl_img_flags(0), init_romfs(false), pad_player_count(1), pad_id_mask(0), pad_style_tag(0) {}
48
49 inline void AddDefaultSharedFont(const PlSharedFontType type) {
50 this->default_shared_fonts.push_back(type);
51 }
52
54 for(u32 i = 0; i < PlSharedFontType_Total; i++) {
55 this->default_shared_fonts.push_back(static_cast<PlSharedFontType>(i));
56 }
57 }
58
59 inline void AddDefaultFontPath(const std::string &font_path) {
60 this->default_font_paths.push_back(font_path);
61 }
62
63 inline void AddExtraDefaultFontSize(const u32 font_size) {
64 this->extra_default_font_sizes.push_back(font_size);
65 }
66
67 inline void UseAudio(const u32 audio_mixer_flags) {
68 this->init_mixer = true;
69 this->audio_mixer_flags = audio_mixer_flags;
70 }
71
72 inline void UseImage(const u32 sdl_img_flags) {
73 this->init_img = true;
74 this->sdl_img_flags = sdl_img_flags;
75 }
76
77 inline void UseRomfs() {
78 this->init_romfs = true;
79 }
80
81 inline void SetInputPlayerCount(const u32 count) {
82 this->pad_player_count = count;
83 }
84
85 inline void AddInputNpadIdType(const u64 type) {
86 this->pad_id_mask |= BITL(type);
87 }
88
89 inline void AddInputNpadStyleTag(const u32 tag) {
90 this->pad_style_tag |= tag;
91 }
92 };
93
98
104
105 static constexpr i32 NoAlpha = -1;
106 static constexpr i32 NoWidth = -1;
107 static constexpr i32 NoHeight = -1;
108 static constexpr float NoRotation = -1.0f;
109
110 static constexpr TextureRenderOptions Default() {
111 return { NoAlpha, NoWidth, NoHeight, NoRotation };
112 }
113
114 static constexpr TextureRenderOptions WithCustomAlpha(const u8 alpha) {
115 return { alpha, NoWidth, NoHeight, NoRotation };
116 }
117
118 static constexpr TextureRenderOptions WithCustomDimensions(const i32 width, const i32 height) {
119 return { NoAlpha, width, height, NoRotation };
120 }
121
122 static constexpr TextureRenderOptions WithCustomAlphaAndDimensions(const u8 alpha, const i32 width, const i32 height) {
123 return { alpha, width, height, NoRotation };
124 }
125 };
126
127 class Renderer {
128 private:
129 RendererInitOptions init_opts;
130 bool ttf_init;
131 bool ok_romfs;
132 bool ok_pl;
133 bool initialized;
134 i32 base_x;
135 i32 base_y;
136 i32 base_a;
137 PadState input_pad;
138
139 inline u8 GetActualAlpha(const u8 input_a) {
140 if(this->base_a >= 0) {
141 return static_cast<u8>(this->base_a);
142 }
143 else {
144 return input_a;
145 }
146 }
147
148 public:
149 Renderer(const RendererInitOptions init_opts) : init_opts(init_opts), ok_romfs(false), ok_pl(false), initialized(false), base_x(0), base_y(0), base_a(0), input_pad() {}
151
153 void Finalize();
154
155 inline bool HasInitialized() {
156 return this->initialized;
157 }
158
159 inline bool HasRomFs() {
160 return this->ok_romfs;
161 }
162
163 void InitializeRender(const Color clr);
165 void RenderTexture(sdl2::Texture texture, const i32 x, const i32 y, const TextureRenderOptions opts = TextureRenderOptions::Default());
166 void RenderRectangle(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height);
167 void RenderRectangleFill(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height);
168
169 inline void RenderRectangleOutline(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 border_width) {
170 this->RenderRectangleFill(clr, x - border_width, y - border_width, width + (border_width * 2), height + (border_width * 2));
171 }
172
173 void RenderRoundedRectangle(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 radius);
174 void RenderRoundedRectangleFill(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 radius);
175 void RenderCircle(const Color clr, const i32 x, const i32 y, const i32 radius);
176 void RenderCircleFill(const Color clr, const i32 x, const i32 y, const i32 radius);
177 void RenderShadowSimple(const i32 x, const i32 y, const i32 width, const i32 height, const i32 base_alpha, const u8 main_alpha = 0xFF);
178
179 inline void SetBaseRenderPosition(const i32 x, const i32 y) {
180 this->base_x = x;
181 this->base_y = y;
182 }
183
185 this->SetBaseRenderPosition(0, 0);
186 }
187
188 inline void SetBaseRenderAlpha(const u8 alpha) {
189 this->base_a = alpha;
190 }
191
192 inline void ResetBaseRenderAlpha() {
193 this->base_a = -1;
194 }
195
196 inline void UpdateInput() {
197 padUpdate(&this->input_pad);
198 }
199
201 return padGetButtonsDown(&this->input_pad);
202 }
203
204 inline u64 GetButtonsUp() {
205 return padGetButtonsUp(&this->input_pad);
206 }
207
209 return padGetButtons(&this->input_pad);
210 }
211 };
212
213 // Global rendering
214
218
220
221 // Font loading
222
223 bool AddFont(const std::string &font_name, std::shared_ptr<ttf::Font> &font);
224
225 bool LoadSingleSharedFontInFont(std::shared_ptr<ttf::Font> &font, const PlSharedFontType type);
226 bool LoadAllSharedFontsInFont(std::shared_ptr<ttf::Font> &font);
227
228 inline void AddDefaultFont(std::shared_ptr<ttf::Font> &font) {
229 AddFont(MakeDefaultFontName(font->GetFontSize()), font);
230 }
231
232 // Text rendering
233
234 bool GetTextDimensions(const std::string &font_name, const std::string &text, i32 &out_width, i32 &out_height);
235 i32 GetTextWidth(const std::string &font_name, const std::string &text);
236 i32 GetTextHeight(const std::string &font_name, const std::string &text);
237 sdl2::Texture RenderText(const std::string &font_name, const std::string &text, const Color clr, const u32 max_width = 0, const u32 max_height = 0);
238
239}
Definition ttf_Font.hpp:9
Definition render_Renderer.hpp:127
u64 GetButtonsUp()
Definition render_Renderer.hpp:204
void ResetBaseRenderAlpha()
Definition render_Renderer.hpp:192
void RenderCircle(const Color clr, const i32 x, const i32 y, const i32 radius)
void RenderRectangleOutline(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 border_width)
Definition render_Renderer.hpp:169
void RenderRectangle(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height)
Renderer(const RendererInitOptions init_opts)
Definition render_Renderer.hpp:149
void RenderShadowSimple(const i32 x, const i32 y, const i32 width, const i32 height, const i32 base_alpha, const u8 main_alpha=0xFF)
void RenderRoundedRectangle(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 radius)
bool HasRomFs()
Definition render_Renderer.hpp:159
void SetBaseRenderAlpha(const u8 alpha)
Definition render_Renderer.hpp:188
void RenderRectangleFill(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height)
void RenderRoundedRectangleFill(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 radius)
u64 GetButtonsDown()
Definition render_Renderer.hpp:200
u64 GetButtonsHeld()
Definition render_Renderer.hpp:208
void RenderTexture(sdl2::Texture texture, const i32 x, const i32 y, const TextureRenderOptions opts=TextureRenderOptions::Default())
void RenderCircleFill(const Color clr, const i32 x, const i32 y, const i32 radius)
void ResetBaseRenderPosition()
Definition render_Renderer.hpp:184
void UpdateInput()
Definition render_Renderer.hpp:196
void SetBaseRenderPosition(const i32 x, const i32 y)
Definition render_Renderer.hpp:179
void InitializeRender(const Color clr)
bool HasInitialized()
Definition render_Renderer.hpp:155
Definition sdl2_Types.hpp:10
Definition ttf_Font.hpp:7
Definition render_Renderer.hpp:20
constexpr u32 MixerAllFlags
Definition render_Renderer.hpp:94
bool LoadAllSharedFontsInFont(std::shared_ptr< ttf::Font > &font)
sdl2::Window GetMainWindow()
bool AddFont(const std::string &font_name, std::shared_ptr< ttf::Font > &font)
bool LoadSingleSharedFontInFont(std::shared_ptr< ttf::Font > &font, const PlSharedFontType type)
sdl2::Texture RenderText(const std::string &font_name, const std::string &text, const Color clr, const u32 max_width=0, const u32 max_height=0)
bool GetTextDimensions(const std::string &font_name, const std::string &text, i32 &out_width, i32 &out_height)
constexpr u32 BaseScreenWidth
Definition render_Renderer.hpp:22
sdl2::Surface GetMainSurface()
constexpr u32 BaseScreenHeight
Definition render_Renderer.hpp:23
constexpr u32 RendererSoftwareFlags
Definition render_Renderer.hpp:96
i32 GetTextWidth(const std::string &font_name, const std::string &text)
std::pair< u32, u32 > GetDimensions()
sdl2::Renderer GetMainRenderer()
constexpr u32 IMGAllFlags
Definition render_Renderer.hpp:95
i32 GetTextHeight(const std::string &font_name, const std::string &text)
void AddDefaultFont(std::shared_ptr< ttf::Font > &font)
Definition render_Renderer.hpp:228
constexpr double ScreenFactor
Definition render_Renderer.hpp:28
constexpr u32 ScreenWidth
Definition render_Renderer.hpp:25
constexpr u32 RendererHardwareFlags
Definition render_Renderer.hpp:97
constexpr u32 ScreenHeight
Definition render_Renderer.hpp:26
#define PU_SMART_CTOR(type)
Definition pu_Include.hpp:20
Definition ui_Types.hpp:44
Definition render_Renderer.hpp:30
u32 height
Definition render_Renderer.hpp:34
std::vector< u32 > extra_default_font_sizes
Definition render_Renderer.hpp:37
u32 pad_style_tag
Definition render_Renderer.hpp:45
void UseImage(const u32 sdl_img_flags)
Definition render_Renderer.hpp:72
void SetInputPlayerCount(const u32 count)
Definition render_Renderer.hpp:81
std::vector< std::string > default_font_paths
Definition render_Renderer.hpp:36
void AddExtraDefaultFontSize(const u32 font_size)
Definition render_Renderer.hpp:63
u32 sdl_render_flags
Definition render_Renderer.hpp:32
u32 audio_mixer_flags
Definition render_Renderer.hpp:39
u64 pad_id_mask
Definition render_Renderer.hpp:44
void AddDefaultFontPath(const std::string &font_path)
Definition render_Renderer.hpp:59
u32 pad_player_count
Definition render_Renderer.hpp:43
bool init_mixer
Definition render_Renderer.hpp:38
RendererInitOptions(const u32 sdl_flags, const u32 sdl_render_flags, const u32 w=ScreenWidth, const u32 h=ScreenHeight)
Definition render_Renderer.hpp:47
bool init_romfs
Definition render_Renderer.hpp:42
void AddInputNpadIdType(const u64 type)
Definition render_Renderer.hpp:85
bool init_img
Definition render_Renderer.hpp:40
std::vector< PlSharedFontType > default_shared_fonts
Definition render_Renderer.hpp:35
u32 width
Definition render_Renderer.hpp:33
u32 sdl_img_flags
Definition render_Renderer.hpp:41
void UseAudio(const u32 audio_mixer_flags)
Definition render_Renderer.hpp:67
void UseRomfs()
Definition render_Renderer.hpp:77
void AddDefaultAllSharedFonts()
Definition render_Renderer.hpp:53
void AddDefaultSharedFont(const PlSharedFontType type)
Definition render_Renderer.hpp:49
void AddInputNpadStyleTag(const u32 tag)
Definition render_Renderer.hpp:89
u32 sdl_flags
Definition render_Renderer.hpp:31
Definition render_Renderer.hpp:99
float rot_angle
Definition render_Renderer.hpp:103
static constexpr i32 NoWidth
Definition render_Renderer.hpp:106
static constexpr TextureRenderOptions Default()
Definition render_Renderer.hpp:110
i32 alpha_mod
Definition render_Renderer.hpp:100
static constexpr i32 NoAlpha
Definition render_Renderer.hpp:105
i32 width
Definition render_Renderer.hpp:101
static constexpr TextureRenderOptions WithCustomDimensions(const i32 width, const i32 height)
Definition render_Renderer.hpp:118
static constexpr TextureRenderOptions WithCustomAlpha(const u8 alpha)
Definition render_Renderer.hpp:114
i32 height
Definition render_Renderer.hpp:102
static constexpr float NoRotation
Definition render_Renderer.hpp:108
static constexpr i32 NoHeight
Definition render_Renderer.hpp:107
static constexpr TextureRenderOptions WithCustomAlphaAndDimensions(const u8 alpha, const i32 width, const i32 height)
Definition render_Renderer.hpp:122