Skip to content

Commit

Permalink
[SUTK] Added font loading/unloading interface in UIDriver, though imp…
Browse files Browse the repository at this point in the history
…lementation is to be defined.
  • Loading branch information
ravi688 committed Oct 10, 2024
1 parent 54013fb commit 4c2e744
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 8 deletions.
2 changes: 2 additions & 0 deletions sutk/include/sutk/IGfxDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ namespace SUTK
virtual GfxDriverObjectHandleType loadTexture(std::string_view str) = 0;
virtual void getTextureAttributes(GfxDriverObjectHandleType texture, TextureAttributes& out) = 0;
virtual void unloadTexture(GfxDriverObjectHandleType handle) = 0;
virtual GfxDriverObjectHandleType loadFont(std::string_view str) = 0;
virtual void unloadFont(GfxDriverObjectHandleType handle) = 0;
virtual GfxDriverObjectHandleType compileGeometry(const Geometry& geometryDsc, GfxDriverObjectHandleType previous = GFX_DRIVER_OBJECT_NULL_HANDLE) = 0;
virtual void destroyGeometry(GfxDriverObjectHandleType geometry) = 0;
virtual GfxDriverObjectHandleType getGeometryObject(GfxDriverObjectHandleType geometry) = 0;
Expand Down
4 changes: 4 additions & 0 deletions sutk/include/sutk/SGEGfxDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace SUTK
std::unordered_map<GfxDriverObjectHandleType, SGEBitmapTextStringData> m_bitmapTextStringMappings;
std::unordered_map<id_generator_id_type_t, SGE::RenderObject> m_renderObjectMappings;
com::IDMap<std::pair<std::string, SGE::Texture>> m_textureData;
com::IDMap<std::pair<std::string, SGE::Font>> m_fontData;
std::unordered_map<id_generator_id_type_t, ObjectType> m_typeTable;
template<typename T>
struct CallbackHandlerData
Expand Down Expand Up @@ -158,6 +159,9 @@ namespace SUTK
virtual void getTextureAttributes(GfxDriverObjectHandleType texture, TextureAttributes& out) override;
virtual void unloadTexture(GfxDriverObjectHandleType handle) override;

virtual GfxDriverObjectHandleType loadFont(std::string_view str) override;
virtual void unloadFont(GfxDriverObjectHandleType handle) override;

// compiles SUTK::Geometry description into SGE objects which can eventually be renderered in SGE
virtual GfxDriverObjectHandleType compileGeometry(const Geometry& geometryDsc, GfxDriverObjectHandleType previous = GFX_DRIVER_OBJECT_NULL_HANDLE) override;
virtual void destroyGeometry(GfxDriverObjectHandleType geometry) override;
Expand Down
22 changes: 19 additions & 3 deletions sutk/include/sutk/ThemeManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace SUTK
private:
com::unordered_map<KeyType, Color4, KeyViewType> m_colors;
com::unordered_map<KeyType, UIDriver::ImageReference, KeyViewType> m_images;
// com::unordered_map<KeyType, UIDriver::FontReference, KeyViewType> m_fonts;
com::unordered_map<KeyType, UIDriver::FontReference, KeyViewType> m_fonts;
ThemeInterfaceType* m_interface;
public:
Theme(UIDriver& driver, ThemeInterfaceType* interface) noexcept : UIDriverObject(driver), m_interface(interface) { }
Expand All @@ -87,17 +87,27 @@ namespace SUTK
else
m_images.insert({ KeyType { key }, std::forward<UIDriver::ImageReference&&>(value) });
}
else if constexpr(std::is_same_v<ValueType, UIDriver::FontReference>)
{
if(m_interface->getType(key) != ThemeInterfaceType::Type::Font)
DEBUG_LOG_ERROR("Type mismatch");
else
m_fonts.insert({ KeyType { key }, std::forward<UIDriver::FontReference&&>(value) });
}
else
static_assert(false, "Type not supported");
}
template<typename ValueType>
void add(const KeyViewType& key, std::string_view view) noexcept
{
bool result = view.ends_with(".png") || view.ends_with(".jpg") || view.ends_with(".bmp");
if(result)
if(view.ends_with(".png") || view.ends_with(".jpg") || view.ends_with(".bmp"))
{
UIDriver::ImageReference image = getUIDriver().loadImage(view);
m_images.insert({ KeyType { key }, image });
} else if(view.ends_with(".ttf"))
{
UIDriver::FontReference font = getUIDriver().loadFont(view);
m_fonts.insert({ KeyType { key }, font });
}
else
DEBUG_LOG_ERROR("Unrecognized image file extension in path: %.*s", view.length(), view.data());
Expand All @@ -110,6 +120,8 @@ namespace SUTK
result = com::find_erase(m_colors, key);
else if constexpr(std::is_same_v<ValueType, UIDriver::ImageReference>)
result = com::find_erase(m_images, key);
else if constexpr(std::is_same_v<ValueType, UIDriver::FontReference>)
result = com::find_erase(m_fonts, key);
else
static_assert(false, "Type not supported");
_com_assert(result);
Expand All @@ -121,6 +133,8 @@ namespace SUTK
return com::find_value(m_colors, key);
else if constexpr(std::is_same_v<ValueType, UIDriver::ImageReference>)
return com::find_value(m_images, key);
else if constexpr(std::is_same_v<ValueType, UIDriver::FontReference>)
return com::find_value(m_fonts, key);
else
static_assert(false, "Type not supported");
}
Expand All @@ -136,6 +150,8 @@ namespace SUTK
m_defs.insert({ KeyType { key }, { Type::Color, { } } });
else if constexpr(std::is_same_v<ValueType, UIDriver::ImageReference>)
m_defs.insert({ KeyType { key }, { Type::Image, { } } });
else if constexpr(std::is_same_v<ValueType, UIDriver::FontReference>)
m_defs.insert({ KeyType { key }, { Type::Font, { } } });
else
static_assert(false, "Type not supported");
}
Expand Down
11 changes: 9 additions & 2 deletions sutk/include/sutk/UIDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <sutk/defines.hpp>
#include <sutk/IGfxDriver.hpp> // for SUTK::IGfxDriver::getSizeInCentimeters()

#include <common/Reference.hpp> // for com::Reference
#include <vector> // for std::vector
#include <utility> // for std::forward

Expand All @@ -29,9 +30,13 @@ namespace SUTK
class UIDriver
{
public:
typedef GfxDriverObjectHandleType ImageReference;
static constexpr ImageReference InvalidImage = GFX_DRIVER_OBJECT_NULL_HANDLE;
struct AuxImage { };
typedef com::Reference<GfxDriverObjectHandleType, GFX_DRIVER_OBJECT_NULL_HANDLE, AuxImage> ImageReference;
static constexpr ImageReference InvalidImage = ImageReference::Null;
typedef TextureAttributes ImageAttributes;
struct AuxFont { };
typedef com::Reference<GfxDriverObjectHandleType, GFX_DRIVER_OBJECT_NULL_HANDLE, AuxFont> FontReference;
static constexpr FontReference InvalidFont = FontReference::Null;
private:
IGfxDriver& m_gfxDriver;
IInputDriver* m_inputDriver;
Expand Down Expand Up @@ -61,6 +66,8 @@ namespace SUTK
ImageReference loadImage(std::string_view path) noexcept;
void unloadImage(ImageReference id) noexcept;
ImageAttributes getImageAttributes(ImageReference id) noexcept;
FontReference loadFont(std::string_view path) noexcept;
void unloadFont(FontReference id) noexcept;

Container* getDebugRootContainer() noexcept { return m_debugRootContainer; }

Expand Down
18 changes: 15 additions & 3 deletions sutk/source/SGEGfxDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ namespace SUTK
{
// if there is no vertex buffer then create one with binding equal to zero
SGE::Buffer vertexBuffer = mesh.getVertexBuffer(createInfo.binding);
if(static_cast<void*>(vertexBuffer) == NULL)
if(!vertexBuffer)
mesh.createAndAddVertexBuffer(com::cast_away_const(createInfo));
else
{
Expand Down Expand Up @@ -849,9 +849,21 @@ namespace SUTK
// TODO : unload the texture from SGE Driver's side
}

GfxDriverObjectHandleType SGEGfxDriver::loadFont(std::string_view str)
{
// TODO: Load the font from another thread
return m_fontData.add({ std::string(str), { } });
}

void SGEGfxDriver::unloadFont(GfxDriverObjectHandleType handle)
{
m_fontData.remove(handle);
// TODO : unload the font from SGE Driver's side
}

SGE::Texture SGEGfxDriver::getTexture(GfxDriverObjectHandleType handle) noexcept
{
if(handle == UIDriver::InvalidImage)
if(handle == GFX_DRIVER_OBJECT_NULL_HANDLE)
return getDefaultTexture();

std::pair<std::string, SGE::Texture>& pair = m_textureData.get(handle);
Expand Down Expand Up @@ -1011,7 +1023,7 @@ namespace SUTK
if(vertexBuffer || createInfo.count)
{
// if there is no vertex buffer then create one with binding equal to 5
if(static_cast<void*>(vertexBuffer) == NULL)
if(!vertexBuffer)
mesh.createAndAddVertexBuffer(createInfo);
else
{
Expand Down
39 changes: 39 additions & 0 deletions sutk/source/ThemeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ namespace SUTK
interface->define<UIDriver::ImageReference>(nameSV);
continue;
}
typeAttr = node_find_attribute(child, str, "Font");
if(typeAttr)
{
interface->define<UIDriver::FontReference>(nameSV);
continue;
}
DEBUG_LOG_WARNING("Type either isn't recognized or not given, skipping \"%.*s\"", nameSV.length(), nameSV.data());
}
ppsr_v3d_generic_parse_result_destroy(NULL, result);
Expand Down Expand Up @@ -231,6 +237,33 @@ namespace SUTK
return image;
}

template<>
template<>
UIDriver::FontReference ThemeManager<std::string, std::string_view>::deriveValue<UIDriver::FontReference>(v3d_generic_node_t* node, const char* str) noexcept
{
// Followings are the possibilies:
// 1. "path/to/a/file.ttf"
// 2. default
UIDriver::FontReference font = UIDriver::InvalidFont;
if(node->qualifier_count == 1)
{
u32_pair_t pair = node->qualifiers[0];
if(com_safe_strncmp(pair.start + str, "default", U32_PAIR_DIFF(pair)) == 0)
{
// The Gfx Driver will automatically load its default font
font = UIDriver::InvalidFont;
}
else
{
auto filePath = std::string_view { pair.start + str, U32_PAIR_DIFF(pair) };
font = getUIDriver().loadFont(filePath);
}
}
else
DEBUG_LOG_ERROR("Neither file path nor \"default\" is provided");
return font;
}

template<>
typename ThemeManager<std::string, std::string_view>::ThemeType* ThemeManager<std::string, std::string_view>::loadTheme(const std::string_view filePath) noexcept
{
Expand Down Expand Up @@ -334,6 +367,12 @@ namespace SUTK
theme->add<UIDriver::ImageReference>(nameSV, std::move(image));
break;
}
case ThemeInterfaceType::Type::Font:
{
UIDriver::FontReference font = deriveValue<UIDriver::FontReference>(child->value, str);
theme->add<UIDriver::FontReference>(nameSV, std::move(font));
break;
}
default:
{
DEBUG_LOG_WARNING("Type either isn't recognized or not given, skipping \"%.*s\"", nameSV.length(), nameSV.data());
Expand Down
10 changes: 10 additions & 0 deletions sutk/source/UIDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ namespace SUTK
getGfxDriver().unloadTexture(id);
}

UIDriver::FontReference UIDriver::loadFont(std::string_view path) noexcept
{
return getGfxDriver().loadFont(path);
}

void UIDriver::unloadFont(FontReference id) noexcept
{
getGfxDriver().unloadFont(id);
}

UIDriver::ImageAttributes UIDriver::getImageAttributes(ImageReference id) noexcept
{
TextureAttributes attr;
Expand Down

0 comments on commit 4c2e744

Please sign in to comment.