fix initial config modification check, add wrap parameter to text object

This commit is contained in:
ohsqueezy 2023-12-12 00:42:38 -05:00
parent c7fb948e39
commit 464e69be56
4 changed files with 46 additions and 29 deletions

View File

@ -146,10 +146,6 @@ void Configuration::merge(const nlohmann::json& incoming)
void Configuration::merge(const fs::path& path) void Configuration::merge(const fs::path& path)
{ {
merge(json_from_file(path)); merge(json_from_file(path));
#ifndef __ANDROID__
config_file_modification_time = fs::last_write_time(path);
#endif
} }
void Configuration::merge(const std::string& path) void Configuration::merge(const std::string& path)
@ -233,12 +229,13 @@ void Configuration::refresh()
#if !defined(__ANDROID__) #if !defined(__ANDROID__)
for (const fs::path& path : files_to_refresh) for (const fs::path& path : files_to_refresh)
{ {
if (fs::exists(path) && fs::last_write_time(path) > config_file_modification_time) if (fs::exists(path) && fs::last_write_time(path) > config_modification_time)
{ {
std::ostringstream message; std::ostringstream message;
message << "config file modified, reloading " << path; message << "config file modified, reloading " << path;
sb::Log::log(message, sb::Log::DEBUG); sb::Log::log(message, sb::Log::DEBUG);
merge(path); merge(path);
config_modification_time = std::filesystem::file_time_type::clock::now();
sb::Delegate::post("reconfig"); sb::Delegate::post("reconfig");
} }
} }

View File

@ -32,7 +32,7 @@ class Configuration : public Node
private: private:
Animation auto_refresher = Animation(std::bind(&Configuration::refresh, this)); Animation auto_refresher = Animation(std::bind(&Configuration::refresh, this));
fs::file_time_type config_file_modification_time; std::filesystem::file_time_type config_modification_time = std::filesystem::file_time_type::clock::now();
std::vector<fs::path> files_to_refresh; std::vector<fs::path> files_to_refresh;
/*! /*!

View File

@ -19,14 +19,12 @@ void Text::bind_textures() const
void Text::content(const std::string& content) void Text::content(const std::string& content)
{ {
_content = content; _content = content;
refresh();
} }
void Text::content(char content) void Text::content(char content)
{ {
_content = ""; _content = "";
_content += content; _content += content;
refresh();
} }
const std::string& Text::content() const const std::string& Text::content() const
@ -37,38 +35,47 @@ const std::string& Text::content() const
void Text::foreground(const Color& foreground) void Text::foreground(const Color& foreground)
{ {
_foreground = foreground; _foreground = foreground;
refresh();
} }
void Text::background(const Color& background) void Text::background(const Color& background)
{ {
_background = background; _background = background;
refresh();
} }
void Text::font(std::shared_ptr<TTF_Font> font) void Text::font(std::shared_ptr<TTF_Font> font)
{ {
_font = font; _font = font;
refresh();
} }
void Text::dimensions(const glm::vec2 &dimensions) void Text::dimensions(const glm::vec2 &dimensions)
{ {
_dimensions = dimensions; _dimensions = dimensions;
refresh();
} }
void Text::scaling_quality(GLint quality) void Text::scaling_quality(GLint quality)
{ {
_scaling_quality = quality; _scaling_quality = quality;
refresh(); }
void Text::wrap(int width)
{
_wrap = width;
} }
void Text::refresh() void Text::refresh()
{ {
/* Render the text with transparent background as RGBA pixel data using the SDL image library. */ /* Render the text with transparent background as RGBA pixel data using the SDL image library. Apply wrap if it is set. */
// std::shared_ptr<SDL_Surface> blended {TTF_RenderText_Blended(_font.get(), _content.c_str(), _foreground), SDL_FreeSurface}; std::shared_ptr<SDL_Surface> blended;
std::shared_ptr<SDL_Surface> blended {TTF_RenderUTF8_Blended_Wrapped(_font.get(), _content.c_str(), _foreground, 3000), SDL_FreeSurface}; if (_wrap.has_value())
{
blended = std::shared_ptr<SDL_Surface>{TTF_RenderUTF8_Blended_Wrapped(_font.get(), _content.c_str(), _foreground, _wrap.value()), SDL_FreeSurface};
}
else
{
blended = std::shared_ptr<SDL_Surface>{TTF_RenderUTF8_Blended(_font.get(), _content.c_str(), _foreground), SDL_FreeSurface};
}
/* Composite the rendered text onto a background surface. */
if (!blended) if (!blended)
{ {
Log::sdl_error("Could not create text"); Log::sdl_error("Could not create text");

View File

@ -24,13 +24,14 @@ namespace sb
std::shared_ptr<TTF_Font> _font; std::shared_ptr<TTF_Font> _font;
std::optional<glm::vec2> _dimensions; std::optional<glm::vec2> _dimensions;
GLint _scaling_quality = GL_LINEAR; GLint _scaling_quality = GL_LINEAR;
std::optional<int> _wrap;
public: public:
/*! /*!
* Construct a sb::Text object from a font, string, and background and foreground colors. A texture will be created and attached. The * Construct a sb::Text object from a font, string, and background and foreground colors. A texture will be created and attached at
* texture is not generated or rendered. Text::refresh() must be called afterward with the GL context loaded. The content, dimensions, * texture index 0. The texture is not generated or rendered. Text::refresh() must be called afterward with the GL context loaded to render
* foreground, background, font, and scaling quality functions call Text::refresh() automatically. * the text to the constructed texture.
* *
* The font must be wrapped in a shared pointer. A default loaded font is available from Game::font(). * The font must be wrapped in a shared pointer. A default loaded font is available from Game::font().
* *
@ -53,54 +54,66 @@ namespace sb
} }
/*! /*!
* Bind the texture rendered from the text object's text content and any other textures attached. * Bind textures attached to this model.
*/ */
void bind_textures() const; void bind_textures() const;
/*! /*!
* @param content text to be displayed * @param content Text to be rendered
*/ */
void content(const std::string& content); void content(const std::string& content);
/*! /*!
* @param content Single character to be used for text content * @param content Single character to be rendered
*/ */
void content(char content); void content(char content);
/*! /*!
* @return Text content to be displayed * @return Text to be rendered
*/ */
const std::string& content() const; const std::string& content() const;
/*! /*!
* @param foreground text color * @param foreground Text color
*/ */
void foreground(const sb::Color& foreground); void foreground(const sb::Color& foreground);
/*! /*!
* @param background text background color which fills the Plane * @param background Text background color
*/ */
void background(const sb::Color& background); void background(const sb::Color& background);
/*! /*!
* @param font shared pointer to a TTF_Font * @param font Shared pointer to a TTF_Font to use for rendering text
*/ */
void font(std::shared_ptr<TTF_Font> font); void font(std::shared_ptr<TTF_Font> font);
/*! /*!
* @param dimensions force the texture to be a certain size in pixels, extra area is filled with the background color * @param dimensions Force the generated texture to be a certain size in pixels. Extra area is filled with the background color.
*/ */
void dimensions(const glm::vec2& dimensions); void dimensions(const glm::vec2& dimensions);
/*! /*!
* This GL parameter will be passed to `glTexParameter` for `GL_TEXTURE_MIN_FILTER` and `GL_TEXTURE_MAG_FILTER`. * This GL parameter will be passed to `glTexParameter` for `GL_TEXTURE_MIN_FILTER` and `GL_TEXTURE_MAG_FILTER`.
* *
* @param quality quality of texture scaling passed to `glTexParameter` * @param quality Quality of texture scaling passed to `glTexParameter`
*/ */
void scaling_quality(GLint quality); void scaling_quality(GLint quality);
/*! /*!
* Load the texture with the appropriate SDL_Surface pixels created by the SDL TTF library. * By default, there is no wrapping on the text added to this object. If this is set, however, the text will be rendered to a
* texture that is limited to the given pixel width. Any text that would exceed that width is moved to a new line in the
* rendering.
*
* @param wrap Pixel width where the text will wrap when rendered to a texture
*/
void wrap(int width);
/*!
* Generate and load the texture at index 0. Render text to it using SDL_Surface pixels created by the SDL TTF library.
*
* The text must be set to a non-zero length string using Text::content(const std::string&) or it will fail to render with an
* error logged.
*/ */
void refresh(); void refresh();