mirror of
https://github.com/ScrelliCopter/Lesson10-SDL3.git
synced 2025-02-21 07:19:26 +11:00
copy data & load from target or bundle resource folder
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -8,6 +8,7 @@ build/
|
|||||||
winbuild/
|
winbuild/
|
||||||
xcode/
|
xcode/
|
||||||
cmake-build-*/
|
cmake-build-*/
|
||||||
|
build-*/
|
||||||
|
|
||||||
Thumbs.db
|
Thumbs.db
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|||||||
@@ -4,10 +4,15 @@ project(Lesson10 LANGUAGES C)
|
|||||||
find_package(SDL3 REQUIRED CONFIG)
|
find_package(SDL3 REQUIRED CONFIG)
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
|
|
||||||
add_executable(Lesson10 Lesson10.c)
|
set(SOURCES Lesson10.c)
|
||||||
set_target_properties(Lesson10 PROPERTIES
|
|
||||||
C_STANDARD 99
|
set(DATA
|
||||||
WIN32_EXECUTABLE ON)
|
Data/Mud.bmp
|
||||||
|
Data/World.txt)
|
||||||
|
|
||||||
|
add_executable(Lesson10 WIN32 MACOSX_BUNDLE ${SOURCES} ${DATA})
|
||||||
|
set_property(TARGET Lesson10 PROPERTY C_STANDARD 99)
|
||||||
|
source_group("Data\\Random" FILES ${DATA})
|
||||||
target_link_libraries(Lesson10 SDL3::SDL3 OpenGL::GL)
|
target_link_libraries(Lesson10 SDL3::SDL3 OpenGL::GL)
|
||||||
target_compile_options(Lesson10 PRIVATE $<$<C_COMPILER_ID:GNU,Clang,AppleClang>:-Wall -Wextra -pedantic>)
|
target_compile_options(Lesson10 PRIVATE $<$<C_COMPILER_ID:GNU,Clang,AppleClang>:-Wall -Wextra -pedantic>)
|
||||||
target_compile_definitions(Lesson10 PRIVATE $<$<C_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>)
|
target_compile_definitions(Lesson10 PRIVATE $<$<C_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>)
|
||||||
@@ -17,4 +22,14 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
|
|||||||
if (SDL3_IMPORTED_LOCATION MATCHES "^/Library/Frameworks/")
|
if (SDL3_IMPORTED_LOCATION MATCHES "^/Library/Frameworks/")
|
||||||
set_property(TARGET Lesson10 PROPERTY BUILD_RPATH "/Library/Frameworks")
|
set_property(TARGET Lesson10 PROPERTY BUILD_RPATH "/Library/Frameworks")
|
||||||
endif()
|
endif()
|
||||||
|
foreach (RESOURCE IN LISTS DATA)
|
||||||
|
set_source_files_properties("${RESOURCE}" PROPERTIES MACOSX_PACKAGE_LOCATION "Resources/Data")
|
||||||
|
endforeach()
|
||||||
|
else()
|
||||||
|
add_custom_command(TARGET Lesson10 POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||||
|
"${CMAKE_SOURCE_DIR}/Data" "$<TARGET_FILE_DIR:Lesson10>/Data")
|
||||||
|
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
||||||
|
add_custom_command(TARGET Lesson10 POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||||
|
$<TARGET_FILE:SDL3::SDL3> $<TARGET_FILE_DIR:Lesson10>)
|
||||||
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
48
Lesson10.c
48
Lesson10.c
@@ -75,6 +75,8 @@ typedef struct tagAPPSTATE
|
|||||||
SDL_Window *win;
|
SDL_Window *win;
|
||||||
SDL_GLContext ctx;
|
SDL_GLContext ctx;
|
||||||
|
|
||||||
|
const char *resdir;
|
||||||
|
|
||||||
bool fullscreen, blend;
|
bool fullscreen, blend;
|
||||||
|
|
||||||
CAMERA camera;
|
CAMERA camera;
|
||||||
@@ -84,6 +86,36 @@ typedef struct tagAPPSTATE
|
|||||||
SECTOR sector1;
|
SECTOR sector1;
|
||||||
} APPSTATE;
|
} APPSTATE;
|
||||||
|
|
||||||
|
static char * resourcePath(const APPSTATE *restrict state, const char *restrict name)
|
||||||
|
{
|
||||||
|
if (!state || !name)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
size_t resdirLen = strlen(state->resdir), nameLen = strlen(name);
|
||||||
|
char *path = malloc(resdirLen + nameLen + 1);
|
||||||
|
if (!path)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memcpy(path, state->resdir, resdirLen);
|
||||||
|
memcpy(&path[resdirLen], name, nameLen);
|
||||||
|
path[resdirLen + nameLen] = '\0';
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
static FILE * fopenResource(const APPSTATE *restrict state, const char *restrict name, const char* restrict mode)
|
||||||
|
{
|
||||||
|
char *path = NULL;
|
||||||
|
if (!mode || !(path = resourcePath(state, name)))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
FILE *f = fopen(path, mode);
|
||||||
|
free(path);
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
static void readstr(FILE *f, char *string)
|
static void readstr(FILE *f, char *string)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
@@ -99,7 +131,7 @@ static void SetupWorld(APPSTATE *state)
|
|||||||
int numtriangles;
|
int numtriangles;
|
||||||
FILE *filein;
|
FILE *filein;
|
||||||
char oneline[255];
|
char oneline[255];
|
||||||
filein = fopen("Data/World.txt", "r"); // File to load world data from
|
filein = fopenResource(state, "Data/World.txt", "r"); // File to load world data from
|
||||||
|
|
||||||
readstr(filein, oneline);
|
readstr(filein, oneline);
|
||||||
sscanf(oneline, "NUMPOLLIES %d\n", &numtriangles);
|
sscanf(oneline, "NUMPOLLIES %d\n", &numtriangles);
|
||||||
@@ -155,13 +187,19 @@ static bool FlipSurface(SDL_Surface *surface)
|
|||||||
|
|
||||||
static bool LoadGLTextures(APPSTATE *state)
|
static bool LoadGLTextures(APPSTATE *state)
|
||||||
{
|
{
|
||||||
SDL_Surface *TextureImage = NULL;
|
|
||||||
|
|
||||||
// Load & flip the bitmap
|
// Load & flip the bitmap
|
||||||
if (!(TextureImage = SDL_LoadBMP("Data/Mud.bmp")) || !FlipSurface(TextureImage))
|
char *path = resourcePath(state, "Data/Mud.bmp");
|
||||||
|
if (!path)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
SDL_Surface *TextureImage = SDL_LoadBMP(path);
|
||||||
|
free(path);
|
||||||
|
if (!TextureImage || !FlipSurface(TextureImage))
|
||||||
|
{
|
||||||
|
SDL_DestroySurface(TextureImage);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
glGenTextures(3, &state->texture[0]); // Create three textures
|
glGenTextures(3, &state->texture[0]); // Create three textures
|
||||||
GLint params[3][3] =
|
GLint params[3][3] =
|
||||||
@@ -555,6 +593,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
|||||||
.win = NULL,
|
.win = NULL,
|
||||||
.ctx = NULL,
|
.ctx = NULL,
|
||||||
|
|
||||||
|
.resdir = SDL_GetBasePath(),
|
||||||
|
|
||||||
.fullscreen = false,
|
.fullscreen = false,
|
||||||
.blend = false, // Blending off
|
.blend = false, // Blending off
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user