Add GetWorld to Actor. Integrate google test. Add Model manager class. Rename *.h to *.hpp files
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
struct CoreCallBacks
|
||||
{
|
||||
void (*Quit)();
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "CoreInstance.h"
|
||||
#include "CoreInstance.hpp"
|
||||
|
||||
#include "SystemCalls.h"
|
||||
#include "SystemCalls.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <cstring>
|
||||
@@ -8,10 +8,9 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#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();
|
||||
|
||||
@@ -6,13 +6,15 @@
|
||||
#include <vector>
|
||||
|
||||
#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_; }
|
||||
};
|
||||
@@ -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];
|
||||
}
|
||||
@@ -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 <set>
|
||||
#include <stdexcept>
|
||||
#include <fstream>
|
||||
|
||||
#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"
|
||||
@@ -11,6 +11,8 @@
|
||||
#include <vulkan/vulkan.h>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "ModelManager.hpp"
|
||||
|
||||
#ifdef _DEBUG
|
||||
#include <assert.h>
|
||||
#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<const char*> 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_; }
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
#ifdef _WIN32
|
||||
|
||||
#include "../SystemCalls.h"
|
||||
#include "../SystemCalls.hpp"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "CoreInstance.h"
|
||||
#include "CoreInstance.hpp"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
try {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<std::string> 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<std::string>& GetTags() const { return tags; }
|
||||
void Destroy();
|
||||
|
||||
World* GetWorld() const { return world_; }
|
||||
|
||||
friend class World;
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "Mesh.hpp"
|
||||
|
||||
#include "Game/SaveMap/SaveMap.h"
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
|
||||
Mesh::Mesh()
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../Actor.h"
|
||||
#include "../Actor.hpp"
|
||||
|
||||
GENERATE_META(Mesh)
|
||||
class Mesh : public Actor
|
||||
|
||||
@@ -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<ObjectFactory> base_object_factories = {
|
||||
GENERATE_FACTORY_OBJECT(Actor)
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
#include "GameInstance.h"
|
||||
#include "GameInstance.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#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<WorldFactory> world_factories;
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ public:
|
||||
virtual ~GameInstance();
|
||||
|
||||
World* GetWorld() const { return world; }
|
||||
CoreInstance& GetCore() const { return core; }
|
||||
|
||||
friend class CoreInstance;
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "Resource.h"
|
||||
#include "Resource.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "IResource.h"
|
||||
#include "IResource.hpp"
|
||||
#include <mutex>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "SaveMap.h"
|
||||
#include "SaveMap.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <stdexcept>
|
||||
#include "Game/ObjectFactory.h"
|
||||
#include "Game/ObjectFactory.hpp"
|
||||
|
||||
extern std::vector<ObjectFactory> factories;
|
||||
extern std::vector<ObjectFactory> base_object_factories;
|
||||
|
||||
@@ -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<SaveMap> World::save()
|
||||
void World::load(std::shared_ptr<SaveMap> save)
|
||||
{
|
||||
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 "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<Actor*>(object)->world_ = this;
|
||||
actors.push_back(object);
|
||||
return object;
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include "Game/SaveMap/SaveMap.h"
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
|
||||
class GameInstance;
|
||||
class World;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
#include "Log.h"
|
||||
#include "Log.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
|
||||
@@ -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)
|
||||
{}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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_); }
|
||||
};
|
||||
Reference in New Issue
Block a user