From 5c826a5d392fc4f9b2be2eda807794685743fe1d Mon Sep 17 00:00:00 2001 From: Rodrigo Delduca Date: Sat, 12 Oct 2024 11:09:01 -0300 Subject: [PATCH] Work in progress --- src/engine.cpp | 8 ++++++-- src/engine.hpp | 2 ++ src/entitymanager.cpp | 19 ++++++++++--------- src/pixmappool.cpp | 5 +++-- src/pixmappool.hpp | 8 +++----- src/resourcemanager.cpp | 7 ++++--- src/resourcemanager.hpp | 6 +++--- src/scenemanager.cpp | 2 +- src/scriptengine.cpp | 11 +++++++---- src/soundmanager.cpp | 8 ++++---- src/soundmanager.hpp | 13 +++++-------- 11 files changed, 48 insertions(+), 41 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index a3141b0..b72d69a 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -89,6 +89,11 @@ void engine::prefetch(const std::vector &filenames) { _resourcemanager->prefetch(filenames); } +void engine::flush() const { + _resourcemanager->pixmappool()->flush(); + _resourcemanager->soundmanager()->flush(); +} + bool engine::is_keydown(const input::keyevent &event) const { return _statemanager->is_keydown(event); } @@ -141,8 +146,7 @@ void engine::_loop() { _eventmanager->update(delta); _entitymanager->update(delta); - std::for_each(_loopables.begin(), _loopables.end(), - [delta](auto &loopable) { loopable->loop(delta); }); + std::for_each(_loopables.begin(), _loopables.end(), [delta](auto &loopable) { loopable->loop(delta); }); _renderer->begin(); _scenemanager->draw(); diff --git a/src/engine.hpp b/src/engine.hpp index 6c0accc..640c3a9 100644 --- a/src/engine.hpp +++ b/src/engine.hpp @@ -48,6 +48,8 @@ class engine : public input::eventreceiver { void prefetch(const std::vector &filenames); + void flush() const; + bool is_keydown(const input::keyevent &event) const; const std::shared_ptr spawn(const std::string &kind); diff --git a/src/entitymanager.cpp b/src/entitymanager.cpp index fb19732..00d6629 100644 --- a/src/entitymanager.cpp +++ b/src/entitymanager.cpp @@ -3,7 +3,6 @@ #include "entity.hpp" #include "entityprops.hpp" #include "io.hpp" -#include "pixmap.hpp" #include "point.hpp" #include "rect.hpp" #include "resourcemanager.hpp" @@ -15,19 +14,21 @@ using namespace framework; using json = nlohmann::json; void entitymanager::set_resourcemanager( - std::shared_ptr resourcemanager) { + std::shared_ptr resourcemanager +) { _resourcemanager = resourcemanager; } std::shared_ptr entitymanager::spawn(const std::string &kind) { const auto buffer = storage::io::read(fmt::format("entities/{}.json", kind)); const auto j = json::parse(buffer); - const auto spritesheet = _resourcemanager->pixmappool()->get(j["spritesheet"].template get()); + const auto spritesheet = _resourcemanager->pixmappool()->get(j["spritesheet"].template get()); const auto gravitic = j.value("gravitic", false); const auto scale = j.value("scale", 1.); const auto size = geometry::size{ static_cast(j.value("width", 0) * scale), - static_cast(j.value("height", 0) * scale)}; + static_cast(j.value("height", 0) * scale) + }; std::map> animations; for (const auto &[key, a] : j["animations"].get()) { @@ -83,7 +84,8 @@ std::shared_ptr entitymanager::spawn(const std::string &kind) { frame, last_frame, velocity, - visible}; + visible + }; const auto e = entity::create(std::move(props)); std::cout << "[entitymanager] spawn: " << e->id() << std::endl; @@ -98,10 +100,9 @@ void entitymanager::destroy(const std::weak_ptr entity) { } std::shared_ptr entitymanager::find(uint64_t id) const { - const auto it = std::find_if(_entities.begin(), _entities.end(), - [id](const std::shared_ptr &entity) { - return entity->id() == id; - }); + const auto it = std::find_if(_entities.begin(), _entities.end(), [id](const std::shared_ptr &entity) { + return entity->id() == id; + }); return (it != _entities.end()) ? *it : nullptr; } diff --git a/src/pixmappool.cpp b/src/pixmappool.cpp index 4209ab3..3e7065c 100644 --- a/src/pixmappool.cpp +++ b/src/pixmappool.cpp @@ -7,17 +7,18 @@ using namespace graphics; pixmappool::pixmappool(const std::shared_ptr renderer) : _renderer(renderer) {} -void pixmappool::preload(const std::vector &filenames) { +void pixmappool::preload(const std::vector &filenames) { for (const auto &filename : filenames) { get(filename); } } -const std::shared_ptr pixmappool::get(const std::string_view filename) { +const std::shared_ptr pixmappool::get(const std::string &filename) { auto [it, added] = _pool.try_emplace(filename, nullptr); if (added) { std::cout << "[pixmappool] cache miss: " << filename << std::endl; + assert(_renderer); it->second = std::make_shared(_renderer, filename); } diff --git a/src/pixmappool.hpp b/src/pixmappool.hpp index 806dceb..da20366 100644 --- a/src/pixmappool.hpp +++ b/src/pixmappool.hpp @@ -8,16 +8,14 @@ class pixmappool { explicit pixmappool(const std::shared_ptr renderer); ~pixmappool() = default; - void preload(const std::vector &filenames); + void preload(const std::vector &filenames); - const std::shared_ptr get(const std::string_view filename); + const std::shared_ptr get(const std::string &filename); void flush(); private: std::shared_ptr _renderer; - - std::unordered_map, std::hash> - _pool; + std::map> _pool; }; } diff --git a/src/resourcemanager.cpp b/src/resourcemanager.cpp index ea4de3f..cc2dfa7 100644 --- a/src/resourcemanager.cpp +++ b/src/resourcemanager.cpp @@ -10,14 +10,15 @@ using namespace framework; resourcemanager::resourcemanager( const std::shared_ptr renderer, - const std::shared_ptr audiodevice) + const std::shared_ptr audiodevice +) : _pixmappool(std::make_shared(renderer)), _soundmanager(std::make_shared(audiodevice)) { - _handlers[".png"] = [this](const std::string_view filename) { + _handlers[".png"] = [this](const std::string &filename) { _pixmappool->get(filename); }; - _handlers[".ogg"] = [this](const std::string_view filename) { + _handlers[".ogg"] = [this](const std::string &filename) { _soundmanager->get(filename); }; } diff --git a/src/resourcemanager.hpp b/src/resourcemanager.hpp index 6975fec..9897e16 100644 --- a/src/resourcemanager.hpp +++ b/src/resourcemanager.hpp @@ -1,14 +1,14 @@ #pragma once #include "common.hpp" -#include namespace framework { class resourcemanager { public: resourcemanager( const std::shared_ptr renderer, - const std::shared_ptr audiodevice); + const std::shared_ptr audiodevice + ); ~resourcemanager() = default; @@ -23,7 +23,7 @@ class resourcemanager { std::shared_ptr soundmanager(); private: - std::unordered_map> + std::map> _handlers; std::list _filenames; diff --git a/src/scenemanager.cpp b/src/scenemanager.cpp index b00603c..6d6444b 100644 --- a/src/scenemanager.cpp +++ b/src/scenemanager.cpp @@ -11,7 +11,7 @@ scenemanager::scenemanager(const std::shared_ptr pixmappoo void scenemanager::load(const std::string_view name) { const auto buffer = storage::io::read(fmt::format("scenes/{}.json", name)); const auto j = json::parse(buffer); - _background = _pixmappool->get(j["background"].template get()); + _background = _pixmappool->get(j["background"].template get()); _size = {j.at("width").get(), j.at("height").get()}; } diff --git a/src/scriptengine.cpp b/src/scriptengine.cpp index 932e363..8dcbbbf 100644 --- a/src/scriptengine.cpp +++ b/src/scriptengine.cpp @@ -12,6 +12,7 @@ #include "soundmanager.hpp" #include "ticks.hpp" #include "vector2d.hpp" +#include using namespace framework; @@ -93,12 +94,14 @@ void scriptengine::run() { "add_loopable", &engine::add_loopable, "set_scene", &engine::set_scene, "prefetch", [](engine &engine, sol::table table) { - std::vector filenames{table.size()}; - for (auto &item : table) { - filenames.push_back(item.second.as()); + std::vector filenames; + filenames.reserve(table.size()); + for (const auto &item : table) { + filenames.emplace_back(item.second.as()); } engine.prefetch(filenames); - } + }, + "flush", &engine::flush ); lua.new_usertype( diff --git a/src/soundmanager.cpp b/src/soundmanager.cpp index 37e4ff9..85d8009 100644 --- a/src/soundmanager.cpp +++ b/src/soundmanager.cpp @@ -5,13 +5,13 @@ using namespace audio; soundmanager::soundmanager(std::shared_ptr audiodevice) : _audiodevice(audiodevice) {} -void soundmanager::prefetch(const std::vector &filenames) { +void soundmanager::prefetch(const std::vector &filenames) { for (const auto &filename : filenames) { get(filename); } } -const std::shared_ptr soundmanager::get(const std::string_view filename) { +const std::shared_ptr soundmanager::get(const std::string &filename) { auto [it, added] = _soundmap.try_emplace(filename, nullptr); if (added) { @@ -27,11 +27,11 @@ const std::shared_ptr soundmanager::get(const std::string_view filename return it->second; } -void soundmanager::play(const std::string_view filename, bool loop = false) { +void soundmanager::play(const std::string &filename, bool loop = false) { get(fmt::format("blobs/{}.ogg", filename))->play(loop); } -void soundmanager::stop(const std::string_view filename) { +void soundmanager::stop(const std::string &filename) { get(filename)->stop(); } diff --git a/src/soundmanager.hpp b/src/soundmanager.hpp index ffadf61..54b4b91 100644 --- a/src/soundmanager.hpp +++ b/src/soundmanager.hpp @@ -9,21 +9,18 @@ class soundmanager { soundmanager(std::shared_ptr audiodevice); ~soundmanager() = default; - void prefetch(const std::vector &filenames); + void prefetch(const std::vector &filenames); - const std::shared_ptr get(const std::string_view filename); + const std::shared_ptr get(const std::string &filename); - void play(const std::string_view filename, bool loop); + void play(const std::string &filename, bool loop); - void stop(const std::string_view filename); + void stop(const std::string &filename); void flush(); private: std::shared_ptr _audiodevice; - - std::unordered_map, - std::hash> - _soundmap; + std::map> _soundmap; }; }