From 7e36afb89d64b5d567c1aebac19c87cf2f290218 Mon Sep 17 00:00:00 2001 From: Rodrigo Delduca Date: Sat, 14 Dec 2024 14:20:30 -0300 Subject: [PATCH] Work in progress --- src/entity.cpp | 6 +-- src/fontfactory.cpp | 117 ++++++++++++++++++++++--------------------- src/pixmappool.cpp | 15 +++--- src/soundmanager.cpp | 15 +++--- 4 files changed, 78 insertions(+), 75 deletions(-) diff --git a/src/entity.cpp b/src/entity.cpp index 29e2dc8..28bf261 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -102,9 +102,9 @@ void entity::draw() const noexcept { geometry::rect destination{_props.position + offset, source.size()}; destination.scale(_props.size.scale()); - if (!_props.spritesheet) { - return; - } + // if (!_props.spritesheet) { + // return; + // } _props.spritesheet->draw( source, diff --git a/src/fontfactory.cpp b/src/fontfactory.cpp index bc072d9..402fc02 100644 --- a/src/fontfactory.cpp +++ b/src/fontfactory.cpp @@ -8,75 +8,76 @@ fontfactory::fontfactory(const std::shared_ptr renderer) noe : _renderer(std::move(renderer)) {} std::shared_ptr fontfactory::get(const std::string &family) { - auto [it, added] = _pool.insert_or_assign(family, nullptr); - - if (added) [[unlikely]] { - fmt::println("[fontfactory] cache miss {}", family); - - const auto &buffer = storage::io::read(fmt::format("fonts/{}.json", family)); - const auto &j = json::parse(buffer); - const auto &alphabet = j["alphabet"].get(); - const auto spacing = j["spacing"].get(); - - std::vector output; - geometry::size size; - std::tie(output, size) = _load_png(j["spritesheet"].get_ref()); - - auto surface = std::unique_ptr{ - SDL_CreateRGBSurfaceWithFormatFrom( - output.data(), - size.width(), - size.height(), - 32, - size.width() * 4, - SDL_PIXELFORMAT_ABGR8888 - ), - SDL_FreeSurface - }; - - if (!surface) [[unlikely]] { - throw std::runtime_error(fmt::format("[SDL_CreateRGBSurfaceWithFormatFrom] Error: {}", SDL_GetError())); - } + if (auto it = _pool.find(family); it != _pool.end()) [[likely]] { + return it->second; + } - const auto pixels = static_cast(surface->pixels); - const auto separator = color(pixels[0], surface->format); + fmt::println("[fontfactory] cache miss {}", family); + + const auto &buffer = storage::io::read(fmt::format("fonts/{}.json", family)); + const auto &j = json::parse(buffer); + const auto &alphabet = j["alphabet"].get(); + const auto spacing = j["spacing"].get(); + + std::vector output; + geometry::size size; + std::tie(output, size) = _load_png(j["spritesheet"].get_ref()); + + auto surface = std::unique_ptr{ + SDL_CreateRGBSurfaceWithFormatFrom( + output.data(), + size.width(), + size.height(), + 32, + size.width() * 4, + SDL_PIXELFORMAT_ABGR8888 + ), + SDL_FreeSurface + }; + + if (!surface) [[unlikely]] { + throw std::runtime_error(fmt::format("[SDL_CreateRGBSurfaceWithFormatFrom] Error: {}", SDL_GetError())); + } - glyphmap map; - int x = 0, y = 0, width = 0, height = 0; + const auto pixels = static_cast(surface->pixels); + const auto separator = color(pixels[0], surface->format); - for (const char letter : alphabet) { - while (x < size.width() && color(pixels[y * size.width() + x], surface->format) == separator) { - ++x; - } + glyphmap map; + int x = 0, y = 0, width = 0, height = 0; - if (x >= size.width()) [[unlikely]] { - throw std::runtime_error(fmt::format("Error: Missing glyph for '{}'", letter)); - } + for (const char letter : alphabet) { + while (x < size.width() && color(pixels[y * size.width() + x], surface->format) == separator) { + ++x; + } - width = 0; - while (x + width < size.width() && - color(pixels[y * size.width() + x + width], surface->format) != separator) { - ++width; - } + if (x >= size.width()) [[unlikely]] { + throw std::runtime_error(fmt::format("Error: Missing glyph for '{}'", letter)); + } - height = 0; - while (y + height < size.height() && - color(pixels[(y + height) * size.width() + x], surface->format) != separator) { - ++height; - } + width = 0; + while (x + width < size.width() && + color(pixels[y * size.width() + x + width], surface->format) != separator) { + ++width; + } - map[letter] = {{x, y}, {width, height}}; - x += width; + height = 0; + while (y + height < size.height() && + color(pixels[(y + height) * size.width() + x], surface->format) != separator) { + ++height; } - it->second = std::make_shared( - std::move(map), - std::make_shared(_renderer, std::move(surface)), - spacing - ); + map[letter] = {{x, y}, {width, height}}; + x += width; } - return it->second; + auto ptr = std::make_shared( + std::move(map), + std::make_shared(_renderer, std::move(surface)), + spacing + ); + + _pool.emplace(family, ptr); + return ptr; } void fontfactory::flush() noexcept { diff --git a/src/pixmappool.cpp b/src/pixmappool.cpp index efb1171..438b77b 100644 --- a/src/pixmappool.cpp +++ b/src/pixmappool.cpp @@ -6,17 +6,18 @@ pixmappool::pixmappool(const std::shared_ptr renderer) noexcept : _renderer(std::move(renderer)) {} const std::shared_ptr pixmappool::get(const std::string &filename) { - auto [it, added] = _pool.insert_or_assign(filename, nullptr); + if (auto it = _pool.find(filename); it != _pool.end()) [[likely]] { + return it->second; + } - if (added) [[unlikely]] { - fmt::println("[pixmappool] cache miss {}", filename); + fmt::println("[pixmappool] cache miss {}", filename); - assert(_renderer); + assert(_renderer); - it->second = std::make_shared(_renderer, filename); - } + auto ptr = std::make_shared(_renderer, filename); + _pool.emplace(filename, ptr); - return it->second; + return ptr; } void pixmappool::flush() noexcept { diff --git a/src/soundmanager.cpp b/src/soundmanager.cpp index d7db777..d266a5a 100644 --- a/src/soundmanager.cpp +++ b/src/soundmanager.cpp @@ -6,17 +6,18 @@ soundmanager::soundmanager(const std::shared_ptr audiodevice) noexc : _audiodevice(std::move(audiodevice)) {} std::shared_ptr soundmanager::get(const std::string &filename) noexcept { - auto [it, added] = _pool.insert_or_assign(filename, nullptr); + if (auto it = _pool.find(filename); it != _pool.end()) [[likely]] { + return it->second; + } - if (added) [[unlikely]] { - fmt::println("[soundmanager] cache miss {}", filename); + fmt::println("[soundmanager] cache miss {}", filename); - assert(_audiodevice); + assert(_audiodevice); - it->second = std::make_shared(_audiodevice, filename); - } + auto ptr = std::make_shared(_audiodevice, filename); + _pool.emplace(filename, ptr); - return it->second; + return ptr; } void soundmanager::play(const std::string &filename) noexcept {