/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* text_library.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/10 11:59:57 by maldavid #+# #+# */ /* Updated: 2023/11/16 13:45:31 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #include #include #include #include namespace mlx { void TextData::init(std::string text, std::vector vbo_data, std::vector ibo_data) { _text = std::move(text); #ifdef DEBUG _vbo.create(sizeof(Vertex) * vbo_data.size(), vbo_data.data(), _text.c_str()); _ibo.create(sizeof(uint16_t) * ibo_data.size(), ibo_data.data(), _text.c_str()); #else _vbo.create(sizeof(Vertex) * vbo_data.size(), vbo_data.data(), nullptr); _ibo.create(sizeof(uint16_t) * ibo_data.size(), ibo_data.data(), nullptr); #endif } void TextData::bind(Renderer& renderer) noexcept { _vbo.bind(renderer); _ibo.bind(renderer); } void TextData::destroy() noexcept { _vbo.destroy(); _ibo.destroy(); } std::shared_ptr TextLibrary::getTextData(TextID id) { if(!_cache.count(id)) core::error::report(e_kind::fatal_error, "Text Library : wrong text ID '%d'", id); return _cache[id]; } TextID TextLibrary::addTextToLibrary(std::shared_ptr text) { auto it = std::find_if(_cache.begin(), _cache.end(), [=](const std::pair>& v) { return v.second->getText() == text->getText(); }); if(it != _cache.end()) return it->first; _cache[_current_id] = text; _current_id++; return _current_id - 1; } void TextLibrary::removeTextFromLibrary(TextID id) { if(_cache.count(id)) { _cache[id]->destroy(); _cache.erase(id); } } void TextLibrary::clearLibrary() { for(auto [id, text] : _cache) text->destroy(); _cache.clear(); } }