diff options
Diffstat (limited to 'src/loader.c')
-rw-r--r-- | src/loader.c | 233 |
1 files changed, 233 insertions, 0 deletions
diff --git a/src/loader.c b/src/loader.c new file mode 100644 index 0000000..7a3d328 --- /dev/null +++ b/src/loader.c @@ -0,0 +1,233 @@ +#include "loader.h" +#include "utils.h" /* mpl_check */ + +#include <SDL_image.h> +#if ! SDL_VERSION_ATLEAST(2,0,0) +#error "This code is only for SDL 2+. No backward compatibility with previous SDL versions, sorry." +#endif + +SDL_Texture * my_sdl_load_texture(SDL_Renderer *rend, char *giffilepath, SDL_Rect *size); + +int loadRessources(gameIni_t *gIni, char databasepath[], gameRess_t *gRess) { + return 1; //FIXME +} + +#if 0 +int loadRessources(gameIni_t *gIni, gameRess_t *gRess) { + int i, res, filenamelen; + char *filepath; + + filenamelen = sizeof(char)*(strlen(PATH_STYLE)+2*strlen(gIni->style.name)+strlen("//om_00.gif")+1); + filepath = malloc(filenamelen); + + gRess->style.tiles=malloc(sizeof(SDL_Surface *)*gIni->style.tiles); + if (gRess->style.tiles==NULL) { + logp(LOG_ERROR, "loadRessources(), malloc()"); + return 1; + } + for(i=0; i < gIni->style.tiles ; ++i) { + snprintf(filepath, filenamelen, "%s/%s/%s_%d.gif", PATH_STYLE, gIni->style.name, gIni->style.name, i); + gRess->style.tiles[i] = loadGif(filepath); + if(gRess->style.tiles[i]==NULL) { + logs2(LOG_WARN, "loadRessources(), loadGif() error for ", filepath); + return 2; + } + } + + gRess->style.objects=malloc(sizeof(SDL_Surface *)*gIni->style.objectCount); + if (gRess->style.objects==NULL) { + logp(LOG_ERROR, "loadRessources(), malloc()"); + return 1; + } + + gRess->style.objectMasks=malloc(sizeof(SDL_Surface *)*gIni->style.objectCount); + if (gRess->style.objectMasks==NULL) { + logp(LOG_ERROR, "loadRessources(), malloc()"); + return 1; + } + + + for(i=0; i < gIni->style.objectCount ; ++i) { + snprintf(filepath, filenamelen, "%s/%s/%so_%d.gif", PATH_STYLE, gIni->style.name, gIni->style.name, i); + gRess->style.objects[i] = loadGif(filepath); + if (gRess->style.objects[i]==NULL) { + logs2(LOG_WARN, "loadRessources(), loadGif() error for ", filepath); + return 2; + } + + switch(gIni->style.objects[i].type) { + case 5: + case 6: + case 7: + case 8: + snprintf(filepath, filenamelen, "%s/%s/%som_%d.gif", PATH_STYLE, gIni->style.name, gIni->style.name, i); + // ... cheater !! I'm not cheating, I just want KISS (Keep It Simple and Stupid) + gRess->style.objectMasks[i] = loadGif(filepath); + if (gRess->style.objectMasks[i]==NULL) { + logs2(LOG_WARN, "loadRessources(), loadGif() error for ", filepath); + return 2; + } + break; + default: + /* No mask for other types */ + break; + } + } + + free(filepath); + + res=_loadMiscRessources(gIni, gRess); + + return res; +} + +int _loadMiscRessources(gameIni_t *gIni, gameRess_t *gRess) { + int i, filenamelen; + char *filepath; + + filenamelen = sizeof(char)*(strlen(PATH_MISC)+16); + filepath = malloc(filenamelen); + + gRess->misc.lemmingAnims=malloc(sizeof(SDL_Surface *)*gIni->misc.lemmingAnimCount); + if (gRess->misc.lemmingAnims==NULL) { + logp(LOG_ERROR, "_loadMiscRessources(), malloc()"); + return 1; + } + + gRess->misc.lemmingMasks=malloc(sizeof(SDL_Surface *)*gIni->misc.lemmingAnimCount); + if (gRess->misc.lemmingMasks==NULL) { + logp(LOG_ERROR, "_loadMiscRessources(), malloc()"); + return 1; + } + + gRess->misc.lemmingImasks=malloc(sizeof(SDL_Surface *)*gIni->misc.lemmingAnimCount); + if (gRess->misc.lemmingImasks==NULL) { + logp(LOG_ERROR, "_loadMiscRessources(), malloc()"); + return 1; + } + + for(i=0; i < gIni->misc.lemmingAnimCount ; ++i) { + snprintf(filepath, filenamelen, "%s/lemm_%d.gif", PATH_MISC, i); + gRess->misc.lemmingAnims[i] = loadGif(filepath); + if (gRess->misc.lemmingAnims[i]==NULL) { + logs2(LOG_WARN, "_loadMiscRessources(), loadGif() error for ", filepath); + return 2; + } + + if ( gIni->misc.lemmingAnims[i].haveImask == 1 ) { + snprintf(filepath, filenamelen, "%s/mask_%d.gif", PATH_MISC, i); + gRess->misc.lemmingImasks[i] = loadGif(filepath); + if (gRess->misc.lemmingImasks[i]==NULL) { + logs2(LOG_WARN, "_loadMiscRessources(), loadGif() error for ", filepath); + return 2; + } + } + + if ( gIni->misc.lemmingAnims[i].haveMask == 1 ) { + snprintf(filepath, filenamelen, "%s/mask_%d.gif", PATH_MISC, i); + gRess->misc.lemmingImasks[i] = loadGif(filepath); + if (gRess->misc.lemmingImasks[i]==NULL) { + logs2(LOG_WARN, "_loadMiscRessources(), loadGif() error for ", filepath); + return 2; + } + } + } + + gRess->misc.icons=malloc(sizeof(SDL_Surface *)*MISC_ICON_COUNT); + if (gRess->misc.icons==NULL) { + logp(LOG_ERROR, "_loadMiscRessources(), malloc()"); + return 1; + } + + for(i=0; i < MISC_ICON_COUNT ; ++i) { + snprintf(filepath, filenamelen, "%s/icon_%d.gif", PATH_MISC, i); + gRess->misc.icons[i] = loadGif(filepath); + if (gRess->misc.icons[i]==NULL) { + logs2(LOG_WARN, "_loadMiscRessources(), loadGif() error for ", filepath); + return 2; + } + } + + snprintf(filepath, filenamelen, "%s/lemmfont.gif", PATH_MISC); + gRess->misc.font1 = loadGif(filepath); + if (gRess->misc.font1==NULL) { + logs2(LOG_WARN, "_loadMiscRessources(), loadGif() error for ", filepath); + return 2; + } + + snprintf(filepath, filenamelen, "%s/numfont.gif", PATH_MISC); + gRess->misc.font2 = loadGif(filepath); + if (gRess->misc.font2==NULL) { + logs2(LOG_WARN, "_loadMiscRessources(), loadGif() error for ", filepath); + return 2; + } + + snprintf(filepath, filenamelen, "%s/lemmfont2.gif", PATH_MISC); + gRess->misc.font2 = loadGif(filepath); + if (gRess->misc.font2==NULL) { + logs2(LOG_WARN, "_loadMiscRessources(), loadGif() error for ", filepath); + return 2; + } + + snprintf(filepath, filenamelen, "%s/countdown.gif", PATH_MISC); + gRess->misc.countdown = loadGif(filepath); + if (gRess->misc.countdown==NULL) { + logs2(LOG_WARN, "_loadMiscRessources(), loadGif() error for ", filepath); + return 2; + } + + snprintf(filepath, filenamelen, "%s/cursor.gif", PATH_MISC); + gRess->misc.cursor = loadGif(filepath); + if (gRess->misc.cursor==NULL) { + logs2(LOG_WARN, "_loadMiscRessources(), loadGif() error for ", filepath); + return 2; + } + + snprintf(filepath, filenamelen, "%s/explode.gif", PATH_MISC); + gRess->misc.explode = loadGif(filepath); + if (gRess->misc.explode==NULL) { + logs2(LOG_WARN, "_loadMiscRessources(), loadGif() error for ", filepath); + return 2; + } + + free(filepath); + + return 0; +} + +#endif + +SDL_Texture * my_sdl_load_texture(SDL_Renderer *rend, char *giffilepath, SDL_Rect *size) { + SDL_Surface *s; + SDL_Texture *t; + SDL_RWops *rwop; + //int res; + + rwop = SDL_RWFromFile(giffilepath,"r"); + s = IMG_LoadGIF_RW(rwop); + SDL_RWclose(rwop); + + mpl_check( + s, + {return NULL;}, + SDL_LOG_PRIORITY_WARN, + "my_sdl_load_texture(rend, \"%s\") failed",giffilepath + ); + + t = SDL_CreateTextureFromSurface(rend, s); + mpl_check( + t, + {SDL_FreeSurface(s); return NULL;}, + SDL_LOG_PRIORITY_WARN, + "my_sdl_load_texture(rend, \"%s\") : can't convert surface to texture",giffilepath + ); + + if (size != NULL) { + size->x=0; size->y=0; + size->w=s->w; size->h=s->h; + } + /* Surface no longer useful (everything was copied in the texture) */ + SDL_FreeSurface(s); + return t; +} + |