# Plutonium framework API: Plutonium/include/pu/ui/render/render_Renderer.hpp Source File

render_Renderer.hpp
Go to the documentation of this file.
/*
Plutonium library
@file render_Renderer.hpp
@brief A Renderer is the object performing basic rendering. (simply, a SDL2 wrapper)
@author XorTroll
@copyright Plutonium project - an easy-to-use UI framework for Nintendo Switch homebrew
*/
#pragma once
#include <pu/ui/ui_Types.hpp>
#include <pu/ui/render/render_SDL2.hpp>
#include <vector>
namespace pu::ui::render {
constexpr u32 ScreenWidth = 1280;
constexpr u32 ScreenHeight = 720;
struct RendererInitOptions {
u32 sdl_flags;
u32 sdl_render_flags;
u32 width;
u32 height;
bool init_ttf;
std::vector<u32> extra_default_font_sizes;
std::string default_font_path;
bool init_mixer;
u32 audio_mixer_flags;
bool init_img;
u32 sdl_img_flags;
bool init_pl;
bool init_romfs;
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) {}
inline void UseTTF(const std::string &default_font_path = "") {
this->init_ttf = true;
// Empty font path = using shared font
if(!default_font_path.empty()) {
this->default_font_path = default_font_path;
}
else {
this->init_pl = true;
}
}
inline void SetExtraDefaultFontSize(const u32 font_size) {
this->extra_default_font_sizes.push_back(font_size);
}
inline void UseAudio(const u32 audio_mixer_flags) {
this->init_mixer = true;
this->audio_mixer_flags = audio_mixer_flags;
}
inline void UseImage(const u32 sdl_img_flags) {
this->init_img = true;
this->sdl_img_flags = sdl_img_flags;
}
inline void UseRomfs() {
this->init_romfs = true;
}
};
constexpr u32 MixerAllFlags = MIX_INIT_FLAC | MIX_INIT_MOD | MIX_INIT_MP3 | MIX_INIT_OGG;
constexpr u32 IMGAllFlags = IMG_INIT_PNG | IMG_INIT_JPG | IMG_INIT_TIF | IMG_INIT_WEBP;
constexpr u32 RendererSoftwareFlags = SDL_RENDERER_SOFTWARE;
constexpr u32 RendererHardwareFlags = SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED;
struct TextureRenderOptions {
i32 alpha_mod;
i32 width;
i32 height;
float rot_angle;
static constexpr i32 NoAlpha = -1;
static constexpr i32 NoWidth = -1;
static constexpr i32 NoHeight = -1;
static constexpr float NoRotation = -1.0f;
static constexpr TextureRenderOptions WithCustomAlpha(const u8 alpha) {
return { alpha, NoWidth, NoHeight, NoRotation };
}
static constexpr TextureRenderOptions WithCustomDimensions(const i32 width, const i32 height) {
return { NoAlpha, width, height, NoRotation };
}
// No special options
static const TextureRenderOptions Default;
};
inline constexpr const TextureRenderOptions TextureRenderOptions::Default = { TextureRenderOptions::NoAlpha, TextureRenderOptions::NoWidth, TextureRenderOptions::NoHeight, TextureRenderOptions::NoRotation };
class Renderer {
private:
RendererInitOptions init_opts;
bool ok_romfs;
bool ok_pl;
bool initialized;
i32 base_x;
i32 base_y;
i32 base_a;
inline u8 GetActualAlpha(const u8 input_a) {
if(this->base_a >= 0) {
return static_cast<u8>(this->base_a);
}
else {
return input_a;
}
}
public:
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) {}
PU_SMART_CTOR(Renderer)
void Initialize();
void Finalize();
inline bool HasInitialized() {
return this->initialized;
}
inline bool HasRomFs() {
return this->ok_romfs;
}
void InitializeRender(const Color clr);
void FinalizeRender();
void RenderTexture(sdl2::Texture texture, const i32 x, const i32 y, const TextureRenderOptions opts = TextureRenderOptions::Default);
void RenderRectangle(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height);
void RenderRectangleFill(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height);
inline void RenderRectangleOutline(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 border_width) {
this->RenderRectangleFill(clr, x - border_width, y - border_width, width + (border_width * 2), height + (border_width * 2));
}
void RenderRoundedRectangle(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 radius);
void RenderRoundedRectangleFill(const Color clr, const i32 x, const i32 y, const i32 width, const i32 height, const i32 radius);
void RenderCircle(const Color clr, const i32 x, const i32 y, const i32 radius);
void RenderCircleFill(const Color clr, const i32 x, const i32 y, const i32 radius);
void RenderShadowSimple(const i32 x, const i32 y, const i32 width, const i32 height, const i32 base_alpha, const u8 main_alpha = 0xFF);
inline void SetBaseRenderPosition(const i32 x, const i32 y) {
this->base_x = x;
this->base_y = y;
}
inline void ResetBaseRenderPosition() {
this->SetBaseRenderPosition(0, 0);
}
inline void SetBaseRenderAlpha(const u8 alpha) {
this->base_a = alpha;
}
inline void ResetBaseRenderAlpha() {
this->base_a = -1;
}
};
// Global rendering
sdl2::Renderer GetMainRenderer();
sdl2::Window GetMainWindow();
sdl2::Surface GetMainSurface();
std::pair<u32, u32> GetDimensions();
// Text rendering
bool AddSharedFont(const std::string &font_name, const u32 font_size, const PlSharedFontType type);
bool AddAllSharedFonts(const std::string &font_name, const u32 font_size);
bool AddFontFile(const std::string &font_name, const u32 font_size, const std::string &path);
inline void AddDefaultFontFromShared(const u32 font_size) {
AddAllSharedFonts(MakeDefaultFontName(font_size), font_size);
}
inline void AddDefaultFontFromFile(const u32 font_size, const std::string &path) {
AddFontFile(MakeDefaultFontName(font_size), font_size, path);
}
sdl2::Texture RenderText(const std::string &font_name, const std::string &text, const Color clr);
i32 GetTextWidth(const std::string &font_name, const std::string &text);
i32 GetTextHeight(const std::string &font_name, const std::string &text);
}
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