Merge pull request #10 from FoxMoss/main

feat: allow cmake built executable to be run anywhere
This commit is contained in:
WSAL Evan 2024-10-20 10:52:23 -04:00 committed by GitHub
commit a34f2fd9a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 4 deletions

View File

@ -2,6 +2,13 @@
#include "imgui_impl_glfw.h" #include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h" #include "imgui_impl_opengl3.h"
#ifdef LINUX
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
@ -59,8 +66,39 @@ float rectangleVertices[] =
-1.0f, 1.0f, 0.0f, 1.0f -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;
resolved_length = i;
break;
}
}
char* assets_path = (char *)malloc(resolved_length + strlen("assets") + 2);
strcpy(assets_path, resolved_path);
strcpy(assets_path + resolved_length + 1, "assets");
struct stat path_stat;
if(stat(assets_path, &path_stat) == -1 || !S_ISDIR(path_stat.st_mode)) {
printf("%s: Asset directory not found\n", argv[0]);
exit(1);
}
free(assets_path);
chdir(resolved_path);
free(resolved_path);
#endif
// Initialize GLFW // Initialize GLFW
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -414,4 +452,4 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos)
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{ {
camera.ProcessMouseScroll(yoffset); camera.ProcessMouseScroll(yoffset);
} }

View File

@ -4,7 +4,11 @@ add_executable(scuffed_mc
Camera.cpp Planet.cpp WorldGen.cpp ../vendor/glad.c 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 # 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") set_target_properties(scuffed_mc PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")
target_link_libraries(scuffed_mc imgui $<IF:$<PLATFORM_ID:Windows>,glfw3,glfw>) target_link_libraries(scuffed_mc imgui $<IF:$<PLATFORM_ID:Windows>,glfw3,glfw>)