relative path for resource loading
This commit is contained in:
parent
a7175973b7
commit
a79e2ca571
123
inc/lzy.h
123
inc/lzy.h
|
@ -80,8 +80,8 @@ typedef struct LZY_Event {
|
|||
LZY_EventUnion u;
|
||||
} LZY_Event;
|
||||
|
||||
int LZY_Init(const char *title, int target_fps, const char *tset_path,
|
||||
const char *font_path);
|
||||
int LZY_Init(int argc, const char **argv, const char *title, int target_fps,
|
||||
const char *tset_path, const char *font_path);
|
||||
void LZY_Quit(void);
|
||||
int LZY_DrawBegin(void);
|
||||
int LZY_DrawEnd(void);
|
||||
|
@ -198,8 +198,8 @@ static unsigned int font_width, font_height;
|
|||
|
||||
static int timer_callback(volatile int *);
|
||||
|
||||
int LZY_Init(const char *title, int target_fps, const char *tset_path,
|
||||
const char *font_path) {
|
||||
int LZY_Init(int argc, const char **argv, const char *title, int target_fps,
|
||||
const char *tset_path, const char *font_path) {
|
||||
#ifdef LZY_GINT_TILESET
|
||||
extern bopti_image_t LZY_GINT_TILESET;
|
||||
tset_width = LZY_GINT_TILESET.width / LZY_TILE_SIZE;
|
||||
|
@ -210,6 +210,8 @@ int LZY_Init(const char *title, int target_fps, const char *tset_path,
|
|||
font_width = LZY_GINT_FONT.width / LZY_CHR_WIDTH;
|
||||
font_height = LZY_GINT_FONT.height / LZY_CHR_HEIGHT;
|
||||
#endif
|
||||
LZY_UNUSED(argc);
|
||||
LZY_UNUSED(argv);
|
||||
LZY_UNUSED(title);
|
||||
LZY_UNUSED(tset_path);
|
||||
LZY_UNUSED(font_path);
|
||||
|
@ -431,7 +433,10 @@ const char *LZY_GetError(void) {
|
|||
#include LZY_SDL_INCLUDE
|
||||
#include LZY_SDL_IMAGE_INCLUDE
|
||||
#include LZY_SDL_MIXER_INCLUDE
|
||||
#include <libgen.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static const SDL_Scancode sc[LZYK_COUNT] = {
|
||||
SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP,
|
||||
|
@ -449,10 +454,61 @@ static int input[LZYK_COUNT] = {0};
|
|||
static int fps_limiter = 0;
|
||||
static uint64_t min_dt;
|
||||
static uint64_t next_time;
|
||||
static char *datadir;
|
||||
|
||||
int LZY_Init(const char *title, int target_fps, const char *tset_path,
|
||||
const char *font_path) {
|
||||
static char *str_dup(const char *src) {
|
||||
char *copy;
|
||||
|
||||
if (src == NULL) {
|
||||
error = "src is NULL";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
copy = calloc(strlen(src) + 1, sizeof(char));
|
||||
if (copy == NULL) {
|
||||
error = "calloc failed";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(copy, src);
|
||||
return copy;
|
||||
}
|
||||
|
||||
static char *path_relative(const char *path) {
|
||||
char *buf;
|
||||
|
||||
if (datadir == NULL) {
|
||||
error = "datadir is NULL";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (path == NULL) {
|
||||
error = "path is NULL";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buf = calloc(strlen(datadir) + strlen(path) + 2, sizeof(char));
|
||||
if (buf == NULL) {
|
||||
error = "calloc failed";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(buf, datadir);
|
||||
strcat(buf, "/");
|
||||
strcat(buf, path);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
int LZY_Init(int argc, const char **argv, const char *title, int target_fps,
|
||||
const char *tset_path, const char *font_path) {
|
||||
const int img_flags = IMG_INIT_PNG;
|
||||
char *buf;
|
||||
|
||||
datadir = str_dup((argc < 1) ? ("./rl") : (argv[0]));
|
||||
if (datadir == NULL)
|
||||
return -1;
|
||||
dirname(datadir);
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
|
||||
error = SDL_GetError();
|
||||
|
@ -461,12 +517,12 @@ int LZY_Init(const char *title, int target_fps, const char *tset_path,
|
|||
|
||||
if ((IMG_Init(img_flags) & img_flags) != img_flags) {
|
||||
error = IMG_GetError();
|
||||
return -2;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
|
||||
error = Mix_GetError();
|
||||
return -3;
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0);
|
||||
|
@ -476,7 +532,7 @@ int LZY_Init(const char *title, int target_fps, const char *tset_path,
|
|||
LZY_DISPLAY_HEIGHT, SDL_WINDOW_RESIZABLE);
|
||||
if (window == NULL) {
|
||||
error = SDL_GetError();
|
||||
return -4;
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_SetWindowMinimumSize(window, LZY_DISPLAY_WIDTH, LZY_DISPLAY_HEIGHT);
|
||||
|
@ -484,7 +540,7 @@ int LZY_Init(const char *title, int target_fps, const char *tset_path,
|
|||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (renderer == NULL) {
|
||||
error = SDL_GetError();
|
||||
return -5;
|
||||
return -1;
|
||||
}
|
||||
|
||||
target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888,
|
||||
|
@ -492,31 +548,39 @@ int LZY_Init(const char *title, int target_fps, const char *tset_path,
|
|||
LZY_DISPLAY_HEIGHT);
|
||||
if (target == NULL) {
|
||||
error = SDL_GetError();
|
||||
return -6;
|
||||
return -1;
|
||||
}
|
||||
|
||||
tset = IMG_LoadTexture(renderer, tset_path);
|
||||
buf = path_relative(tset_path);
|
||||
if (buf == NULL)
|
||||
return -1;
|
||||
tset = IMG_LoadTexture(renderer, buf);
|
||||
if (tset == NULL) {
|
||||
error = IMG_GetError();
|
||||
return -7;
|
||||
return -1;
|
||||
}
|
||||
free(buf);
|
||||
|
||||
if (SDL_QueryTexture(tset, NULL, NULL, &tset_width, &tset_height) < 0) {
|
||||
error = SDL_GetError();
|
||||
return -8;
|
||||
return -1;
|
||||
}
|
||||
tset_width /= LZY_TILE_SIZE;
|
||||
tset_height /= LZY_TILE_SIZE;
|
||||
|
||||
font = IMG_LoadTexture(renderer, font_path);
|
||||
buf = path_relative(font_path);
|
||||
if (buf == NULL)
|
||||
return -1;
|
||||
font = IMG_LoadTexture(renderer, buf);
|
||||
if (font == NULL) {
|
||||
error = IMG_GetError();
|
||||
return -9;
|
||||
return -1;
|
||||
}
|
||||
free(buf);
|
||||
|
||||
if (SDL_QueryTexture(font, NULL, NULL, &font_width, &font_height) < 0) {
|
||||
error = SDL_GetError();
|
||||
return -10;
|
||||
return -1;
|
||||
}
|
||||
font_width /= LZY_CHR_WIDTH;
|
||||
font_height /= LZY_CHR_HEIGHT;
|
||||
|
@ -531,6 +595,11 @@ int LZY_Init(const char *title, int target_fps, const char *tset_path,
|
|||
}
|
||||
|
||||
void LZY_Quit(void) {
|
||||
if (datadir != NULL) {
|
||||
free(datadir);
|
||||
datadir = NULL;
|
||||
}
|
||||
|
||||
if (tset != NULL) {
|
||||
SDL_DestroyTexture(tset);
|
||||
tset = NULL;
|
||||
|
@ -741,9 +810,17 @@ int LZY_DrawText(const char *text, int x, int y) {
|
|||
}
|
||||
|
||||
LZY_Music *LZY_MusicLoad(const char *path) {
|
||||
LZY_Music *const music = Mix_LoadMUS(path);
|
||||
char *buf;
|
||||
LZY_Music *music;
|
||||
|
||||
buf = path_relative(path);
|
||||
if (buf == NULL)
|
||||
return NULL;
|
||||
music = Mix_LoadMUS(buf);
|
||||
if (music == NULL)
|
||||
error = Mix_GetError();
|
||||
free(buf);
|
||||
|
||||
return music;
|
||||
}
|
||||
|
||||
|
@ -771,9 +848,17 @@ int LZY_MusicPlay(LZY_Music *music, int loops) {
|
|||
}
|
||||
|
||||
LZY_Sound *LZY_SoundLoad(const char *path) {
|
||||
LZY_Sound *const sound = Mix_LoadWAV(path);
|
||||
char *buf;
|
||||
LZY_Sound *sound;
|
||||
|
||||
buf = path_relative(path);
|
||||
if (buf == NULL)
|
||||
return NULL;
|
||||
sound = Mix_LoadWAV(buf);
|
||||
if (sound == NULL)
|
||||
error = Mix_GetError();
|
||||
free(buf);
|
||||
|
||||
return sound;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,13 +8,14 @@
|
|||
|
||||
static void draw_player(int x, int y);
|
||||
|
||||
int main(void) {
|
||||
int main(int argc, const char **argv) {
|
||||
LZY_Music *music;
|
||||
LZY_Sound *sound;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
||||
if (LZY_Init("lzy example", 30, "res/tset.png", "res/font.png")) {
|
||||
if (LZY_Init(argc, argv, "lzy example", 30, "res/tset.png",
|
||||
"res/font.png")) {
|
||||
LZY_Log(LZY_GetError());
|
||||
LZY_Quit();
|
||||
return 1;
|
||||
|
|
Loading…
Reference in New Issue