From 8819114b6380ed3c80641d439059db63de68c944 Mon Sep 17 00:00:00 2001 From: Jiga228 Date: Mon, 13 Oct 2025 19:31:27 +0700 Subject: [PATCH] Add GetWorld to Actor. Integrate google test. Add Model manager class. Rename *.h to *.hpp files --- CMakeLists.txt | 1 + Core/Core/CoreCallBacks.h | 6 - Core/Core/CoreInstance.cpp | 13 +- .../Core/{CoreInstance.h => CoreInstance.hpp} | 5 +- Core/Core/RenderEngine/ModelManager.cpp | 73 ++++++++++ Core/Core/RenderEngine/ModelManager.hpp | 47 +++++++ Core/Core/{ => RenderEngine}/RenderEngine.cpp | 10 +- .../RenderEngine.hpp} | 6 + Core/Core/{SystemCalls.h => SystemCalls.hpp} | 0 Core/Core/Windows/WindowsCalls.cpp | 2 +- Core/Core/main.cpp | 2 +- Core/Game/Actors/Actor.cpp | 17 ++- Core/Game/Actors/{Actor.h => Actor.hpp} | 14 +- Core/Game/Actors/Mesh/Mesh.cpp | 2 +- Core/Game/Actors/Mesh/Mesh.hpp | 2 +- Core/Game/BaseObjectFactory.cpp | 6 +- Core/Game/GameInstance.cpp | 10 +- .../Game/{GameInstance.h => GameInstance.hpp} | 1 + .../{ObjectFactory.h => ObjectFactory.hpp} | 0 .../Resource/{IResource.h => IResource.hpp} | 0 Core/Game/Resource/Resource.cpp | 2 +- .../Resource/{Resource.h => Resource.hpp} | 2 +- Core/Game/SaveMap/SaveMap.cpp | 4 +- Core/Game/SaveMap/{SaveMap.h => SaveMap.hpp} | 0 Core/Game/World/World.cpp | 7 +- Core/Game/World/{World.h => World.hpp} | 4 +- .../Game/{WorldFactory.h => WorldFactory.hpp} | 2 +- Core/Log/Log.cpp | 2 +- Core/Log/{Log.h => Log.hpp} | 0 Core/Math/Vector.cpp | 4 +- Core/Math/{Vector.h => Vector.hpp} | 0 Core/Types/UString.cpp | 131 ++++++++++++++++++ Core/Types/UString.hpp | 32 +++++ FastRTTI/RTTI_Meta.h | 2 +- TestGame/src/Actors/TestActor.cpp | 2 +- TestGame/src/Actors/TestActor.h | 2 +- TestGame/src/Factories/ObjectFactory.cpp | 2 +- TestGame/src/Factories/WorldFactory.cpp | 2 +- TestGame/src/TestGameInstance.cpp | 6 +- TestGame/src/TestGameInstance.h | 2 +- TestGame/src/Worlds/TestWorld.cpp | 6 +- TestGame/src/Worlds/TestWorld.h | 3 +- Tests/CMakeLists.txt | 24 ++++ Tests/UStringTest.cpp | 25 ++++ 44 files changed, 424 insertions(+), 59 deletions(-) delete mode 100644 Core/Core/CoreCallBacks.h rename Core/Core/{CoreInstance.h => CoreInstance.hpp} (86%) create mode 100644 Core/Core/RenderEngine/ModelManager.cpp create mode 100644 Core/Core/RenderEngine/ModelManager.hpp rename Core/Core/{ => RenderEngine}/RenderEngine.cpp (99%) rename Core/Core/{RenderEngine.h => RenderEngine/RenderEngine.hpp} (97%) rename Core/Core/{SystemCalls.h => SystemCalls.hpp} (100%) rename Core/Game/Actors/{Actor.h => Actor.hpp} (87%) rename Core/Game/{GameInstance.h => GameInstance.hpp} (92%) rename Core/Game/{ObjectFactory.h => ObjectFactory.hpp} (100%) rename Core/Game/Resource/{IResource.h => IResource.hpp} (100%) rename Core/Game/Resource/{Resource.h => Resource.hpp} (98%) rename Core/Game/SaveMap/{SaveMap.h => SaveMap.hpp} (100%) rename Core/Game/World/{World.h => World.hpp} (94%) rename Core/Game/{WorldFactory.h => WorldFactory.hpp} (96%) rename Core/Log/{Log.h => Log.hpp} (100%) rename Core/Math/{Vector.h => Vector.hpp} (100%) create mode 100644 Core/Types/UString.cpp create mode 100644 Core/Types/UString.hpp create mode 100644 Tests/CMakeLists.txt create mode 100644 Tests/UStringTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 93282a8..9bd95eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,3 +21,4 @@ add_subdirectory(glfw) add_subdirectory(Core) add_subdirectory(TestGame) add_subdirectory(ProjectGenerator) +add_subdirectory(Tests) diff --git a/Core/Core/CoreCallBacks.h b/Core/Core/CoreCallBacks.h deleted file mode 100644 index 9a0ecfd..0000000 --- a/Core/Core/CoreCallBacks.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -struct CoreCallBacks -{ - void (*Quit)(); -}; \ No newline at end of file diff --git a/Core/Core/CoreInstance.cpp b/Core/Core/CoreInstance.cpp index cbe0ceb..7d2e47b 100644 --- a/Core/Core/CoreInstance.cpp +++ b/Core/Core/CoreInstance.cpp @@ -1,6 +1,6 @@ -#include "CoreInstance.h" +#include "CoreInstance.hpp" -#include "SystemCalls.h" +#include "SystemCalls.hpp" #include #include @@ -8,10 +8,9 @@ #include #include -#include "RenderEngine.h" -#include "Game/GameInstance.h" -#include "Log/Log.h" -#include "Game/SaveMap/SaveMap.h" +#include "RenderEngine/RenderEngine.hpp" +#include "Game/GameInstance.hpp" +#include "Game/SaveMap/SaveMap.hpp" extern GameInstance* GameFactory(CoreInstance&); @@ -78,8 +77,8 @@ CoreInstance::~CoreInstance() { modules_.clear(); - delete render_engine_; delete game_; + delete render_engine_; glfwDestroyWindow(window_); glfwTerminate(); diff --git a/Core/Core/CoreInstance.h b/Core/Core/CoreInstance.hpp similarity index 86% rename from Core/Core/CoreInstance.h rename to Core/Core/CoreInstance.hpp index 8e13676..260e0bd 100644 --- a/Core/Core/CoreInstance.h +++ b/Core/Core/CoreInstance.hpp @@ -6,13 +6,15 @@ #include #include "Game/SaveMap/ISave.h" -#include "Core/CoreCallBacks.h" class SaveMap; class RenderEngine; class GameInstance; struct GLFWwindow; +/* + * Внимательно следите, что бы у ваших объектов была "мягкая" зависимость + */ class CoreInstance { struct Module { const char* name; @@ -58,4 +60,5 @@ public: const std::string& getBaseWorldName() const { return main_config_.base_world; } const std::string& getGameName() const { return main_config_.game_name; } GameInstance* GetGameInstance() const { return game_; } + RenderEngine* GetRenderEngine() const { return render_engine_; } }; diff --git a/Core/Core/RenderEngine/ModelManager.cpp b/Core/Core/RenderEngine/ModelManager.cpp new file mode 100644 index 0000000..0bb347e --- /dev/null +++ b/Core/Core/RenderEngine/ModelManager.cpp @@ -0,0 +1,73 @@ +#include "ModelManager.hpp" + +ModelManager::StaticModel::StaticModel(const std::vector& voxels, std::string name): +name_(std::move(name)), +voxels_(voxels) +{ + glm::ivec3 sum_moments{0, 0, 0}; + int sum_masses = 0; + + for (auto& i : voxels_) + { + sum_moments += glm::ivec3{i.loc.x * i.mass, i.loc.y * i.mass, i.loc.z * i.mass}; + sum_masses += i.mass; + } + + if(sum_masses != 0) + sum_moments /= sum_masses; + else if (!voxels_.empty()) + { + sum_moments.x /= static_cast(voxels_.size()); + sum_moments.y /= static_cast(voxels_.size()); + sum_moments.z /= static_cast(voxels_.size()); + } + + mass_center_ = sum_moments; +} + +ModelManager::StaticModel::~StaticModel() +{ + OnDestroy.Call(name_); +} + +unsigned int ModelManager::FNV1aHash(const char* buf) +{ + unsigned int h_val = 0x811c9dc5; + + while (*buf) + { + h_val ^= static_cast(*buf++); + h_val *= 0x01000193; + } + + return h_val; +} + +void ModelManager::OnDestroySometimeModelCaller(const std::string& name) +{ + OnDestroySometimeModel.Call(name); +} + +ModelManager::~ModelManager() +{ + for (const auto& [it, model] : models) + { + model->OnDestroy.unbind(this, &ModelManager::OnDestroySometimeModelCaller); + } + models.clear(); +} + +std::shared_ptr ModelManager::LoadModel(const std::string& name) +{ + unsigned int hash = FNV1aHash(name.c_str()); + auto model = models.find(hash); + if (model != models.cend()) + return model->second; + + std::vector model_data; + // Load model_data + auto newModel = std::make_shared(model_data, name); + newModel->OnDestroy.bind(this, &ModelManager::OnDestroySometimeModelCaller); + models[hash] = newModel; + return models[hash]; +} diff --git a/Core/Core/RenderEngine/ModelManager.hpp b/Core/Core/RenderEngine/ModelManager.hpp new file mode 100644 index 0000000..3779329 --- /dev/null +++ b/Core/Core/RenderEngine/ModelManager.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include +#include +#include +#include +#include + +class ModelManager +{ +public: + struct Voxel + { + glm::ivec3 loc, color; + int mass; + }; + class StaticModel + { + std::string name_; + std::vector voxels_; + glm::vec3 mass_center_; + public: + Delegate OnDestroy; + + StaticModel(const std::vector& voxels, std::string name); + ~StaticModel(); + + const std::vector& GetVoxels() const { return voxels_; } + glm::vec3 GetMassCenter() const { return mass_center_; } + const std::string& GetName() const { return name_; } + }; + +private: + + std::unordered_map> models; + + static unsigned int FNV1aHash (const char *buf); + + void OnDestroySometimeModelCaller(const std::string& name); + +public: + Delegate OnDestroySometimeModel; + + ~ModelManager(); + + std::shared_ptr LoadModel(const std::string& name); +}; \ No newline at end of file diff --git a/Core/Core/RenderEngine.cpp b/Core/Core/RenderEngine/RenderEngine.cpp similarity index 99% rename from Core/Core/RenderEngine.cpp rename to Core/Core/RenderEngine/RenderEngine.cpp index 753a3e1..2d1683d 100644 --- a/Core/Core/RenderEngine.cpp +++ b/Core/Core/RenderEngine/RenderEngine.cpp @@ -1,15 +1,15 @@ -#include "RenderEngine.h" +#include "RenderEngine.hpp" #include #include #include #include -#include "CoreInstance.h" -#include "Log/Log.h" +#include "Core/CoreInstance.hpp" +#include "Log/Log.hpp" -#include "Game/GameInstance.h" -#include "Game/World/World.h" +#include "Game/GameInstance.hpp" +#include "Game/World/World.hpp" #include "Game/Actors/Mesh/Mesh.hpp" #include "GLFW/glfw3.h" diff --git a/Core/Core/RenderEngine.h b/Core/Core/RenderEngine/RenderEngine.hpp similarity index 97% rename from Core/Core/RenderEngine.h rename to Core/Core/RenderEngine/RenderEngine.hpp index c7d5f32..4a1ae17 100644 --- a/Core/Core/RenderEngine.h +++ b/Core/Core/RenderEngine/RenderEngine.hpp @@ -11,6 +11,8 @@ #include #include +#include "ModelManager.hpp" + #ifdef _DEBUG #include #define VK_CHECK(res) assert(res == VK_SUCCESS) @@ -55,6 +57,8 @@ class RenderEngine void copy_memory(VkBuffer src, VkBuffer dst, VkDeviceSize size); #pragma endregion + ModelManager model_manager_; + static const std::vector deviceExtensions; int current_frame_ = 0; @@ -128,4 +132,6 @@ public: void stop_render() const; GLFWwindow* get_window() const { return window_; } + + ModelManager* GetActiveModelManager() { return &model_manager_; } }; \ No newline at end of file diff --git a/Core/Core/SystemCalls.h b/Core/Core/SystemCalls.hpp similarity index 100% rename from Core/Core/SystemCalls.h rename to Core/Core/SystemCalls.hpp diff --git a/Core/Core/Windows/WindowsCalls.cpp b/Core/Core/Windows/WindowsCalls.cpp index e735662..81824f4 100644 --- a/Core/Core/Windows/WindowsCalls.cpp +++ b/Core/Core/Windows/WindowsCalls.cpp @@ -1,6 +1,6 @@ #ifdef _WIN32 -#include "../SystemCalls.h" +#include "../SystemCalls.hpp" #include diff --git a/Core/Core/main.cpp b/Core/Core/main.cpp index 3ac734d..be5bc25 100644 --- a/Core/Core/main.cpp +++ b/Core/Core/main.cpp @@ -1,6 +1,6 @@ #include -#include "CoreInstance.h" +#include "CoreInstance.hpp" int main(int argc, char* argv[]) { try { diff --git a/Core/Game/Actors/Actor.cpp b/Core/Game/Actors/Actor.cpp index 4781b32..dce154a 100644 --- a/Core/Game/Actors/Actor.cpp +++ b/Core/Game/Actors/Actor.cpp @@ -1,7 +1,12 @@ -#include "Actor.h" +#include "Actor.hpp" -#include "Game/SaveMap/SaveMap.h" -#include "Log/Log.h" +#include "Game/SaveMap/SaveMap.hpp" +#include "Game/World/World.hpp" +#include "Log/Log.hpp" + +void Actor::OnDestroy() +{ +} Actor::Actor() { @@ -55,3 +60,9 @@ void Actor::RemoveTag(const std::string& tag) noexcept { tags.remove(tag); } + +void Actor::Destroy() +{ + OnDestroy(); + //world_->actors.remove(this); +} diff --git a/Core/Game/Actors/Actor.h b/Core/Game/Actors/Actor.hpp similarity index 87% rename from Core/Game/Actors/Actor.h rename to Core/Game/Actors/Actor.hpp index 0ab89e1..5901a48 100644 --- a/Core/Game/Actors/Actor.h +++ b/Core/Game/Actors/Actor.hpp @@ -6,16 +6,23 @@ #include "RTTI_Meta.h" #include "Game/SaveMap/ISave.h" #include "Delegate/Delegate.h" -#include "Math/Vector.h" +#include "Math/Vector.hpp" +class World; GENERATE_META(Actor) + class Actor : public ISave, public IRTTI { + World* world_; + Vector3D loc; Vector3D rot; Vector3D scale; std::list tags; + +protected: + virtual void OnDestroy(); public: Actor(); @@ -47,4 +54,9 @@ public: void AddTag(const std::string& tag) noexcept; void RemoveTag(const std::string& tag) noexcept; const std::list& GetTags() const { return tags; } + void Destroy(); + + World* GetWorld() const { return world_; } + + friend class World; }; diff --git a/Core/Game/Actors/Mesh/Mesh.cpp b/Core/Game/Actors/Mesh/Mesh.cpp index 4e007d9..52c0f14 100644 --- a/Core/Game/Actors/Mesh/Mesh.cpp +++ b/Core/Game/Actors/Mesh/Mesh.cpp @@ -1,6 +1,6 @@ #include "Mesh.hpp" -#include "Game/SaveMap/SaveMap.h" +#include "Game/SaveMap/SaveMap.hpp" Mesh::Mesh() { diff --git a/Core/Game/Actors/Mesh/Mesh.hpp b/Core/Game/Actors/Mesh/Mesh.hpp index ffe9dac..7c0e65a 100644 --- a/Core/Game/Actors/Mesh/Mesh.hpp +++ b/Core/Game/Actors/Mesh/Mesh.hpp @@ -2,7 +2,7 @@ #include -#include "../Actor.h" +#include "../Actor.hpp" GENERATE_META(Mesh) class Mesh : public Actor diff --git a/Core/Game/BaseObjectFactory.cpp b/Core/Game/BaseObjectFactory.cpp index 1991b16..1b5418c 100644 --- a/Core/Game/BaseObjectFactory.cpp +++ b/Core/Game/BaseObjectFactory.cpp @@ -1,8 +1,8 @@ -#include "ObjectFactory.h" +#include "ObjectFactory.hpp" #include "Actors/Mesh/StaticMesh.hpp" -#include "Game/Actors/Actor.h" -#include "Math/Vector.h" +#include "Game/Actors/Actor.hpp" +#include "Math/Vector.hpp" std::vector base_object_factories = { GENERATE_FACTORY_OBJECT(Actor) diff --git a/Core/Game/GameInstance.cpp b/Core/Game/GameInstance.cpp index 9a61a4b..abb70be 100644 --- a/Core/Game/GameInstance.cpp +++ b/Core/Game/GameInstance.cpp @@ -1,14 +1,14 @@ -#include "GameInstance.h" +#include "GameInstance.hpp" #include #include #include #include -#include "Core/CoreInstance.h" -#include "Game/WorldFactory.h" -#include "SaveMap/SaveMap.h" -#include "Game/World/World.h" +#include "Core/CoreInstance.hpp" +#include "Game/WorldFactory.hpp" +#include "SaveMap/SaveMap.hpp" +#include "Game/World/World.hpp" extern std::vector world_factories; diff --git a/Core/Game/GameInstance.h b/Core/Game/GameInstance.hpp similarity index 92% rename from Core/Game/GameInstance.h rename to Core/Game/GameInstance.hpp index ac26b8c..8357518 100644 --- a/Core/Game/GameInstance.h +++ b/Core/Game/GameInstance.hpp @@ -28,6 +28,7 @@ public: virtual ~GameInstance(); World* GetWorld() const { return world; } + CoreInstance& GetCore() const { return core; } friend class CoreInstance; }; diff --git a/Core/Game/ObjectFactory.h b/Core/Game/ObjectFactory.hpp similarity index 100% rename from Core/Game/ObjectFactory.h rename to Core/Game/ObjectFactory.hpp diff --git a/Core/Game/Resource/IResource.h b/Core/Game/Resource/IResource.hpp similarity index 100% rename from Core/Game/Resource/IResource.h rename to Core/Game/Resource/IResource.hpp diff --git a/Core/Game/Resource/Resource.cpp b/Core/Game/Resource/Resource.cpp index e96bbb6..479db4b 100644 --- a/Core/Game/Resource/Resource.cpp +++ b/Core/Game/Resource/Resource.cpp @@ -1,4 +1,4 @@ -#include "Resource.h" +#include "Resource.hpp" #include #include diff --git a/Core/Game/Resource/Resource.h b/Core/Game/Resource/Resource.hpp similarity index 98% rename from Core/Game/Resource/Resource.h rename to Core/Game/Resource/Resource.hpp index 44d08e9..e719af1 100644 --- a/Core/Game/Resource/Resource.h +++ b/Core/Game/Resource/Resource.hpp @@ -1,6 +1,6 @@ #pragma once -#include "IResource.h" +#include "IResource.hpp" #include #include #include diff --git a/Core/Game/SaveMap/SaveMap.cpp b/Core/Game/SaveMap/SaveMap.cpp index 8b933cb..426baf8 100644 --- a/Core/Game/SaveMap/SaveMap.cpp +++ b/Core/Game/SaveMap/SaveMap.cpp @@ -1,8 +1,8 @@ -#include "SaveMap.h" +#include "SaveMap.hpp" #include #include -#include "Game/ObjectFactory.h" +#include "Game/ObjectFactory.hpp" extern std::vector factories; extern std::vector base_object_factories; diff --git a/Core/Game/SaveMap/SaveMap.h b/Core/Game/SaveMap/SaveMap.hpp similarity index 100% rename from Core/Game/SaveMap/SaveMap.h rename to Core/Game/SaveMap/SaveMap.hpp diff --git a/Core/Game/World/World.cpp b/Core/Game/World/World.cpp index 591b539..b14731b 100644 --- a/Core/Game/World/World.cpp +++ b/Core/Game/World/World.cpp @@ -1,6 +1,6 @@ -#include "World.h" +#include "World.hpp" -#include "Game/SaveMap/SaveMap.h" +#include "Game/SaveMap/SaveMap.hpp" World::World(GameInstance& game_instance) : game_instance(game_instance) { @@ -35,4 +35,7 @@ std::shared_ptr World::save() void World::load(std::shared_ptr save) { actors = std::move(reinterpret_cast&>(save->GetListObject("Actors"))); + + for (auto& i : actors) + i->world_ = this; } \ No newline at end of file diff --git a/Core/Game/World/World.h b/Core/Game/World/World.hpp similarity index 94% rename from Core/Game/World/World.h rename to Core/Game/World/World.hpp index 87ea13e..df899e8 100644 --- a/Core/Game/World/World.h +++ b/Core/Game/World/World.hpp @@ -5,11 +5,12 @@ #include #include "RTTI.h" -#include "Game/Actors/Actor.h" +#include "Game/Actors/Actor.hpp" #include "Game/Actors/Mesh/Mesh.hpp" class GameInstance; +GENERATE_META(World); class World : public ISave { GameInstance& game_instance; @@ -49,6 +50,7 @@ public: T* object = new T(); object->SetActorLocate(loc); object->SetActorRotate(rot); + static_cast(object)->world_ = this; actors.push_back(object); return object; } diff --git a/Core/Game/WorldFactory.h b/Core/Game/WorldFactory.hpp similarity index 96% rename from Core/Game/WorldFactory.h rename to Core/Game/WorldFactory.hpp index 29e7dbb..5e8a006 100644 --- a/Core/Game/WorldFactory.h +++ b/Core/Game/WorldFactory.hpp @@ -3,7 +3,7 @@ #include #include #include -#include "Game/SaveMap/SaveMap.h" +#include "Game/SaveMap/SaveMap.hpp" class GameInstance; class World; diff --git a/Core/Log/Log.cpp b/Core/Log/Log.cpp index 2b0c4e7..8bae583 100644 --- a/Core/Log/Log.cpp +++ b/Core/Log/Log.cpp @@ -1,4 +1,4 @@ -#include "Log.h" +#include "Log.hpp" #include #include diff --git a/Core/Log/Log.h b/Core/Log/Log.hpp similarity index 100% rename from Core/Log/Log.h rename to Core/Log/Log.hpp diff --git a/Core/Math/Vector.cpp b/Core/Math/Vector.cpp index bac260a..a86d94b 100644 --- a/Core/Math/Vector.cpp +++ b/Core/Math/Vector.cpp @@ -1,6 +1,6 @@ -#include "Vector.h" +#include "Vector.hpp" -#include "Game/SaveMap/SaveMap.h" +#include "Game/SaveMap/SaveMap.hpp" Vector2D::Vector2D(const double x, const double y) : x(x), y(y) {} diff --git a/Core/Math/Vector.h b/Core/Math/Vector.hpp similarity index 100% rename from Core/Math/Vector.h rename to Core/Math/Vector.hpp diff --git a/Core/Types/UString.cpp b/Core/Types/UString.cpp new file mode 100644 index 0000000..16745cc --- /dev/null +++ b/Core/Types/UString.cpp @@ -0,0 +1,131 @@ +#include "UString.hpp" + +#include + +unsigned int UString::FNV1aHash(const char* buf) +{ + unsigned int h_val = 0x811c9dc5; + + while (*buf) + { + h_val ^= static_cast(*buf++); + h_val *= 0x01000193; + } + + return h_val; +} + +UString::UString(const char* str) +{ + if (str == nullptr) + return; + size_ = strlen(str); + + str_ = new char[size_ + 1]; + for (size_t i = 0; i < size_; ++i) + str_[i] = str[i]; + str_[size_] = '\0'; + + hash_ = FNV1aHash(str); +} + +UString::UString(const UString& str) : size_(str.size_) +{ + if (size_ != 0 && str.str_ != nullptr) + { + str_ = new char[str.size_ + 1]; + strcpy_s(str_, size_ + 1, str.str_); + } + hash_ = str.hash_; +} + +UString::UString(UString&& str) noexcept +{ + is_moved_.store(true); + str_ = str.str_; + size_ = str.size_; + hash_ = str.hash_; +} + +UString::~UString() +{ + if (is_moved_.load() == false) + delete[] str_; +} + +UString& UString::operator=(const UString& str) +{ + if (&str != this) + { + if (size_ != 0 && str.str_ != nullptr) + { + str_ = new char[str.size_]; + strcpy_s(str_, size_, str.str_); + } + } + is_moved_.store(false); + hash_ = FNV1aHash(str_); + return *this; +} + +UString& UString::operator=(UString&& str) noexcept +{ + str.is_moved_.store(true); + str_ = str.str_; + size_ = str.size_; + is_moved_.store(false); + hash_ = FNV1aHash(str_); + return *this; +} + +void UString::operator+=(const UString& str) +{ + char* new_str = new char[size_ + str.size_ + 1]; + strcpy_s(new_str, size_ + str.size_ + 1, str_); + strcpy_s(new_str + size_, str.size_ + 1, str.str_); + + if (is_moved_.load() == false) + delete[] str_; + str_ = new_str; + size_ += str.size_; + + is_moved_.store(false); + hash_ = FNV1aHash(str_); +} + +void UString::operator+=(const char* str) +{ + size_t str_size = strlen(str); + char* new_str = new char[size_ + str_size + 1]; + strcpy_s(new_str, size_ + str_size + 1, str_); + strcpy_s(new_str + size_, str_size + 1, str); + + if (is_moved_.load() == false) + delete[] str_; + str_ = new_str; + size_ += str_size; + + is_moved_.store(false); + hash_ = FNV1aHash(str_); +} + +bool UString::operator==(const UString& str) const +{ + return hash_ == str.hash_; +} + +bool UString::operator==(const char* str) const +{ + unsigned int hash = FNV1aHash(str); + return hash_ == hash; +} + +bool UString::operator!=(const UString& str) const +{ + return !(*this == str); +} + +bool UString::operator!=(const char* str) const +{ + return !(*this == str); +} diff --git a/Core/Types/UString.hpp b/Core/Types/UString.hpp new file mode 100644 index 0000000..8c863ae --- /dev/null +++ b/Core/Types/UString.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include + +class UString +{ + unsigned int hash_ = 0; + std::atomic_bool is_moved_ = false; + char* str_ = nullptr; + size_t size_ = 0; + + static unsigned int FNV1aHash (const char *buf); +public: + UString(const char* str); + UString(const UString& str); + UString(UString&& str) noexcept; + ~UString(); + + UString& operator=(const UString& str); + UString& operator=(UString&& str) noexcept; + void operator+=(const UString& str); + void operator+=(const char* str); + bool operator==(const UString& str) const; + bool operator==(const char* str) const; + bool operator!=(const UString& str) const; + bool operator!=(const char* str) const; + + size_t length() const { return size_; } + const char* c_str() const { return str_; } + std::string std_str() const { return std::string(str_, size_); } +}; diff --git a/FastRTTI/RTTI_Meta.h b/FastRTTI/RTTI_Meta.h index ac33feb..d348cee 100644 --- a/FastRTTI/RTTI_Meta.h +++ b/FastRTTI/RTTI_Meta.h @@ -13,5 +13,5 @@ template struct Meta; // Enum type classes enum class Classes { - IRTTI, IResource, Actor, TestActor, Mesh, StaticMesh + IRTTI, IResource, Actor, TestActor, Mesh, StaticMesh, World, TestWorld }; diff --git a/TestGame/src/Actors/TestActor.cpp b/TestGame/src/Actors/TestActor.cpp index da903e9..1d398f1 100644 --- a/TestGame/src/Actors/TestActor.cpp +++ b/TestGame/src/Actors/TestActor.cpp @@ -1,6 +1,6 @@ #include "TestActor.h" -#include "Log/Log.h" +#include "Log/Log.hpp" void TestActor::BeginPlay() { diff --git a/TestGame/src/Actors/TestActor.h b/TestGame/src/Actors/TestActor.h index ebecad9..b27b1b0 100644 --- a/TestGame/src/Actors/TestActor.h +++ b/TestGame/src/Actors/TestActor.h @@ -1,6 +1,6 @@ #pragma once -#include "Game/Actors/Actor.h" +#include "Game/Actors/Actor.hpp" GENERATE_META(TestActor) class TestActor final : public Actor diff --git a/TestGame/src/Factories/ObjectFactory.cpp b/TestGame/src/Factories/ObjectFactory.cpp index 4690e4b..0c8baf5 100644 --- a/TestGame/src/Factories/ObjectFactory.cpp +++ b/TestGame/src/Factories/ObjectFactory.cpp @@ -1,4 +1,4 @@ -#include "Game/ObjectFactory.h" +#include "Game/ObjectFactory.hpp" #include "../Actors/TestActor.h" diff --git a/TestGame/src/Factories/WorldFactory.cpp b/TestGame/src/Factories/WorldFactory.cpp index 74185dd..b44b49d 100644 --- a/TestGame/src/Factories/WorldFactory.cpp +++ b/TestGame/src/Factories/WorldFactory.cpp @@ -1,4 +1,4 @@ -#include "Game/WorldFactory.h" +#include "Game/WorldFactory.hpp" #include "../Worlds/TestWorld.h" diff --git a/TestGame/src/TestGameInstance.cpp b/TestGame/src/TestGameInstance.cpp index fe4677e..e062916 100644 --- a/TestGame/src/TestGameInstance.cpp +++ b/TestGame/src/TestGameInstance.cpp @@ -1,10 +1,10 @@ #include "TestGameInstance.h" -#include "Game/Resource/Resource.h" +#include "Game/Resource/Resource.hpp" #include -#include "Game/SaveMap/SaveMap.h" -#include "Game/World/World.h" +#include "Game/SaveMap/SaveMap.hpp" +#include "Game/World/World.hpp" GENERATE_FACTORY_GAME_INSTANCE(TestGameInstance) TestGameInstance::TestGameInstance(CoreInstance& core) : GameInstance(core) diff --git a/TestGame/src/TestGameInstance.h b/TestGame/src/TestGameInstance.h index 15f50f6..e68e9f5 100644 --- a/TestGame/src/TestGameInstance.h +++ b/TestGame/src/TestGameInstance.h @@ -1,6 +1,6 @@ #pragma once -#include "Game/GameInstance.h" +#include "Game/GameInstance.hpp" class TestGameInstance : public GameInstance { diff --git a/TestGame/src/Worlds/TestWorld.cpp b/TestGame/src/Worlds/TestWorld.cpp index b40064b..9b8731f 100644 --- a/TestGame/src/Worlds/TestWorld.cpp +++ b/TestGame/src/Worlds/TestWorld.cpp @@ -1,8 +1,8 @@ #include "TestWorld.h" -#include "Game/GameInstance.h" -#include "Game/SaveMap/SaveMap.h" -#include "Log/Log.h" +#include "Game/GameInstance.hpp" +#include "Game/SaveMap/SaveMap.hpp" +#include "Log/Log.hpp" TestWorld::TestWorld(GameInstance& game_instance) : World(game_instance) {} diff --git a/TestGame/src/Worlds/TestWorld.h b/TestGame/src/Worlds/TestWorld.h index 1e6081b..a54a1c1 100644 --- a/TestGame/src/Worlds/TestWorld.h +++ b/TestGame/src/Worlds/TestWorld.h @@ -1,7 +1,8 @@ #pragma once -#include "Game/World/World.h" +#include "Game/World/World.hpp" +GENERATE_META(TestWorld) class TestWorld final : public World { double time = 0; diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt new file mode 100644 index 0000000..025c18c --- /dev/null +++ b/Tests/CMakeLists.txt @@ -0,0 +1,24 @@ +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/refs/tags/v1.15.0.zip + DOWNLOAD_EXTRACT_TIMESTAMP true +) +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +enable_testing() + +file(GLOB SRC "UStringTest.cpp" "${PROJECT_SOURCE_DIR}/Core/Types/UString.cpp") + +add_executable(Tests ${SRC}) +target_link_libraries(Tests PRIVATE + GTest::gtest_main +) +target_include_directories(Tests PRIVATE + ${PROJECT_SOURCE_DIR}/Core +) + + +include(GoogleTest) +gtest_discover_tests(Tests) \ No newline at end of file diff --git a/Tests/UStringTest.cpp b/Tests/UStringTest.cpp new file mode 100644 index 0000000..19de15d --- /dev/null +++ b/Tests/UStringTest.cpp @@ -0,0 +1,25 @@ +#include +#include "Types/UString.hpp" +#include + +TEST(UStringTest, creae_string) +{ + UString str("Hello, World!"); + EXPECT_EQ(str, "Hello, World!"); + EXPECT_NE(str, "Hello, UwU!"); + EXPECT_EQ(str.length(), 13); +} + +TEST(UStringTest, concatenate_string) +{ + UString str1("Hello, "); + UString str2("World!"); + str1 += str2; + EXPECT_EQ(str1, "Hello, World!"); + EXPECT_EQ(str1.length(), 13); + + UString str3("Hello, "); + str3 += "UwU!"; + EXPECT_EQ(str3, "Hello, UwU!"); + EXPECT_EQ(str3.length(), 11); +} \ No newline at end of file