added project loader

This commit is contained in:
2026-05-14 23:16:16 -04:00
parent 2dd5f3a648
commit 0ceea230cf
10 changed files with 134 additions and 30 deletions

6
.gitmodules vendored
View File

@@ -20,3 +20,9 @@
[submodule "_ThirdParty/fmt"]
path = _ThirdParty/fmt
url = https://github.com/fmtlib/fmt.git
[submodule "_ThirdParty/json"]
path = _ThirdParty/json
url = https://github.com/nlohmann/json.git
[submodule "_ThirdParty/nfd"]
path = _ThirdParty/nfd
url = https://github.com/btzy/nativefiledialog-extended.git

View File

@@ -12,8 +12,23 @@ set (GLFW_BUILD_TESTS OFF)
set (GLFW_BUILD_DOCS OFF)
add_subdirectory(_ThirdParty/glfw)
add_subdirectory(_ThirdParty/glm)
include_directories(src/imgui)
file(GLOB_RECURSE IMGUI_SOURCES src/imgui/*.cpp)
file(GLOB_RECURSE SOURCES ${CMAKE_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE HEADERS ${CMAKE_SOURCE_DIR}/src/*.h)
add_subdirectory(_ThirdParty/entt)
add_subdirectory(_ThirdParty/fmt)
add_subdirectory(_ThirdParty/spdlog)
set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(_ThirdParty/json)
add_subdirectory(_ThirdParty/nfd)
set (ASSIMP_INSTALL OFF)
if(UNIX)
find_path(UNZIP_PATH unzip.h PATHS
@@ -29,23 +44,13 @@ target_compile_options(assimp PRIVATE
-Wno-unused-but-set-variable
)
add_subdirectory(_ThirdParty/glm)
add_subdirectory(_ThirdParty/entt)
add_subdirectory(_ThirdParty/fmt)
include_directories(src/imgui)
file(GLOB_RECURSE IMGUI_SOURCES src/imgui/*.cpp)
file(GLOB_RECURSE SOURCES ${CMAKE_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE HEADERS ${CMAKE_SOURCE_DIR}/src/*.h)
add_executable(${PROJECT_NAME}
${IMGUI_SOURCES}
${GLAD_SOURCE_DIR}/gl.c
${SOURCES}
${HEADERS})
${HEADERS}
src/ProjectManifestFile.cpp
src/ProjectManifestFile.h)
add_custom_target(copy_resources
COMMAND ${CMAKE_COMMAND} -E copy_directory
@@ -54,6 +59,7 @@ add_custom_target(copy_resources
COMMENT "Copying resources..."
)
target_link_libraries(${PROJECT_NAME} glfw spdlog assimp glm EnTT fmt)
target_link_libraries(${PROJECT_NAME} glfw spdlog assimp glm EnTT fmt nfd)
target_include_directories(${PROJECT_NAME} PRIVATE ${ASSIMP_INCLUDE_INSTALL_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE src)
target_include_directories(${PROJECT_NAME} PRIVATE _ThirdParty/json/single_include)

1
_ThirdParty/json vendored Submodule

Submodule _ThirdParty/json added at 630beaeb05

1
_ThirdParty/nfd vendored Submodule

Submodule _ThirdParty/nfd added at c9cc7baf28

View File

@@ -5,9 +5,12 @@
#ifndef B_ENGINE_PROJECTMANIFEST_H
#define B_ENGINE_PROJECTMANIFEST_H
struct ProjectManifest
{
#include <string>
};
typedef struct
{
std::string name;
std::string assetFolder;
} ProjectManifest;
#endif //B_ENGINE_PROJECTMANIFEST_H

View File

@@ -0,0 +1,38 @@
//
// Created by slinky on 5/14/26.
//
#include "ProjectManifestFile.h"
#include <fstream>
#include "spdlog/spdlog.h"
#include "nlohmann/json.hpp"
using json = nlohmann::json;
std::unique_ptr<ProjectManifest> ProjectManifestFile::load(std::string_view _path) {
std::ifstream file;
file.open(_path.data(), std::ios::in);
if (!file.is_open()) {
spdlog::error("failed to open project file {}", _path);
return nullptr;
}
std::stringstream ss;
ss << file.rdbuf();
std::string fileData = ss.str();
auto data = json::parse(fileData);
ProjectManifest manifest;
manifest.name = data.at("name").get<std::string>();
manifest.assetFolder = data.at("assetFolder").get<std::string>();
return std::make_unique<ProjectManifest>(manifest);
}
bool ProjectManifestFile::save(std::string_view _path, ProjectManifest *_project) {
return true;
}

19
src/ProjectManifestFile.h Normal file
View File

@@ -0,0 +1,19 @@
//
// Created by slinky on 5/14/26.
//
#ifndef B_ENGINE_PROJECTMANIFESTFILE_H
#define B_ENGINE_PROJECTMANIFESTFILE_H
#include <string_view>
#include <memory>
#include "ProjectManifest.h"
class ProjectManifestFile {
public:
static std::unique_ptr<ProjectManifest> load(std::string_view _path);
static bool save(std::string_view _path, ProjectManifest* _project);
};
#endif //B_ENGINE_PROJECTMANIFESTFILE_H

View File

@@ -15,6 +15,8 @@
#include "FreeCameraController.h"
#include "InputManager.h"
#include "ModelManager.h"
#include "ProjectManifest.h"
#include "ProjectManifestFile.h"
#include "Scene.h"
#include "ShaderManager.h"
#include "ShaderProgram.h"
@@ -26,6 +28,8 @@
#include "ui/UISceneGraph.h"
#include "ui/UISceneViewer.h"
const char* B_ENGINE_VERSION = "v0.0.3";
GLFWwindow* gWindow = nullptr;
int gWindowWidth = 1920;
int gWindowHeight = 1080;
@@ -37,6 +41,8 @@ float gAspectRatio = 0.f;
float gMouseSensitivity = 0.1f;
std::unique_ptr<ProjectManifest> gCurrentProject {nullptr};
Camera gCamera {};
Scene gScene{};
EditorContext gEditorCtx{};
@@ -58,13 +64,14 @@ void load_default_models();
void loop();
void create_entity(entt::entity parent);
void open_project(std::string_view _path);
auto xAxis = glm::vec3{1.f, 0.f, 0.f};
auto yAxis = glm::vec3{0.f, 1.f, 0.f};
auto zAxis = glm::vec3{0.f, 0.f, 1.f};
int main() {
spdlog::info("b_engine v0.0.3 start");
spdlog::info("b_engine {} start", B_ENGINE_VERSION);
init_glfw();
@@ -83,8 +90,9 @@ int main() {
load_default_models();
UIManager::init(gWindow);
UIMenuBar* menuBar = UIManager::add_ui_panel<UIMenuBar>("menu_bar");
auto* menuBar = UIManager::add_ui_panel<UIMenuBar>("menu_bar");
menuBar->create_entity = create_entity;
menuBar->open_project = open_project;
UIManager::add_ui_panel<UISceneViewer>("scene_viewer");
UIManager::add_ui_panel<UISceneGraph>("scene_graph");
UIManager::add_ui_panel<UIEntityInspector>("entity_inspector");
@@ -142,7 +150,8 @@ void init_glfw()
void create_main_window()
{
gWindow = glfwCreateWindow(gWindowWidth, gWindowHeight, "b_engine v0.0.3", nullptr, nullptr);
std::string title = fmt::format("b_engine {}", B_ENGINE_VERSION);
gWindow = glfwCreateWindow(gWindowWidth, gWindowHeight, title.c_str(), nullptr, nullptr);
if (!gWindow) {
spdlog::error("failed to create glfw window");
std::exit(1);
@@ -272,10 +281,12 @@ void loop() {
}
void create_entity(entt::entity parent) {
auto entity = gScene.create_game_object(parent);
auto& drawable = gScene.attach_component<Components::Drawable>(entity);
drawable.model = ModelManager::models["vette"];
auto& transform = gScene.fetch_component<Components::Transform>(entity);
transform.scale = {0.1, 0.1, 0.1};
transform.dirty = true;
gScene.create_game_object(parent);
}
void open_project(std::string_view _path) {
gCurrentProject = ProjectManifestFile::load(_path);
const std::string title = fmt::format("project \"{}\" | b_engine {}", gCurrentProject->name, B_ENGINE_VERSION);
glfwSetWindowTitle(gWindow, title.c_str());
}

View File

@@ -4,16 +4,34 @@
#include "UIMenuBar.h"
#include "UIManager.h"
#include "UISceneGraph.h"
#include "imgui.h"
#include "UISceneGraph.h"
#include "nfd.h"
void UIMenuBar::update(EditorContext &context) {
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("New Scene")) {
if (ImGui::MenuItem("New Project")) {
}
if (ImGui::MenuItem("Open Scene")) {
if (ImGui::MenuItem("Open Project")) {
NFD_Init();
nfdu8char_t *outPath;
nfdu8filteritem_t filters[1] = { { "b_engine project files", "json" } };
nfdopendialogu8args_t args = {0};
args.filterList = filters;
args.filterCount = 1;
nfdresult_t result = NFD_OpenDialogU8_With(&outPath, &args);
if (result == NFD_OKAY && open_project)
{
open_project(outPath);
NFD_FreePathU8(outPath);
}
NFD_Quit();
}
ImGui::Separator(); // Adds a visual line between sections
if (ImGui::MenuItem("Save", "Ctrl+S")) {

View File

@@ -12,6 +12,7 @@ public:
void update(EditorContext &context) override;
std::function<void(entt::entity)> create_entity;
std::function<void(std::string_view path)> open_project;
};