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 {
|
||||
|
||||
Reference in New Issue
Block a user