From 4e3b66e4a82f7997ad9f1c2112e2af20728e406d Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Sun, 13 Jan 2019 19:00:35 +0100 Subject: Use SDL_RenderCopy instead of SDL_UpdateTexture --- level.c | 13 ++++++++++++- main.c | 5 ----- render.c | 53 ++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/level.c b/level.c index 6097f23..b00e235 100644 --- a/level.c +++ b/level.c @@ -67,7 +67,9 @@ struct level_struct // Resources SDL_Surface * surf_entities; // Player and echidna pictures + SDL_Texture * surf_entities_tex; SDL_Surface * surf_tiles; // Level tiles/squares + SDL_Texture * surf_tiles_tex; // A various selection of losing screens SDL_Surface * surf_losingscreens[5]; @@ -77,7 +79,6 @@ struct level_struct // Up to ten echidnas struct echidna echidnas[10]; - }; @@ -106,10 +107,20 @@ void level_load_resources(SDL_Renderer* renderer, struct level_struct *level ) level->surf_entities = SDL_ConvertSurfaceFormat(surf_entities, SDL_PIXELFORMAT_ARGB8888, 0); + level->surf_entities_tex = SDL_CreateTextureFromSurface(renderer, + level->surf_entities); + SDL_Surface* surf_tiles = IMG_Load( "tiles.png" ); level->surf_tiles = SDL_ConvertSurfaceFormat(surf_tiles, SDL_PIXELFORMAT_ARGB8888, 0); + + level->surf_tiles_tex = SDL_CreateTextureFromSurface(renderer, + level->surf_tiles); + int w, h; + Uint32 format; + SDL_QueryTexture(level->surf_tiles_tex, &format, NULL, &w, &h); + printf("surf_tiles_tex: w: %d, h: %d, format: %s\n", w, h, SDL_GetPixelFormatName(format)); // Do the same for the losing screens SDL_Surface* surf_losing = IMG_Load("failscreen_01.png"); diff --git a/main.c b/main.c index 02b3e36..a98b479 100644 --- a/main.c +++ b/main.c @@ -84,11 +84,6 @@ int main(int argc, char *argv[]) error_sdl("could not get title texture from surface\n"); } - int w, h; - Uint32 format; - SDL_QueryTexture(surf_title_tex, &format, NULL, &w, &h); - printf("w: %d, h: %d, format: %s\n", w, h, SDL_GetPixelFormatName(format)); - /* We loop the menu continously for a number of reasons: * 1) To make sure we capture and respond to user input * 2) To continue redrawing the menu graphic. diff --git a/render.c b/render.c index 4e5db41..a713d5e 100644 --- a/render.c +++ b/render.c @@ -87,14 +87,33 @@ void render(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Textu /// Draw this tile onto the screen //SDL_BlitSurface ( currentlevel->surf_tiles, &pixmap_crop, surf_screen, &destination ); - // TODO: use pixmap_crop + // TODO: use pixmap_crop with SDL_RenderCopy? // printf("surf_tile format: %s\n", SDL_GetPixelFormatName(currentlevel->surf_tiles->format->format)); - SDL_UpdateTexture(surf_screen, &destination, - currentlevel->surf_tiles->pixels, - currentlevel->surf_tiles->pitch); + // SDL_UpdateTexture(surf_screen, &destination, + // currentlevel->surf_tiles->pixels, + // currentlevel->surf_tiles->pitch); + + printf("pixmap_crop x: %d, y: %d, w: %d, h: %d\n", + pixmap_crop.x, pixmap_crop.y, pixmap_crop.w, pixmap_crop.h); + + printf("destination x: %d, y: %d, w: %d, h: %d\n", + destination.x, destination.y, destination.w, destination.h); + + int ret = SDL_RenderCopy(renderer, currentlevel->surf_tiles_tex, + &pixmap_crop, + &destination); + printf("SDL_RenderCopy ret: %d\n", ret); + if (ret < 0 ) { + printf("Error (SDL): %s: %s\n", "could not render copy surf_tiles", SDL_GetError()); + } + + //printf("surf_tile pitch: %d\n", currentlevel->surf_tiles->pitch); } } - + + // SDL_RenderPresent(renderer); + // SDL_Delay(500); + /// Entity drawing // First of all the character get_pixmap_lemon( currentlevel->player_face, &pixmap_crop ); @@ -103,9 +122,13 @@ void render(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Textu // Draw onto screen //SDL_BlitSurface ( currentlevel->surf_entities, &pixmap_crop, surf_screen, &destination ); - SDL_UpdateTexture(surf_screen, &destination, - currentlevel->surf_entities->pixels, - currentlevel->surf_entities->pitch); + //SDL_UpdateTexture(surf_screen, &destination, + // currentlevel->surf_entities->pixels, + // currentlevel->surf_entities->pitch); + + SDL_RenderCopy(renderer, currentlevel->surf_entities_tex, + &pixmap_crop, + &destination); // Now draw all of the echidnas int ech_no; // Echidna number @@ -124,17 +147,21 @@ void render(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Textu // Draw it onscreen // SDL_BlitSurface ( currentlevel->surf_entities, &pixmap_crop, surf_screen, &destination ); - SDL_UpdateTexture(surf_screen, &destination, - currentlevel->surf_entities->pixels, - currentlevel->surf_entities->pitch); + // SDL_UpdateTexture(surf_screen, &destination, + // currentlevel->surf_entities->pixels, + // currentlevel->surf_entities->pitch); + + SDL_RenderCopy(renderer, currentlevel->surf_entities_tex, + &pixmap_crop, + &destination); } } /// Flip screen, so everything we have 'blitted' can now be seen //SDL_Flip( surf_screen ); - SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, surf_screen, NULL, NULL); + //SDL_RenderClear(renderer); + //SDL_RenderCopy(renderer, surf_screen, NULL, NULL); SDL_RenderPresent(renderer); } -- cgit v1.2.1-18-gbd029