From df23a3e31fd492fb2cebf218eccdbeca8c3129ad Mon Sep 17 00:00:00 2001 From: Rodrigo Delduca Date: Wed, 11 Dec 2024 13:23:53 -0300 Subject: [PATCH] Work in progress --- CMakeLists.txt | 3 +-- conanfile.txt | 1 - src/color.cpp | 4 ++-- src/common.hpp | 2 +- src/entity.cpp | 3 +-- src/entitymanager.cpp | 4 ++-- src/filesystem.cpp | 2 +- src/fontfactory.cpp | 8 ++++---- src/framerate.cpp | 2 +- src/helpers.cpp | 8 ++++---- src/io.cpp | 6 +++--- src/pixmap.cpp | 6 +++--- src/pixmappool.cpp | 6 +++--- src/renderer.cpp | 2 +- src/scenemanager.cpp | 2 +- src/scriptengine.cpp | 2 +- src/socket.cpp | 8 ++++---- src/soundfx.cpp | 4 ++-- src/soundmanager.cpp | 4 ++-- src/timermanager.cpp | 2 +- src/window.cpp | 2 +- 21 files changed, 39 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cc7d27..07922f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,6 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) find_package(OpenAL CONFIG REQUIRED) find_package(chipmunk2d CONFIG REQUIRED) -find_package(fmt CONFIG REQUIRED) find_package(Ogg CONFIG REQUIRED) find_package(PhysFS CONFIG REQUIRED) find_package(SDL2 CONFIG REQUIRED) @@ -53,6 +52,7 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Emscripten") "-s PTHREAD_POOL_SIZE=4" "-s INITIAL_MEMORY=128MB" "-l websocket.js" + "-s USE_SDL=2" "-s WEBSOCKET_SUBPROTOCOL=text" "-s EXPORTED_RUNTIME_METHODS=['callMain']" # Debugging @@ -72,7 +72,6 @@ endif() target_link_libraries(${PROJECT_NAME} PRIVATE chipmunk2d::chipmunk2d - fmt::fmt Ogg::ogg physfs-static SDL2::SDL2-static diff --git a/conanfile.txt b/conanfile.txt index 06decc6..754793a 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -1,6 +1,5 @@ [requires] chipmunk2d/7.0.3 -fmt/11.0.2 libspng/0.7.4 nlohmann_json/3.11.3 ogg/1.3.5 diff --git a/src/color.cpp b/src/color.cpp index b369002..a02b9d4 100644 --- a/src/color.cpp +++ b/src/color.cpp @@ -11,11 +11,11 @@ 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)); + throw std::invalid_argument(std::format("Invalid hex code format: '{}'. Use #RRGGBB or #RRGGBBAA.", hex)); } if (hex[0] != '#') [[unlikely]] { - throw std::invalid_argument(fmt::format("Hex code '{}' must start with '#'.", hex)); + throw std::invalid_argument(std::format("Hex code '{}' must start with '#'.", hex)); } _r = static_cast(std::stoi(hex.substr(1, 2), nullptr, 16)); diff --git a/src/common.hpp b/src/common.hpp index f39c6a5..1ec0bd7 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -38,6 +37,7 @@ #include #include #include +#include #include #include #include diff --git a/src/entity.cpp b/src/entity.cpp index 47fdf3f..7feaff2 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -1,5 +1,4 @@ #include "entity.hpp" -#include using namespace framework; @@ -7,7 +6,7 @@ entity::entity(entityprops &&props) noexcept : _props(std::move(props)) {} entity::~entity() noexcept { - fmt::print("entity destroyed {}\n", kind()); + std::println("entity of type '{}' destroyed", kind()); } std::shared_ptr entity::create(entityprops &&props) { diff --git a/src/entitymanager.cpp b/src/entitymanager.cpp index 6cb1e80..ae2b6e1 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::format("entities/{}.json", kind)); 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::println("[entitymanager] spawn {} kind {}", e->id(), kind); _entities.emplace_back(e); return e; } diff --git a/src/filesystem.cpp b/src/filesystem.cpp index bc4f42d..987f15e 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -4,6 +4,6 @@ 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()))); + throw std::runtime_error(std::format("[PHYSFS_mount] failed to mount {} to {}. reason: {}", filename, mountpoint, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))); } } diff --git a/src/fontfactory.cpp b/src/fontfactory.cpp index bc072d9..b2af9af 100644 --- a/src/fontfactory.cpp +++ b/src/fontfactory.cpp @@ -11,9 +11,9 @@ 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); + std::println("[fontfactory] cache miss {}", family); - const auto &buffer = storage::io::read(fmt::format("fonts/{}.json", family)); + const auto &buffer = storage::io::read(std::format("fonts/{}.json", family)); const auto &j = json::parse(buffer); const auto &alphabet = j["alphabet"].get(); const auto spacing = j["spacing"].get(); @@ -35,7 +35,7 @@ std::shared_ptr fontfactory::get(const std::string &family) { }; if (!surface) [[unlikely]] { - throw std::runtime_error(fmt::format("[SDL_CreateRGBSurfaceWithFormatFrom] Error: {}", SDL_GetError())); + throw std::runtime_error(std::format("[SDL_CreateRGBSurfaceWithFormatFrom] Error: {}", SDL_GetError())); } const auto pixels = static_cast(surface->pixels); @@ -50,7 +50,7 @@ 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)); + throw std::runtime_error(std::format("Error: Missing glyph for '{}'", letter)); } width = 0; diff --git a/src/framerate.cpp b/src/framerate.cpp index e78c5ce..44c2ac7 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::println("{:.1f}", (_frames / (_elapsed / 1000.0f))); _elapsed = 0; _frames = 0; } diff --git a/src/helpers.cpp b/src/helpers.cpp index 4509e08..8518b78 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -4,23 +4,23 @@ 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))); + throw std::runtime_error(std::format("[spng_set_png_buffer] error while parsing image: {}, error: {}", filename, spng_strerror(error))); } 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))); + throw std::runtime_error(std::format("[spng_get_ihdr] error while getting image information: {}, error: {}", filename, spng_strerror(error))); } 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))); + throw std::runtime_error(std::format("[spng_decoded_image_size] error while getting image size: {}, error: {}", filename, spng_strerror(error))); } 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))); + throw std::runtime_error(std::format("[spng_decode_image] error while decoding image: {}, error: {}", filename, spng_strerror(error))); } 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..ad10d3d 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -5,18 +5,18 @@ 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()))); + throw std::runtime_error(std::format("[PHYSFS_openRead] error while opening file: {}, error: {}", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))); } 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()))); + throw std::runtime_error(std::format("[PHYSFS_fileLength] invalid file length, file: {}, error: {}", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))); } 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()))); + throw std::runtime_error(std::format("[PHYSFS_readBytes] error reading file: {}, expected {} bytes but read {}, error: {}", filename, length, bytesRead, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))); } return buffer; diff --git a/src/pixmap.cpp b/src/pixmap.cpp index 5903353..9d6569a 100644 --- a/src/pixmap.cpp +++ b/src/pixmap.cpp @@ -18,14 +18,14 @@ 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())); + throw std::runtime_error(std::format("[SDL_CreateRGBSurfaceWithFormat] error while creating surface, file: {}, error: {}", filename, SDL_GetError())); } 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)); + throw std::runtime_error(std::format("[SDL_CreateTextureFromSurface] error while creating texture from surface, file: {}", filename)); } } @@ -34,7 +34,7 @@ pixmap::pixmap(const std::shared_ptr &renderer, std::unique_ptr pixmappool::get(const std::string &filename) { auto [it, added] = _pool.insert_or_assign(filename, nullptr); if (added) [[unlikely]] { - fmt::println("[pixmappool] cache miss {}", filename); + std::println("[pixmappool] cache miss {}", filename); assert(_renderer); @@ -20,10 +20,10 @@ const std::shared_ptr pixmappool::get(const std::string &filename) { } void pixmappool::flush() noexcept { - fmt::println("[pixmappool] actual size {}", _pool.size()); + std::println("[pixmappool] actual size {}", _pool.size()); 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::println("[pixmappool] {} objects have been flushed", count); } void pixmappool::update(float_t delta) noexcept { diff --git a/src/renderer.cpp b/src/renderer.cpp index 9f68ae5..8959be9 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -5,7 +5,7 @@ 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())); + throw std::runtime_error(std::format("[SDL_CreateRenderer] failed to create renderer: {}", SDL_GetError())); } } diff --git a/src/scenemanager.cpp b/src/scenemanager.cpp index 0f56f6f..ad46bca 100644 --- a/src/scenemanager.cpp +++ b/src/scenemanager.cpp @@ -8,7 +8,7 @@ scenemanager::scenemanager(std::shared_ptr pixmappool) noe : _pixmappool(std::move(pixmappool)) {} void scenemanager::set(const std::string_view name) noexcept { - const auto buffer = storage::io::read(fmt::format("scenes/{}.json", name)); + const auto buffer = storage::io::read(std::format("scenes/{}.json", name)); 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/scriptengine.cpp b/src/scriptengine.cpp index a37bf96..c473daf 100644 --- a/src/scriptengine.cpp +++ b/src/scriptengine.cpp @@ -26,7 +26,7 @@ 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)); + const auto data = io::read(std::format("scripts/{}.lua", module)); const auto script = std::string(data.begin(), data.end()); const auto result = lua.script(script); diff --git a/src/socket.cpp b/src/socket.cpp index afe9054..37aab4b 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -7,7 +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 = std::format("http://{}:3000/socket", emscripten_run_script_string("window.location.hostname")); EmscriptenWebSocketCreateAttributes attrs = { url.c_str(), @@ -116,16 +116,16 @@ socket::~socket() noexcept { } void socket::emit(const std::string &topic, const std::string &data) noexcept { - send(fmt::format(R"({{"event": {{"topic": "{}", "data": {}}}}})", topic, data)); + send(std::format(R"({{"event": {{"topic": "{}", "data": {}}}}})", topic, data)); } void socket::on(const std::string &topic, std::function callback) noexcept { - send(fmt::format(R"({{"subscribe": "{}"}})", topic)); + send(std::format(R"({{"subscribe": "{}"}})", topic)); _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)); + send(std::format(R"({{"rpc": {{"request": {{"id": {}, "method": "{}", "arguments": {}}}}}}})", ++counter, method, arguments)); _callbacks[std::to_string(counter)].push_back(std::move(callback)); } diff --git a/src/soundfx.cpp b/src/soundfx.cpp index 2b8aaa7..17b81e8 100644 --- a/src/soundfx.cpp +++ b/src/soundfx.cpp @@ -74,7 +74,7 @@ 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()))); + throw std::runtime_error(std::format("[PHYSFS_openRead] error while opening file: {}, error: {}", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))); } std::unique_ptr vf{new OggVorbis_File, ov_clear}; @@ -106,7 +106,7 @@ 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))); + throw std::runtime_error(std::format("[ov_read] error while reading file: {}, error: {}", filename, ov_strerror(offset))); } 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 d7db777..60479de 100644 --- a/src/soundmanager.cpp +++ b/src/soundmanager.cpp @@ -9,7 +9,7 @@ std::shared_ptr soundmanager::get(const std::string &filename) noexcept auto [it, added] = _pool.insert_or_assign(filename, nullptr); if (added) [[unlikely]] { - fmt::println("[soundmanager] cache miss {}", filename); + std::println("[soundmanager] cache miss {}", filename); assert(_audiodevice); @@ -20,7 +20,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(std::format("blobs/{}.ogg", filename)); sound) { sound->play(); } } diff --git a/src/timermanager.cpp b/src/timermanager.cpp index 790a97c..03d2ae3 100644 --- a/src/timermanager.cpp +++ b/src/timermanager.cpp @@ -41,7 +41,7 @@ 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())); + throw std::runtime_error(std::format("[SDL_AddTimer] failed to set timer. reason: {}", SDL_GetError())); } _timers.emplace(id, ptr); diff --git a/src/window.cpp b/src/window.cpp index 1365a9f..0c416aa 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -6,7 +6,7 @@ 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())); + throw std::runtime_error(std::format("[SDL_CreateWindow] failed to create window: {}", SDL_GetError())); } }