Skip to content

Commit

Permalink
feat: relative texture paths
Browse files Browse the repository at this point in the history
Note: slightly refactored to not have paths saved and instead rely on
working directory
  • Loading branch information
BenMcAvoy committed Oct 14, 2024
1 parent f0da30e commit 28b51fa
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 40 deletions.
7 changes: 1 addition & 6 deletions engine/include/jenjin/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ class Manager {

void show_all(Jenjin::Scene *scene);

struct {
std::string projectPath;
std::string openScenePath;
std::string liveScenePath;
std::string scriptsPath;
} paths;
bool hasProjectOpen = false;

private:
Jenjin::GameObject *selectedObject = nullptr;
Expand Down
57 changes: 30 additions & 27 deletions engine/src/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ Manager::Manager() {}
void Manager::menu() {
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) {
if (!this->paths.projectPath.empty()) {
if (this->hasProjectOpen) {
if (ImGui::MenuItem("New Scene")) {
auto scene = std::make_shared<Jenjin::Scene>();
Jenjin::EngineRef->AddScene(scene, true);
}

if (ImGui::MenuItem("Open Scene")) {
Jenjin::EngineRef->GetCurrentScene()->Load(this->paths.openScenePath);
Jenjin::EngineRef->GetCurrentScene()->Load("main.jenscene");
}

if (ImGui::MenuItem("Save Scene")) {
Jenjin::EngineRef->GetCurrentScene()->Save(this->paths.openScenePath);
Jenjin::EngineRef->GetCurrentScene()->Save("main.jenscene");
}
}

Expand All @@ -52,13 +52,13 @@ void Manager::menu() {
ImGui::EndMenu();
}

if (!this->paths.openScenePath.empty()) {
if (this->hasProjectOpen) {
if (ImGui::BeginMenu("Scripts")) {
if (ImGui::MenuItem("Reload")) {
auto luaManager =
Jenjin::EngineRef->GetCurrentScene()->GetLuaManager();

luaManager->ReloadScripts(this->paths.projectPath + "/scripts/");
luaManager->ReloadScripts("scripts/");
luaManager->Ready();
}

Expand All @@ -72,25 +72,24 @@ void Manager::menu() {
auto scene = Jenjin::EngineRef->GetCurrentScene();

if (this->running) {
scene->Save(this->paths.liveScenePath);
scene->Save("live.jenscene");

Jenjin::EngineRef->GetCurrentScene()
->GetLuaManager()
->LoadDirectory(
(this->paths.projectPath + "/scripts/").c_str());
->LoadDirectory(("scripts/"));

spdlog::info("Readying lua manager");
scene->GetLuaManager()->Ready();
} else {
scene->Load(this->paths.liveScenePath);
scene->Load("live.jenscene");
}
}

ImGui::EndMenu();
}
}

auto toDisplay = this->paths.openScenePath;
auto toDisplay = fmt::format("FPS: {:.2f}", ImGui::GetIO().Framerate);
ImGui::SetCursorPosX(ImGui::GetWindowWidth() -
ImGui::CalcTextSize(toDisplay.c_str()).x - 10);
ImGui::Text("%s", toDisplay.c_str());
Expand Down Expand Up @@ -251,6 +250,11 @@ void Manager::hierarchy(Jenjin::Scene *scene) {
}
}

// Autoscroll if the user goes to the bottom
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
ImGui::SetScrollHereY(1.0f);
}

ImGui::End();
}

Expand Down Expand Up @@ -360,8 +364,7 @@ void Manager::explorer(Jenjin::Scene *scene) {
static auto title = ICON_FA_FOLDER " Explorer";
ImGui::Begin(title);

for (auto file :
std::filesystem::directory_iterator(this->paths.projectPath)) {
for (auto file : std::filesystem::directory_iterator(".")) {
if (file.is_directory()) {
bool shouldBeOpenByDefault =
file.path().filename().string() == "textures";
Expand All @@ -381,8 +384,7 @@ void Manager::explorer(Jenjin::Scene *scene) {
fmt::format("{} {}", ICON_FA_FILE_CODE,
subfile.path().filename().string());
if (ImGui::Selectable(script_text.c_str())) {
scene->GetLuaManager()->ReloadScripts(this->paths.projectPath +
"/scripts/");
scene->GetLuaManager()->ReloadScripts("scripts/");
scene->GetLuaManager()->Ready();
}
} else if (ext == ".png" || ext == ".jpg") {
Expand Down Expand Up @@ -436,7 +438,7 @@ void Manager::backup_prompts(Jenjin::Scene *scene) {
ImGui::Separator();

if (ImGui::Button("Save and Exit")) {
Jenjin::EngineRef->GetCurrentScene()->Save(this->paths.openScenePath);
Jenjin::EngineRef->GetCurrentScene()->Save("main.jenscene");
glfwSetWindowShouldClose(glfwGetCurrentContext(), GLFW_TRUE);
}

Expand Down Expand Up @@ -472,7 +474,7 @@ void Manager::show_all(Jenjin::Scene *scene) {
ImGui::PushStyleColor(ImGuiCol_TitleBgActive, red_bg);
}

if (this->paths.projectPath.empty()) {
if (!this->hasProjectOpen) {
menu();
dockspace();
welcome();
Expand Down Expand Up @@ -513,17 +515,16 @@ void Manager::welcome() {
auto open_project = [&]() {
auto file = std::string(selectedPreExisting);
auto jendir = Jenjin::Editor::get_jendir();
this->paths.projectPath = jendir + "/Projects/" + file;
this->paths.openScenePath = jendir + "/Projects/" + file + "/main.jenscene";
this->paths.liveScenePath = jendir + "/Projects/" + file + "/live.jenscene";
this->hasProjectOpen = true;

std::filesystem::current_path(jendir + "/Projects/" + file);
spdlog::info("Changed cwd to {}", std::filesystem::current_path().string());

Jenjin::EngineRef->GetCurrentScene()->GetGameObjects()->clear();
std::ifstream ifile(this->paths.openScenePath);
Jenjin::EngineRef->GetCurrentScene()->Load(this->paths.openScenePath);
Jenjin::EngineRef->GetCurrentScene()->Load("main.jenscene");

// Load all the lua files
Jenjin::EngineRef->GetCurrentScene()->GetLuaManager()->LoadDirectory(
(this->paths.projectPath + "/scripts/").c_str());
"scripts/");
};

for (auto file : std::filesystem::directory_iterator(
Expand Down Expand Up @@ -601,11 +602,13 @@ void Manager::welcome() {
Jenjin::Editor::ensure_dir(projectPath + "/scripts");
Jenjin::Editor::ensure_dir(projectPath + "/textures");

this->paths.projectPath = projectPath;
this->paths.openScenePath = projectPath + "/main.jenscene";
this->paths.liveScenePath = projectPath + "/live.jenscene";
this->hasProjectOpen = true;
std::filesystem::current_path(projectPath);
spdlog::info("Changed cwd to {}",
std::filesystem::current_path().string());

Jenjin::EngineRef->GetCurrentScene()->GetGameObjects()->clear();
Jenjin::EngineRef->GetCurrentScene()->Save(this->paths.openScenePath);
Jenjin::EngineRef->GetCurrentScene()->Save("main.jenscene");
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
Expand Down
21 changes: 19 additions & 2 deletions engine/src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,17 @@ void Scene::Save(std::ofstream &file) {
.name = {0}};

strncpy(gobj.name, go->name.c_str(), sizeof(gobj.name));
strncpy(gobj.texturePath, go->texturePath.c_str(),
sizeof(gobj.texturePath));
auto path = go->texturePath;
auto pos = path.find("textures/");
if (pos != std::string::npos) {
const char *text = path.c_str() + pos;
spdlog::debug("Texture path is in the textures directory: {}", text);
strncpy(gobj.texturePath, text, sizeof(gobj.texturePath));
} else {
spdlog::warn("Texture path is not in the textures directory: {}", path);
strncpy(gobj.texturePath, path.c_str(), sizeof(gobj.texturePath));
}

gobj.name[sizeof(gobj.name) - 1] = 0;
gobj.texturePath[sizeof(gobj.texturePath) - 1] = 0;

Expand All @@ -220,10 +229,18 @@ void Scene::Load(const std::string &path) {
void Scene::Load(std::ifstream &file) {
spdlog::trace("Scene::Load({})", (void *)&file);

file.seekg(0, std::ios::end);
int size = file.tellg();
file.seekg(0, std::ios::beg);

spdlog::debug("File size: {}", size);

this->gameObjects.clear();

for (GOBJSAVABLE gobj;
file.read(reinterpret_cast<char *>(&gobj), sizeof(GOBJSAVABLE));) {
spdlog::debug("Reading GOBJSAVABLE from file");

auto go = std::make_shared<GameObject>(
gobj.name, Jenjin::Helpers::CreateQuad(2.0f, 2.0f));
go->transform = gobj.transform;
Expand Down
12 changes: 7 additions & 5 deletions engine/src/targets/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>

#include <glm/gtc/type_ptr.hpp>
#include <IconsFontAwesome6.h>
#include <glm/gtc/type_ptr.hpp>

#include <GLFW/glfw3.h>

Expand Down Expand Up @@ -50,13 +50,14 @@ void EditorTarget::PreRender() {
void EditorTarget::Render() {
renderTexture.Unbind();

if (this->editor.paths.openScenePath.empty()) {
if (!this->editor.hasProjectOpen) {
return;
}

static auto title = VIEWPORT_TITLE;
static auto title = VIEWPORT_TITLE;
ImGui::Begin(title, nullptr,
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollWithMouse);

ImVec2 size = ImGui::GetContentRegionAvail();
this->width = size.x;
Expand Down Expand Up @@ -100,7 +101,8 @@ glm::vec2 EditorTarget::GetMousePosition() {
static double gx, gy;
glfwGetCursorPos(ctx, &gx, &gy);

auto v = ImGui::GetWindowPos(); // HACK: This is wrong!!! It doesn't get the viewport window pos.
auto v = ImGui::GetWindowPos(); // HACK: This is wrong!!! It doesn't get the
// viewport window pos.
auto lx = gx - v.x;
auto ly = gy - v.y;

Expand Down

0 comments on commit 28b51fa

Please sign in to comment.