Add GetWorld to Actor. Integrate google test. Add Model manager class. Rename *.h to *.hpp files

This commit is contained in:
Jiga228
2025-10-13 19:31:27 +07:00
parent fedffa7634
commit 8819114b63
44 changed files with 424 additions and 59 deletions
+1
View File
@@ -21,3 +21,4 @@ add_subdirectory(glfw)
add_subdirectory(Core) add_subdirectory(Core)
add_subdirectory(TestGame) add_subdirectory(TestGame)
add_subdirectory(ProjectGenerator) add_subdirectory(ProjectGenerator)
add_subdirectory(Tests)
-6
View File
@@ -1,6 +0,0 @@
#pragma once
struct CoreCallBacks
{
void (*Quit)();
};
+6 -7
View File
@@ -1,6 +1,6 @@
#include "CoreInstance.h" #include "CoreInstance.hpp"
#include "SystemCalls.h" #include "SystemCalls.hpp"
#include <exception> #include <exception>
#include <cstring> #include <cstring>
@@ -8,10 +8,9 @@
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include "RenderEngine.h" #include "RenderEngine/RenderEngine.hpp"
#include "Game/GameInstance.h" #include "Game/GameInstance.hpp"
#include "Log/Log.h" #include "Game/SaveMap/SaveMap.hpp"
#include "Game/SaveMap/SaveMap.h"
extern GameInstance* GameFactory(CoreInstance&); extern GameInstance* GameFactory(CoreInstance&);
@@ -78,8 +77,8 @@ CoreInstance::~CoreInstance()
{ {
modules_.clear(); modules_.clear();
delete render_engine_;
delete game_; delete game_;
delete render_engine_;
glfwDestroyWindow(window_); glfwDestroyWindow(window_);
glfwTerminate(); glfwTerminate();
@@ -6,13 +6,15 @@
#include <vector> #include <vector>
#include "Game/SaveMap/ISave.h" #include "Game/SaveMap/ISave.h"
#include "Core/CoreCallBacks.h"
class SaveMap; class SaveMap;
class RenderEngine; class RenderEngine;
class GameInstance; class GameInstance;
struct GLFWwindow; struct GLFWwindow;
/*
* Внимательно следите, что бы у ваших объектов была "мягкая" зависимость
*/
class CoreInstance { class CoreInstance {
struct Module { struct Module {
const char* name; const char* name;
@@ -58,4 +60,5 @@ public:
const std::string& getBaseWorldName() const { return main_config_.base_world; } const std::string& getBaseWorldName() const { return main_config_.base_world; }
const std::string& getGameName() const { return main_config_.game_name; } const std::string& getGameName() const { return main_config_.game_name; }
GameInstance* GetGameInstance() const { return game_; } GameInstance* GetGameInstance() const { return game_; }
RenderEngine* GetRenderEngine() const { return render_engine_; }
}; };
+73
View File
@@ -0,0 +1,73 @@
#include "ModelManager.hpp"
ModelManager::StaticModel::StaticModel(const std::vector<Voxel>& 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<int>(voxels_.size());
sum_moments.y /= static_cast<int>(voxels_.size());
sum_moments.z /= static_cast<int>(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<unsigned int>(*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::StaticModel> 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<Voxel> model_data;
// Load model_data
auto newModel = std::make_shared<StaticModel>(model_data, name);
newModel->OnDestroy.bind(this, &ModelManager::OnDestroySometimeModelCaller);
models[hash] = newModel;
return models[hash];
}
+47
View File
@@ -0,0 +1,47 @@
#pragma once
#include <unordered_map>
#include <vector>
#include <string>
#include <Delegate/Delegate.h>
#include <glm/glm.hpp>
class ModelManager
{
public:
struct Voxel
{
glm::ivec3 loc, color;
int mass;
};
class StaticModel
{
std::string name_;
std::vector<Voxel> voxels_;
glm::vec3 mass_center_;
public:
Delegate<const std::string&> OnDestroy;
StaticModel(const std::vector<Voxel>& voxels, std::string name);
~StaticModel();
const std::vector<Voxel>& GetVoxels() const { return voxels_; }
glm::vec3 GetMassCenter() const { return mass_center_; }
const std::string& GetName() const { return name_; }
};
private:
std::unordered_map<unsigned int, std::shared_ptr<StaticModel>> models;
static unsigned int FNV1aHash (const char *buf);
void OnDestroySometimeModelCaller(const std::string& name);
public:
Delegate<std::string> OnDestroySometimeModel;
~ModelManager();
std::shared_ptr<StaticModel> LoadModel(const std::string& name);
};
@@ -1,15 +1,15 @@
#include "RenderEngine.h" #include "RenderEngine.hpp"
#include <queue> #include <queue>
#include <set> #include <set>
#include <stdexcept> #include <stdexcept>
#include <fstream> #include <fstream>
#include "CoreInstance.h" #include "Core/CoreInstance.hpp"
#include "Log/Log.h" #include "Log/Log.hpp"
#include "Game/GameInstance.h" #include "Game/GameInstance.hpp"
#include "Game/World/World.h" #include "Game/World/World.hpp"
#include "Game/Actors/Mesh/Mesh.hpp" #include "Game/Actors/Mesh/Mesh.hpp"
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
@@ -11,6 +11,8 @@
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "ModelManager.hpp"
#ifdef _DEBUG #ifdef _DEBUG
#include <assert.h> #include <assert.h>
#define VK_CHECK(res) assert(res == VK_SUCCESS) #define VK_CHECK(res) assert(res == VK_SUCCESS)
@@ -55,6 +57,8 @@ class RenderEngine
void copy_memory(VkBuffer src, VkBuffer dst, VkDeviceSize size); void copy_memory(VkBuffer src, VkBuffer dst, VkDeviceSize size);
#pragma endregion #pragma endregion
ModelManager model_manager_;
static const std::vector<const char*> deviceExtensions; static const std::vector<const char*> deviceExtensions;
int current_frame_ = 0; int current_frame_ = 0;
@@ -128,4 +132,6 @@ public:
void stop_render() const; void stop_render() const;
GLFWwindow* get_window() const { return window_; } GLFWwindow* get_window() const { return window_; }
ModelManager* GetActiveModelManager() { return &model_manager_; }
}; };
+1 -1
View File
@@ -1,6 +1,6 @@
#ifdef _WIN32 #ifdef _WIN32
#include "../SystemCalls.h" #include "../SystemCalls.hpp"
#include <windows.h> #include <windows.h>
+1 -1
View File
@@ -1,6 +1,6 @@
#include <iostream> #include <iostream>
#include "CoreInstance.h" #include "CoreInstance.hpp"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
try { try {
+14 -3
View File
@@ -1,7 +1,12 @@
#include "Actor.h" #include "Actor.hpp"
#include "Game/SaveMap/SaveMap.h" #include "Game/SaveMap/SaveMap.hpp"
#include "Log/Log.h" #include "Game/World/World.hpp"
#include "Log/Log.hpp"
void Actor::OnDestroy()
{
}
Actor::Actor() Actor::Actor()
{ {
@@ -55,3 +60,9 @@ void Actor::RemoveTag(const std::string& tag) noexcept
{ {
tags.remove(tag); tags.remove(tag);
} }
void Actor::Destroy()
{
OnDestroy();
//world_->actors.remove(this);
}
@@ -6,16 +6,23 @@
#include "RTTI_Meta.h" #include "RTTI_Meta.h"
#include "Game/SaveMap/ISave.h" #include "Game/SaveMap/ISave.h"
#include "Delegate/Delegate.h" #include "Delegate/Delegate.h"
#include "Math/Vector.h" #include "Math/Vector.hpp"
class World;
GENERATE_META(Actor) GENERATE_META(Actor)
class Actor : public ISave, public IRTTI class Actor : public ISave, public IRTTI
{ {
World* world_;
Vector3D loc; Vector3D loc;
Vector3D rot; Vector3D rot;
Vector3D scale; Vector3D scale;
std::list<std::string> tags; std::list<std::string> tags;
protected:
virtual void OnDestroy();
public: public:
Actor(); Actor();
@@ -47,4 +54,9 @@ public:
void AddTag(const std::string& tag) noexcept; void AddTag(const std::string& tag) noexcept;
void RemoveTag(const std::string& tag) noexcept; void RemoveTag(const std::string& tag) noexcept;
const std::list<std::string>& GetTags() const { return tags; } const std::list<std::string>& GetTags() const { return tags; }
void Destroy();
World* GetWorld() const { return world_; }
friend class World;
}; };
+1 -1
View File
@@ -1,6 +1,6 @@
#include "Mesh.hpp" #include "Mesh.hpp"
#include "Game/SaveMap/SaveMap.h" #include "Game/SaveMap/SaveMap.hpp"
Mesh::Mesh() Mesh::Mesh()
{ {
+1 -1
View File
@@ -2,7 +2,7 @@
#include <string> #include <string>
#include "../Actor.h" #include "../Actor.hpp"
GENERATE_META(Mesh) GENERATE_META(Mesh)
class Mesh : public Actor class Mesh : public Actor
+3 -3
View File
@@ -1,8 +1,8 @@
#include "ObjectFactory.h" #include "ObjectFactory.hpp"
#include "Actors/Mesh/StaticMesh.hpp" #include "Actors/Mesh/StaticMesh.hpp"
#include "Game/Actors/Actor.h" #include "Game/Actors/Actor.hpp"
#include "Math/Vector.h" #include "Math/Vector.hpp"
std::vector<ObjectFactory> base_object_factories = { std::vector<ObjectFactory> base_object_factories = {
GENERATE_FACTORY_OBJECT(Actor) GENERATE_FACTORY_OBJECT(Actor)
+5 -5
View File
@@ -1,14 +1,14 @@
#include "GameInstance.h" #include "GameInstance.hpp"
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include "Core/CoreInstance.h" #include "Core/CoreInstance.hpp"
#include "Game/WorldFactory.h" #include "Game/WorldFactory.hpp"
#include "SaveMap/SaveMap.h" #include "SaveMap/SaveMap.hpp"
#include "Game/World/World.h" #include "Game/World/World.hpp"
extern std::vector<WorldFactory> world_factories; extern std::vector<WorldFactory> world_factories;
@@ -28,6 +28,7 @@ public:
virtual ~GameInstance(); virtual ~GameInstance();
World* GetWorld() const { return world; } World* GetWorld() const { return world; }
CoreInstance& GetCore() const { return core; }
friend class CoreInstance; friend class CoreInstance;
}; };
+1 -1
View File
@@ -1,4 +1,4 @@
#include "Resource.h" #include "Resource.hpp"
#include <stdexcept> #include <stdexcept>
#include <cstring> #include <cstring>
@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "IResource.h" #include "IResource.hpp"
#include <mutex> #include <mutex>
#include <fstream> #include <fstream>
#include <string> #include <string>
+2 -2
View File
@@ -1,8 +1,8 @@
#include "SaveMap.h" #include "SaveMap.hpp"
#include <filesystem> #include <filesystem>
#include <stdexcept> #include <stdexcept>
#include "Game/ObjectFactory.h" #include "Game/ObjectFactory.hpp"
extern std::vector<ObjectFactory> factories; extern std::vector<ObjectFactory> factories;
extern std::vector<ObjectFactory> base_object_factories; extern std::vector<ObjectFactory> base_object_factories;
+5 -2
View File
@@ -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) World::World(GameInstance& game_instance) : game_instance(game_instance)
{ {
@@ -35,4 +35,7 @@ std::shared_ptr<SaveMap> World::save()
void World::load(std::shared_ptr<SaveMap> save) void World::load(std::shared_ptr<SaveMap> save)
{ {
actors = std::move(reinterpret_cast<std::list<Actor*>&>(save->GetListObject("Actors"))); actors = std::move(reinterpret_cast<std::list<Actor*>&>(save->GetListObject("Actors")));
for (auto& i : actors)
i->world_ = this;
} }
@@ -5,11 +5,12 @@
#include <string> #include <string>
#include "RTTI.h" #include "RTTI.h"
#include "Game/Actors/Actor.h" #include "Game/Actors/Actor.hpp"
#include "Game/Actors/Mesh/Mesh.hpp" #include "Game/Actors/Mesh/Mesh.hpp"
class GameInstance; class GameInstance;
GENERATE_META(World);
class World : public ISave class World : public ISave
{ {
GameInstance& game_instance; GameInstance& game_instance;
@@ -49,6 +50,7 @@ public:
T* object = new T(); T* object = new T();
object->SetActorLocate(loc); object->SetActorLocate(loc);
object->SetActorRotate(rot); object->SetActorRotate(rot);
static_cast<Actor*>(object)->world_ = this;
actors.push_back(object); actors.push_back(object);
return object; return object;
} }
@@ -3,7 +3,7 @@
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include "Game/SaveMap/SaveMap.h" #include "Game/SaveMap/SaveMap.hpp"
class GameInstance; class GameInstance;
class World; class World;
+1 -1
View File
@@ -1,4 +1,4 @@
#include "Log.h" #include "Log.hpp"
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
View File
+2 -2
View File
@@ -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) Vector2D::Vector2D(const double x, const double y) : x(x), y(y)
{} {}
+131
View File
@@ -0,0 +1,131 @@
#include "UString.hpp"
#include <cstring>
unsigned int UString::FNV1aHash(const char* buf)
{
unsigned int h_val = 0x811c9dc5;
while (*buf)
{
h_val ^= static_cast<unsigned int>(*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);
}
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include <atomic>
#include <string>
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_); }
};
+1 -1
View File
@@ -13,5 +13,5 @@ template<class T> struct Meta;
// Enum type classes // Enum type classes
enum class Classes enum class Classes
{ {
IRTTI, IResource, Actor, TestActor, Mesh, StaticMesh IRTTI, IResource, Actor, TestActor, Mesh, StaticMesh, World, TestWorld
}; };
+1 -1
View File
@@ -1,6 +1,6 @@
#include "TestActor.h" #include "TestActor.h"
#include "Log/Log.h" #include "Log/Log.hpp"
void TestActor::BeginPlay() void TestActor::BeginPlay()
{ {
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Game/Actors/Actor.h" #include "Game/Actors/Actor.hpp"
GENERATE_META(TestActor) GENERATE_META(TestActor)
class TestActor final : public Actor class TestActor final : public Actor
+1 -1
View File
@@ -1,4 +1,4 @@
#include "Game/ObjectFactory.h" #include "Game/ObjectFactory.hpp"
#include "../Actors/TestActor.h" #include "../Actors/TestActor.h"
+1 -1
View File
@@ -1,4 +1,4 @@
#include "Game/WorldFactory.h" #include "Game/WorldFactory.hpp"
#include "../Worlds/TestWorld.h" #include "../Worlds/TestWorld.h"
+3 -3
View File
@@ -1,10 +1,10 @@
#include "TestGameInstance.h" #include "TestGameInstance.h"
#include "Game/Resource/Resource.h" #include "Game/Resource/Resource.hpp"
#include <iostream> #include <iostream>
#include "Game/SaveMap/SaveMap.h" #include "Game/SaveMap/SaveMap.hpp"
#include "Game/World/World.h" #include "Game/World/World.hpp"
GENERATE_FACTORY_GAME_INSTANCE(TestGameInstance) GENERATE_FACTORY_GAME_INSTANCE(TestGameInstance)
TestGameInstance::TestGameInstance(CoreInstance& core) : GameInstance(core) TestGameInstance::TestGameInstance(CoreInstance& core) : GameInstance(core)
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Game/GameInstance.h" #include "Game/GameInstance.hpp"
class TestGameInstance : public GameInstance class TestGameInstance : public GameInstance
{ {
+3 -3
View File
@@ -1,8 +1,8 @@
#include "TestWorld.h" #include "TestWorld.h"
#include "Game/GameInstance.h" #include "Game/GameInstance.hpp"
#include "Game/SaveMap/SaveMap.h" #include "Game/SaveMap/SaveMap.hpp"
#include "Log/Log.h" #include "Log/Log.hpp"
TestWorld::TestWorld(GameInstance& game_instance) : World(game_instance) TestWorld::TestWorld(GameInstance& game_instance) : World(game_instance)
{} {}
+2 -1
View File
@@ -1,7 +1,8 @@
#pragma once #pragma once
#include "Game/World/World.h" #include "Game/World/World.hpp"
GENERATE_META(TestWorld)
class TestWorld final : public World class TestWorld final : public World
{ {
double time = 0; double time = 0;
+24
View File
@@ -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)
+25
View File
@@ -0,0 +1,25 @@
#include <gtest/gtest.h>
#include "Types/UString.hpp"
#include <cstring>
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);
}