From 3ccd438657da39dcdcd615d4a49cbaa5237cddf9 Mon Sep 17 00:00:00 2001 From: Rodrigo Delduca Date: Sun, 15 Dec 2024 20:03:31 -0300 Subject: [PATCH] Work in progress --- conanfile.py | 1 - src/color.cpp | 8 ++++++-- src/common.hpp | 1 + src/engine.cpp | 4 ---- src/engine.hpp | 1 - src/entity.cpp | 2 +- src/entitymanager.cpp | 4 ++-- src/filesystem.cpp | 4 +++- src/fontfactory.cpp | 12 ++++++++---- src/framerate.cpp | 2 +- src/helpers.cpp | 20 ++++++++++++++++---- src/io.cpp | 16 +++++++++++++--- src/pixmap.cpp | 15 ++++++++++++--- src/pixmappool.cpp | 6 +++--- src/renderer.cpp | 4 +++- src/scenemanager.cpp | 4 ++-- src/scenemanager.hpp | 2 +- src/scriptengine.cpp | 10 ++++++---- src/socket.cpp | 18 +++++++++++++----- src/soundfx.cpp | 10 ++++++++-- src/soundmanager.cpp | 4 ++-- src/timermanager.cpp | 4 +++- src/window.cpp | 4 +++- 23 files changed, 107 insertions(+), 49 deletions(-) diff --git a/conanfile.py b/conanfile.py index 9f1cbe9..75f2204 100644 --- a/conanfile.py +++ b/conanfile.py @@ -9,7 +9,6 @@ class MeuProjetoConan(ConanFile): def requirements(self): self.requires("chipmunk2d/7.0.3") - self.requires("fmt/11.0.2") self.requires("libspng/0.7.4") self.requires("nlohmann_json/3.11.3") self.requires("ogg/1.3.5") diff --git a/src/color.cpp b/src/color.cpp index b369002..6ac02a0 100644 --- a/src/color.cpp +++ b/src/color.cpp @@ -11,11 +11,15 @@ color::color(const SDL_Color &scolor) noexcept color::color(const std::string &hex) : _r(0), _g(0), _b(0), _a(255) { if (hex.length() != 7 && hex.length() != 9) [[unlikely]] { - throw std::invalid_argument(fmt::format("Invalid hex code format: '{}'. Use #RRGGBB or #RRGGBBAA.", hex)); + std::ostringstream oss; + oss << "Invalid hex code format: '" << hex << "'. Use #RRGGBB or #RRGGBBAA."; + throw std::invalid_argument(oss.str()); } if (hex[0] != '#') [[unlikely]] { - throw std::invalid_argument(fmt::format("Hex code '{}' must start with '#'.", hex)); + std::ostringstream oss; + oss << "Hex code '" << hex << "' must start with '#'."; + throw std::invalid_argument(oss.str()); } _r = static_cast(std::stoi(hex.substr(1, 2), nullptr, 16)); diff --git a/src/common.hpp b/src/common.hpp index a4e612a..c1965a2 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -51,6 +51,7 @@ #include #include #include +#include #include #include #include diff --git a/src/engine.cpp b/src/engine.cpp index 17fed16..de1ed47 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -134,10 +134,6 @@ void engine::prefetch(const std::vector &filenames) noexcept { _resourcemanager->prefetch(filenames); } -void engine::set_scene(const std::string_view name) noexcept { - _scenemanager->set(name); -} - #ifdef EMSCRIPTEN template inline void run(void *arg) noexcept { diff --git a/src/engine.hpp b/src/engine.hpp index 5cdcb2d..ef95da6 100644 --- a/src/engine.hpp +++ b/src/engine.hpp @@ -43,7 +43,6 @@ class engine : public input::eventreceiver { bool is_keydown(const input::keyevent &event) const noexcept; void prefetch(const std::vector &filenames) noexcept; void run() noexcept; - void set_scene(const std::string_view name) noexcept; void _loop() noexcept; protected: diff --git a/src/entity.cpp b/src/entity.cpp index 8652a06..068477c 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -6,7 +6,7 @@ entity::entity(entityprops &&props) noexcept : _props(std::move(props)) {} entity::~entity() noexcept { - fmt::println("entity of type '{}' destroyed", kind()); + std::cout << "entity of type '" << kind() << "' destroyed" << std::endl; } std::shared_ptr entity::create(entityprops &&props) { diff --git a/src/entitymanager.cpp b/src/entitymanager.cpp index 6cb1e80..5e2f531 100644 --- a/src/entitymanager.cpp +++ b/src/entitymanager.cpp @@ -9,7 +9,7 @@ entitymanager::entitymanager(std::shared_ptr world, std::shared_ptr entitymanager::spawn(const std::string &kind) { - const auto buffer = storage::io::read(fmt::format("entities/{}.json", kind)); + const auto buffer = storage::io::read((std::ostringstream() << "entities/" << kind << ".json").str()); const auto j = json::parse(buffer); auto spritesheet = j.contains("spritesheet") @@ -93,7 +93,7 @@ std::shared_ptr entitymanager::spawn(const std::string &kind) { }; auto e = entity::create(std::move(props)); - fmt::println("[entitymanager] spawn {} kind {}", e->id(), kind); + std::cout << "[entitymanager] spawn " << e->id() << " kind " << kind << std::endl; _entities.emplace_back(e); return e; } diff --git a/src/filesystem.cpp b/src/filesystem.cpp index bc4f42d..e418015 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -4,6 +4,8 @@ using namespace storage; void filesystem::mount(std::string_view filename, std::string_view mountpoint) noexcept(false) { if (PHYSFS_mount(filename.data(), mountpoint.data(), true) == 0) [[unlikely]] { - throw std::runtime_error(fmt::format("[PHYSFS_mount] failed to mount {} to {}. reason: {}", filename, mountpoint, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))); + std::ostringstream oss; + oss << "[PHYSFS_mount] failed to mount " << filename << " to " << mountpoint << ". reason: " << PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()); + throw std::runtime_error(oss.str()); } } diff --git a/src/fontfactory.cpp b/src/fontfactory.cpp index d4a6f52..be4ba22 100644 --- a/src/fontfactory.cpp +++ b/src/fontfactory.cpp @@ -12,9 +12,9 @@ std::shared_ptr fontfactory::get(const std::string &family) { return it->second; } - fmt::println("[fontfactory] cache miss {}", family); + std::cout << "[fontfactory] cache miss " << family << std::endl; - const auto &buffer = storage::io::read(fmt::format("fonts/{}.json", family)); + const auto &buffer = storage::io::read("fonts/" + family + ".json"); const auto &j = json::parse(buffer); const auto &alphabet = j["alphabet"].get(); const auto spacing = j["spacing"].get(); @@ -37,7 +37,9 @@ std::shared_ptr fontfactory::get(const std::string &family) { }; if (!surface) [[unlikely]] { - throw std::runtime_error(fmt::format("[SDL_CreateRGBSurfaceWithFormatFrom] Error: {}", SDL_GetError())); + std::ostringstream oss; + oss << "[SDL_CreateRGBSurfaceWithFormatFrom] Error: " << SDL_GetError(); + throw std::runtime_error(oss.str()); } const auto pixels = static_cast(surface->pixels); @@ -52,7 +54,9 @@ std::shared_ptr fontfactory::get(const std::string &family) { } if (x >= size.width()) [[unlikely]] { - throw std::runtime_error(fmt::format("Error: Missing glyph for '{}'", letter)); + std::ostringstream oss; + oss << "Error: Missing glyph for '" << letter << "'"; + throw std::runtime_error(oss.str()); } width = 0; diff --git a/src/framerate.cpp b/src/framerate.cpp index e78c5ce..654e7ff 100644 --- a/src/framerate.cpp +++ b/src/framerate.cpp @@ -15,7 +15,7 @@ void framerate::loop(float_t delta) noexcept { _start = now; if (_elapsed >= 1000) { - fmt::println("{:.1f}", (_frames / (_elapsed / 1000.0f))); + std::cout << std::fixed << std::setprecision(1) << (_frames / (_elapsed / 1000.0f)) << std::endl; _elapsed = 0; _frames = 0; } diff --git a/src/helpers.cpp b/src/helpers.cpp index 4509e08..fdb3071 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -4,23 +4,35 @@ std::pair, geometry::size> _load_png(std::string_view filen auto ctx = std::unique_ptr(spng_ctx_new(0), spng_ctx_free); if (const auto error = spng_set_png_buffer(ctx.get(), buffer.data(), buffer.size()); error != SPNG_OK) [[unlikely]] { - throw std::runtime_error(fmt::format("[spng_set_png_buffer] error while parsing image: {}, error: {}", filename, spng_strerror(error))); + std::ostringstream oss; + oss << "[spng_set_png_buffer] error while parsing image: " << filename + << ", error: " << spng_strerror(error); + throw std::runtime_error(oss.str()); } spng_ihdr ihdr{}; if (const auto error = spng_get_ihdr(ctx.get(), &ihdr); error != SPNG_OK) [[unlikely]] { - throw std::runtime_error(fmt::format("[spng_get_ihdr] error while getting image information: {}, error: {}", filename, spng_strerror(error))); + std::ostringstream oss; + oss << "[spng_get_ihdr] error while getting image information: " << filename + << ", error: " << spng_strerror(error); + throw std::runtime_error(oss.str()); } const int format{SPNG_FMT_RGBA8}; size_t length{0}; if (const auto error = spng_decoded_image_size(ctx.get(), format, &length); error != SPNG_OK) [[unlikely]] { - throw std::runtime_error(fmt::format("[spng_decoded_image_size] error while getting image size: {}, error: {}", filename, spng_strerror(error))); + std::ostringstream oss; + oss << "[spng_decoded_image_size] error while getting image size: " << filename + << ", error: " << spng_strerror(error); + throw std::runtime_error(oss.str()); } std::vector output(length); if (const auto error = spng_decode_image(ctx.get(), output.data(), length, format, SPNG_DECODE_TRNS); error != SPNG_OK) [[unlikely]] { - throw std::runtime_error(fmt::format("[spng_decode_image] error while decoding image: {}, error: {}", filename, spng_strerror(error))); + std::ostringstream oss; + oss << "[spng_decode_image] error while decoding image: " << filename + << ", error: " << spng_strerror(error); + throw std::runtime_error(oss.str()); } const auto size = geometry::size{static_cast(ihdr.width), static_cast(ihdr.height)}; diff --git a/src/io.cpp b/src/io.cpp index 777a976..8fed204 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -5,18 +5,28 @@ using namespace storage; std::vector io::read(std::string_view filename) noexcept(false) { std::unique_ptr ptr(PHYSFS_openRead(filename.data()), PHYSFS_close); if (!ptr) [[unlikely]] { - throw std::runtime_error(fmt::format("[PHYSFS_openRead] error while opening file: {}, error: {}", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))); + std::ostringstream oss; + oss << "[PHYSFS_openRead] error while opening file: " << filename + << ", error: " << PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()); + throw std::runtime_error(oss.str()); } PHYSFS_sint64 length = PHYSFS_fileLength(ptr.get()); if (length <= 0) [[unlikely]] { - throw std::runtime_error(fmt::format("[PHYSFS_fileLength] invalid file length, file: {}, error: {}", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))); + std::ostringstream oss; + oss << "[PHYSFS_fileLength] invalid file length, file: " << filename + << ", error: " << PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()); + throw std::runtime_error(oss.str()); } std::vector buffer(length); auto bytesRead = PHYSFS_readBytes(ptr.get(), buffer.data(), length); if (bytesRead != length) [[unlikely]] { - throw std::runtime_error(fmt::format("[PHYSFS_readBytes] error reading file: {}, expected {} bytes but read {}, error: {}", filename, length, bytesRead, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))); + std::ostringstream oss; + oss << "[PHYSFS_readBytes] error reading file: " << filename + << ", expected " << length << " bytes but read " << bytesRead + << ", error: " << PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()); + throw std::runtime_error(oss.str()); } return buffer; diff --git a/src/pixmap.cpp b/src/pixmap.cpp index 5903353..56ed1cf 100644 --- a/src/pixmap.cpp +++ b/src/pixmap.cpp @@ -18,14 +18,20 @@ pixmap::pixmap(const std::shared_ptr &renderer, std::string_view filen SDL_FreeSurface }; if (!surface) [[unlikely]] { - throw std::runtime_error(fmt::format("[SDL_CreateRGBSurfaceWithFormat] error while creating surface, file: {}, error: {}", filename, SDL_GetError())); + std::ostringstream oss; + oss << "[SDL_CreateRGBSurfaceWithFormat] error while creating surface, file: " + << filename << ", error: " << SDL_GetError(); + throw std::runtime_error(oss.str()); } std::memcpy(surface->pixels, output.data(), output.size()); _texture = texture_ptr(SDL_CreateTextureFromSurface(*renderer, surface.get()), SDL_Deleter()); if (!_texture) [[unlikely]] { - throw std::runtime_error(fmt::format("[SDL_CreateTextureFromSurface] error while creating texture from surface, file: {}", filename)); + std::ostringstream oss; + oss << "[SDL_CreateTextureFromSurface] error while creating texture from surface, file: " + << filename; + throw std::runtime_error(oss.str()); } } @@ -34,7 +40,10 @@ pixmap::pixmap(const std::shared_ptr &renderer, std::unique_ptr pixmappool::get(const std::string &filename) { return it->second; } - fmt::println("[pixmappool] cache miss {}", filename); + std::cout << "[pixmappool] cache miss " << filename << std::endl; assert(_renderer); @@ -21,10 +21,10 @@ const std::shared_ptr pixmappool::get(const std::string &filename) { } void pixmappool::flush() noexcept { - fmt::println("[pixmappool] actual size {}", _pool.size()); + std::cout << "[pixmappool] actual size " << _pool.size() << std::endl; const auto count = std::erase_if(_pool, [](const auto &pair) { return pair.second.use_count() == MINIMAL_USE_COUNT; }); - fmt::println("[pixmappool] {} objects have been flushed", count); + std::cout << "[pixmappool] " << count << " objects have been flushed" << std::endl; } void pixmappool::update(float_t delta) noexcept { diff --git a/src/renderer.cpp b/src/renderer.cpp index 9f68ae5..0bfe851 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -5,7 +5,9 @@ using namespace graphics; renderer::renderer(SDL_Window *window) : _renderer(SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC), SDL_Deleter()) { if (!_renderer) [[unlikely]] { - throw std::runtime_error(fmt::format("[SDL_CreateRenderer] failed to create renderer: {}", SDL_GetError())); + std::ostringstream oss; + oss << "[SDL_CreateRenderer] failed to create renderer: " << SDL_GetError(); + throw std::runtime_error(oss.str()); } } diff --git a/src/scenemanager.cpp b/src/scenemanager.cpp index 0f56f6f..038c884 100644 --- a/src/scenemanager.cpp +++ b/src/scenemanager.cpp @@ -7,8 +7,8 @@ using json = nlohmann::json; scenemanager::scenemanager(std::shared_ptr pixmappool) noexcept : _pixmappool(std::move(pixmappool)) {} -void scenemanager::set(const std::string_view name) noexcept { - const auto buffer = storage::io::read(fmt::format("scenes/{}.json", name)); +void scenemanager::set(const std::string &name) noexcept { + const auto buffer = storage::io::read("scenes/" + name + ".json"); const auto j = json::parse(buffer); _background = _pixmappool->get(j["background"].get()); _size = {j.at("width").get(), j.at("height").get()}; diff --git a/src/scenemanager.hpp b/src/scenemanager.hpp index 49ec8d6..f3e7f60 100644 --- a/src/scenemanager.hpp +++ b/src/scenemanager.hpp @@ -8,7 +8,7 @@ class scenemanager { public: scenemanager(std::shared_ptr pixmappool) noexcept; - void set(const std::string_view name) noexcept; + void set(const std::string &name) noexcept; void update(float_t delta) noexcept; void draw() const noexcept; diff --git a/src/scriptengine.cpp b/src/scriptengine.cpp index 5e5b118..824591e 100644 --- a/src/scriptengine.cpp +++ b/src/scriptengine.cpp @@ -25,8 +25,8 @@ using namespace math; using namespace storage; using namespace network; -sol::table require(sol::state &lua, std::string_view module) { - const auto data = io::read(fmt::format("scripts/{}.lua", module)); +sol::table require(sol::state &lua, const std::string &module) { + const auto data = io::read("scripts/" + module + ".lua"); const auto script = std::string(data.begin(), data.end()); const auto result = lua.script(script); @@ -116,7 +116,7 @@ void scriptengine::run() { lua.open_libraries(); - lua["require"] = [&lua](std::string_view module) { + lua["require"] = [&lua](const std::string &module) { return require(lua, module); }; @@ -179,7 +179,9 @@ void scriptengine::run() { } else if (value.is()) { e.set_kv(key, value.as()); } else { - throw std::runtime_error(fmt::format("Unsupported type for key: {}", key)); + std::ostringstream oss; + oss << "Unsupported type for key: " << key; + throw std::runtime_error(oss.str()); } } }; diff --git a/src/socket.cpp b/src/socket.cpp index afe9054..d6f0c85 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -7,8 +7,7 @@ using json = nlohmann::json; socket::socket() noexcept { _queue.reserve(8); - const auto url = fmt::format("http://{}:3000/socket", emscripten_run_script_string("window.location.hostname")); - + const auto url = "http://" + std::string(emscripten_run_script_string("window.location.hostname")) + ":3000/socket"; EmscriptenWebSocketCreateAttributes attrs = { url.c_str(), nullptr, @@ -116,16 +115,25 @@ socket::~socket() noexcept { } void socket::emit(const std::string &topic, const std::string &data) noexcept { - send(fmt::format(R"({{"event": {{"topic": "{}", "data": {}}}}})", topic, data)); + std::ostringstream oss; + oss << R"({"event": {"topic": ")" << topic << R"(", "data": )" << data << R"(}})"; + send(oss.str()); } void socket::on(const std::string &topic, std::function callback) noexcept { - send(fmt::format(R"({{"subscribe": "{}"}})", topic)); + std::ostringstream oss; + oss << R"({"subscribe": ")" << topic << R"("})"; + send(oss.str()); _callbacks[topic].push_back(std::move(callback)); } void socket::rpc(const std::string &method, const std::string &arguments, std::function callback) noexcept { - send(fmt::format(R"({{"rpc": {{"request": {{"id": {}, "method": "{}", "arguments": {}}}}}}})", ++counter, method, arguments)); + std::ostringstream oss; + oss << R"({"rpc": {"request": {"id": )" << ++counter + << R"(, "method": ")" << method + << R"(", "arguments": )" << arguments << R"(}}})"; + send(oss.str()); + _callbacks[std::to_string(counter)].push_back(std::move(callback)); } diff --git a/src/soundfx.cpp b/src/soundfx.cpp index 2b8aaa7..00283d2 100644 --- a/src/soundfx.cpp +++ b/src/soundfx.cpp @@ -74,7 +74,10 @@ soundfx::soundfx(std::shared_ptr audiodevice, std::string_view file : _audiodevice(std::move(audiodevice)) { std::unique_ptr fp{PHYSFS_openRead(filename.data()), PHYSFS_close}; if (!fp) [[unlikely]] { - throw std::runtime_error(fmt::format("[PHYSFS_openRead] error while opening file: {}, error: {}", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))); + std::ostringstream oss; + oss << "[PHYSFS_openRead] error while opening file: " << filename + << ", error: " << PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()); + throw std::runtime_error(oss.str()); } std::unique_ptr vf{new OggVorbis_File, ov_clear}; @@ -106,7 +109,10 @@ soundfx::soundfx(std::shared_ptr audiodevice, std::string_view file do { offset = ov_read(vf.get(), reinterpret_cast(array.data()), length, bigendian, 2, 1, nullptr); if (offset < 0) [[unlikely]] { - throw std::runtime_error(fmt::format("[ov_read] error while reading file: {}, error: {}", filename, ov_strerror(offset))); + std::ostringstream oss; + oss << "[ov_read] error while reading file: " << filename + << ", error: " << ov_strerror(offset); + throw std::runtime_error(oss.str()); } data.insert(data.end(), array.begin(), std::ranges::next(array.begin(), offset)); } while (offset > 0); diff --git a/src/soundmanager.cpp b/src/soundmanager.cpp index d266a5a..307a4b7 100644 --- a/src/soundmanager.cpp +++ b/src/soundmanager.cpp @@ -10,7 +10,7 @@ std::shared_ptr soundmanager::get(const std::string &filename) noexcept return it->second; } - fmt::println("[soundmanager] cache miss {}", filename); + std::cout << "[soundmanager] cache miss " << filename << std::endl; assert(_audiodevice); @@ -21,7 +21,7 @@ std::shared_ptr soundmanager::get(const std::string &filename) noexcept } void soundmanager::play(const std::string &filename) noexcept { - if (const auto &sound = get(fmt::format("blobs/{}.ogg", filename)); sound) { + if (const auto &sound = get("blobs/" + filename + ".ogg"); sound) { sound->play(); } } diff --git a/src/timermanager.cpp b/src/timermanager.cpp index 790a97c..fa1669a 100644 --- a/src/timermanager.cpp +++ b/src/timermanager.cpp @@ -41,7 +41,9 @@ void timermanager::add_timer(int32_t interval, std::function fn, bool re const auto ptr = std::make_shared>(std::move(fn)); const auto id = SDL_AddTimer(interval, repeat ? wrapper : singleshot_wrapper, ptr.get()); if (!id) [[unlikely]] { - throw std::runtime_error(fmt::format("[SDL_AddTimer] failed to set timer. reason: {}", SDL_GetError())); + std::ostringstream oss; + oss << "[SDL_AddTimer] failed to set timer. reason: " << SDL_GetError(); + throw std::runtime_error(oss.str()); } _timers.emplace(id, ptr); diff --git a/src/window.cpp b/src/window.cpp index 1365a9f..5c86e58 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -6,7 +6,9 @@ window::window(std::string_view title, int32_t width, int32_t height, bool fulls : _width(width), _height(height), _window(SDL_CreateWindow(title.data(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN | (fullscreen ? SDL_WINDOW_FULLSCREEN : 0)), SDL_Deleter()) { if (_window == nullptr) [[unlikely]] { - throw std::runtime_error(fmt::format("[SDL_CreateWindow] failed to create window: {}", SDL_GetError())); + std::ostringstream oss; + oss << "[SDL_CreateWindow] failed to create window: " << SDL_GetError(); + throw std::runtime_error(oss.str()); } }