Plutonium framework API 0.3.0 UI framework libraries for libnx

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 <vector>
18
19 namespace pu::ui::render {
20
21 constexpr u32 ScreenWidth = 1280;
22 constexpr u32 ScreenHeight = 720;
23
24 struct RendererInitOptions {
25 u32 sdl_flags;
26 u32 sdl_render_flags;
27 u32 width;
28 u32 height;
29 bool init_ttf;
30 std::vector<u32> extra_default_font_sizes;
31 std::string default_font_path;
32 bool init_mixer;
33 u32 audio_mixer_flags;
34 bool init_img;
35 u32 sdl_img_flags;
36 bool init_pl;
37 bool init_romfs;
38
39 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), init_ttf(false), extra_default_font_sizes(), default_font_path(), init_mixer(false), audio_mixer_flags(0), init_img(false), sdl_img_flags(0), init_pl(false), init_romfs(false) {}
40
41 inline void UseTTF(const std::string &default_font_path = "") {
42 this->init_ttf = true;
43
44 // Empty font path = using shared font
45 if(!default_font_path.empty()) {
46 this->default_font_path = default_font_path;
47 }
48 else {
49 this->init_pl = true;
50 }
51 }
52
53 inline void SetExtraDefaultFontSize(const u32 font_size) {
54 this->extra_default_font_sizes.push_back(font_size);
55 }
56
57 inline void UseAudio(const u32 audio_mixer_flags) {
58 this->init_mixer = true;
59 this->audio_mixer_flags = audio_mixer_flags;
60 }
61
62 inline void UseImage(const u32 sdl_img_flags) {
63 this->init_img = true;
64 this->sdl_img_flags = sdl_img_flags;
65 }
66
67 inline void UseRomfs() {
68 this->init_romfs = true;
69 }
70 };
71
72 constexpr u32 MixerAllFlags = MIX_INIT_FLAC | MIX_INIT_MOD | MIX_INIT_MP3 | MIX_INIT_OGG;
73 constexpr u32 IMGAllFlags = IMG_INIT_PNG | IMG_INIT_JPG | IMG_INIT_TIF | IMG_INIT_WEBP;
74 constexpr u32 RendererSoftwareFlags = SDL_RENDERER_SOFTWARE;
75 constexpr u32 RendererHardwareFlags = SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED;
76
77 struct TextureRenderOptions {
78 i32 alpha_mod;
79 i32 width;
80 i32 height;
81 float rot_angle;
82
83 static constexpr i32 NoAlpha = -1;
84 static constexpr i32 NoWidth = -1;
85 static constexpr i32 NoHeight = -1;
86 static constexpr float NoRotation = -1.0f;
87
88 static constexpr TextureRenderOptions WithCustomAlpha(const u8 alpha) {
89 return { alpha, NoWidth, NoHeight, NoRotation };
90 }
91
92 static constexpr TextureRenderOptions WithCustomDimensions(const i32 width, const i32 height) {
93 return { NoAlpha, width, height, NoRotation };
94 }
95
96 // No special options
97 static const TextureRenderOptions Default;
98 };
99
100 inline constexpr const TextureRenderOptions TextureRenderOptions::Default = { TextureRenderOptions::NoAlpha, TextureRenderOptions::NoWidth, TextureRenderOptions::NoHeight, TextureRenderOptions::NoRotation };
101
102 class Renderer {
103 private:
104 RendererInitOptions init_opts;
105 bool ok_romfs;
106 bool ok_pl;
107 bool initialized;
108 i32 base_x;
109 i32 base_y;
110 i32 base_a;
111
112 inline u8 GetActualAlpha(const u8 input_a) {
113 if(this->base_a >= 0) {
114 return static_cast<u8>(this->base_a);
115 }
116 else {
117 return input_a;
118 }
119 }
120
121 public:
122 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) {}
123 PU_SMART_CTOR(Renderer)
124
125 void Initialize();
126 void Finalize();
127
128 inline bool HasInitialized() {
129 return this->initialized;
130 }
131
132 inline bool HasRomFs() {
133 return this->ok_romfs;
134 }
135
136 void InitializeRender(const Color clr);
137 void FinalizeRender();
138 void RenderTexture(sdl2::Texture texture, const i32 x, const i32 y, const TextureRenderOptions opts = TextureRenderOptions::Default);
139 void RenderRectangle(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height);
140 void RenderRectangleFill(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height);
141
142 inline void RenderRectangleOutline(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 border_width) {
143 this->RenderRectangleFill(clr, x - border_width, y - border_width, width + (border_width * 2), height + (border_width * 2));
144 }
145
146 void RenderRoundedRectangle(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 radius);
147 void RenderRoundedRectangleFill(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 radius);
148 void RenderCircle(const Color clr, const i32 x, const i32 y, const i32 radius);
149 void RenderCircleFill(const Color clr, const i32 x, const i32 y, const i32 radius);
150 void RenderShadowSimple(const i32 x, const i32 y, const i32 width, const i32 height, const i32 base_alpha, const u8 main_alpha = 0xFF);
151
152 inline void SetBaseRenderPosition(const i32 x, const i32 y) {
153 this->base_x = x;
154 this->base_y = y;
155 }
156
157 inline void ResetBaseRenderPosition() {
158 this->SetBaseRenderPosition(0, 0);
159 }
160
161 inline void SetBaseRenderAlpha(const u8 alpha) {
162 this->base_a = alpha;
163 }
164
165 inline void ResetBaseRenderAlpha() {
166 this->base_a = -1;
167 }
168 };
169
170 // Global rendering
171
172 sdl2::Renderer GetMainRenderer();
173 sdl2::Window GetMainWindow();
174 sdl2::Surface GetMainSurface();
175
176 std::pair<u32, u32> GetDimensions();
177
178 // Text rendering
179
180 bool AddSharedFont(const std::string &font_name, const u32 font_size, const PlSharedFontType type);
181 bool AddAllSharedFonts(const std::string &font_name, const u32 font_size);
182 bool AddFontFile(const std::string &font_name, const u32 font_size, const std::string &path);
183
184 inline void AddDefaultFontFromShared(const u32 font_size) {
185 AddAllSharedFonts(MakeDefaultFontName(font_size), font_size);
186 }
187
188 inline void AddDefaultFontFromFile(const u32 font_size, const std::string &path) {
189 AddFontFile(MakeDefaultFontName(font_size), font_size, path);
190 }
191
192 sdl2::Texture RenderText(const std::string &font_name, const std::string &text, const Color clr);
193 i32 GetTextWidth(const std::string &font_name, const std::string &text);
194 i32 GetTextHeight(const std::string &font_name, const std::string &text);
195
196}
pu::ui::render::Renderer
Definition: render_Renderer.hpp:102
pu::ui::render::Renderer::ResetBaseRenderAlpha
void ResetBaseRenderAlpha()
Definition: render_Renderer.hpp:165
pu::ui::render::Renderer::RenderCircle
void RenderCircle(const Color clr, const i32 x, const i32 y, const i32 radius)
pu::ui::render::Renderer::RenderRectangleOutline
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:142
pu::ui::render::Renderer::Finalize
void Finalize()
pu::ui::render::Renderer::RenderRectangle
void RenderRectangle(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height)
pu::ui::render::Renderer::Renderer
Renderer(const RendererInitOptions init_opts)
Definition: render_Renderer.hpp:122
pu::ui::render::Renderer::RenderShadowSimple
void RenderShadowSimple(const i32 x, const i32 y, const i32 width, const i32 height, const i32 base_alpha, const u8 main_alpha=0xFF)
pu::ui::render::Renderer::RenderRoundedRectangle
void RenderRoundedRectangle(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 radius)
pu::ui::render::Renderer::Initialize
void Initialize()
pu::ui::render::Renderer::HasRomFs
bool HasRomFs()
Definition: render_Renderer.hpp:132
pu::ui::render::Renderer::SetBaseRenderAlpha
void SetBaseRenderAlpha(const u8 alpha)
Definition: render_Renderer.hpp:161
pu::ui::render::Renderer::RenderRectangleFill
void RenderRectangleFill(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height)
pu::ui::render::Renderer::RenderRoundedRectangleFill
void RenderRoundedRectangleFill(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 radius)
pu::ui::render::Renderer::FinalizeRender
void FinalizeRender()
pu::ui::render::Renderer::RenderCircleFill
void RenderCircleFill(const Color clr, const i32 x, const i32 y, const i32 radius)
pu::ui::render::Renderer::ResetBaseRenderPosition
void ResetBaseRenderPosition()
Definition: render_Renderer.hpp:157
pu::ui::render::Renderer::RenderTexture
void RenderTexture(sdl2::Texture texture, const i32 x, const i32 y, const TextureRenderOptions opts=TextureRenderOptions::Default)
pu::ui::render::Renderer::SetBaseRenderPosition
void SetBaseRenderPosition(const i32 x, const i32 y)
Definition: render_Renderer.hpp:152
pu::ui::render::Renderer::InitializeRender
void InitializeRender(const Color clr)
pu::ui::render::Renderer::HasInitialized
bool HasInitialized()
Definition: render_Renderer.hpp:128
pu::sdl2::Surface
SDL_Surface * Surface
Definition: sdl2_Types.hpp:15
pu::sdl2::Renderer
SDL_Renderer * Renderer
Definition: sdl2_Types.hpp:13
pu::sdl2::Window
SDL_Window * Window
Definition: sdl2_Types.hpp:12
pu::sdl2::Texture
SDL_Texture * Texture
Definition: sdl2_Types.hpp:11
pu::ui::render
Definition: render_Renderer.hpp:19
pu::ui::render::MixerAllFlags
constexpr u32 MixerAllFlags
Definition: render_Renderer.hpp:72
pu::ui::render::GetMainWindow
sdl2::Window GetMainWindow()
pu::ui::render::RenderText
sdl2::Texture RenderText(const std::string &font_name, const std::string &text, const Color clr)
pu::ui::render::AddFontFile
bool AddFontFile(const std::string &font_name, const u32 font_size, const std::string &path)
pu::ui::render::GetMainSurface
sdl2::Surface GetMainSurface()
pu::ui::render::AddDefaultFontFromFile
void AddDefaultFontFromFile(const u32 font_size, const std::string &path)
Definition: render_Renderer.hpp:188
pu::ui::render::AddSharedFont
bool AddSharedFont(const std::string &font_name, const u32 font_size, const PlSharedFontType type)
pu::ui::render::RendererSoftwareFlags
constexpr u32 RendererSoftwareFlags
Definition: render_Renderer.hpp:74
pu::ui::render::AddAllSharedFonts
bool AddAllSharedFonts(const std::string &font_name, const u32 font_size)
pu::ui::render::GetTextWidth
i32 GetTextWidth(const std::string &font_name, const std::string &text)
pu::ui::render::GetDimensions
std::pair< u32, u32 > GetDimensions()
pu::ui::render::GetMainRenderer
sdl2::Renderer GetMainRenderer()
pu::ui::render::IMGAllFlags
constexpr u32 IMGAllFlags
Definition: render_Renderer.hpp:73
pu::ui::render::GetTextHeight
i32 GetTextHeight(const std::string &font_name, const std::string &text)
pu::ui::render::AddDefaultFontFromShared
void AddDefaultFontFromShared(const u32 font_size)
Definition: render_Renderer.hpp:184
pu::ui::render::ScreenWidth
constexpr u32 ScreenWidth
Definition: render_Renderer.hpp:21
pu::ui::render::RendererHardwareFlags
constexpr u32 RendererHardwareFlags
Definition: render_Renderer.hpp:75
pu::ui::render::ScreenHeight
constexpr u32 ScreenHeight
Definition: render_Renderer.hpp:22
pu::ui::MakeDefaultFontName
std::string MakeDefaultFontName(const u32 font_size)
Definition: ui_Types.hpp:32
pu::i32
s32 i32
Definition: pu_Include.hpp:28
PU_SMART_CTOR
#define PU_SMART_CTOR(type)
Definition: pu_Include.hpp:19
render_SDL2.hpp
pu::ui::Color
Definition: ui_Types.hpp:44
pu::ui::render::RendererInitOptions
Definition: render_Renderer.hpp:24
pu::ui::render::RendererInitOptions::height
u32 height
Definition: render_Renderer.hpp:28
pu::ui::render::RendererInitOptions::extra_default_font_sizes
std::vector< u32 > extra_default_font_sizes
Definition: render_Renderer.hpp:30
pu::ui::render::RendererInitOptions::init_ttf
bool init_ttf
Definition: render_Renderer.hpp:29
pu::ui::render::RendererInitOptions::UseImage
void UseImage(const u32 sdl_img_flags)
Definition: render_Renderer.hpp:62
pu::ui::render::RendererInitOptions::SetExtraDefaultFontSize
void SetExtraDefaultFontSize(const u32 font_size)
Definition: render_Renderer.hpp:53
pu::ui::render::RendererInitOptions::sdl_render_flags
u32 sdl_render_flags
Definition: render_Renderer.hpp:26
pu::ui::render::RendererInitOptions::audio_mixer_flags
u32 audio_mixer_flags
Definition: render_Renderer.hpp:33
pu::ui::render::RendererInitOptions::init_mixer
bool init_mixer
Definition: render_Renderer.hpp:32
pu::ui::render::RendererInitOptions::RendererInitOptions
RendererInitOptions(const u32 sdl_flags, const u32 sdl_render_flags, const u32 w=ScreenWidth, const u32 h=ScreenHeight)
Definition: render_Renderer.hpp:39
pu::ui::render::RendererInitOptions::init_romfs
bool init_romfs
Definition: render_Renderer.hpp:37
pu::ui::render::RendererInitOptions::init_pl
bool init_pl
Definition: render_Renderer.hpp:36
pu::ui::render::RendererInitOptions::init_img
bool init_img
Definition: render_Renderer.hpp:34
pu::ui::render::RendererInitOptions::UseTTF
void UseTTF(const std::string &default_font_path="")
Definition: render_Renderer.hpp:41
pu::ui::render::RendererInitOptions::width
u32 width
Definition: render_Renderer.hpp:27
pu::ui::render::RendererInitOptions::default_font_path
std::string default_font_path
Definition: render_Renderer.hpp:31
pu::ui::render::RendererInitOptions::sdl_img_flags
u32 sdl_img_flags
Definition: render_Renderer.hpp:35
pu::ui::render::RendererInitOptions::UseAudio
void UseAudio(const u32 audio_mixer_flags)
Definition: render_Renderer.hpp:57
pu::ui::render::RendererInitOptions::UseRomfs
void UseRomfs()
Definition: render_Renderer.hpp:67
pu::ui::render::RendererInitOptions::sdl_flags
u32 sdl_flags
Definition: render_Renderer.hpp:25
pu::ui::render::TextureRenderOptions
Definition: render_Renderer.hpp:77
pu::ui::render::TextureRenderOptions::rot_angle
float rot_angle
Definition: render_Renderer.hpp:81
pu::ui::render::TextureRenderOptions::NoWidth
static constexpr i32 NoWidth
Definition: render_Renderer.hpp:84
pu::ui::render::TextureRenderOptions::alpha_mod
i32 alpha_mod
Definition: render_Renderer.hpp:78
pu::ui::render::TextureRenderOptions::NoAlpha
static constexpr i32 NoAlpha
Definition: render_Renderer.hpp:83
pu::ui::render::TextureRenderOptions::width
i32 width
Definition: render_Renderer.hpp:79
pu::ui::render::TextureRenderOptions::WithCustomDimensions
static constexpr TextureRenderOptions WithCustomDimensions(const i32 width, const i32 height)
Definition: render_Renderer.hpp:92
pu::ui::render::TextureRenderOptions::Default
static const TextureRenderOptions Default
Definition: render_Renderer.hpp:97
pu::ui::render::TextureRenderOptions::WithCustomAlpha
static constexpr TextureRenderOptions WithCustomAlpha(const u8 alpha)
Definition: render_Renderer.hpp:88
pu::ui::render::TextureRenderOptions::height
i32 height
Definition: render_Renderer.hpp:80
pu::ui::render::TextureRenderOptions::NoRotation
static constexpr float NoRotation
Definition: render_Renderer.hpp:86
pu::ui::render::TextureRenderOptions::NoHeight
static constexpr i32 NoHeight
Definition: render_Renderer.hpp:85
ui_Types.hpp