Skip to content

Commit

Permalink
Merge pull request #201 from TaixuEngineGroup/refactor
Browse files Browse the repository at this point in the history
更新tool
  • Loading branch information
xmmmmmovo committed Jul 24, 2023
2 parents d00d7cd + 2295645 commit 1abb956
Show file tree
Hide file tree
Showing 30 changed files with 231 additions and 413 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ configure_file(cmake/path.hpp.in ${TAIXUENGINE_ROOT_DIR}/engine/src/common/base/

add_subdirectory(engine)
add_subdirectory(docs)
add_subdirectory(tests)
add_subdirectory(tests)
add_subdirectory(tools)
4 changes: 3 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ set(glslangValidator_executable ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE})

set(SHADER_COMPILE_TARGET TaixuShaderCompile)
set(COMMON_COMPILE_TARGET TaixuCommonLib)
set(SCRIPT_COMPILE_TARGET TaixuScriptLib)

add_subdirectory(shaders)

# editor and runtime
add_subdirectory(src/editor)
add_subdirectory(src/common)
add_subdirectory(src/runtime)
add_subdirectory(src/runtime)
add_subdirectory(src/script)
4 changes: 2 additions & 2 deletions engine/src/common/base/macro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ namespace taixu {
/**
* @brief Define Opengl version
*/
constexpr std::string_view OPENGL_VERSION = "#version 460";
constexpr std::string_view OPENGL_VERSION = "#version 410";
/**
* @brief Define Opengl major version
*/
constexpr std::int32_t OPENGL_MAJOR_VERSION = 4;
/**
* @brief Define Opengl minor version
*/
constexpr std::int32_t OPENGL_MINOR_VERSION = 6;
constexpr std::int32_t OPENGL_MINOR_VERSION = 1;

/**
* @brief status enum
Expand Down
15 changes: 1 addition & 14 deletions engine/src/editor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@

#include "ui/main_window/main_window.hpp"

/**
* @brief initWindow spdlog config
*/
void initSpdlog() {
#ifndef NDEBUG
spdlog::set_level(spdlog::level::debug);// Set global log level to debug
#else
spdlog::set_level(spdlog::level::info);// Set global log level to info
#endif
}

/**
* @brief Main window width
*/
Expand All @@ -38,10 +27,8 @@ int main(int argc, char* argv[]) {
// avoid c-style array
std::vector<std::string> const args(argv, argv + argc);

initSpdlog();

taixu::Engine* engine_ptr = &taixu::Engine::getInstance();
engine_ptr->loadParams(args);
taixu::Engine::loadParams(args);

spdlog::info("start init the application!");

Expand Down
41 changes: 27 additions & 14 deletions engine/src/runtime/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <optional>

#include "engine_args.hpp"
#include "resource/converted_data/project.hpp"
#include "resource/manager/project_manager.hpp"
#include <runtime/management/ecs/ecs_coordinator.hpp>
#include <runtime/management/graphics/render/render_api.hpp>
#include <runtime/management/input/input_system.hpp>
Expand Down Expand Up @@ -33,9 +35,8 @@ void Engine::init(std::unique_ptr<WindowContext> context,
}
_renderer->init();

_project_manager = std::make_unique<ProjectManager>();
_asset_manager = std::make_unique<AssetManager>();
_scene_manager = std::make_unique<SceneManager>();
_asset_manager = std::make_unique<AssetManager>();
_scene_manager = std::make_unique<SceneManager>();

_renderer->set_default_texture(
_asset_manager->loadTexture(".", "assets/texture/rgba1111.png")
Expand Down Expand Up @@ -71,20 +72,32 @@ void Engine::destroy() { this->_window_ptr->destroy(); }

AbstractRenderer* Engine::getRenderer() const { return _renderer.get(); }

Status Engine::loadProject(const std::string_view& path) {
spdlog::info("Loading project: {}", path);
Status const status = _project_manager->openProject(path);
if (Status::OK != status) {
spdlog::error("Failed to load project: {}",
magic_enum::enum_name(status));
return status;
void Engine::loadProject(const std::filesystem::path& path) {
spdlog::info("Loading project: {}", path.generic_string());
std::optional<Project> tpj{std::nullopt};
try {
tpj = openProject(path);
} catch (std::exception& e) {
spdlog::error("Failed to load project: {}", e.what());
return;
}

return Status::OK;
glfwSetWindowTitle(this->_context_ptr->_window,
fmt::format("{} - {} - {}", this->_context_ptr->_title,
tpj.value().manifest.name,
tpj.value().current_path.generic_string())
.c_str());
this->_opened_project = std::move(tpj);
}

Project* Engine::getOpenedProject() const {
return this->_project_manager->getCurrentProject();
/**
* @brief 获取打开的项目文件
* @return
*/
Project const* Engine::getOpenedProject() const {
if (this->_opened_project.has_value()) {
return &this->_opened_project.value();
}
return nullptr;
}

Scene* Engine::getScene() const { return _current_scene; }
Expand Down
15 changes: 11 additions & 4 deletions engine/src/runtime/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <common/base/macro.hpp>
#include <common/base/public_singleton.hpp>

#include <filesystem>
#include <runtime/gameplay/gui/window.hpp>
#include <runtime/gameplay/gui/window_context.hpp>
#include <runtime/management/ecs/ecs_coordinator.hpp>
Expand All @@ -23,21 +24,27 @@ class Engine final : public PublicSingleton<Engine> {
private:
std::unique_ptr<AbstractRenderer> _renderer{nullptr};
std::unique_ptr<AssetManager> _asset_manager{nullptr};
std::unique_ptr<ProjectManager> _project_manager{nullptr};
std::unique_ptr<SceneManager> _scene_manager{nullptr};


std::unique_ptr<WindowContext> _context_ptr{nullptr};
std::unique_ptr<IWindow> _window_ptr{nullptr};

Scene* _current_scene{nullptr};

std::optional<Project> _opened_project{std::nullopt};
Scene* _current_scene{nullptr};

/**
* @brief editor state
*/
EngineState _state{EngineState::EDITORMODE};

/**
* @brief engine clock
*/
Clock _clock{};

private:
void update();

public:
Expand All @@ -50,11 +57,11 @@ class Engine final : public PublicSingleton<Engine> {

void destroy();

Status loadProject(std::string_view const& path);
void loadProject(std::filesystem::path const& path);

[[nodiscard]] AbstractRenderer* getRenderer() const;

[[nodiscard]] Project* getOpenedProject() const;
[[nodiscard]] Project const* getOpenedProject() const;

[[nodiscard]] Scene* getScene() const;

Expand Down
5 changes: 5 additions & 0 deletions engine/src/runtime/engine_args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef TAIXUENGINE_RUNTIME_ENGINE_ARGS_HPP
#define TAIXUENGINE_RUNTIME_ENGINE_ARGS_HPP

#include "spdlog/common.h"
#include <argparse/argparse.hpp>
#include <common/base/public_singleton.hpp>
#include <runtime/management/graphics/render/render_api.hpp>
Expand Down Expand Up @@ -65,6 +66,10 @@ class EngineArgs : public PublicSingleton<EngineArgs> {

#ifdef NDEBUG
this->_is_debug = false;
spdlog::set_level(spdlog::level::info);// Set global log level to debug
#else
this->_is_debug = true;
spdlog::set_level(spdlog::level::debug);// Set global log level to info
#endif
}
};
Expand Down
24 changes: 24 additions & 0 deletions engine/src/runtime/resource/converted_data/project.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Created by xmmmmmovo on 2023/7/23.
//

#ifndef ENGINE_SRC_RUNTIME_RESOURCE_CONVERTED_DATA_PROJECT_HPP
#define ENGINE_SRC_RUNTIME_RESOURCE_CONVERTED_DATA_PROJECT_HPP

#include "runtime/resource/converted_data/world.hpp"
#include "runtime/resource/json/manifest_json.hpp"
#include <filesystem>
#include <string>
#include <vector>

namespace taixu {

struct Project {
std::filesystem::path current_path;
Manifest manifest;
std::vector<World> worlds;
};

}// namespace taixu

#endif// ENGINE_SRC_RUNTIME_RESOURCE_CONVERTED_DATA_PROJECT_HPP
14 changes: 14 additions & 0 deletions engine/src/runtime/resource/converted_data/world.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Created by xmmmmmovo on 2023/7/23.
//

#ifndef ENGINE_SRC_RUNTIME_RESOURCE_CONVERTED_DATA_WORLD_HPP
#define ENGINE_SRC_RUNTIME_RESOURCE_CONVERTED_DATA_WORLD_HPP

namespace taixu {

struct World {};

}// namespace taixu

#endif// ENGINE_SRC_RUNTIME_RESOURCE_CONVERTED_DATA_WORLD_HPP
6 changes: 3 additions & 3 deletions engine/src/runtime/resource/json/serializable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Created by xmmmmmovo on 2023/2/26.
//

#ifndef TAIXUENGINE_JSONABLE_HPP
#define TAIXUENGINE_JSONABLE_HPP
#ifndef TAIXUENGINE_SRC_RUNTIME_RESOURCE_SERIALIZABLE_HPP
#define TAIXUENGINE_SRC_RUNTIME_RESOURCE_SERIALIZABLE_HPP

#include <refl.hpp>

Expand All @@ -18,4 +18,4 @@ class ISerializableList : refl::attr::usage::field {};

}// namespace taixu

#endif// TAIXUENGINE_JSONABLE_HPP
#endif// TAIXUENGINE_SRC_RUNTIME_RESOURCE_SERIALIZABLE_HPP
43 changes: 43 additions & 0 deletions engine/src/runtime/resource/json/world_json.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Created by xmmmmmovo on 2023/7/23.
//

#ifndef ENGINE_SRC_RUNTIME_RESOURCE_JSON_WORLD_JSON_HPP
#define ENGINE_SRC_RUNTIME_RESOURCE_JSON_WORLD_JSON_HPP

#include <refl.hpp>

#include <vector>

#include "serializable.hpp"

namespace taixu {

struct World {
struct Level {
std::string level_name{};
std::string level_path{};
};

struct Global {
std::string render{};
};

std::string name{};
Global global{};
std::vector<Level> levels{};
};

}// namespace taixu

REFL_AUTO(type(taixu::World), field(name, taixu::ISerializableStr()),
field(global, taixu::ISerializableObject()),
field(levels, taixu::ISerializableList()))

REFL_AUTO(type(taixu::World::Level),
field(level_name, taixu::ISerializableStr()),
field(level_path, taixu::ISerializableStr()))

REFL_AUTO(type(taixu::World::Global), field(render, taixu::ISerializableStr()))

#endif// ENGINE_SRC_RUNTIME_RESOURCE_JSON_WORLD_JSON_HPP
15 changes: 7 additions & 8 deletions engine/src/runtime/resource/manager/asset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <vector>

#include "asset_manager.hpp"
#include <runtime/resource/helper/image_helper.hpp>
#include <runtime/resource/raw_data/material.hpp>
#include <runtime/resource/raw_data/mesh.hpp>
#include <runtime/resource/raw_data/texture.hpp>
Expand Down Expand Up @@ -235,8 +234,8 @@ Model* AssetManager::loadModel(std::filesystem::path const& root_path,
return nullptr;
}

if (_models.count(full_path.generic_string())) {
return &_models[full_path.generic_string()];
if (_models.count(full_path.generic_string().data())) {
return &_models[full_path.generic_string().data()];
}

Model ret_model{};
Expand All @@ -261,8 +260,8 @@ Model* AssetManager::loadModel(std::filesystem::path const& root_path,
processMaterial(scene, root_path, ret_model);
processNode(scene->mRootNode, scene, ret_model);

auto [model_ref, was_ins] =
_models.insert({full_path.generic_string(), std::move(ret_model)});
auto [model_ref, was_ins] = _models.insert(
{full_path.generic_string().data(), std::move(ret_model)});


return &model_ref->second;
Expand All @@ -279,12 +278,12 @@ AssetManager::loadTexture(std::filesystem::path const& root_path,
return nullptr;
}

if (_textures.count(full_path.generic_string())) {
return &_textures[full_path.generic_string()];
if (_textures.count(full_path.generic_string().data())) {
return &_textures[full_path.generic_string().data()];
}

auto [tex_ref, was_ins] = _textures.insert(
{full_path.generic_string(),
{full_path.generic_string().data(),
transferCPUTextureToGPU(root_path, relative_path, type)});
return &tex_ref->second;
}
Expand Down
14 changes: 3 additions & 11 deletions engine/src/runtime/resource/manager/asset_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "glm/gtc/type_ptr.hpp"
#include <runtime/platform/os/path.hpp>
#include <runtime/resource/raw_data/fbx_data.hpp>
#include <runtime/resource/raw_data/mesh.hpp>
#include <runtime/resource/raw_data/model.hpp>
#include <runtime/resource/raw_data/texture.hpp>
Expand All @@ -28,15 +27,13 @@ namespace taixu {

class AssetManager final {
private:
std::unordered_map<std::string_view, Texture2DAsset> _textures{};
std::unordered_map<std::string_view, Model> _models{};
std::unordered_map<std::string_view, FBXData> _fbx_files{};
std::unordered_map<char const*, Texture2DAsset> _textures{};
std::unordered_map<char const*, Model> _models{};

static Mesh processMesh(aiMesh* mesh);

void processNode(aiNode* node, aiScene const* scene, Model& model);
void processNode(aiNode* node, aiScene const* scene, Model& model,
FBXData* fbx);


void processMaterial(aiScene const* scene,
std::filesystem::path const& root_path, Model& model);
Expand All @@ -63,14 +60,9 @@ class AssetManager final {
std::filesystem::path const& relative_path,
std::function<void(Texture2DAsset*)> const& callback);

// for animation
FBXData* loadFBX(std::filesystem::path const& root_path,
std::filesystem::path const& relative_path);

void reset() {
_textures.clear();
_models.clear();
_fbx_files.clear();
}
};
}// namespace taixu
Expand Down
Loading

0 comments on commit 1abb956

Please sign in to comment.