From 7c8a73421a72f21669334a2ecae7f48c44350ac7 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Thu, 18 Jul 2013 00:00:17 +0200 Subject: Chargement des ressources OK. testrender doit être amélioré. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile.am | 2 +- src/graphic.c | 82 +++++++++++++ src/include/data_ress.h | 4 +- src/include/graphic.h | 8 ++ src/include/loader.h | 9 +- src/include/utils.h | 4 +- src/loader.c | 305 ++++++++++++++++------------------------------- src/mplemmings.c | 15 ++- src/sandbox/sprite.c | 16 +-- src/sandbox/sprite_gif.c | 12 +- src/test/testparseall.c | 4 +- src/test/testrender.c | 66 ++++++++-- 12 files changed, 291 insertions(+), 236 deletions(-) create mode 100644 src/graphic.c create mode 100644 src/include/graphic.h diff --git a/Makefile.am b/Makefile.am index d08a130..f3f9ae4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -18,7 +18,7 @@ mplemmings_ds_SOURCES = src/mplemmings_ds.c sbsprite_SOURCES = src/sandbox/sprite.c sbspritegif_SOURCES = src/sandbox/sprite_gif.c testparseall_SOURCES = $(PARSER_MODULES) src/test/testparseall.c -testrender_SOURCES = $(PARSER_MODULES) src/loader.c src/test/testrender.c +testrender_SOURCES = $(PARSER_MODULES) src/loader.c src/graphic.c src/test/testrender.c # Extra files to be shipped in the tarball (make dist) EXTRA_DIST = m4/.dont-remove rm-all-generated-files.sh diff --git a/src/graphic.c b/src/graphic.c new file mode 100644 index 0000000..dbdb55c --- /dev/null +++ b/src/graphic.c @@ -0,0 +1,82 @@ +#include "graphic.h" +#include "utils.h" + +void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint32 win_flags, Uint32 rend_flags, SDL_Window **win, SDL_Renderer **rend, SDL_RendererInfo *rend_info, SDL_Rect *viewport) +{ + int res, i; + Uint32 tf; + + res=SDL_Init(init_flags); + MPL_CHECK( + res==0, // Expression to evaluate + {exit(1);} , // Code to execute if expression is not true + SDL_LOG_PRIORITY_CRITICAL, // SDL_LogPriority (_CRITICAL, _ERROR, _WARN, _INFO, DEBUG, _VERBOSE) + "SDL_Init failed (%i)",res // var args list starting with a fmt string like in printf() + ); + + + *win=SDL_CreateWindow(title,win_pos.x,win_pos.y,win_pos.w,win_pos.h,win_flags); + MPL_CHECK( + *win, // Just put the pointer if you want to check if it's not NULL + {SDL_Quit(); exit(2);}, + SDL_LOG_PRIORITY_CRITICAL, + "SDL_CreateWindow failed" + ); + + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO, + "SDL_GetWindowPixelFormat() returns %i", SDL_GetWindowPixelFormat(*win) + ); + + *rend=SDL_CreateRenderer(*win,-1, rend_flags); + MPL_CHECK( + *rend, + {SDL_DestroyWindow(*win);SDL_Quit(); exit(3);}, + SDL_LOG_PRIORITY_CRITICAL, + "SDL_CreateRenderer(...,SDL_RENDERER_ACCELERATED) failed" + ); + + res=SDL_GetRendererInfo(*rend, rend_info); + MPL_CHECK( + res==0, + {SDL_DestroyRenderer(*rend);SDL_DestroyWindow(*win);SDL_Quit(); exit(4);}, + SDL_LOG_PRIORITY_CRITICAL, + "res=SDL_GetRendererInfo() failed" + ); + + tf=1; + for(i=0;i<16 && tf!=0;i++) { + tf=rend_info->texture_formats[i]; + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO, + "rend_info->texture_formats[%i] == %i", i, tf + ); + } + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO, + "rend_info->flags==%i", rend_info->flags ); + + if ( (rend_info->flags & SDL_RENDERER_SOFTWARE) == SDL_RENDERER_SOFTWARE) { + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO, + "rend_info->flags contains SDL_RENDERER_SOFTWARE"); + } + + if ( (rend_info->flags & SDL_RENDERER_ACCELERATED) == SDL_RENDERER_ACCELERATED) { + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO, + "rend_info->flags contains SDL_RENDERER_ACCELERATED"); + } + + if ( (rend_info->flags & SDL_RENDERER_PRESENTVSYNC) == SDL_RENDERER_PRESENTVSYNC) { + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO, + "rend_info->flags contains SDL_RENDERER_PRESENTVSYNC"); + } + + if ( (rend_info->flags & SDL_RENDERER_TARGETTEXTURE) == SDL_RENDERER_TARGETTEXTURE) { + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO, + "rend_info->flags contains SDL_RENDERER_TARGETTEXTURE"); + } + + // Remarks : When the window is resized, the current viewport is automatically centered within the new window size. + SDL_RenderGetViewport(*rend, viewport); +} + diff --git a/src/include/data_ress.h b/src/include/data_ress.h index 1b8bba7..07fbd19 100644 --- a/src/include/data_ress.h +++ b/src/include/data_ress.h @@ -8,7 +8,7 @@ typedef struct { SDL_Texture *t; SDL_Rect size; - /* int frames; Already in gIni->style->frames, but convenient here also */ + int frames; /* Already in gIni->style->frames, but convenient here also */ } sprite_t; typedef struct { @@ -19,7 +19,7 @@ typedef struct { /* Misc */ sprite_t lemmingAnims[MAX_LEMMANIM_COUNT]; sprite_t lemmingMasks[MAX_LEMMANIM_COUNT]; - sprite_t lemmingImask[MAX_LEMMANIM_COUNT]; + sprite_t lemmingIMasks[MAX_LEMMANIM_COUNT]; sprite_t font1, font2, countdown, cursor; /* Music */ Mix_Music *musics[MAX_MUSICS_COUNT]; diff --git a/src/include/graphic.h b/src/include/graphic.h new file mode 100644 index 0000000..c1b2a42 --- /dev/null +++ b/src/include/graphic.h @@ -0,0 +1,8 @@ +#ifndef GRAPHIC_H +#define GRAPHIC_H + +#include + +void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint32 win_flags, Uint32 rend_flags, SDL_Window **win, SDL_Renderer **rend, SDL_RendererInfo *rend_info, SDL_Rect *viewport); + +#endif /*GRAPHIC_H*/ diff --git a/src/include/loader.h b/src/include/loader.h index 81c39cb..8759eb6 100644 --- a/src/include/loader.h +++ b/src/include/loader.h @@ -1,10 +1,15 @@ #ifndef LOADER_H #define LOADER_H +#include #include "data_ini.h" #include "data_ress.h" -// Load all textures needed for style and level from gIni -int loadRessources(gameIni_t *gIni, char data_basepath[], gameRess_t *gRess); +// Load all textures needed for style from gIni +int loadStyleRes(SDL_Renderer *rend, gameIni_t *gIni, char data_basepath[], gameRess_t *gRess); +void unloadStyleRes(gameRess_t *gRess); +// Load all misc textures (lemmings, fonts, cursor...) +int loadMiscRes(SDL_Renderer *rend, char data_basepath[], gameRess_t *gRess); +void unloadMiscRes(gameRess_t *gRess); #endif /*LOADER_H*/ diff --git a/src/include/utils.h b/src/include/utils.h index f0af3d0..7f43d8f 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -1,9 +1,11 @@ /* Macro for error checking and logging */ -#define mpl_check(expr, fail_code, priority, ...) \ +#define MPL_CHECK(expr, fail_code, priority, ...) \ if (! (expr)) { \ SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,priority,__VA_ARGS__); \ SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,priority, \ "-> last SDL error : %s\n", SDL_GetError()); \ fail_code; \ } + +#define MAX_PATH_LEN 255 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 #include -#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; itiles[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; iobjects[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; ilemmingAnims[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; ilemmingAnims[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; } diff --git a/src/mplemmings.c b/src/mplemmings.c index 8537216..3d86b67 100644 --- a/src/mplemmings.c +++ b/src/mplemmings.c @@ -10,12 +10,21 @@ freely. */ +#include +#if ! SDL_VERSION_ATLEAST(2,0,0) +#error "This code is only for SDL 2+. No backward compatibility with previous SDL versions, sorry." +#endif + #include "mplemmings_config.h" -#include +#include "utils.h" + int main(void) { - printf("Starting %s\n", PACKAGE_STRING); - printf("If any questions, please visit %s.\n", PACKAGE_URL); + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO, + "Starting %s\nIf any questions, please visit %s.\n", + PACKAGE_STRING, PACKAGE_URL + ); + return 0; } diff --git a/src/sandbox/sprite.c b/src/sandbox/sprite.c index 57c6f06..ec33b36 100644 --- a/src/sandbox/sprite.c +++ b/src/sandbox/sprite.c @@ -90,7 +90,7 @@ void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint3 Uint32 tf; res=SDL_Init(init_flags); - mpl_check( + MPL_CHECK( res==0, // Expression to evaluate {exit(1);} , // Code to execute if expression is not true SDL_LOG_PRIORITY_CRITICAL, // SDL_LogPriority (_CRITICAL, _ERROR, _WARN, _INFO, DEBUG, _VERBOSE) @@ -99,7 +99,7 @@ void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint3 *win=SDL_CreateWindow(title,win_pos.x,win_pos.y,win_pos.w,win_pos.h,win_flags); - mpl_check( + MPL_CHECK( *win, // Just put the pointer if you want to check if it's not NULL {SDL_Quit(); exit(2);}, SDL_LOG_PRIORITY_CRITICAL, @@ -112,7 +112,7 @@ void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint3 ); *rend=SDL_CreateRenderer(*win,-1, rend_flags); - mpl_check( + MPL_CHECK( *rend, {SDL_DestroyWindow(*win);SDL_Quit(); exit(3);}, SDL_LOG_PRIORITY_CRITICAL, @@ -120,7 +120,7 @@ void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint3 ); res=SDL_GetRendererInfo(*rend, rend_info); - mpl_check( + MPL_CHECK( res==0, {SDL_DestroyRenderer(*rend);SDL_DestroyWindow(*win);SDL_Quit(); exit(4);}, SDL_LOG_PRIORITY_CRITICAL, @@ -169,13 +169,13 @@ SDL_Texture * my_sdl_load_texture(SDL_Renderer *rend, char *bmpfilepath, SDL_Rec int res; s = SDL_LoadBMP(bmpfilepath); - mpl_check( + MPL_CHECK( s, {return NULL;}, SDL_LOG_PRIORITY_WARN, "my_sdl_load_texture(rend, \"%s\") failed",bmpfilepath ); - mpl_check( + MPL_CHECK( s->format->BitsPerPixel==24, {SDL_FreeSurface(s); return NULL;}, SDL_LOG_PRIORITY_WARN, @@ -183,7 +183,7 @@ SDL_Texture * my_sdl_load_texture(SDL_Renderer *rend, char *bmpfilepath, SDL_Rec ); res=SDL_SetColorKey(s, SDL_TRUE, MPL_COLOR_KEY); - mpl_check( + MPL_CHECK( res==0, {SDL_FreeSurface(s); return NULL;}, SDL_LOG_PRIORITY_WARN, @@ -191,7 +191,7 @@ SDL_Texture * my_sdl_load_texture(SDL_Renderer *rend, char *bmpfilepath, SDL_Rec ); t = SDL_CreateTextureFromSurface(rend, s); - mpl_check( + MPL_CHECK( t, {SDL_FreeSurface(s); return NULL;}, SDL_LOG_PRIORITY_WARN, diff --git a/src/sandbox/sprite_gif.c b/src/sandbox/sprite_gif.c index 28c57aa..48657df 100644 --- a/src/sandbox/sprite_gif.c +++ b/src/sandbox/sprite_gif.c @@ -92,7 +92,7 @@ void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint3 Uint32 tf; res=SDL_Init(init_flags); - mpl_check( + MPL_CHECK( res==0, // Expression to evaluate {exit(1);} , // Code to execute if expression is not true SDL_LOG_PRIORITY_CRITICAL, // SDL_LogPriority (_CRITICAL, _ERROR, _WARN, _INFO, DEBUG, _VERBOSE) @@ -101,7 +101,7 @@ void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint3 *win=SDL_CreateWindow(title,win_pos.x,win_pos.y,win_pos.w,win_pos.h,win_flags); - mpl_check( + MPL_CHECK( *win, // Just put the pointer if you want to check if it's not NULL {SDL_Quit(); exit(2);}, SDL_LOG_PRIORITY_CRITICAL, @@ -114,7 +114,7 @@ void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint3 ); *rend=SDL_CreateRenderer(*win,-1, rend_flags); - mpl_check( + MPL_CHECK( *rend, {SDL_DestroyWindow(*win);SDL_Quit(); exit(3);}, SDL_LOG_PRIORITY_CRITICAL, @@ -122,7 +122,7 @@ void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint3 ); res=SDL_GetRendererInfo(*rend, rend_info); - mpl_check( + MPL_CHECK( res==0, {SDL_DestroyRenderer(*rend);SDL_DestroyWindow(*win);SDL_Quit(); exit(4);}, SDL_LOG_PRIORITY_CRITICAL, @@ -175,7 +175,7 @@ SDL_Texture * my_sdl_load_texture(SDL_Renderer *rend, char *giffilepath, SDL_Rec s = IMG_LoadGIF_RW(rwop); SDL_RWclose(rwop); - mpl_check( + MPL_CHECK( s, {return NULL;}, SDL_LOG_PRIORITY_WARN, @@ -183,7 +183,7 @@ SDL_Texture * my_sdl_load_texture(SDL_Renderer *rend, char *giffilepath, SDL_Rec ); t = SDL_CreateTextureFromSurface(rend, s); - mpl_check( + MPL_CHECK( t, {SDL_FreeSurface(s); return NULL;}, SDL_LOG_PRIORITY_WARN, diff --git a/src/test/testparseall.c b/src/test/testparseall.c index 0ea18a4..9528b4e 100644 --- a/src/test/testparseall.c +++ b/src/test/testparseall.c @@ -1,8 +1,8 @@ #include "parser.h" +#include "utils.h" + #include // printf, perror -#include // TODO : remove chdir() call -#define MAX_PATH_LEN 64 struct test_ini_file { enum ini_type type; diff --git a/src/test/testrender.c b/src/test/testrender.c index 98a940f..93fa217 100644 --- a/src/test/testrender.c +++ b/src/test/testrender.c @@ -1,8 +1,9 @@ +#include "graphic.h" #include "parser.h" #include "loader.h" +#include "utils.h" #define DATA_BASEPATH "./data" -#define MAX_PATH_LEN 255 int main(int argc, char **argv) { int res; @@ -29,10 +30,12 @@ int main(int argc, char **argv) { if (res!=0) exit(res); // Check if we found a "style =" line in level ini file - if (gIni.level.style==NULL) { - fprintf(stderr, "No valid style detected\n"); - exit(1); - } + MPL_CHECK( + gIni.level.style, + { exit(1); }, + 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", DATA_BASEPATH, gIni.level.style, gIni.level.style); @@ -45,7 +48,56 @@ int main(int argc, char **argv) { if (res!=0) exit(res); */ - res=loadRessources(&gIni, DATA_BASEPATH, &gRess); + SDL_Window *sdl_win; + SDL_Renderer *sdl_rend; + SDL_RendererInfo sdl_rend_info; + SDL_Rect sdl_viewport; + + SDL_Event sdl_ev; + int mainloop_end=0; + + SDL_Rect win_pos = { .x=SDL_WINDOWPOS_UNDEFINED, .y=SDL_WINDOWPOS_UNDEFINED, .w=640, .h=480 }; + Uint32 init_flags = SDL_INIT_TIMER|SDL_INIT_VIDEO;//|SDL_INIT_EVENTS; + Uint32 win_flags = SDL_WINDOW_SHOWN; + Uint32 rend_flags = SDL_RENDERER_ACCELERATED; + + my_SDL_init_or_die(__FILE__, win_pos, init_flags, win_flags, rend_flags, &sdl_win, &sdl_rend, &sdl_rend_info, &sdl_viewport); + + // Setting default values + SDL_memset(&gRess,0,sizeof(gameRess_t)); + + res=loadMiscRes(sdl_rend, DATA_BASEPATH, &gRess); + if (res!=0) exit(res); + res=loadStyleRes(sdl_rend, &gIni, DATA_BASEPATH, &gRess); + if (res!=0) exit(res); + + /* Main render loop */ + while (!mainloop_end) { + /* Check for events */ + while (SDL_PollEvent(&sdl_ev)) { + if (sdl_ev.type == SDL_QUIT || sdl_ev.type == SDL_KEYDOWN) { + mainloop_end = 1; + } + } + /* Draw a gray background */ + SDL_SetRenderDrawColor(sdl_rend, 0xA0, 0xA0, 0xA0, 0xFF); + SDL_RenderClear(sdl_rend); + + /* Blit the sprite onto the screen */ + SDL_RenderCopy(sdl_rend, gRess.objects[0].t, NULL, NULL); + //SDL_RenderCopy(sdl_rend, t1, &t1_src, &t1_dst); + + /* Update the screen */ + SDL_RenderPresent(sdl_rend); + + SDL_Delay(50); + } + unloadStyleRes(&gRess); + unloadMiscRes(&gRess); + + SDL_DestroyRenderer(sdl_rend); + SDL_DestroyWindow(sdl_win); + SDL_Quit(); - return res; + return 0; } -- cgit v1.2.3