diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/crepe/ComponentManager.h | 2 | ||||
| -rw-r--r-- | src/crepe/ComponentManager.hpp | 74 | ||||
| -rw-r--r-- | src/crepe/Components.cpp | 11 | ||||
| -rw-r--r-- | src/crepe/Components.h | 12 | ||||
| -rw-r--r-- | src/crepe/GameObject.cpp | 4 | ||||
| -rw-r--r-- | src/crepe/GameObject.h | 12 | ||||
| -rw-r--r-- | src/crepe/GameObject.hpp | 6 | ||||
| -rw-r--r-- | src/crepe/api/AudioSource.cpp | 10 | ||||
| -rw-r--r-- | src/crepe/util/log.cpp | 10 | ||||
| -rw-r--r-- | src/crepe/util/log.h | 10 | 
11 files changed, 75 insertions, 79 deletions
| diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 62ca9a0..49d65a6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,10 +3,7 @@ cmake_minimum_required(VERSION 3.28)  set(CMAKE_C_STANDARD 11)  set(CMAKE_CXX_STANDARD 20)  set(CMAKE_EXPORT_COMPILE_COMMANDS 1) - -# enable debug features  set(CMAKE_BUILD_TYPE Debug) -add_compile_definitions(DEBUG)  add_subdirectory(../lib/soloud soloud)  add_subdirectory(../lib/googletest googletest) diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index b88ad27..cf6edad 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -58,5 +58,3 @@ private:  };  } // namespace crepe - -// #include "ComponentManager.hpp" diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 30b0e38..999cdcf 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -5,15 +5,16 @@  namespace crepe {  template <typename T, typename... Args> -void ComponentManager::add_component(std::uint32_t id, Args &&... args) { +void ComponentManager::add_component(uint32_t id, Args &&... args) { +	using namespace std; +  	// Determine the type of T (this is used as the key of the unordered_map<>) -	std::type_index type = typeid(T); +	type_index type = typeid(T);  	// Check if this component type is already in the unordered_map<>  	if (components.find(type) == components.end()) {  		//If not, create a new (empty) vector<> of vector<unique_ptr<Component>> -		components[type] -			= std::vector<std::vector<std::unique_ptr<Component>>>(); +		components[type] = vector<vector<unique_ptr<Component>>>();  	}  	// Resize the vector<> if the id is greater than the current size @@ -24,25 +25,26 @@ void ComponentManager::add_component(std::uint32_t id, Args &&... args) {  	// Create a new component of type T using perfect forwarding and store its  	// unique_ptr in the vector<> -	components[type][id].push_back( -		std::make_unique<T>(std::forward<Args>(args)...)); +	components[type][id].push_back(make_unique<T>(forward<Args>(args)...));  }  template <typename T> -void ComponentManager::delete_components_by_id(std::uint32_t id) { +void ComponentManager::delete_components_by_id(uint32_t id) { +	using namespace std; +  	// Determine the type of T (this is used as the key of the unordered_map<>) -	std::type_index type = typeid(T); +	type_index type = typeid(T);  	// Find the type (in the unordered_map<>)  	if (components.find(type) != components.end()) {  		// Get the correct vector<> -		std::vector<std::vector<std::unique_ptr<Component>>> & componentArray +		vector<vector<unique_ptr<Component>>> & component_array  			= components[type];  		// Make sure that the id (that we are looking for) is within the boundaries of the vector<> -		if (id < componentArray.size()) { +		if (id < component_array.size()) {  			// Clear the whole vector<> of this specific type and id -			componentArray[id].clear(); +			component_array[id].clear();  		}  	}  } @@ -61,72 +63,74 @@ void ComponentManager::delete_components() {  template <typename T>  std::vector<std::reference_wrapper<T>> -ComponentManager::get_components_by_id(std::uint32_t id) const { +ComponentManager::get_components_by_id(uint32_t id) const { +	using namespace std; +  	// Determine the type of T (this is used as the key of the unordered_map<>) -	std::type_index type = typeid(T); +	type_index type = typeid(T);  	// Create an empty vector<> -	std::vector<std::reference_wrapper<T>> componentVector; +	vector<reference_wrapper<T>> component_vector;  	// Find the type (in the unordered_map<>)  	if (components.find(type) != components.end()) {  		// Get the correct vector<> -		const std::vector<std::vector<std::unique_ptr<Component>>> & -			componentArray +		const vector<vector<unique_ptr<Component>>> & component_array  			= components.at(type);  		// Make sure that the id (that we are looking for) is within the boundaries of the vector<> -		if (id < componentArray.size()) { +		if (id < component_array.size()) {  			// Loop trough the whole vector<> -			for (const std::unique_ptr<Component> & componentPtr : -				 componentArray[id]) { +			for (const unique_ptr<Component> & component_ptr : +				 component_array[id]) {  				// Cast the unique_ptr to a raw pointer -				T * castedComponent = static_cast<T *>(componentPtr.get()); +				T * casted_component = static_cast<T *>(component_ptr.get());  				// Ensure that the cast was successful -				if (castedComponent) { +				if (casted_component) {  					// Add the dereferenced raw pointer to the vector<> -					componentVector.push_back(*castedComponent); +					component_vector.push_back(*casted_component);  				}  			}  		}  	}  	// Return the vector<> -	return componentVector; +	return component_vector;  }  template <typename T>  std::vector<std::reference_wrapper<T>>  ComponentManager::get_components_by_type() const { +	using namespace std; +  	// Determine the type of T (this is used as the key of the unordered_map<>) -	std::type_index type = typeid(T); +	type_index type = typeid(T);  	// Create an empty vector<> -	std::vector<std::reference_wrapper<T>> componentVector; +	vector<reference_wrapper<T>> component_vector;  	// Set the id to 0 (the id will also be stored in the returned vector<>) -	// std::uint32_t id = 0; +	// uint32_t id = 0;  	// Find the type (in the unordered_map<>)  	if (components.find(type) != components.end()) {  		// Get the correct vector<> -		const std::vector<std::vector<std::unique_ptr<Component>>> & -			componentArray +		const vector<vector<unique_ptr<Component>>> & component_array  			= components.at(type);  		// Loop through the whole vector<> -		for (const std::vector<std::unique_ptr<Component>> & component : -			 componentArray) { +		for (const vector<unique_ptr<Component>> & component : +			 component_array) {  			// Loop trough the whole vector<> -			for (const std::unique_ptr<Component> & componentPtr : component) { +			for (const unique_ptr<Component> & component_ptr : component) {  				// Cast the unique_ptr to a raw pointer -				T * castedComponent = static_cast<T *>(componentPtr.get()); +				T * casted_component = static_cast<T *>(component_ptr.get());  				// Ensure that the cast was successful -				if (castedComponent) { +				if (casted_component) {  					// Pair the dereferenced raw pointer and the id and add it to the vector<> -					componentVector.emplace_back(std::ref(*castedComponent)); +					component_vector.emplace_back(ref(*casted_component));  				}  			} @@ -136,7 +140,7 @@ ComponentManager::get_components_by_type() const {  	}  	// Return the vector<> -	return componentVector; +	return component_vector;  }  } // namespace crepe diff --git a/src/crepe/Components.cpp b/src/crepe/Components.cpp index 4a43692..f27c7a4 100644 --- a/src/crepe/Components.cpp +++ b/src/crepe/Components.cpp @@ -1,14 +1,13 @@  #include "Components.h" -#include <iostream>  using namespace crepe; +using namespace std; -Component::Component() : m_active(true) {} +Component::Component() : active(true) {} -Sprite::Sprite(std::string path) : m_path(path) {} +Sprite::Sprite(string path) : path(path) {}  Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) -	: m_mass(mass), m_gravity_scale(gravityScale), m_body_type(bodyType) {} - -Collider::Collider(int size) : m_size(size) {} +	: mass(mass), gravity_scale(gravityScale), body_type(bodyType) {} +Collider::Collider(int size) : size(size) {} diff --git a/src/crepe/Components.h b/src/crepe/Components.h index 1ad9ef2..139599c 100644 --- a/src/crepe/Components.h +++ b/src/crepe/Components.h @@ -8,7 +8,7 @@ class Component {  public:  	Component(); -	bool m_active; +	bool active;  };  // TODO: these should be in separate files @@ -17,23 +17,23 @@ class Sprite : public Component {  public:  	Sprite(std::string path); -	std::string m_path; +	std::string path;  };  class Rigidbody : public Component {  public:  	Rigidbody(int mass, int gravityScale, int bodyType); -	int m_mass; -	int m_gravity_scale; -	int m_body_type; +	int mass; +	int gravity_scale; +	int body_type;  };  class Collider : public Component {  public:  	Collider(int size); -	int m_size; +	int size;  };  } // namespace crepe diff --git a/src/crepe/GameObject.cpp b/src/crepe/GameObject.cpp index 16dc62f..304f75a 100644 --- a/src/crepe/GameObject.cpp +++ b/src/crepe/GameObject.cpp @@ -1,9 +1,7 @@  #include "GameObject.h" -#include "ComponentManager.h" -  using namespace crepe;  GameObject::GameObject(std::uint32_t id, std::string name, std::string tag,  					   int layer) -	: m_id(id), m_name(name), m_tag(tag), m_active(true), m_layer(layer) {} +	: id(id), name(name), tag(tag), active(true), layer(layer) {} diff --git a/src/crepe/GameObject.h b/src/crepe/GameObject.h index f0f5ea0..114990c 100644 --- a/src/crepe/GameObject.h +++ b/src/crepe/GameObject.h @@ -7,16 +7,16 @@ namespace crepe {  class GameObject {  public: -	GameObject(std::uint32_t id, std::string name, std::string tag, int layer); +	GameObject(uint32_t id, std::string name, std::string tag, int layer);  	template <typename T, typename... Args>  	void add_component(Args &&... args); -	std::uint32_t m_id; -	std::string m_name; -	std::string m_tag; -	bool m_active; -	int m_layer; +	uint32_t id; +	std::string name; +	std::string tag; +	bool active; +	int layer;  };  } // namespace crepe diff --git a/src/crepe/GameObject.hpp b/src/crepe/GameObject.hpp index 1152ddf..5966fbf 100644 --- a/src/crepe/GameObject.hpp +++ b/src/crepe/GameObject.hpp @@ -7,9 +7,9 @@  namespace crepe {  template <typename T, typename... Args> -void GameObject::AddComponent(Args &&... args) { -	ComponentManager::get_instance().AddComponent<T>( -		mId, std::forward<Args>(args)...); +void GameObject::add_component(Args &&... args) { +	auto & mgr = ComponentManager::get_instance(); +	mgr.add_component<T>(id, std::forward<Args>(args)...);  }  } // namespace crepe diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index 656fc46..b512d27 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -6,17 +6,17 @@  using namespace crepe::api;  AudioSource::AudioSource(std::unique_ptr<Asset> audio_clip) { -	this->_sound = std::make_unique<crepe::Sound>(std::move(audio_clip)); +	this->sound = std::make_unique<crepe::Sound>(std::move(audio_clip));  }  void AudioSource::play() { return this->play(false); }  void AudioSource::play(bool looping) { -	this->_sound->set_looping(looping); -	this->_sound->play(); +	this->sound->set_looping(looping); +	this->sound->play();  }  void AudioSource::stop() { -	this->_sound->pause(); -	this->_sound->rewind(); +	this->sound->pause(); +	this->sound->rewind();  } diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp index 6829ec3..f91d52c 100644 --- a/src/crepe/util/log.cpp +++ b/src/crepe/util/log.cpp @@ -8,10 +8,10 @@  using namespace crepe::util;  static const char * const LOG_PREFIX[] = { -	[log_level::debug] = "[DBG] ", -	[log_level::info] = "[INFO] ", -	[log_level::warning] = "[WARN] ", -	[log_level::error] = "[ERR] ", +	[log_level::DEBUG] = "[DBG] ", +	[log_level::INFO] = "[INFO] ", +	[log_level::WARNING] = "[WARN] ", +	[log_level::ERROR] = "[ERR] ",  };  static void va_logf(enum log_level level, va_list args, const std::string fmt) { @@ -38,7 +38,7 @@ static void va_logf(enum log_level level, va_list args, const std::string fmt) {  void crepe::util::logf(const char * fmt, ...) {  	va_list args;  	va_start(args, fmt); -	va_logf(crepe::util::log_level::debug, args, fmt); +	va_logf(crepe::util::log_level::DEBUG, args, fmt);  	va_end(args);  } diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index bfe7291..2b0fbe1 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -7,7 +7,7 @@  // utility macros  #define _crepe_logf_here(fmt, ...) \ -	crepe::util::logf(util::log_level::debug, "%s%s (%s:%d)" fmt "\n", \ +	crepe::util::logf(util::log_level::DEBUG, "%s%s (%s:%d)" fmt "\n", \  					  crepe::util::color::FG_WHITE, __PRETTY_FUNCTION__, \  					  __FILE_NAME__, __LINE__, crepe::util::color::RESET, \  					  __VA_ARGS__) @@ -24,10 +24,10 @@  namespace crepe::util {  enum log_level { -	debug, -	info, -	warning, -	error, +	DEBUG, +	INFO, +	WARNING, +	ERROR,  };  void logf(const char * fmt, ...); |