diff options
author | Ludovic Pouzenc <lpouzenc@gmail.com> | 2013-07-18 00:00:17 +0200 |
---|---|---|
committer | Ludovic Pouzenc <lpouzenc@gmail.com> | 2013-07-18 00:00:17 +0200 |
commit | 7c8a73421a72f21669334a2ecae7f48c44350ac7 (patch) | |
tree | cd78593b462f679052a0cebc069dd9c858e9df28 /src/loader.c | |
parent | 3f4026dd46e0eba4c6f60e78e58069c58cb1cb5c (diff) | |
download | mplemmings-7c8a73421a72f21669334a2ecae7f48c44350ac7.tar.gz mplemmings-7c8a73421a72f21669334a2ecae7f48c44350ac7.tar.bz2 mplemmings-7c8a73421a72f21669334a2ecae7f48c44350ac7.zip |
Chargement des ressources OK. testrender doit être amélioré.
Diffstat (limited to 'src/loader.c')
-rw-r--r-- | src/loader.c | 305 |
1 files changed, 101 insertions, 204 deletions
diff --git a/src/loader.c b/src/loader.c index 6e53298..e6e748d 100644 --- a/src/loader.c +++ b/src/loader.c @@ -1,251 +1,148 @@ #include "loader.h" -#include "utils.h" /* mpl_check */ +#include "utils.h" /* MPL_CHECK */ -#include <SDL_stdinc.h> #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 data_basepath[], gameRess_t *gRess) { - int i, res, maxlen; - char *filepath; - - /* This is the lenght of the longest filepath we need in this proc */ - maxlen = strlen(databasepath) + 2*strlen(gIni->style.name) + sizeof("/style//om_00.gif"); - filepath = SDL_malloc(maxlen+1); - - for(i=0; i < gIni->style.tiles ; ++i) { - (void) SDL_snprintf(filepath, maxlen, "%s/style/%s/%s_%d.gif", data_basepath, 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; - } - } - - SDL_free(filepath); - return 1; //FIXME -} -#if 0 -int loadRessources(gameIni_t *gIni, gameRess_t *gRess) { - int i, res, filenamelen; - char *filepath; +int loadSprite(SDL_Renderer *rend, char *giffilepath, int frames, sprite_t *sprite); - filenamelen = sizeof(char)*(strlen(PATH_STYLE)+2*strlen(gIni->style.name)+strlen("//om_00.gif")+1); - filepath = malloc(filenamelen); +int loadStyleRes(SDL_Renderer *rend, gameIni_t *gIni, char data_basepath[], gameRess_t *gRess) { + int i, res; + char filepath[MAX_PATH_LEN+1]; + char *stylename=gIni->level.style; - 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; - } + for(i=0; i < gIni->style.tiles; i++) { + (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/style/%s/%s_%d.gif", data_basepath, stylename, stylename, i); + res=loadSprite(rend,filepath,1,&gRess->tiles[i]); + if (res != 0) return res; } - 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++) { + (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/style/%s/%so_%d.gif", data_basepath, stylename, stylename, i); + res=loadSprite(rend,filepath,gIni->style.frames[i],&gRess->objects[i]); + if (res != 0) return res; - - 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; + /* Some object types needs a mask */ + if ( gIni->style.type[i] >= 5 && gIni->style.type[i] <= 8 ) { + (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/style/%s/%som_%d.gif", data_basepath, stylename, stylename, i); + res=loadSprite(rend,filepath,1,&gRess->objectMasks[i]); + if (res != 0) return res; } } - - free(filepath); - - res=_loadMiscRessources(gIni, gRess); - - return res; + return 0; } -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; +void unloadStyleRes(gameRess_t *gRess) { + int i; + for(i=0; i<MAX_TILES_COUNT;i++) { + SDL_DestroyTexture(gRess->tiles[i].t); } - - 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; - } - } + for(i=0; i<MAX_OBJECTS_COUNT;i++) { + SDL_DestroyTexture(gRess->objects[i].t); + SDL_DestroyTexture(gRess->objectMasks[i].t); } +} - gRess->misc.icons=malloc(sizeof(SDL_Surface *)*MISC_ICON_COUNT); - if (gRess->misc.icons==NULL) { - logp(LOG_ERROR, "_loadMiscRessources(), malloc()"); - return 1; - } +int loadMiscRes(SDL_Renderer *rend, char data_basepath[], gameRess_t *gRess) { - 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; - } - } + int lemmanim_frames[MAX_LEMMANIM_COUNT] = {8,4,8,8,10,16,16,16,14,8,16,16,8,16,32,24,4}; + int lemmanim_hasmask[6] = {6,10,11,13,14,15}; + int lemmanim_hasimask[3] = {13,14,15}; - 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; - } + char filepath[MAX_PATH_LEN+1]; + int i,ii,res; - 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; + for(i=0; i<MAX_LEMMANIM_COUNT; i++) { + (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/lemm_%d.gif", data_basepath, i); + res=loadSprite(rend,filepath,lemmanim_frames[i],&gRess->lemmingAnims[i]); + if (res != 0) return res; } - 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; + for(ii=0; ii<6; ii++) { + i=lemmanim_hasmask[ii]; + (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/mask_%d.gif", data_basepath, i); + res=loadSprite(rend,filepath,1,&gRess->lemmingMasks[i]); + if (res != 0) return res; } - 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; + for(ii=0; ii<3; ii++) { + i=lemmanim_hasimask[ii]; + (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/imask_%d.gif", data_basepath, i); + res=loadSprite(rend,filepath,1,&gRess->lemmingIMasks[i]); + if (res != 0) return res; } - 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; - } + (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/cursor.gif", data_basepath); + res=loadSprite(rend,filepath,8,&gRess->cursor); + if (res != 0) return res; - 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; - } + (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/countdown.gif", data_basepath); + res=loadSprite(rend,filepath,6,&gRess->countdown); + if (res != 0) return res; - free(filepath); + (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/lemmfont.gif", data_basepath); + res=loadSprite(rend,filepath,94,&gRess->font1); + if (res != 0) return res; - return 0; + (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/lemmfont2.gif", data_basepath); + res=loadSprite(rend,filepath,94,&gRess->font2); + if (res != 0) return res; + + return 0; } -#endif +void unloadMiscRes(gameRess_t *gRess) { + int i; + for(i=0; i<MAX_LEMMANIM_COUNT; i++) { + SDL_DestroyTexture(gRess->lemmingAnims[i].t); + SDL_DestroyTexture(gRess->lemmingMasks[i].t); + SDL_DestroyTexture(gRess->lemmingIMasks[i].t); + } + SDL_DestroyTexture(gRess->cursor.t); + SDL_DestroyTexture(gRess->countdown.t); + SDL_DestroyTexture(gRess->font1.t); + SDL_DestroyTexture(gRess->font2.t); +} -SDL_Texture * my_sdl_load_texture(SDL_Renderer *rend, char *giffilepath, SDL_Rect *size) { - SDL_Surface *s; - SDL_Texture *t; +/* frames is for animated sprites and is the number of images. Assumed to be all in a column */ +int loadSprite(SDL_Renderer *rend, char *giffilepath, int frames, sprite_t *sprite) { + SDL_Surface *surf; SDL_RWops *rwop; //int res; rwop = SDL_RWFromFile(giffilepath,"r"); - s = IMG_LoadGIF_RW(rwop); + MPL_CHECK( + rwop, + {return 1;}, + SDL_LOG_PRIORITY_WARN, + "loadSprite(rend, \"%s\", frames, sprite) failed : problem opening file", giffilepath + ); + surf = IMG_LoadGIF_RW(rwop); SDL_RWclose(rwop); - mpl_check( - s, - {return NULL;}, + MPL_CHECK( + surf, + {return 2;}, SDL_LOG_PRIORITY_WARN, - "my_sdl_load_texture(rend, \"%s\") failed",giffilepath + "loadSprite(rend, \"%s\", frames, sprite) failed : can't decode file", giffilepath ); - t = SDL_CreateTextureFromSurface(rend, s); - mpl_check( - t, - {SDL_FreeSurface(s); return NULL;}, + sprite->t = SDL_CreateTextureFromSurface(rend, surf); + MPL_CHECK( + sprite->t, + {SDL_FreeSurface(surf); return 2;}, SDL_LOG_PRIORITY_WARN, - "my_sdl_load_texture(rend, \"%s\") : can't convert surface to texture",giffilepath + "loadSprite(rend, \"%s\", frames, sprite) failed : can't convert surface to texture", giffilepath ); - if (size != NULL) { - size->x=0; size->y=0; - size->w=s->w; size->h=s->h; - } + sprite->size.x=0; + sprite->size.y=0; + sprite->size.w=surf->w; + sprite->size.h=surf->h / frames; + sprite->frames = frames; + /* Surface no longer useful (everything was copied in the texture) */ - SDL_FreeSurface(s); - return t; + SDL_FreeSurface(surf); + return 0; } |