diff options
author | Ludovic Pouzenc <lpouzenc@gmail.com> | 2013-07-23 20:48:22 +0200 |
---|---|---|
committer | Ludovic Pouzenc <lpouzenc@gmail.com> | 2013-07-23 20:48:22 +0200 |
commit | 5bd7068586d82cf8d11c9751b86062c2ab031946 (patch) | |
tree | ea8a7de0944543bb7ed5a46b8b27e1fad5949a35 /src/graphic.c | |
parent | 8d9a2b8fd5d2088d699c2bceb3a54592c33b7b84 (diff) | |
download | mplemmings-5bd7068586d82cf8d11c9751b86062c2ab031946.tar.gz mplemmings-5bd7068586d82cf8d11c9751b86062c2ab031946.tar.bz2 mplemmings-5bd7068586d82cf8d11c9751b86062c2ab031946.zip |
Gros refactoring alors que j'ai oublié de faire un git pull avant, ça va etre sympa...
Diffstat (limited to 'src/graphic.c')
-rw-r--r-- | src/graphic.c | 67 |
1 files changed, 31 insertions, 36 deletions
diff --git a/src/graphic.c b/src/graphic.c index 3a64cfa..57f1fba 100644 --- a/src/graphic.c +++ b/src/graphic.c @@ -1,7 +1,7 @@ #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 mySDLInit(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; @@ -9,7 +9,7 @@ void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint3 res=SDL_Init(init_flags); MPL_CHECK( res==0, // Expression to evaluate - {exit(1);} , // Code to execute if expression is not true + {return 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() ); @@ -18,7 +18,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( *win, // Just put the pointer if you want to check if it's not NULL - {SDL_Quit(); exit(2);}, + {SDL_Quit(); return 2;}, SDL_LOG_PRIORITY_CRITICAL, "SDL_CreateWindow failed" ); @@ -31,7 +31,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( *rend, - {SDL_DestroyWindow(*win);SDL_Quit(); exit(3);}, + {SDL_DestroyWindow(*win);SDL_Quit(); return 3;}, SDL_LOG_PRIORITY_CRITICAL, "SDL_CreateRenderer(...,SDL_RENDERER_ACCELERATED) failed" ); @@ -41,7 +41,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( res==0, - {SDL_DestroyRenderer(*rend);SDL_DestroyWindow(*win);SDL_Quit(); exit(4);}, + {SDL_DestroyRenderer(*rend);SDL_DestroyWindow(*win);SDL_Quit(); return 4;}, SDL_LOG_PRIORITY_CRITICAL, "res=SDL_GetRendererInfo() failed" ); @@ -80,11 +80,13 @@ void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint3 // Remarks : When the window is resized, the current viewport is automatically centered within the new window size. SDL_RenderGetViewport(*rend, viewport); + + return 0; } #define PIXEL32(s,x,y) (((Uint32 *)s->pixels)[y*s->w + x]) -int paint_terrain(gameIni_t *gIni, gameRess_t *gRess) { +int paintTerrain(gameIni_t *gIni, gameRess_t *gRess, gameState_t *gState) { int res, res2, bpp; int i, modifier; int x,y,xmin,xmax,ymin,ymax,y2,xdst,ydst; @@ -102,11 +104,11 @@ int paint_terrain(gameIni_t *gIni, gameRess_t *gRess) { SDL_LOG_PRIORITY_CRITICAL, "paintTerrain(), SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_ARGB8888, ...) failed" ); - gRess->terrain=SDL_CreateRGBSurface(0, LEVEL_WIDTH, LEVEL_HEIGHT, bpp, rmask, gmask, bmask, amask); - gRess->stencil=SDL_CreateRGBSurface(0, LEVEL_WIDTH, LEVEL_HEIGHT, bpp, rmask, gmask, bmask, amask); + gState->terrain=SDL_CreateRGBSurface(0, LEVEL_WIDTH, LEVEL_HEIGHT, bpp, rmask, gmask, bmask, amask); + gState->stencil=SDL_CreateRGBSurface(0, LEVEL_WIDTH, LEVEL_HEIGHT, bpp, rmask, gmask, bmask, amask); MPL_CHECK( - gRess->terrain && gRess->stencil, + gState->terrain && gState->stencil, { return 2; }, SDL_LOG_PRIORITY_CRITICAL, "paintTerrain(), SDL_CreateRGBSurface() failed" @@ -114,8 +116,8 @@ int paint_terrain(gameIni_t *gIni, gameRess_t *gRess) { /* Fill the entire surfaces with default color */ - res =SDL_FillRect(gRess->terrain, NULL, gIni->style.bgColor); - res2=SDL_FillRect(gRess->stencil, NULL, cc_nothing); + res =SDL_FillRect(gState->terrain, NULL, gIni->style.bgColor); + res2=SDL_FillRect(gState->stencil, NULL, cc_nothing); MPL_CHECK( res==0 && res2==0, { return 3; }, @@ -123,8 +125,6 @@ int paint_terrain(gameIni_t *gIni, gameRess_t *gRess) { "paintTerrain(), SDL_FillRect() failed" ); - /* SDL_LockSurface(gRess->terrain); //XXX Only if RLE encoded. To be removed ? - SDL_LockSurface(gRess->stencil); */ for(i=0 ; i < gIni->level.terrainCount ; i++) { int tid=gIni->level.terrains[i].id; MPL_CHECK( @@ -164,11 +164,10 @@ int paint_terrain(gameIni_t *gIni, gameRess_t *gRess) { // For each tile pixel, without going outside of the terrain ymin=(gIni->level.terrains[i].ypos>=0)?0:-gIni->level.terrains[i].ypos; - ymax=SDL_min(tile->clip_rect.h, gRess->terrain->clip_rect.h - gIni->level.terrains[i].ypos); + ymax=SDL_min(tile->clip_rect.h, gState->terrain->clip_rect.h - gIni->level.terrains[i].ypos); xmin=(gIni->level.terrains[i].xpos>=0)?0:-gIni->level.terrains[i].xpos; - xmax=SDL_min(tile->clip_rect.w, gRess->terrain->clip_rect.w - gIni->level.terrains[i].xpos); + xmax=SDL_min(tile->clip_rect.w, gState->terrain->clip_rect.w - gIni->level.terrains[i].xpos); - //SDL_LockSurface(tile); for (y=ymin; y<ymax; y++) { for (x=xmin; x<xmax; x++) { // If we have Upside Down modifier, count lines in reverse order @@ -186,7 +185,7 @@ int paint_terrain(gameIni_t *gIni, gameRess_t *gRess) { // Always paint pixel, except in one case: // If we are in NO_OVERRIDE mode and there is already a terrain on the current (source) pixel if ( !( (modifier & 8) == 8 && - PIXEL32(gRess->stencil, xdst, ydst) == cc_terrain ) ) { + PIXEL32(gState->stencil, xdst, ydst) == cc_terrain ) ) { // If we have REMOVE modifier, dstPixel will be rolled back to bgColor, else, it will be identical to the source pixel. We have to update stencil consistenly. if ( (modifier & 2) == 2 ) { dstPixel=gIni->style.bgColor; @@ -195,26 +194,21 @@ int paint_terrain(gameIni_t *gIni, gameRess_t *gRess) { dstPixel=srcPixel; dstStencil=cc_terrain; } - PIXEL32(gRess->terrain,xdst,ydst)=dstPixel; - PIXEL32(gRess->stencil,xdst,ydst)=dstStencil; + PIXEL32(gState->terrain,xdst,ydst)=dstPixel; + PIXEL32(gState->stencil,xdst,ydst)=dstStencil; } } } } - SDL_UnlockSurface(tile); } -/* - SDL_UnlockSurface(gRess->stencil); - SDL_UnlockSurface(gRess->terrain); -*/ return 0; } -int render_terrain(SDL_Renderer *rend, SDL_Surface *terrain, int xPos) { +int renderTerrain(SDL_Renderer *rend, gameState_t *gState) { int res; SDL_Rect src_rect, dst_rect; SDL_Surface *tmp_surf; - SDL_PixelFormat *pf = terrain->format; + SDL_PixelFormat *pf = gState->terrain->format; SDL_Texture * tmp_tex; tmp_surf = SDL_CreateRGBSurface(0,MPL_WIN_W, LEVEL_HEIGHT, @@ -227,7 +221,7 @@ int render_terrain(SDL_Renderer *rend, SDL_Surface *terrain, int xPos) { "Can't SDL_CreateRGBSurface() for converting terrain" ); - src_rect.x=xPos; + src_rect.x=gState->cameraX; src_rect.y=0; src_rect.w=MPL_WIN_W; src_rect.h=LEVEL_HEIGHT; @@ -235,7 +229,7 @@ int render_terrain(SDL_Renderer *rend, SDL_Surface *terrain, int xPos) { dst_rect=src_rect; dst_rect.x=0; - res = SDL_BlitSurface(terrain, &src_rect, tmp_surf, &dst_rect); + res = SDL_BlitSurface(gState->terrain, &src_rect, tmp_surf, &dst_rect); MPL_CHECK( res==0, { SDL_FreeSurface(tmp_surf); return 2; }, @@ -265,16 +259,17 @@ int render_terrain(SDL_Renderer *rend, SDL_Surface *terrain, int xPos) { return 0; } -int render_sprites(SDL_Renderer *rend, render_item_t render_list[], int list_size) { +int renderSprites(SDL_Renderer *rend, gameState_t *gState) { int i, res; SDL_Rect src,dst; - - for(i=0,res=0;i<list_size && res==0;i++) { - src = dst = render_list[i].sprite->size; - src.y = src.h * render_list[i].currframe; - dst.x = render_list[i].x; - dst.y = render_list[i].y; - res=SDL_RenderCopy(rend, render_list[i].sprite->t, &src, &dst); + renderItem_t *renderList = gState->renderList; + + for(i=0,res=0 ; i < gState->renderListSize && res==0 ; i++) { + src = dst = renderList[i].sprite->size; + src.y = src.h * renderList[i].currframe; + dst.x = renderList[i].x; + dst.y = renderList[i].y; + res=SDL_RenderCopy(rend, renderList[i].sprite->t, &src, &dst); } return res; } |