From 7192271eb025088ff286bb0a8b04491c9ad87d73 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Wed, 24 Jul 2013 12:06:25 +0200 Subject: La portion visible du terrain est maintenant un sprite et pas juste une texture --- src/graphic.c | 47 ++++++++++++++++++++++++++++++----------------- src/include/data_game.h | 5 +++-- src/include/graphic.h | 2 +- src/test/testrender.c | 34 +++++++++++++++++++++------------- 4 files changed, 55 insertions(+), 33 deletions(-) diff --git a/src/graphic.c b/src/graphic.c index f708304..c0b1d24 100644 --- a/src/graphic.c +++ b/src/graphic.c @@ -204,12 +204,12 @@ int paintTerrain(gameIni_t *gIni, gameRess_t *gRess, gameState_t *gState) { return 0; } -int renderTerrain(SDL_Renderer *rend, gameState_t *gState) { - int res; +int renderTerrainToTexture(SDL_Renderer *rend, gameState_t *gState) { + int res, pitch; SDL_Rect src_rect, dst_rect; SDL_Surface *tmp_surf; SDL_PixelFormat *pf = gState->terrain->format; - SDL_Texture * tmp_tex; + Uint32 *pixels; tmp_surf = SDL_CreateRGBSurface(0,MPL_WIN_W, LEVEL_HEIGHT, // pf->BitsPerPixel, pf->Rmask, pf->Gmask, pf->Bmask, pf->Amask); @@ -238,25 +238,34 @@ int renderTerrain(SDL_Renderer *rend, gameState_t *gState) { "Can't SDL_BlitSurface(terrain,...)" ); - tmp_tex = SDL_CreateTextureFromSurface(rend, tmp_surf); - MPL_CHECK( - tmp_tex, - { SDL_FreeSurface(tmp_surf); return 3; }, - SDL_LOG_PRIORITY_CRITICAL, - "Can't convert terrain to texture" - ); - + if (gState->terrainSprite.t == NULL) { + gState->terrainSprite.t = SDL_CreateTexture(rend, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, dst_rect.w, dst_rect.h); + MPL_CHECK( + gState->terrainSprite.t, + { SDL_FreeSurface(tmp_surf); return 3; }, + SDL_LOG_PRIORITY_CRITICAL, + "Can't SDL_CreateTexture for terrainSprite" + ); + gState->terrainSprite.size = dst_rect; + gState->terrainSprite.frames = 1; + } - res=SDL_RenderCopy(rend, tmp_tex, &dst_rect, &dst_rect); + res=SDL_LockTexture(gState->terrainSprite.t, NULL, (void **)&pixels, &pitch); MPL_CHECK( - res==0, - { SDL_FreeSurface(tmp_surf); SDL_DestroyTexture(tmp_tex); return 4; }, + (res==0) && (pitch == dst_rect.w * sizeof(Uint32)), + { SDL_FreeSurface(tmp_surf); + SDL_UnlockTexture(gState->terrainSprite.t); + return 3; + }, SDL_LOG_PRIORITY_CRITICAL, - "Can't SDL_RenderCopy() the terrain" + "Can't SDL_CreateTexture for terrainSprite" ); + (void) SDL_memcpy(pixels, tmp_surf->pixels, dst_rect.h * pitch); + SDL_UnlockTexture(gState->terrainSprite.t); + SDL_FreeSurface(tmp_surf); - SDL_DestroyTexture(tmp_tex); +// SDL_DestroyTexture(gState->terrainSprite.t); //TODO mais pas ici !! return 0; } @@ -270,7 +279,11 @@ int renderSprites(SDL_Renderer *rend, gameState_t *gState) { 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; + if ( renderList[i].absolute ) { + dst.x = renderList[i].x; + } else { + dst.x = renderList[i].x - gState->cameraX; + } dst.y = renderList[i].y; res=SDL_RenderCopy(rend, renderList[i].sprite->t, &src, &dst); } diff --git a/src/include/data_game.h b/src/include/data_game.h index d9bd68e..b89986d 100644 --- a/src/include/data_game.h +++ b/src/include/data_game.h @@ -9,7 +9,8 @@ typedef struct { sprite_t *sprite; int currframe; int animate; /* Is currently animating ? */ - int x,y; /* Level-based coords, not camera-based */ + int x,y; + int absolute; /* 0 if level-based coords, 1 if camera-based */ } renderItem_t; typedef struct { @@ -25,7 +26,7 @@ typedef struct { /* streamTerrain : viewable portion of terrain, in texture format updated by render_terrain() */ - SDL_Texture *terrainStream; + sprite_t terrainSprite; /* Render list of all in-game sprites (for batch processing) Items are in paint order diff --git a/src/include/graphic.h b/src/include/graphic.h index 92be5a9..b85d516 100644 --- a/src/include/graphic.h +++ b/src/include/graphic.h @@ -14,7 +14,7 @@ int mySDLInit(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint32 win_flag int paintTerrain(gameIni_t *gIni, gameRess_t *gRess, gameState_t *gState); -int renderTerrain(SDL_Renderer *rend, gameState_t *gState); +int renderTerrainToTexture(SDL_Renderer *rend, gameState_t *gState); int renderSprites(SDL_Renderer *rend, gameState_t *gState); #endif /*GRAPHIC_H*/ diff --git a/src/test/testrender.c b/src/test/testrender.c index a8fba18..ec82e41 100644 --- a/src/test/testrender.c +++ b/src/test/testrender.c @@ -4,9 +4,8 @@ #include "utils.h" #define DATA_BASEPATH "./data" -#define MAX_RENDERLIST_LEN 256 -int buildTestRL(gameRess_t *gRess, int rlMaxLen, renderItem_t renderList[]); +int buildTestRL(gameRess_t *gRess, gameState_t *gState, int rlMaxLen, renderItem_t renderList[]); int main(int argc, char **argv) { gameIni_t gIni; @@ -21,10 +20,15 @@ int main(int argc, char **argv) { int res, mainLoopEnd, uiTick; - SDL_Rect winPos = { .x=SDL_WINDOWPOS_UNDEFINED, .y=SDL_WINDOWPOS_UNDEFINED, .w=MPL_WIN_W, .h=MPL_WIN_H }; Uint32 initFlags = SDL_INIT_TIMER|SDL_INIT_VIDEO; Uint32 winFlags = SDL_WINDOW_SHOWN; Uint32 rendFlags = SDL_RENDERER_ACCELERATED; // | SDL_RENDERER_PRESENTVSYNC; + SDL_Rect winPos = { + .x=SDL_WINDOWPOS_UNDEFINED, + .y=SDL_WINDOWPOS_UNDEFINED, + .w=MPL_WIN_W, + .h=MPL_WIN_H + }; if (argc != 3) { fprintf(stderr, "Usage %s \n", argv[0]); @@ -46,9 +50,9 @@ int main(int argc, char **argv) { res=loadStyleRes(rend, &gIni, &gRess); if (res!=0) exit(res); res=paintTerrain(&gIni, &gRess, &gState); if (res!=0) exit(res); + res=renderTerrainToTexture(rend, &gState); if (res!=0) exit(res); - gState.renderListSize=buildTestRL(&gRess, MAX_RENDERLIST_LEN, gState.renderList); - + gState.renderListSize=buildTestRL(&gRess, &gState, MAX_RENDERLIST_SIZE, gState.renderList); gState.cameraX = gIni.level.xPos; @@ -65,8 +69,9 @@ int main(int argc, char **argv) { } } + (void) renderTerrainToTexture(rend, &gState); + SDL_RenderClear(rend); - (void) renderTerrain(rend, &gState); (void) renderSprites(rend, &gState); SDL_RenderPresent(rend); @@ -98,9 +103,13 @@ int addRLitem(renderItem_t list[], sprite_t *it, int rlMaxLen) { static int cur=0, curr_x=0, curr_y=0, tmp_h=0; if (it == NULL) { - curr_y += tmp_h; + if (cur>1) curr_y += tmp_h; curr_x = tmp_h = 0; return cur; + } else if (curr_x >= 640) { + curr_x = 0; + curr_y += tmp_h; + tmp_h=0; } if (cur > rlMaxLen || it->t == NULL) return cur; @@ -108,6 +117,7 @@ int addRLitem(renderItem_t list[], sprite_t *it, int rlMaxLen) { list[cur].x = curr_x; list[cur].y = curr_y; list[cur].currframe = 0; + list[cur].absolute = 1; if (it->frames > 1) { list[cur].animate = 1; /* By defaut, animate */ @@ -116,18 +126,16 @@ int addRLitem(renderItem_t list[], sprite_t *it, int rlMaxLen) { if (tmp_h < it->size.h) tmp_h = it->size.h; cur++; - if (curr_x >= 640) { - curr_x = 0; - curr_y += tmp_h; - tmp_h=0; - } return cur; } -int buildTestRL(gameRess_t *gRess, int rlMaxLen, renderItem_t renderList[]) { +int buildTestRL(gameRess_t *gRess, gameState_t *gState, int rlMaxLen, renderItem_t renderList[]) { int i, rlLen; + rlLen=addRLitem(renderList, &gState->terrainSprite, rlMaxLen); + rlLen=addRLitem(renderList, NULL, rlMaxLen); + for (i=0; ilemmingAnims[i], rlMaxLen); } -- cgit v1.2.3