Skip to content

Commit 37b7d97

Browse files
committed
Handle project or model path as cli argument
1 parent 14765cc commit 37b7d97

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

include/core/Project.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Project {
3636
Config config;
3737

3838
static Project* get_current();
39-
static void load(std::filesystem::path);
39+
static Project* load(std::filesystem::path);
4040
void store();
4141

4242
FSCacheNode* get_fs_cache();

include/core/Scene.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct InstancedNode {
2727
Transform transform;
2828

2929
// Can be nullptr!
30-
Node const* node;
30+
Node const* node{nullptr};
3131

3232
glm::mat4 model_matrix{0.0f};
3333
std::vector<std::unique_ptr<InstancedNode>> children;

src/core/Project.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Project* Project::get_current()
5151
return current.get();
5252
}
5353

54-
void Project::load(std::filesystem::path path)
54+
Project* Project::load(std::filesystem::path path)
5555
{
5656
// Ugly workaround for `std::make_unique` not being able to access private constructors
5757
current = std::unique_ptr<Project>(new Project(path));
@@ -77,6 +77,8 @@ void Project::load(std::filesystem::path path)
7777
} catch (std::exception const& e) {
7878
std::cerr << e.what() << "\n";
7979
}
80+
81+
return current.get();
8082
}
8183

8284
Project::Project(std::filesystem::path root)

src/main.cpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <iostream>
2121

2222
auto framebuffer = Framebuffer::get_default(1920, 1080);
23+
auto focus_on_scene = false;
2324

2425
void glfw_error_callback([[maybe_unused]] int error, char const* description)
2526
{
@@ -53,7 +54,7 @@ void setup_dock_builder()
5354
}
5455
}
5556

56-
int main()
57+
int main(int argc, char** argv)
5758
{
5859
glfwSetErrorCallback(glfw_error_callback);
5960

@@ -102,40 +103,37 @@ int main()
102103
Input::init(window);
103104
AsyncTaskQueue::init();
104105

105-
// assuming we set the CWD to root
106-
// TODO: Replace with some kind of "Open Project" dialog
107-
auto path = std::filesystem::current_path() / "assets";
108-
std::cout << path.string().c_str() << std::endl;
109-
Project::load(path);
106+
auto input_path = std::filesystem::current_path();
110107

111-
auto project = Project::get_current();
112-
113-
// Instanciate the object if it didn't get loaded
114-
// TODO: Remove when objects can be added at runtime
115-
116-
// This is just some temporary workaround
117-
auto root_transform = Transform{
118-
.position = glm::vec3{0.0f, -15000.0f, -4000.0f},
119-
.orientation = glm::vec3{0.0f},
120-
.scale = glm::vec3{1.0f},
121-
};
122-
123-
auto scene = Node::create("scene", root_transform, NodeLocation::empty());
124-
125-
if (!project->scene) {
126-
auto model_path = path / "Models/TUD_Innenstadt.FBX";
127-
auto obj = project->get_model(model_path);
108+
if (argc == 2) {
109+
input_path = std::filesystem::path{argv[1]};
110+
if (input_path.is_relative()) {
111+
input_path = std::filesystem::canonical(std::filesystem::current_path() / input_path);
112+
}
113+
}
128114

129-
if (!obj) {
130-
std::abort();
115+
if (std::filesystem::is_directory(input_path)) {
116+
auto project = Project::load(input_path);
117+
if (!project->scene) {
118+
project->scene = std::make_unique<InstancedNode>();
131119
}
120+
} else if (std::filesystem::is_regular_file(input_path)) {
121+
auto project = Project::load(std::filesystem::current_path());
122+
if (!project->scene) {
123+
project->scene = std::make_unique<InstancedNode>();
124+
auto obj = project->get_model(input_path);
125+
if (obj) {
126+
project->scene->children.push_back(obj->instantiate());
127+
focus_on_scene = true;
128+
}
129+
}
130+
}
132131

133-
scene.children.push_back(*obj);
132+
// TODO: Add with some kind of "Open Project" dialog
134133

135-
project->scene = scene.instantiate();
136-
}
134+
auto project = Project::get_current();
137135

138-
if (!project->scene) {
136+
if (!project || !project->scene) {
139137
std::abort();
140138
}
141139

@@ -183,6 +181,11 @@ int main()
183181
performance_window.render(delta_time);
184182
#endif
185183

184+
if (focus_on_scene) {
185+
focus_on_scene = false;
186+
viewport_window.camera_controller().focus_on(*project->scene);
187+
}
188+
186189
ImGui::Render();
187190
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
188191

0 commit comments

Comments
 (0)