code formatting.

This commit is contained in:
Andrea Blankenstijn 2021-08-12 16:10:49 +02:00
parent d1cd97809e
commit 5cb15abe11
10 changed files with 151 additions and 157 deletions

View File

@ -44,8 +44,7 @@ namespace bwidgets
[[nodiscard]] auto blend_mode() -> SDL_BlendMode;
auto blend_mode(SDL_BlendMode) -> Renderer*;
auto clear() -> Renderer*;
auto copy(const Texture*, const SDL_Rect*, const SDL_Rect*)
-> Renderer*;
auto copy(const Texture*, const SDL_Rect*, const SDL_Rect*) -> Renderer*;
[[nodiscard]] auto draw_color() -> Color;
auto draw_color(const Color&) -> Renderer*;
auto draw_line(const SDL_Point&, const SDL_Point&) -> Renderer*;
@ -61,20 +60,17 @@ namespace bwidgets
[[nodiscard]] auto viewport() noexcept -> SDL_Rect;
auto viewport(const SDL_Rect*) -> Renderer*;
inline auto copy(const Texture* t, const SDL_Rect* src,
const SDL_Rect& dst)
inline auto copy(const Texture* t, const SDL_Rect* src, const SDL_Rect& dst)
{
const auto d = dst;
return copy(t, src, &d);
}
inline auto copy(const Texture* t, const SDL_Rect& src,
const SDL_Rect* dst)
inline auto copy(const Texture* t, const SDL_Rect& src, const SDL_Rect* dst)
{
const auto s = src;
return copy(t, &s, dst);
}
inline auto copy(const Texture* t, const SDL_Rect& src,
const SDL_Rect& dst)
inline auto copy(const Texture* t, const SDL_Rect& src, const SDL_Rect& dst)
{
const auto s = src;
const auto d = dst;

View File

@ -29,8 +29,7 @@ namespace bwidgets
public:
Texture(SDL_Texture*);
Texture(Renderer*, SDL_PixelFormatEnum, SDL_TextureAccess,
int, int);
Texture(Renderer*, SDL_PixelFormatEnum, SDL_TextureAccess, int, int);
Texture(Renderer*, SDL_Surface*);
Texture(const Texture&) = delete;
Texture(Texture&&) = delete;
@ -65,10 +64,11 @@ namespace bwidgets
[[nodiscard]] static inline auto attributes(SDL_Texture* t)
{
Attr attr {};
SDLError::success_or_throw(SDL_QueryTexture(t, &attr.format_raw,
(int*)&attr.access, &attr.w,
&attr.h),
__FILE__, __FUNCTION__, __LINE__);
SDLError::success_or_throw(
SDL_QueryTexture(t, &attr.format_raw,
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
(int*)&attr.access, &attr.w, &attr.h),
__FILE__, __FUNCTION__, __LINE__);
attr.format = SDLError::ptr_or_throw(SDL_AllocFormat(attr.format_raw),
__FILE__, __FUNCTION__, __LINE__);

View File

@ -30,9 +30,8 @@ namespace bwidgets
bool _operate_on_colors;
template<Numeric N>
inline auto
_operate(N operand,
const std::function<N(N, N)>& operator_) const noexcept
inline auto _operate(N operand,
const std::function<N(N, N)>& operator_) const noexcept
{
Color c(sdl_type, _operate_on_alpha, _operate_on_colors);
if (_operate_on_alpha) {

View File

@ -17,7 +17,9 @@ namespace bwidgets
VERTICAL
} alignment;
AlignedLayout(Alignment align, Widget* p = nullptr) noexcept : Layout(p), alignment(align) {}
AlignedLayout(Alignment align, Widget* p = nullptr) noexcept
: Layout(p), alignment(align)
{}
[[nodiscard]] auto size() const noexcept -> Size override;
};

View File

@ -36,8 +36,7 @@ namespace bwidgets
[[nodiscard]] auto size() const noexcept -> Size override = 0;
virtual auto add_widget(const std::shared_ptr<Widget>&) -> Layout*;
virtual void
for_widgets(const std::function<void(Widget*)>&);
virtual void for_widgets(const std::function<void(Widget*)>&);
};
}

View File

@ -30,10 +30,10 @@ namespace bwidgets
virtual ~Widget() = default;
virtual auto handle_event(const SDL_Event&) -> Widget*;
virtual auto handle_event(const SDL_Event&) -> Widget*;
[[nodiscard]] virtual auto size() const noexcept -> Size = 0;
virtual auto render() -> Widget* final;
virtual auto render() -> Widget* final;
virtual auto renderer(const std::shared_ptr<Renderer>&) -> Widget* final;
virtual auto viewport(const SDL_Rect&) -> Widget* final;
[[nodiscard]] virtual auto viewport() const -> const SDL_Rect& final;

View File

@ -14,10 +14,10 @@ namespace bwidgets
virtual void _handle_focus_change(bool) = 0;
public:
FocusHandler() = default;
FocusHandler() = default;
FocusHandler(const FocusHandler&) = delete;
FocusHandler(FocusHandler&&) = delete;
virtual ~FocusHandler() = default;
FocusHandler(FocusHandler&&) = delete;
virtual ~FocusHandler() = default;
auto operator=(FocusHandler&&) -> FocusHandler& = delete;
auto operator=(const FocusHandler&) -> FocusHandler& = delete;

View File

@ -12,147 +12,145 @@ using namespace bwidgets;
const Color Font::default_color_bg {255, 255, 255, SDL_ALPHA_OPAQUE};
const Color Font::default_color_fg {0, 0, 0, SDL_ALPHA_OPAQUE};
Font::Font(TTF_Font* f)
: Wrapper(ptr_or_throw<SDLError>(f, __FILE__, __FUNCTION__, __LINE__),
[f](TTF_Font*) noexcept { TTF_CloseFont(f); }),
ascent(TTF_FontAscent(f)),
descent(TTF_FontDescent(f)),
faces(TTF_FontFaces(f)),
family_name(TTF_FontFaceFamilyName(f)),
fixed_width(TTF_FontFaceIsFixedWidth(f) != 0),
height(TTF_FontHeight(f)),
line_skip(TTF_FontLineSkip(f)),
style_name(TTF_FontFaceStyleName(f))
{}
Font::Font(TTF_Font* f)
: Wrapper(ptr_or_throw<SDLError>(f, __FILE__, __FUNCTION__, __LINE__),
[f](TTF_Font*) noexcept { TTF_CloseFont(f); }),
ascent(TTF_FontAscent(f)),
descent(TTF_FontDescent(f)),
faces(TTF_FontFaces(f)),
family_name(TTF_FontFaceFamilyName(f)),
fixed_width(TTF_FontFaceIsFixedWidth(f) != 0),
height(TTF_FontHeight(f)),
line_skip(TTF_FontLineSkip(f)),
style_name(TTF_FontFaceStyleName(f))
{}
Font::Font(const std::string& file, int size)
: Font(TTF_OpenFont(file.c_str(), size))
{}
Font::Font(const std::string& file, int size) : Font(TTF_OpenFont(file.c_str(), size)) {}
auto Font::hinting() const noexcept -> Font::Hinting
{
return (Hinting)TTF_GetFontHinting(_data());
auto Font::hinting() const noexcept -> Font::Hinting
{
return (Hinting)TTF_GetFontHinting(_data());
}
auto Font::hinting(const Font::Hinting h) noexcept -> Font*
{
TTF_SetFontHinting(_data(), (int)h);
return this;
}
auto Font::kerning() const noexcept -> bool
{
return TTF_GetFontKerning(_data()) != 0;
}
auto Font::kerning(const bool allowed) noexcept -> Font*
{
TTF_SetFontKerning(_data(), static_cast<int>(allowed));
return this;
}
auto Font::outline() const noexcept -> int
{
return TTF_GetFontOutline(_data());
}
auto Font::outline(const int size) noexcept -> Font*
{
TTF_SetFontOutline(_data(), size);
return this;
}
auto Font::render(const RenderMode m, const std::string& str, const Color& fg,
const Color& bg) -> SDL_Surface*
{
std::function<SDL_Surface*()> renderer;
const char* c_str = str.empty() ? " " : str.c_str();
switch (m) {
case RenderMode::BLENDED:
renderer = [&fg, c_str, this]() {
return TTF_RenderUTF8_Blended(_data(), c_str, fg());
};
break;
case RenderMode::SHADED:
renderer = [&bg, &fg, c_str, this]() {
return TTF_RenderUTF8_Shaded(_data(), c_str, fg(), bg());
};
break;
case RenderMode::SOLID:
renderer = [&fg, c_str, this]() {
return TTF_RenderUTF8_Solid(_data(), c_str, fg());
};
break;
}
auto Font::hinting(const Font::Hinting h) noexcept -> Font*
{
TTF_SetFontHinting(_data(), (int)h);
return this;
}
return ptr_or_throw<SDLError>(renderer(), __FILE__, __FUNCTION__, __LINE__);
}
auto Font::kerning() const noexcept -> bool
{
return TTF_GetFontKerning(_data()) != 0;
}
auto Font::style() const noexcept -> uint8_t
{
return TTF_GetFontStyle(_data());
}
auto Font::kerning(const bool allowed) noexcept -> Font*
{
TTF_SetFontKerning(_data(), static_cast<int>(allowed));
return this;
}
auto Font::style(const uint8_t s) noexcept -> Font*
{
TTF_SetFontStyle(_data(), s);
return this;
}
auto Font::outline() const noexcept -> int
{
return TTF_GetFontOutline(_data());
}
auto Font::text_size(const std::string& str) const noexcept -> Size
{
Size s {};
TTF_SizeUTF8(_data(), str.c_str(), &s.w, &s.h);
return s;
}
auto Font::outline(const int size) noexcept -> Font*
{
TTF_SetFontOutline(_data(), size);
return this;
}
auto Font::find(const std::string& pat) -> std::string
{
std::stack<std::function<void()>> cleaners;
auto Font::render(const RenderMode m, const std::string& str, const Color& fg,
const Color& bg) -> SDL_Surface*
{
std::function<SDL_Surface*()> renderer;
const char* c_str = str.empty() ? " " : str.c_str();
FcConfig* conf = ptr_or_throw<FCError>(FcInitLoadConfigAndFonts(), __FILE__,
__FUNCTION__, __LINE__, "init failed");
cleaners.emplace([conf]() { FcConfigDestroy(conf); });
switch (m) {
case RenderMode::BLENDED:
renderer = [&fg, c_str, this]() {
return TTF_RenderUTF8_Blended(_data(), c_str, fg());
};
break;
case RenderMode::SHADED:
renderer = [&bg, &fg, c_str, this]() {
return TTF_RenderUTF8_Shaded(_data(), c_str, fg(), bg());
};
break;
case RenderMode::SOLID:
renderer = [&fg, c_str, this]() {
return TTF_RenderUTF8_Solid(_data(), c_str, fg());
};
break;
}
return ptr_or_throw<SDLError>(renderer(), __FILE__, __FUNCTION__, __LINE__);
}
auto Font::style() const noexcept -> uint8_t
{
return TTF_GetFontStyle(_data());
}
auto Font::style(const uint8_t s) noexcept -> Font*
{
TTF_SetFontStyle(_data(), s);
return this;
}
auto Font::text_size(const std::string& str) const noexcept -> Size
{
Size s {};
TTF_SizeUTF8(_data(), str.c_str(), &s.w, &s.h);
return s;
}
auto Font::find(const std::string& pat) -> std::string
{
std::stack<std::function<void()>> cleaners;
FcConfig* conf = ptr_or_throw<FCError>(FcInitLoadConfigAndFonts(), __FILE__,
__FUNCTION__, __LINE__, "init failed");
cleaners.emplace([conf]() { FcConfigDestroy(conf); });
FcPattern* pattern = nullptr;
try {
pattern =
ptr_or_throw<FCError>(FcNameParse((FcChar8*)pat.c_str()), __FILE__,
__FUNCTION__, __LINE__, "pattern parsing failed");
cleaners.emplace([pattern]() { FcPatternDestroy(pattern); });
if (FcConfigSubstitute(conf, pattern, FcMatchPattern) == FcFalse)
throw FCError {__FILE__, __FUNCTION__, __LINE__,
"FcConfigSubstitute failed"};
} catch (const std::exception& e) {
while (!cleaners.empty()) {
cleaners.top()();
cleaners.pop();
}
throw e;
}
FcDefaultSubstitute(pattern);
std::string file_path;
FcResult res {};
FcPattern* font = FcFontMatch(conf, pattern, &res);
if (font != nullptr) {
FcChar8* file = nullptr;
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
file_path = (char*)file;
}
FcPatternDestroy(font);
}
FcPattern* pattern = nullptr;
try {
pattern =
ptr_or_throw<FCError>(FcNameParse((FcChar8*)pat.c_str()), __FILE__,
__FUNCTION__, __LINE__, "pattern parsing failed");
cleaners.emplace([pattern]() { FcPatternDestroy(pattern); });
if (FcConfigSubstitute(conf, pattern, FcMatchPattern) == FcFalse)
throw FCError {__FILE__, __FUNCTION__, __LINE__,
"FcConfigSubstitute failed"};
} catch (const std::exception& e) {
while (!cleaners.empty()) {
cleaners.top()();
cleaners.pop();
}
if (file_path.empty())
throw FCError {__FILE__, __FUNCTION__, __LINE__, "no font found"};
return file_path;
throw e;
}
FcDefaultSubstitute(pattern);
std::string file_path;
FcResult res {};
FcPattern* font = FcFontMatch(conf, pattern, &res);
if (font != nullptr) {
FcChar8* file = nullptr;
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
file_path = (char*)file;
}
FcPatternDestroy(font);
}
while (!cleaners.empty()) {
cleaners.top()();
cleaners.pop();
}
if (file_path.empty())
throw FCError {__FILE__, __FUNCTION__, __LINE__, "no font found"};
return file_path;
}

View File

@ -39,8 +39,8 @@ auto Renderer::clear() -> Renderer*
return this;
}
auto Renderer::copy(const Texture* t, const SDL_Rect* src,
const SDL_Rect* dst) -> Renderer*
auto Renderer::copy(const Texture* t, const SDL_Rect* src, const SDL_Rect* dst)
-> Renderer*
{
SDLError::success_or_throw(SDL_RenderCopy(_data(), t->_data(), src, dst), __FILE__,
__FUNCTION__, __LINE__);

View File

@ -10,8 +10,8 @@ Texture::Texture(SDL_Texture* t)
_attributes = attributes(t);
}
Texture::Texture(Renderer* r, const SDL_PixelFormatEnum f,
const SDL_TextureAccess a, int w, int h)
Texture::Texture(Renderer* r, const SDL_PixelFormatEnum f, const SDL_TextureAccess a,
int w, int h)
: Texture(SDL_CreateTexture(r->_data(), f, a, w, h))
{}