try to change to working directory on startup so assets can be found

This commit is contained in:
Sven Balzer 2025-09-29 06:24:58 +02:00
parent c3c0f8adf7
commit 5a77a4aacb
4 changed files with 48 additions and 1 deletions

View File

@ -60,6 +60,7 @@ add_executable(mikemon
src/log.cpp
src/smol-atlas.cpp
src/math_graphics.cpp
src/change_directory.c
src/shaders/shaders.c
src/main.cpp
)

16
src/change_directory.c Normal file
View File

@ -0,0 +1,16 @@
#include "change_directory.h"
#include <SDL3/SDL_platform.h>
#if SDL_PLATFORM_LINUX
#include <unistd.h>
#elif SDL_PLATFORM_WINDOWS
#include <direct.h>
#endif
void change_directory(const char *path) {
#if SDL_PLATFORM_LINUX
chdir(path);
#elif SDL_PLATFORM_WINDOWS
_chdir(path);
#endif
}

14
src/change_directory.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef INCLUDE_CHANGE_DIRECTORY_H
#define INCLUDE_CHANGE_DIRECTORY_H
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void change_directory(const char *path);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // INCLUDE_CHANGE_DIRECTORY_H

View File

@ -15,12 +15,13 @@
#include "log.h"
#include "math_graphics.h"
#include "stb_image.h"
#include "change_directory.h"
#include "shaders/shaders.h"
using namespace M;
#define ASSETS_PATH "../assets/"
#define ASSETS_PATH "./assets/"
#define NEAR_PLANE (0.01f)
#define TILE_SIZE (32)
@ -1365,8 +1366,23 @@ WGPUSurface create_wgpu_surface_for_SDL_window(SDL_Window *window) {
return NULL;
}
void setup_working_directory() {
if (SDL_GetPathInfo(ASSETS_PATH, NULL)) return;
const char *current_directory = SDL_GetCurrentDirectory();
change_directory(SDL_GetBasePath());
if (SDL_GetPathInfo(ASSETS_PATH, NULL)) return;
change_directory("..");
if (SDL_GetPathInfo(ASSETS_PATH, NULL)) return;
change_directory(current_directory);
}
int main(int argc, char **argv) {
setup_memory_functions();
setup_working_directory();
#ifdef SDL_PLATFORM_LINUX
if (getenv("ENABLE_VULKAN_RENDERDOC_CAPTURE"))