Fixed loading of tilesets and images from tilemaps to correctly use relative paths.
This commit is contained in:
67
src/util.c
67
src/util.c
@@ -1,5 +1,6 @@
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
@@ -9,6 +10,72 @@
|
||||
#include <akgl/heap.h>
|
||||
#include <akgl/registry.h>
|
||||
#include <akgl/game.h>
|
||||
#include <akgl/staticstring.h>
|
||||
|
||||
#include <akstdlib.h>
|
||||
|
||||
akerr_ErrorContext *akgl_path_relative_root(char *root, char *path, akgl_String *dst)
|
||||
{
|
||||
PREPARE_ERROR(e);
|
||||
akgl_String *pathbuf;
|
||||
akgl_String *strbuf;
|
||||
char *result;
|
||||
int rootlen;
|
||||
int pathlen;
|
||||
int count;
|
||||
|
||||
FAIL_ZERO_RETURN(e, root, AKERR_NULLPOINTER, "NULL argument");
|
||||
FAIL_ZERO_RETURN(e, path, AKERR_NULLPOINTER, "NULL argument");
|
||||
FAIL_ZERO_RETURN(e, dst, AKERR_NULLPOINTER, "NULL argument");
|
||||
|
||||
PASS(e, akgl_heap_next_string(&strbuf));
|
||||
PASS(e, akgl_heap_next_string(&pathbuf));
|
||||
|
||||
ATTEMPT {
|
||||
// Is it relative to the root?
|
||||
rootlen = strlen(root);
|
||||
pathlen = strlen(path);
|
||||
if ( (rootlen + pathlen) >= AKGL_MAX_STRING_LENGTH ) {
|
||||
FAIL_RETURN(e, AKERR_OUTOFBOUNDS, "Total path length (%d) is greater than maximum akgl_String length (%d)", (rootlen + pathlen), AKGL_MAX_STRING_LENGTH);
|
||||
}
|
||||
CATCH(e, aksl_sprintf(&count, (char *)&pathbuf->data, "%s/%s", root, path));
|
||||
CATCH(e, aksl_realpath((char *)&pathbuf->data, (char *)&strbuf->data));
|
||||
CATCH(e, akgl_string_copy(strbuf, dst, 0));
|
||||
} CLEANUP {
|
||||
IGNORE(akgl_heap_release_string(strbuf));
|
||||
IGNORE(akgl_heap_release_string(pathbuf));
|
||||
} PROCESS(e) {
|
||||
} FINISH(e, true);
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akgl_path_relative(char *root, char *path, akgl_String *dst)
|
||||
{
|
||||
PREPARE_ERROR(e);
|
||||
akgl_String *strbuf;
|
||||
char *result;
|
||||
|
||||
FAIL_ZERO_RETURN(e, root, AKERR_NULLPOINTER, "NULL argument");
|
||||
FAIL_ZERO_RETURN(e, path, AKERR_NULLPOINTER, "NULL argument");
|
||||
FAIL_ZERO_RETURN(e, dst, AKERR_NULLPOINTER, "NULL argument");
|
||||
|
||||
PASS(e, akgl_heap_next_string(&strbuf));
|
||||
|
||||
ATTEMPT {
|
||||
// Is path relative to our current working directory?
|
||||
CATCH(e, aksl_realpath(path, (char *)&strbuf->data));
|
||||
// Yes it is. strbuf->data contains the absolute path.
|
||||
CATCH(e, akgl_string_copy(strbuf, dst, 0));
|
||||
} CLEANUP {
|
||||
IGNORE(akgl_heap_release_string(strbuf));
|
||||
} PROCESS(e) {
|
||||
} HANDLE(e, ENOENT) {
|
||||
// Path is not relative to our current working directory
|
||||
// Noop - execution proceeds after the break
|
||||
return akgl_path_relative_root(root, path, dst);
|
||||
} FINISH(e, true);
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akgl_rectangle_points(RectanglePoints *dest, SDL_FRect *rect)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user