diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-01 21:14:35 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-01 21:14:35 +0100 |
commit | c396ae5f78222a7c3547ae5e2ce719ae143acb66 (patch) | |
tree | 8c593a5d4718ea399ce8e42bd826a660f86f3720 /src/crepe/api | |
parent | 34218886689a57004576486976038027732d8bcc (diff) |
start of text component
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/Button.h | 4 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/crepe/api/Font.cpp | 45 | ||||
-rw-r--r-- | src/crepe/api/Font.h | 60 | ||||
-rw-r--r-- | src/crepe/api/Text.h | 42 |
5 files changed, 153 insertions, 1 deletions
diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index 18f3def..0dfad5d 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -1,8 +1,10 @@ #pragma once -#include "UiObject.h" #include <functional> +#include "UiObject.h" + + namespace crepe { /** diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index aeb451d..2d86968 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -25,6 +25,7 @@ target_sources(crepe PUBLIC Script.cpp Button.cpp UiObject.cpp + Font.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -62,4 +63,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Asset.h Button.h UiObject.h + Font.h + Text.h ) diff --git a/src/crepe/api/Font.cpp b/src/crepe/api/Font.cpp new file mode 100644 index 0000000..8db4b23 --- /dev/null +++ b/src/crepe/api/Font.cpp @@ -0,0 +1,45 @@ +#include <SDL2/SDL_ttf.h> + +#include "facade/SDLContext.h" +#include "util/Log.h" + +#include "Asset.h" +#include "Font.h" + +using namespace crepe; +using namespace std; + +Font::Font(const Asset &src, int size) : font_size(size) { + dbg_trace(); + this->load(src, size); +} + +Font::~Font() { + dbg_trace(); + this->font.reset(); +} + +void Font::load(const Asset &res, int size) { + SDLContext &ctx = SDLContext::get_instance(); + // Open the font using SDL's TTF_OpenFontRW, which supports loading from memory + SDL_RWops *rw_ops = SDL_RWFromFile(res.get_path().c_str(), "rb"); + if (!rw_ops) { + // dbg_log("Failed to create RWops for font: %s", SDL_GetError()); + return; + } + + TTF_Font *loaded_font = TTF_OpenFontRW(rw_ops, 1, size); // 1 indicates SDL should free the RWops + if (!loaded_font) { + // dbg_log("Failed to load font from asset: %s", TTF_GetError()); + return; + } + + // Wrap the TTF_Font with a unique_ptr for automatic cleanup + this->font = unique_ptr<TTF_Font, function<void(TTF_Font *)>>(loaded_font, [](TTF_Font *f) { + if (f) TTF_CloseFont(f); + }); +} + +int Font::get_size() const { + return this->font_size; +} diff --git a/src/crepe/api/Font.h b/src/crepe/api/Font.h new file mode 100644 index 0000000..012c271 --- /dev/null +++ b/src/crepe/api/Font.h @@ -0,0 +1,60 @@ +#pragma once + +#include <SDL2/SDL_ttf.h> +#include <functional> +#include <memory> + +#include "Asset.h" + +namespace crepe { + +class SDLContext; + +/** + * \class Font + * \brief Manages font loading and text rendering properties. + * + * The Font class is responsible for loading font resources and providing a way to render text + * with different styles and sizes. It can be used for text rendering in the game engine. + */ +class Font { + +public: + /** + * \brief Constructs a Font from an Asset resource. + * \param src Asset with font data to load. + * \param size The point size to render the font at. + */ + Font(const Asset &src, int size); + + /** + * \brief Destroys the Font instance, freeing associated resources. + */ + ~Font(); + + /** + * \brief Gets the size of the font. + * \return The point size of the font. + */ + int get_size() const; + +private: + /** + * \brief Loads the font from an Asset resource. + * \param res The Asset resource containing the font data. + * \param size The point size to render the font at. + */ + void load(const Asset &res, int size); + +private: + //! The font resource from the SDL_ttf library. + std::unique_ptr<TTF_Font, std::function<void(TTF_Font *)>> font; + + //! The size of the font in points. + int font_size; + + //! Grants SDLContext access to private members. + friend class SDLContext; +}; + +} // namespace crepe diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h new file mode 100644 index 0000000..6bb011c --- /dev/null +++ b/src/crepe/api/Text.h @@ -0,0 +1,42 @@ +#pragma once + +#include <string> + +#include "Color.h" +#include "UiObject.h" +#include "" +namespace crepe { + +/** + * \class Button + * \brief Represents a clickable UI button, derived from the UiObject class. + * + * This class provides functionality for a button in the UI, including toggle state, + * click handling, and mouse hover detection. A callback function can be provided to + * handle button clicks. + */ +class Text : public UiObject { +public: + /** + * \brief Constructs a Button with the specified game object ID and dimensions. + * + * \param id The unique ID of the game object associated with this button. + * \param width The width of the button. + * \param height The height of the button. + */ + Text(game_object_id_t id, int width, int height); + + Color color = Color{0,0,0,0}; + std::string text = ""; + int size = 0; + const std::shared_ptr<Font> sprite_image; +public: + /** + * \brief Retrieves the maximum number of instances allowed for this button type. + * + * \return Always returns 1, as only a single instance of this type is allowed. + */ + virtual int get_instances_max() const override { return 10; } +}; + +} // namespace crepe |