#include "loader.h" #include "utils.h" /* MPL_CHECK */ #include int loadSprite(SDL_Renderer *rend, char *giffilepath, int frames, sprite_t *sprite); int loadSurface(SDL_Renderer *rend, char *giffilepath, SDL_Surface **surf); int loadLevel(char data_basepath[], char packId[], char levelId[], gameIni_t *gIni) { int res; char filepath[MAX_PATH_LEN]; SDL_strlcpy(gIni->data_basepath, data_basepath, MAX_PATH_LEN); SDL_snprintf(filepath, MAX_PATH_LEN, "%s/level/%s/levelpack.ini", data_basepath, packId); res=loadIni(ini_levelpack, filepath, gIni); if (res!=0) return res; SDL_strlcpy(gIni->levelPack.id, packId, MAX_NAMELEN); SDL_snprintf(filepath, MAX_PATH_LEN, "%s/level/%s/%s.ini", gIni->data_basepath, packId, levelId); res=loadIni(ini_level, filepath, gIni); if (res!=0) return res; // Check if we found a "style =" line in level ini file MPL_CHECK( gIni->level.style[0] != '\0', { return 10; }, SDL_LOG_PRIORITY_CRITICAL, "No valid style detected in level ini file" ); // Loading style ini file SDL_snprintf(filepath, MAX_PATH_LEN, "%s/style/%s/%s.ini", gIni->data_basepath, gIni->level.style, gIni->level.style); res=loadIni(ini_style, filepath, gIni); if (res!=0) return res; return 0; } void unloadLevel(gameIni_t *gIni, gameRess_t *gRess) { //FIXME Not yet implemented } /* Load all textures needed for style from gIni */ int loadStyleRes(SDL_Renderer *rend, gameIni_t *gIni, gameRess_t *gRess) { int i, res; char filepath[MAX_PATH_LEN+1]; char *stylename=gIni->level.style; for(i=0; i < gIni->style.tiles; i++) { (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/style/%s/%s_%d.gif", gIni->data_basepath, stylename, stylename, i); res=loadSurface(rend,filepath,&gRess->tiles[i]); if (res != 0) return res; } for(i=0; i < gIni->style.objectCount; i++) { (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/style/%s/%so_%d.gif", gIni->data_basepath, stylename, stylename, i); res=loadSprite(rend,filepath,gIni->style.frames[i],&gRess->objects[i]); if (res != 0) return res; /* 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", gIni->data_basepath, stylename, stylename, i); res=loadSurface(rend,filepath,&gRess->objectMasks[i]); if (res != 0) return res; } } return 0; } void unloadStyleRes(gameRess_t *gRess) { int i; /* Nullpointer checked for surface but not for textures in SDL code */ for(i=0; itiles[i]); } for(i=0; iobjects[i].t); DESTROYSURFACE_SAFE(gRess->objectMasks[i]); } } /* Load all misc textures (lemmings, fonts, cursor...) */ int loadMiscRes(SDL_Renderer *rend, gameIni_t *gIni, gameRess_t *gRess) { 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}; char filepath[MAX_PATH_LEN+1]; int i,ii,res; for(i=0; idata_basepath, i); res=loadSprite(rend,filepath,lemmanim_frames[i],&gRess->lemmingAnims[i]); if (res != 0) return res; } for(ii=0; ii<6; ii++) { i=lemmanim_hasmask[ii]; (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/mask_%d.gif", gIni->data_basepath, i); res=loadSurface(rend,filepath,&gRess->lemmingMasks[i]); if (res != 0) return res; } for(ii=0; ii<3; ii++) { i=lemmanim_hasimask[ii]; (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/imask_%d.gif", gIni->data_basepath, i); res=loadSurface(rend,filepath,&gRess->lemmingIMasks[i]); if (res != 0) return res; } (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/cursor.gif", gIni->data_basepath); res=loadSprite(rend,filepath,8,&gRess->cursor); if (res != 0) return res; (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/countdown.gif", gIni->data_basepath); res=loadSprite(rend,filepath,5,&gRess->countdown); if (res != 0) return res; (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/lemmfont.gif", gIni->data_basepath); res=loadSprite(rend,filepath,94,&gRess->font1); if (res != 0) return res; (void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/lemmfont2.gif", gIni->data_basepath); res=loadSprite(rend,filepath,94,&gRess->font2); if (res != 0) return res; return 0; } void unloadMiscRes(gameRess_t *gRess) { int i; for(i=0; ilemmingAnims[i].t); DESTROYSURFACE_SAFE(gRess->lemmingMasks[i]); DESTROYSURFACE_SAFE(gRess->lemmingIMasks[i]); } DESTROYTEXTURE_SAFE(gRess->cursor.t); DESTROYTEXTURE_SAFE(gRess->countdown.t); DESTROYTEXTURE_SAFE(gRess->font1.t); DESTROYTEXTURE_SAFE(gRess->font2.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) { int res; SDL_Surface *surf; MPL_CHECK( frames > 0, {return 1;}, SDL_LOG_PRIORITY_WARN, "loadSprite(rend, \"%s\", frames, sprite) failed : invalid frames value %i", giffilepath, frames ); res = loadSurface(rend, giffilepath, &surf); if (res !=0) return res; sprite->t = SDL_CreateTextureFromSurface(rend, surf); MPL_CHECK( sprite->t, {SDL_FreeSurface(surf); return 4;}, SDL_LOG_PRIORITY_WARN, "loadSprite(rend, \"%s\", frames, sprite) failed : can't convert surface to texture", giffilepath ); 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(surf); return 0; } int loadSurface(SDL_Renderer *rend, char *giffilepath, SDL_Surface **surf) { SDL_RWops *rwop; //int res; rwop = SDL_RWFromFile(giffilepath,"r"); MPL_CHECK( rwop, {return 2;}, SDL_LOG_PRIORITY_WARN, "loadSurface(rend, \"%s\", frames, sprite) failed : problem opening file", giffilepath ); *surf = IMG_LoadGIF_RW(rwop); SDL_RWclose(rwop); MPL_CHECK( *surf, {return 3;}, SDL_LOG_PRIORITY_WARN, "loadSurface(rend, \"%s\", frames, sprite) failed : can't decode file", giffilepath ); *surf = SDL_ConvertSurfaceFormat(*surf, SDL_PIXELFORMAT_ARGB8888, 0); // TODO : conversion en fonction du renderer ou bien systématiquement en ARGB8888, a voir. return 0; }