From 777556306a5ab23d97d955701c93ae764673ddfe Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 5 Nov 2024 11:32:17 +0100 Subject: make string format RAII --- src/crepe/util/fmt.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/crepe/util') diff --git a/src/crepe/util/fmt.cpp b/src/crepe/util/fmt.cpp index 8ef1164..397bf9f 100644 --- a/src/crepe/util/fmt.cpp +++ b/src/crepe/util/fmt.cpp @@ -7,17 +7,19 @@ using namespace std; string crepe::util::va_stringf(va_list args, const char * fmt) { + string out; + va_list args_copy; va_copy(args_copy, args); - - size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1; - char * msg = (char *) malloc(sz); + size_t length = vsnprintf(NULL, 0, fmt, args_copy); + // resize to include terminating null byte + out.resize(length + 1); va_end(args_copy); - vsnprintf(msg, sz, fmt, args); - - string out = msg; - free(msg); + // vsnprintf adds terminating null byte + vsnprintf(out.data(), out.size(), fmt, args); + // resize to actual length + out.resize(length); va_end(args); -- cgit v1.2.3 From a47b981c2254e1d49f58ebd4244c1be4624ba998 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 5 Nov 2024 15:49:59 +0100 Subject: remove util and api namespaces --- src/crepe/SDLContext.cpp | 3 +- src/crepe/SDLContext.h | 9 ++-- src/crepe/api/AssetManager.cpp | 2 +- src/crepe/api/AssetManager.h | 4 +- src/crepe/api/AssetManager.hpp | 4 +- src/crepe/api/AudioSource.cpp | 2 +- src/crepe/api/AudioSource.h | 8 ++-- src/crepe/api/BehaviorScript.h | 4 +- src/crepe/api/BehaviorScript.hpp | 4 +- src/crepe/api/CircleCollider.h | 4 +- src/crepe/api/Color.cpp | 2 +- src/crepe/api/Color.h | 4 +- src/crepe/api/Config.h | 6 +-- src/crepe/api/Force.cpp | 4 +- src/crepe/api/Force.h | 4 +- src/crepe/api/GameObject.cpp | 2 +- src/crepe/api/GameObject.h | 4 +- src/crepe/api/GameObject.hpp | 4 +- src/crepe/api/Point.h | 4 +- src/crepe/api/Rigidbody.cpp | 2 +- src/crepe/api/Rigidbody.h | 4 +- src/crepe/api/Script.cpp | 2 +- src/crepe/api/Script.h | 6 +-- src/crepe/api/Script.hpp | 4 +- src/crepe/api/Sprite.cpp | 5 +- src/crepe/api/Sprite.h | 4 +- src/crepe/api/Texture.cpp | 6 +-- src/crepe/api/Texture.h | 4 +- src/crepe/api/Transform.cpp | 2 +- src/crepe/api/Transform.h | 4 +- src/crepe/system/PhysicsSystem.cpp | 1 - src/crepe/system/RenderSystem.cpp | 1 - src/crepe/system/ScriptSystem.cpp | 1 - src/crepe/system/ScriptSystem.h | 9 ++-- src/crepe/util/CMakeLists.txt | 4 +- src/crepe/util/LogColor.cpp | 92 +++++++++++++++++++++++++++++++++++++ src/crepe/util/LogColor.h | 51 ++++++++++++++++++++ src/crepe/util/color.cpp | 91 ------------------------------------ src/crepe/util/color.h | 51 -------------------- src/crepe/util/fmt.cpp | 4 +- src/crepe/util/fmt.h | 4 +- src/crepe/util/log.cpp | 10 ++-- src/crepe/util/log.h | 16 +++---- src/example/asset_manager.cpp | 2 +- src/example/audio_internal.cpp | 5 +- src/example/components_internal.cpp | 1 - src/example/log.cpp | 5 +- src/example/particle.cpp | 1 - src/example/physics.cpp | 1 - src/example/rendering.cpp | 1 - src/example/script.cpp | 5 +- 51 files changed, 230 insertions(+), 247 deletions(-) create mode 100644 src/crepe/util/LogColor.cpp create mode 100644 src/crepe/util/LogColor.h delete mode 100644 src/crepe/util/color.cpp delete mode 100644 src/crepe/util/color.h (limited to 'src/crepe/util') diff --git a/src/crepe/SDLContext.cpp b/src/crepe/SDLContext.cpp index 8bc5bc6..e61faa3 100644 --- a/src/crepe/SDLContext.cpp +++ b/src/crepe/SDLContext.cpp @@ -89,8 +89,7 @@ SDLContext::SDLContext() { void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer); } -void SDLContext::draw(const api::Sprite & sprite, - const api::Transform & transform) { +void SDLContext::draw(const Sprite & sprite, const Transform & transform) { static SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) diff --git a/src/crepe/SDLContext.h b/src/crepe/SDLContext.h index ea05c7b..94fc549 100644 --- a/src/crepe/SDLContext.h +++ b/src/crepe/SDLContext.h @@ -8,12 +8,9 @@ #include "system/RenderSystem.h" -namespace crepe::api { -class Texture; -} - namespace crepe { +class Texture; class SDLContext { public: @@ -34,13 +31,13 @@ private: virtual ~SDLContext(); private: - friend class api::Texture; + friend class Texture; SDL_Texture * texture_from_path(const char *); //SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col); private: friend class RenderSystem; - void draw(const api::Sprite &, const api::Transform &); + void draw(const Sprite &, const Transform &); void clear_screen(); void present_screen(); diff --git a/src/crepe/api/AssetManager.cpp b/src/crepe/api/AssetManager.cpp index 560df6c..b891760 100644 --- a/src/crepe/api/AssetManager.cpp +++ b/src/crepe/api/AssetManager.cpp @@ -2,7 +2,7 @@ #include "AssetManager.h" -using namespace crepe::api; +using namespace crepe; AssetManager & AssetManager::get_instance() { static AssetManager instance; diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h index 3e72a49..fefbed9 100644 --- a/src/crepe/api/AssetManager.h +++ b/src/crepe/api/AssetManager.h @@ -5,7 +5,7 @@ #include #include -namespace crepe::api { +namespace crepe { class AssetManager { @@ -30,6 +30,6 @@ public: bool reload = false); }; -} // namespace crepe::api +} // namespace crepe #include "AssetManager.hpp" diff --git a/src/crepe/api/AssetManager.hpp b/src/crepe/api/AssetManager.hpp index 468724c..977b4e1 100644 --- a/src/crepe/api/AssetManager.hpp +++ b/src/crepe/api/AssetManager.hpp @@ -2,7 +2,7 @@ #include "AssetManager.h" -namespace crepe::api { +namespace crepe { template std::shared_ptr AssetManager::cache(const std::string & file_path, @@ -21,4 +21,4 @@ std::shared_ptr AssetManager::cache(const std::string & file_path, return new_asset; } -} // namespace crepe::api +} // namespace crepe diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index 35b8d83..2bacc75 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -4,7 +4,7 @@ #include "AudioSource.h" -using namespace crepe::api; +using namespace crepe; AudioSource::AudioSource(std::unique_ptr audio_clip) { this->sound = std::make_unique(std::move(audio_clip)); diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 7980212..42add50 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -6,10 +6,8 @@ #include "../Component.h" namespace crepe { -class Sound; -} -namespace crepe::api { +class Sound; //! Audio source component class AudioSource : Component { @@ -35,7 +33,7 @@ public: float volume; private: - std::unique_ptr sound; + std::unique_ptr sound; }; -} // namespace crepe::api +} // namespace crepe diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 6133cc8..21638f4 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -9,7 +9,7 @@ class ScriptSystem; class ComponentManager; } // namespace crepe -namespace crepe::api { +namespace crepe { class Script; @@ -30,6 +30,6 @@ protected: std::unique_ptr