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

View file

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

View file

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

View file

@ -17,7 +17,9 @@ namespace bwidgets
VERTICAL VERTICAL
} alignment; } 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; [[nodiscard]] auto size() const noexcept -> Size override;
}; };

View file

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

View file

@ -30,10 +30,10 @@ namespace bwidgets
virtual ~Widget() = default; 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; [[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 renderer(const std::shared_ptr<Renderer>&) -> Widget* final;
virtual auto viewport(const SDL_Rect&) -> Widget* final; virtual auto viewport(const SDL_Rect&) -> Widget* final;
[[nodiscard]] virtual auto viewport() const -> const SDL_Rect& 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; virtual void _handle_focus_change(bool) = 0;
public: public:
FocusHandler() = default; FocusHandler() = default;
FocusHandler(const FocusHandler&) = delete; FocusHandler(const FocusHandler&) = delete;
FocusHandler(FocusHandler&&) = delete; FocusHandler(FocusHandler&&) = delete;
virtual ~FocusHandler() = default; virtual ~FocusHandler() = default;
auto operator=(FocusHandler&&) -> FocusHandler& = delete; auto operator=(FocusHandler&&) -> FocusHandler& = delete;
auto operator=(const 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_bg {255, 255, 255, SDL_ALPHA_OPAQUE};
const Color Font::default_color_fg {0, 0, 0, SDL_ALPHA_OPAQUE}; const Color Font::default_color_fg {0, 0, 0, SDL_ALPHA_OPAQUE};
Font::Font(TTF_Font* f) Font::Font(TTF_Font* f)
: Wrapper(ptr_or_throw<SDLError>(f, __FILE__, __FUNCTION__, __LINE__), : Wrapper(ptr_or_throw<SDLError>(f, __FILE__, __FUNCTION__, __LINE__),
[f](TTF_Font*) noexcept { TTF_CloseFont(f); }), [f](TTF_Font*) noexcept { TTF_CloseFont(f); }),
ascent(TTF_FontAscent(f)), ascent(TTF_FontAscent(f)),
descent(TTF_FontDescent(f)), descent(TTF_FontDescent(f)),
faces(TTF_FontFaces(f)), faces(TTF_FontFaces(f)),
family_name(TTF_FontFaceFamilyName(f)), family_name(TTF_FontFaceFamilyName(f)),
fixed_width(TTF_FontFaceIsFixedWidth(f) != 0), fixed_width(TTF_FontFaceIsFixedWidth(f) != 0),
height(TTF_FontHeight(f)), height(TTF_FontHeight(f)),
line_skip(TTF_FontLineSkip(f)), line_skip(TTF_FontLineSkip(f)),
style_name(TTF_FontFaceStyleName(f)) style_name(TTF_FontFaceStyleName(f))
{} {}
Font::Font(const std::string& file, int size) Font::Font(const std::string& file, int size) : Font(TTF_OpenFont(file.c_str(), size)) {}
: Font(TTF_OpenFont(file.c_str(), size))
{}
auto Font::hinting() const noexcept -> Font::Hinting auto Font::hinting() const noexcept -> Font::Hinting
{ {
return (Hinting)TTF_GetFontHinting(_data()); 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* return ptr_or_throw<SDLError>(renderer(), __FILE__, __FUNCTION__, __LINE__);
{ }
TTF_SetFontHinting(_data(), (int)h);
return this;
}
auto Font::kerning() const noexcept -> bool auto Font::style() const noexcept -> uint8_t
{ {
return TTF_GetFontKerning(_data()) != 0; return TTF_GetFontStyle(_data());
} }
auto Font::kerning(const bool allowed) noexcept -> Font* auto Font::style(const uint8_t s) noexcept -> Font*
{ {
TTF_SetFontKerning(_data(), static_cast<int>(allowed)); TTF_SetFontStyle(_data(), s);
return this; return this;
} }
auto Font::outline() const noexcept -> int auto Font::text_size(const std::string& str) const noexcept -> Size
{ {
return TTF_GetFontOutline(_data()); Size s {};
} TTF_SizeUTF8(_data(), str.c_str(), &s.w, &s.h);
return s;
}
auto Font::outline(const int size) noexcept -> Font* auto Font::find(const std::string& pat) -> std::string
{ {
TTF_SetFontOutline(_data(), size); std::stack<std::function<void()>> cleaners;
return this;
}
auto Font::render(const RenderMode m, const std::string& str, const Color& fg, FcConfig* conf = ptr_or_throw<FCError>(FcInitLoadConfigAndFonts(), __FILE__,
const Color& bg) -> SDL_Surface* __FUNCTION__, __LINE__, "init failed");
{ cleaners.emplace([conf]() { FcConfigDestroy(conf); });
std::function<SDL_Surface*()> renderer;
const char* c_str = str.empty() ? " " : str.c_str();
switch (m) { FcPattern* pattern = nullptr;
case RenderMode::BLENDED: try {
renderer = [&fg, c_str, this]() { pattern =
return TTF_RenderUTF8_Blended(_data(), c_str, fg()); ptr_or_throw<FCError>(FcNameParse((FcChar8*)pat.c_str()), __FILE__,
}; __FUNCTION__, __LINE__, "pattern parsing failed");
break; cleaners.emplace([pattern]() { FcPatternDestroy(pattern); });
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);
}
if (FcConfigSubstitute(conf, pattern, FcMatchPattern) == FcFalse)
throw FCError {__FILE__, __FUNCTION__, __LINE__,
"FcConfigSubstitute failed"};
} catch (const std::exception& e) {
while (!cleaners.empty()) { while (!cleaners.empty()) {
cleaners.top()(); cleaners.top()();
cleaners.pop(); cleaners.pop();
} }
throw e;
if (file_path.empty())
throw FCError {__FILE__, __FUNCTION__, __LINE__, "no font found"};
return file_path;
} }
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; return this;
} }
auto Renderer::copy(const Texture* t, const SDL_Rect* src, auto Renderer::copy(const Texture* t, const SDL_Rect* src, const SDL_Rect* dst)
const SDL_Rect* dst) -> Renderer* -> Renderer*
{ {
SDLError::success_or_throw(SDL_RenderCopy(_data(), t->_data(), src, dst), __FILE__, SDLError::success_or_throw(SDL_RenderCopy(_data(), t->_data(), src, dst), __FILE__,
__FUNCTION__, __LINE__); __FUNCTION__, __LINE__);

View file

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