Навёл порядок в тестах. Добавил тесты для мира. Исправил ошибку в SpawnActorFromClass

This commit is contained in:
Jiga228
2026-06-28 19:26:23 +07:00
parent ad881a97a9
commit 82ba9fd191
21 changed files with 404 additions and 56 deletions
+1 -1
View File
@@ -91,7 +91,7 @@ void CoreInstance::start()
game_thread.join();
}
void CoreInstance::quit()
void CoreInstance::quit() const
{
// Stop the main loop
render_engine_->stop_render();
+1 -1
View File
@@ -50,7 +50,7 @@ public:
~CoreInstance();
void start();
void quit();
void quit() const;
bool IsReady() const { return is_ready; }
int getCountCPU() const { return countCPU_; }
+4 -4
View File
@@ -18,7 +18,7 @@ std::shared_ptr<SaveMap> Actor::save()
save->SaveObject("loc", loc_);
save->SaveObject("rot", rot_);
save->SaveObject("scale", scale_);
save->SaveListStrings("tags", std::move(tags));
save->SaveListStrings("tags", std::move(tags_));
return save;
}
@@ -27,7 +27,7 @@ void Actor::load(std::shared_ptr<SaveMap> save)
loc_ = std::static_pointer_cast<Vector3D>(save->GetObject("loc"));
rot_ = std::static_pointer_cast<Vector3D>(save->GetObject("rot"));
scale_ = std::static_pointer_cast<Vector3D>(save->GetObject("scale"));
tags = std::move(save->GetListString("tags"));
tags_ = std::move(save->GetListString("tags"));
}
void Actor::BeginPlay()
@@ -52,10 +52,10 @@ void Actor::SetActorRotate(const Vector3D& rot) noexcept
void Actor::AddTag(const std::string& tag) noexcept
{
tags.push_back(tag);
tags_.push_back(tag);
}
void Actor::RemoveTag(const std::string& tag) noexcept
{
tags.remove(tag);
tags_.remove(tag);
}
+2 -2
View File
@@ -18,7 +18,7 @@ class Actor : public ISave, public IRTTI
std::shared_ptr<Vector3D> loc_, rot_, scale_;
std::list<std::string> tags;
std::list<std::string> tags_;
protected:
virtual void OnDestroy();
@@ -52,7 +52,7 @@ public:
void AddTag(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_; }
std::shared_ptr<World> GetWorld() const { return world_; }
const std::string& GetName() const { return name_; }
+1 -1
View File
@@ -21,7 +21,7 @@ ISave* SaveMap::MakeObjectByName(const std::string& name)
return factory.factory();
}
}
throw std::runtime_error("Object not found");
throw std::runtime_error("Object not found (" + name + ")");
}
SaveMap::SaveMap(const char* class_name) : class_name(class_name)
+5 -5
View File
@@ -49,7 +49,7 @@ public:
std::shared_ptr<T> object = std::make_shared<T>();
object->SetActorLocate(loc);
object->SetActorRotate(rot);
std::static_pointer_cast<Actor>(object)->world_ = this;
std::static_pointer_cast<Actor>(object)->world_ = std::static_pointer_cast<World>(shared_from_this());
std::static_pointer_cast<Actor>(object)->name_ = name;
actors_map.try_emplace(name, object);
return object;
@@ -57,7 +57,7 @@ public:
void DestroyActor(std::weak_ptr<Actor> ptr);
template<class T, class Container = std::vector<T*>>
template<class T, class Container = std::vector<std::weak_ptr<T>>>
Container GetActorsByTag(std::string tag)
{
Container container;
@@ -68,7 +68,7 @@ public:
for (auto& j : tags)
{
if (j == tag)
container.push_back(i);
container.push_back(i.second);
}
}
@@ -76,9 +76,9 @@ public:
}
template<class T>
std::weak_ptr<T> GetActorByID(const std::string& id)
std::weak_ptr<T> GetActorByName(const std::string& name)
{
auto it = actors_map.find(id);
auto it = actors_map.find(name);
if (it == actors_map.end() || it->second == nullptr)
return {};
-2
View File
@@ -1,9 +1,7 @@
#include "TestGameInstance.h"
#include "Game/Resource/Resource.hpp"
#include <iostream>
#include "Game/SaveMap/SaveMap.hpp"
#include "Game/World/World.hpp"
GENERATE_FACTORY_GAME_INSTANCE(TestGameInstance)
+6 -8
View File
@@ -1,4 +1,4 @@
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(
googletest
@@ -11,18 +11,16 @@ FetchContent_MakeAvailable(googletest)
enable_testing()
file(GLOB SRC
"SaveMapTest.cpp"
"ObjectPtrTest.cpp"
"${PROJECT_SOURCE_DIR}/Core/Game/SaveMap/SaveMap.cpp"
"ObjectFactory.cpp" "WorldFactory.cpp" "TestRenderEngine.cpp" "TestRenderEngine.h" "TestGameInstance.cpp" "TestGameInstance.h"
"SaveMap/SaveMapTest.cpp" "SaveMap/CustomObject.h"
"World/WorldTest.cpp" "World/TestWorld.h" "World/TestActor.cpp" "World/TestActor.h"
)
add_executable(Tests ${SRC})
target_link_libraries(Tests PRIVATE
GTest::gtest_main
)
target_link_libraries(Tests PRIVATE GTest::gtest_main Core RenderEngineSDK)
target_include_directories(Tests PRIVATE
${PROJECT_SOURCE_DIR}/Core
${PROJECT_SOURCE_DIR}/RenderEngineSDK
)
+10
View File
@@ -0,0 +1,10 @@
#include "Game/ObjectFactory.hpp"
#include "SaveMap/CustomObject.h"
#include "World/TestActor.h"
FACTORIES_LIST{
GENERATE_FACTORY_OBJECT(CustomObject)
GENERATE_FACTORY_OBJECT(TestActor)
};
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include "Game/SaveMap/ISave.h"
#include <string>
#include <memory>
class CustomObject : public ISave
{
public:
int num;
std::string str;
double dbl;
std::shared_ptr<SaveMap> save() override;
void load(std::shared_ptr<SaveMap> save) override;
bool operator==(const CustomObject& obj) const;
};
@@ -1,42 +1,29 @@
#include <gtest/gtest.h>
#include <iostream>
#include "Game/ObjectFactory.hpp"
#include "Game/SaveMap/SaveMap.hpp"
#include "CustomObject.h"
class CustomObject : public ISave
std::shared_ptr<SaveMap> CustomObject::save()
{
public:
int num;
std::string str;
double dbl;
std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("CustomObject");
save->SaveInteger("num", num);
save->SaveString("str", str);
save->SaveDouble("dbl", dbl);
return save;
}
std::shared_ptr<SaveMap> save() override
{
std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("CustomObject");
save->SaveInteger("num", num);
save->SaveString("str", str);
save->SaveDouble("dbl", dbl);
return save;
}
void load(std::shared_ptr<SaveMap> save) override
{
num = static_cast<int>(save->GetInteger("num"));
str = save->GetString("str");
dbl = save->GetDouble("dbl");
}
void CustomObject::load(std::shared_ptr<SaveMap> save)
{
num = static_cast<int>(save->GetInteger("num"));
str = save->GetString("str");
dbl = save->GetDouble("dbl");
}
bool operator==(const CustomObject& obj) const
{
return num == obj.num && str == obj.str && dbl == obj.dbl;
}
};
FACTORIES_LIST {
GENERATE_FACTORY_OBJECT(CustomObject)
};
// A compatibility plug
std::vector<ObjectFactory> base_object_factories = {};
bool CustomObject::operator==(const CustomObject& obj) const
{
return num == obj.num && str == obj.str && dbl == obj.dbl;
}
TEST(SaveMapTest, check_clean)
{
+10
View File
@@ -0,0 +1,10 @@
#include "TestGameInstance.h"
#include <iostream>
#include "Game/World/World.hpp"
GENERATE_FACTORY_GAME_INSTANCE(TestGameInstance)
TestGameInstance::TestGameInstance(CoreInstance& core) : GameInstance(core)
{
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "Game/GameInstance.hpp"
class TestGameInstance : public GameInstance
{
public:
TestGameInstance(CoreInstance& core);
};
+15
View File
@@ -0,0 +1,15 @@
#include "TestRenderEngine.h"
RENDER_ENGINE_FACTORY_GENERATE(TestRenderEngine)
TestRenderEngine::TestRenderEngine(CoreInstance& core):RenderEngineBase(core)
{
}
void TestRenderEngine::start()
{
}
void TestRenderEngine::stop_render() const
{
}
+12
View File
@@ -0,0 +1,12 @@
#pragma once
#include "RenderEngineBase.hpp"
class TestRenderEngine final : public RenderEngineBase
{
public:
TestRenderEngine(CoreInstance& core);
void start() override;
void stop_render() const override;
};
+19
View File
@@ -0,0 +1,19 @@
#include "TestActor.h"
#include "Game/SaveMap/SaveMap.hpp"
TestActor::TestActor()
{
SetType(Classes::TestActor);
}
std::shared_ptr<SaveMap> TestActor::save()
{
std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("TestActor");
save->connect_to(Actor::save());
return save;
}
void TestActor::load(std::shared_ptr<SaveMap> save)
{
Actor::load(save->getParent());
}
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#include "RTTI_Meta.h"
#include "Game/Actors/Actor.hpp"
GENERATE_META(TestActor)
class TestActor : public Actor
{
public:
TestActor();
std::shared_ptr<SaveMap> save() override;
void load(std::shared_ptr<SaveMap> save) override;
};
+12
View File
@@ -0,0 +1,12 @@
#pragma once
#include "Game/World/World.hpp"
class TestWorld : public World
{
public:
explicit TestWorld(GameInstance& game_instance);
std::shared_ptr<SaveMap> save() override;
void load(std::shared_ptr<SaveMap> save) override;
};
+238
View File
@@ -0,0 +1,238 @@
#include <filesystem>
#include <fstream>
#include <gtest/gtest.h>
#include "TestActor.h"
#include "Game/World/World.hpp"
#include "TestWorld.h"
#include "Core/CoreInstance.hpp"
#include "Game/GameInstance.hpp"
#include "Game/SaveMap/SaveMap.hpp"
TestWorld::TestWorld(GameInstance& game_instance) : World(game_instance)
{
}
std::shared_ptr<SaveMap> TestWorld::save()
{
std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("TestWorld");
save->connect_to(World::save());
return save;
}
void TestWorld::load(std::shared_ptr<SaveMap> save)
{
World::load(save->getParent());
}
TEST(World, check_load)
{
const std::string main_config =
R"({
"Class name":"MainConfig",
"dmin_memory_size":4096.0,
"imin_CPU_count":1,
"sgame_name":"tests",
"sbase_world":"tests"
})";
std::ofstream main_config_file("main_config.conf");
main_config_file << main_config;
main_config_file.close();
const std::string world_config =
R"(
{
"Class name": "TestWorld",
"Parent parameters": {
"Class name": "World",
"vsActorsIDs": [
"TempActor"
],
"loActors": [
{
"Class name": "Actor",
"oloc": {
"Class name": "Vector3D",
"dx": 1,
"dy": -1,
"dz": 1
},
"orot": {
"Class name": "Vector3D",
"dx": 0,
"dy": 0,
"dz": 0
},
"oscale": {
"Class name": "Vector3D",
"dx": 1,
"dy": 1,
"dz": 1
},
"lstags": []
}
]
}
}
)";
std::filesystem::create_directories("Worlds");
std::ofstream world_file("Worlds/tests.world");
world_file << world_config;
world_file.close();
CoreInstance tmp_core;
tmp_core.GetGameInstance()->GetWorld()->SpawnActorFormClass<TestActor>("Test");
std::vector<std::weak_ptr<Actor>> actors = tmp_core.GetGameInstance()->GetWorld()->GetActorsByClass<Actor>();
ASSERT_EQ(actors.size(), 2);
std::filesystem::remove("Worlds/tests.world");
std::filesystem::remove("Worlds");
std::filesystem::remove("main_config.conf");
}
TEST(World, check_finders)
{
const std::string main_config =
R"({
"Class name":"MainConfig",
"dmin_memory_size":4096.0,
"imin_CPU_count":1,
"sgame_name":"tests",
"sbase_world":"tests"
})";
std::ofstream main_config_file("main_config.conf");
main_config_file << main_config;
main_config_file.close();
const std::string world_config =
R"(
{
"Class name": "TestWorld",
"Parent parameters": {
"Class name": "World",
"vsActorsIDs": [
"TempActor1",
"TempActor2"
],
"loActors": [
{
"Class name": "TestActor",
"Parent parameters":{
"Class name": "Actor",
"oloc": {
"Class name": "Vector3D",
"dx": 1,
"dy": -1,
"dz": 1
},
"orot": {
"Class name": "Vector3D",
"dx": 0,
"dy": 0,
"dz": 0
},
"oscale": {
"Class name": "Vector3D",
"dx": 1,
"dy": 1,
"dz": 1
},
"lstags": []
}
},
{
"Class name": "Actor",
"oloc": {
"Class name": "Vector3D",
"dx": 1,
"dy": -1,
"dz": 1
},
"orot": {
"Class name": "Vector3D",
"dx": 0,
"dy": 0,
"dz": 0
},
"oscale": {
"Class name": "Vector3D",
"dx": 1,
"dy": 1,
"dz": 1
},
"lstags": ["Simple"]
}
]
}
}
)";
std::filesystem::create_directories("Worlds");
std::ofstream world_file("Worlds/tests.world");
world_file << world_config;
world_file.close();
CoreInstance tmp_core;
// Test find by class
std::vector<std::weak_ptr<TestActor>> test_actors = tmp_core.GetGameInstance()->GetWorld()->GetActorsByClass<TestActor>();
ASSERT_EQ(test_actors.size(), 1);
// Test find by tag
std::vector<std::weak_ptr<Actor>> actors = tmp_core.GetGameInstance()->GetWorld()->GetActorsByTag<Actor>("Simple");
ASSERT_EQ(actors.size(), 1);
// Test find by name
std::weak_ptr<Actor> actor_by_name = tmp_core.GetGameInstance()->GetWorld()->GetActorByName<Actor>("TempActor2");
ASSERT_EQ(actor_by_name.expired(), false);
ASSERT_EQ(*actor_by_name.lock()->GetTags().begin(), "Simple");
std::filesystem::remove("Worlds/tests.world");
std::filesystem::remove("Worlds");
std::filesystem::remove("main_config.conf");
}
TEST(World, check_spawn_actor_from_class)
{
const std::string main_config =
R"({
"Class name":"MainConfig",
"dmin_memory_size":4096.0,
"imin_CPU_count":1,
"sgame_name":"tests",
"sbase_world":"tests"
})";
std::ofstream main_config_file("main_config.conf");
main_config_file << main_config;
main_config_file.close();
const std::string world_config =
R"(
{
"Class name": "TestWorld",
"Parent parameters": {
"Class name": "World",
"vsActorsIDs": [
],
"loActors": [
]
}
}
)";
std::filesystem::create_directories("Worlds");
std::ofstream world_file("Worlds/tests.world");
world_file << world_config;
world_file.close();
CoreInstance tmp_core;
std::vector<std::weak_ptr<TestActor>> test_actors = tmp_core.GetGameInstance()->GetWorld()->GetActorsByClass<TestActor>();
ASSERT_EQ(test_actors.size(), 0);
tmp_core.GetGameInstance()->GetWorld()->SpawnActorFormClass<TestActor>("Test");
test_actors = tmp_core.GetGameInstance()->GetWorld()->GetActorsByClass<TestActor>();
ASSERT_EQ(test_actors.size(), 1);
std::filesystem::remove("Worlds/tests.world");
std::filesystem::remove("Worlds");
std::filesystem::remove("main_config.conf");
}
+8
View File
@@ -0,0 +1,8 @@
#include <Game/WorldFactory.hpp>
#include "World/TestWorld.h"
WORLDS_LIST
{
GENERATE_WORLD_FACTORY(TestWorld, tests)
};
+1 -1
View File
@@ -25,5 +25,5 @@ void Camera::load(std::shared_ptr<SaveMap> save)
void Camera::SetActive() const noexcept
{
GET_RENDER_ENGINE->SetActiveCamera(GetWorld()->GetActorByID<Camera>(GetName()));
GET_RENDER_ENGINE->SetActiveCamera(GetWorld()->GetActorByName<Camera>(GetName()));
}