From ec70bb48acb4bc9cafafb6652ff4ec2e7af9f880 Mon Sep 17 00:00:00 2001 From: FoxMoss Date: Sat, 19 Oct 2024 13:03:39 -0500 Subject: [PATCH] feat: allow cmake built executable to be run anywhere --- ScuffedMinecraft/src/Application.cpp | 28 +++++++++++++++++++++++++--- ScuffedMinecraft/src/CMakeLists.txt | 6 +++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/ScuffedMinecraft/src/Application.cpp b/ScuffedMinecraft/src/Application.cpp index 953a117..e8fbf25 100644 --- a/ScuffedMinecraft/src/Application.cpp +++ b/ScuffedMinecraft/src/Application.cpp @@ -2,6 +2,10 @@ #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" +#ifdef LINUX +#include +#endif + #include #include #define STB_IMAGE_IMPLEMENTATION @@ -56,8 +60,26 @@ float rectangleVertices[] = -1.0f, 1.0f, 0.0f, 1.0f }; -int main() -{ +int main (int argc, char *argv[]) { +#ifdef LINUX + char* resolved_path = realpath(argv[0],NULL); + if (resolved_path == NULL) { + printf("%s: Please do not place binary in PATH\n", argv[0]); + exit(1); + } + size_t resolved_length = strlen(resolved_path); + + // remove executable from path + for (size_t i = resolved_length; i > 0; i--) { + if (resolved_path[i] == '/' && resolved_path[i+1] != 0) { + resolved_path[i+1] = 0; + break; + } + } + chdir(resolved_path); + free(resolved_path); +#endif + // Initialize GLFW glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); @@ -405,4 +427,4 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos) void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) { camera.ProcessMouseScroll(yoffset); -} \ No newline at end of file +} diff --git a/ScuffedMinecraft/src/CMakeLists.txt b/ScuffedMinecraft/src/CMakeLists.txt index 0c4a1d8..c7683e3 100644 --- a/ScuffedMinecraft/src/CMakeLists.txt +++ b/ScuffedMinecraft/src/CMakeLists.txt @@ -4,7 +4,11 @@ add_executable(scuffed_mc Camera.cpp Planet.cpp WorldGen.cpp ../vendor/glad.c ) +if(LINUX) + add_compile_definitions(LINUX) +endif() + # make sure the program runs in the right place from visual studio set_target_properties(scuffed_mc PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/bin") -target_link_libraries(scuffed_mc imgui $,glfw3,glfw>) \ No newline at end of file +target_link_libraries(scuffed_mc imgui $,glfw3,glfw>)