diff options
Diffstat (limited to 'src/crepe')
29 files changed, 82 insertions, 118 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 52a781e..3b05742 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -4,7 +4,6 @@ target_sources(crepe PUBLIC ComponentManager.cpp Component.cpp Collider.cpp - Exception.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -15,8 +14,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Collider.h ValueBroker.h ValueBroker.hpp - Exception.h - Exception.hpp ) add_subdirectory(api) diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 12c10cb..670446f 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -26,6 +26,8 @@ protected: Component(game_object_id_t id); //! Only the ComponentManager can create components friend class ComponentManager; +public: + virtual ~Component() = default; public: /** diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 7af0380..67c6fc7 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -6,6 +6,9 @@ using namespace crepe; using namespace std; +ComponentManager::ComponentManager() { dbg_trace(); } +ComponentManager::~ComponentManager() { dbg_trace(); } + void ComponentManager::delete_all_components_of_id(game_object_id_t id) { // Loop through all the types (in the unordered_map<>) for (auto & [type, componentArray] : this->components) { @@ -18,18 +21,14 @@ void ComponentManager::delete_all_components_of_id(game_object_id_t id) { } void ComponentManager::delete_all_components() { - // Clear the whole unordered_map<> this->components.clear(); + this->next_id = 0; } -ComponentManager::ComponentManager() { dbg_trace(); } -ComponentManager::~ComponentManager() { dbg_trace(); } - GameObject ComponentManager::new_object(const string & name, const string & tag, const Vector2 & position, double rotation, double scale) { - GameObject object{*this, this->next_id, name, tag, - position, rotation, scale}; + GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; this->next_id++; return object; } diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 51c84a4..ca2e7c6 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -6,9 +6,10 @@ #include <unordered_map> #include <vector> -#include "Component.h" #include "api/Vector2.h" +#include "Component.h" + namespace crepe { class GameObject; diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 98efb49..0a84468 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -40,7 +40,6 @@ T & ComponentManager::add_component(game_object_id_t id, Args &&... args) { // Check if the vector size is not greater than get_instances_max int max_instances = instance->get_instances_max(); if (max_instances != -1 && components[type][id].size() >= max_instances) { - // TODO: Exception throw std::runtime_error( "Exceeded maximum number of instances for this component type"); } diff --git a/src/crepe/Exception.cpp b/src/crepe/Exception.cpp deleted file mode 100644 index 5a24e7e..0000000 --- a/src/crepe/Exception.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "Exception.h" - -using namespace crepe; -using namespace std; - -const char * Exception::what() const noexcept { return error.c_str(); } - -Exception::Exception(const string & msg) { this->error = msg; } diff --git a/src/crepe/Exception.h b/src/crepe/Exception.h deleted file mode 100644 index 580fc16..0000000 --- a/src/crepe/Exception.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include <exception> -#include <format> -#include <string> - -namespace crepe { - -//! Exception class -class Exception : public std::exception { -public: - //! Exception with plain message - Exception(const std::string & msg); - - //! Exception with \c std::format message - template <class... Args> - Exception(std::format_string<Args...> fmt, Args &&... args); - - //! Get formatted error message - const char * what() const noexcept; - -protected: - Exception() = default; - - //! Formatted error message - std::string error; -}; - -} // namespace crepe - -#include "Exception.hpp" diff --git a/src/crepe/Exception.hpp b/src/crepe/Exception.hpp deleted file mode 100644 index 7c462a3..0000000 --- a/src/crepe/Exception.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include "Exception.h" - -namespace crepe { - -template <class... Args> -Exception::Exception(std::format_string<Args...> fmt, Args &&... args) { - this->error = std::format(fmt, std::forward<Args>(args)...); -} - -} // namespace crepe diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 54b2ec3..cccbc67 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -1,6 +1,4 @@ -#include <cstdint> - #include "util/Log.h" #include "Animator.h" diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 75b8139..3573403 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,7 +1,5 @@ #pragma once -#include <cstdint> - #include "Component.h" #include "Sprite.h" diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 2982358..1a8910d 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -3,6 +3,7 @@ #include <memory> #include "../Component.h" + #include "GameObject.h" namespace crepe { @@ -47,8 +48,6 @@ protected: std::unique_ptr<Script> script = nullptr; //! Reference to component manager ComponentManager & component_manager; - -private: //! Script accesses the component manager directly via its parent // (BehaviorScript) reference friend class Script; diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 85696c4..f9b370f 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,7 +1,6 @@ target_sources(crepe PUBLIC # AudioSource.cpp BehaviorScript.cpp - Script.cpp GameObject.cpp Rigidbody.cpp ParticleEmitter.cpp diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index d8c08a6..bf97731 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -1,7 +1,5 @@ #pragma once -#include <cstdint> - #include "Color.h" #include "Component.h" diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index e3f86bf..92b20b7 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -4,16 +4,21 @@ namespace crepe { +/** + * \brief Global configuration interface + * + * This class stores engine default settings. Properties on this class are only + * supposed to be modified *before* execution is handed over from the game + * programmer to the engine (i.e. the main loop is started). + */ class Config { -private: - Config() = default; - -public: - ~Config() = default; - public: //! Retrieve handle to global Config instance static Config & get_instance(); + +private: + Config() = default; + // singleton Config(const Config &) = delete; Config(Config &&) = delete; diff --git a/src/crepe/api/LoopManager.hpp b/src/crepe/api/LoopManager.hpp index 8fb9aa3..85a329b 100644 --- a/src/crepe/api/LoopManager.hpp +++ b/src/crepe/api/LoopManager.hpp @@ -2,8 +2,8 @@ #include <cassert> #include <memory> +#include <format> -#include "../Exception.h" #include "../system/System.h" #include "LoopManager.h" @@ -18,7 +18,7 @@ T & LoopManager::get_system() { const type_info & type = typeid(T); if (!this->systems.contains(type)) - throw Exception("LoopManager: %s is not initialized", type.name()); + throw runtime_error(format("LoopManager: {} is not initialized", type.name())); System * system = this->systems.at(type).get(); T * concrete_system = dynamic_cast<T *>(system); diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index 85687be..aff4bf7 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -1,7 +1,6 @@ #pragma once #include <chrono> -#include <cstdint> namespace crepe { diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index 2e20288..11544e5 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -1,7 +1,5 @@ #pragma once -#include <cstdint> - #include "../Component.h" #include "Vector2.h" diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h index 553194f..e854794 100644 --- a/src/crepe/api/SceneManager.h +++ b/src/crepe/api/SceneManager.h @@ -13,7 +13,6 @@ class ComponentManager; class SceneManager { public: SceneManager(ComponentManager & mgr); - virtual ~SceneManager() = default; public: /** @@ -38,8 +37,6 @@ public: private: std::vector<std::unique_ptr<Scene>> scenes; std::string next_scene; - -protected: ComponentManager & component_manager; }; diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp deleted file mode 100644 index 390cec7..0000000 --- a/src/crepe/api/Script.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "Script.h" - -using namespace crepe; diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index aceb38b..13b8077 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -1,7 +1,6 @@ #pragma once #include "../ComponentManager.h" -#include "../Exception.h" #include "BehaviorScript.h" #include "Script.h" @@ -10,11 +9,10 @@ namespace crepe { template <typename T> T & Script::get_component() const { - std::vector<std::reference_wrapper<T>> all_components - = this->get_components<T>(); + using namespace std; + vector<reference_wrapper<T>> all_components = this->get_components<T>(); if (all_components.size() < 1) - throw Exception("Script: no component found with type = {}", - typeid(T).name()); + throw runtime_error(format("Script: no component found with type = {}", typeid(T).name())); return all_components.back().get(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index deb3f93..754184b 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,6 +1,5 @@ #pragma once -#include <cstdint> #include <memory> #include "Color.h" diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 902dafa..7d6f785 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -28,9 +28,11 @@ protected: * \param rotation The rotation of the GameObject * \param scale The scale of the GameObject */ - Transform(game_object_id_t id, const Vector2 & point, double rotation = 0, - double scale = 0); - //! There is always exactly one transform component per entity + Transform(game_object_id_t id, const Vector2 & point, double rotation, double scale); + /** + * There is always exactly one transform component per entity + * \return 1 + */ virtual int get_instances_max() const { return 1; } //! ComponentManager instantiates all components friend class ComponentManager; diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp index 80047a6..322ecc0 100644 --- a/src/crepe/facade/DB.cpp +++ b/src/crepe/facade/DB.cpp @@ -1,6 +1,5 @@ #include <cstring> -#include "Exception.h" #include "util/Log.h" #include "DB.h" @@ -15,18 +14,18 @@ DB::DB(const string & path) { // init database struct libdb::DB * db; if ((ret = libdb::db_create(&db, NULL, 0)) != 0) - throw Exception("db_create: {}", libdb::db_strerror(ret)); + throw runtime_error(format("db_create: {}", libdb::db_strerror(ret))); this->db = {db, [](libdb::DB * db) { db->close(db, 0); }}; // load or create database file ret = this->db->open(this->db.get(), NULL, path.c_str(), NULL, libdb::DB_BTREE, DB_CREATE, 0); - if (ret != 0) throw Exception("db->open: {}", libdb::db_strerror(ret)); + if (ret != 0) throw runtime_error(format("db->open: {}", libdb::db_strerror(ret))); // create cursor libdb::DBC * cursor; ret = this->db->cursor(this->db.get(), NULL, &cursor, 0); - if (ret != 0) throw Exception("db->cursor: {}", libdb::db_strerror(ret)); + if (ret != 0) throw runtime_error(format("db->cursor: {}", libdb::db_strerror(ret))); this->cursor = {cursor, [](libdb::DBC * cursor) { cursor->close(cursor); }}; } @@ -44,21 +43,28 @@ string DB::get(const string & key) { memset(&db_val, 0, sizeof(libdb::DBT)); int ret = this->cursor->get(this->cursor.get(), &db_key, &db_val, DB_FIRST); - if (ret != 0) throw Exception("cursor->get: {}", libdb::db_strerror(ret)); - return {static_cast<char *>(db_val.data), db_val.size}; + if (ret == 0) + return {static_cast<char *>(db_val.data), db_val.size}; + + string err = format("cursor->get: {}", libdb::db_strerror(ret)); + if (ret == DB_NOTFOUND) + throw out_of_range(err); + else + throw runtime_error(err); } void DB::set(const string & key, const string & value) { libdb::DBT db_key = this->to_thing(key); libdb::DBT db_val = this->to_thing(value); int ret = this->db->put(this->db.get(), NULL, &db_key, &db_val, 0); - if (ret != 0) throw Exception("cursor->get: {}", libdb::db_strerror(ret)); + if (ret != 0) + throw runtime_error(format("cursor->get: {}", libdb::db_strerror(ret))); } -bool DB::has(const std::string & key) noexcept { +bool DB::has(const std::string & key) { try { this->get(key); - } catch (...) { + } catch (std::out_of_range &) { return false; } return true; diff --git a/src/crepe/facade/DB.h b/src/crepe/facade/DB.h index 7c757a2..2112216 100644 --- a/src/crepe/facade/DB.h +++ b/src/crepe/facade/DB.h @@ -34,7 +34,8 @@ public: * * \return The value * - * \throws Exception if value is not found in DB or other error occurs + * \throws std::out_of_range if value is not found in DB + * \throws std::runtime_error if other error occurs */ std::string get(const std::string & key); /** @@ -43,7 +44,7 @@ public: * \param key The value key * \param value The value to store * - * \throws Exception if an error occurs + * \throws std::runtime_error if an error occurs */ void set(const std::string & key, const std::string & value); /** @@ -53,7 +54,7 @@ public: * * \returns True if the key exists, or false if it does not */ - bool has(const std::string & key) noexcept; + bool has(const std::string & key); private: //! RAII wrapper around \c DB struct diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index a68d940..65ea962 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -10,17 +10,16 @@ #include <iostream> #include <memory> #include <string> -#include <utility> #include "../api/Sprite.h" #include "../api/Texture.h" #include "../api/Transform.h" #include "../util/Log.h" -#include "Exception.h" #include "SDLContext.h" using namespace crepe; +using namespace std; SDLContext & SDLContext::get_instance() { static SDLContext instance; @@ -171,7 +170,7 @@ SDLContext::texture_from_path(const std::string & path) { this->game_renderer.get(), img_surface.get()); if (tmp_texture == nullptr) { - throw Exception("Texture cannot be load from {}", path); + throw runtime_error(format("Texture cannot be load from {}", path)); } std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> diff --git a/src/crepe/util/Log.cpp b/src/crepe/util/Log.cpp index e583734..84d80a8 100644 --- a/src/crepe/util/Log.cpp +++ b/src/crepe/util/Log.cpp @@ -1,9 +1,8 @@ -#include <cstdarg> -#include <cstdio> -#include <cstdlib> +#include <iostream> #include <string> #include "../api/Config.h" + #include "Log.h" using namespace crepe; @@ -33,6 +32,6 @@ void Log::log(const Level & level, const string & msg) { if (!out.ends_with("\n")) out += "\n"; // TODO: also log to file or smth - fwrite(out.c_str(), 1, out.size(), stdout); - fflush(stdout); + cout.write(out.data(), out.size()); + cout.flush(); } diff --git a/src/crepe/util/Log.h b/src/crepe/util/Log.h index 01452b2..e0844ca 100644 --- a/src/crepe/util/Log.h +++ b/src/crepe/util/Log.h @@ -76,6 +76,8 @@ private: * \brief Output a message prefix depending on the log level * * \param level Message severity + * + * \return Colored message severity prefix string */ static std::string prefix(const Level & level); }; diff --git a/src/crepe/util/LogColor.cpp b/src/crepe/util/LogColor.cpp index ae44d72..170ddcf 100644 --- a/src/crepe/util/LogColor.cpp +++ b/src/crepe/util/LogColor.cpp @@ -1,6 +1,7 @@ #include <cstdarg> #include "../api/Config.h" + #include "LogColor.h" using namespace crepe; diff --git a/src/crepe/util/LogColor.h b/src/crepe/util/LogColor.h index 4b65127..132fb94 100644 --- a/src/crepe/util/LogColor.h +++ b/src/crepe/util/LogColor.h @@ -12,7 +12,13 @@ namespace crepe { */ class LogColor { public: - //! Get color code as stl string (or color content string) + /** + * \brief Get color code as STL string + * + * \param content If given, color this string and append a color reset escape sequence. + * + * \returns Color escape sequence + */ const std::string str(const std::string & content = "") const; public: @@ -20,6 +26,13 @@ public: LogColor & reset(); public: + /** + * \name Foreground colors + * + * These functions set the foreground (text) color. The \c bright parameter + * makes the color brighter, or bold on some terminals. + * \{ + */ LogColor & fg_black(bool bright = false); LogColor & fg_red(bool bright = false); LogColor & fg_green(bool bright = false); @@ -28,8 +41,16 @@ public: LogColor & fg_magenta(bool bright = false); LogColor & fg_cyan(bool bright = false); LogColor & fg_white(bool bright = false); + /// \} public: + /** + * \name Background colors + * + * These functions set the background color. The \c bright parameter makes + * the color brighter. + * \{ + */ LogColor & bg_black(bool bright = false); LogColor & bg_red(bool bright = false); LogColor & bg_green(bool bright = false); @@ -38,6 +59,7 @@ public: LogColor & bg_magenta(bool bright = false); LogColor & bg_cyan(bool bright = false); LogColor & bg_white(bool bright = false); + /// \} private: /** |