From 514fed1a940d82a812ff2c38564517772ba6ed15 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Sun, 13 Jan 2019 19:32:43 +0100 Subject: Get rid of unneeded surfaces --- game.c | 16 ++++++---------- level.c | 24 ++++++++++++++---------- main.c | 12 +----------- render.c | 19 +++++++------------ 4 files changed, 28 insertions(+), 43 deletions(-) diff --git a/game.c b/game.c index 9d967d3..e0246b6 100644 --- a/game.c +++ b/game.c @@ -75,7 +75,7 @@ int player_action( signed int xdiff, signed int ydiff, struct level_struct *curr } -int play_level(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Texture *surf_screen) +int play_level(SDL_Renderer* renderer, struct level_struct *currentlevel) { // Loop until the user completes the level int player_status = PLAYER_IS_OK; @@ -87,7 +87,7 @@ int play_level(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Te // WaitEvent is like PollEvent, but instead we pause the program's // execution until we get input SDL_WaitEvent( &userinput ); - render(renderer, currentlevel, surf_screen); + render(renderer, currentlevel); // Player movement signed int y, x; @@ -146,7 +146,7 @@ int play_level(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Te if ( check_echidna_proximity( currentlevel ) == 1 ) player_status = PLAYER_IS_DEAD; // Show a losing screen if applicable - if (player_status == PLAYER_IS_DEAD) render_a_losingscreen(renderer, currentlevel, surf_screen); + if (player_status == PLAYER_IS_DEAD) render_a_losingscreen(renderer, currentlevel); } } @@ -155,7 +155,7 @@ int play_level(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Te return player_status; } -void game_loop(SDL_Renderer* renderer, SDL_Texture* surf_screen) +void game_loop(SDL_Renderer* renderer) { struct level_struct currentlevel; level_load_resources(renderer, ¤tlevel ); // load pixmaps @@ -181,7 +181,7 @@ void game_loop(SDL_Renderer* renderer, SDL_Texture* surf_screen) currentlevel.py = currentlevel.sy; // Main game loop - level_state = play_level(renderer, ¤tlevel, surf_screen ); + level_state = play_level(renderer, ¤tlevel); } // When the game loop ends, then the user has successfully completed the level, and we can move on @@ -189,12 +189,8 @@ void game_loop(SDL_Renderer* renderer, SDL_Texture* surf_screen) // The user has now completed all of the game // Show them the winning screen - - SDL_Surface* surf_win_screen = currentlevel.surf_winning_screen; - SDL_UpdateTexture(surf_screen, NULL, surf_win_screen->pixels, surf_win_screen->pitch); - SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, surf_screen, NULL, NULL); + SDL_RenderCopy(renderer, currentlevel.winning_screen, NULL, NULL); SDL_RenderPresent(renderer); SDL_Delay(20 * 1000); // 20 * 1000msecs = 20 seconds diff --git a/level.c b/level.c index 4fd4277..d8a82e4 100644 --- a/level.c +++ b/level.c @@ -66,14 +66,14 @@ struct level_struct int sx, sy; // Player X and Y starting positions // Resources - SDL_Texture * surf_entities; - SDL_Texture * surf_tiles; + SDL_Texture * entities; + SDL_Texture * tiles; // A various selection of losing screens - SDL_Surface * surf_losingscreens[5]; + SDL_Texture * losingscreens[5]; // The screen to display when the game ends - SDL_Surface * surf_winning_screen; + SDL_Texture * winning_screen; // Up to ten echidnas struct echidna echidnas[10]; @@ -102,20 +102,24 @@ void level_load_resources(SDL_Renderer* renderer, struct level_struct *level ) { // Load the images for things into their own 'surfaces' SDL_Surface* surf_entities = IMG_Load("entities.png"); - level->surf_entities = SDL_CreateTextureFromSurface(renderer, + level->entities = SDL_CreateTextureFromSurface(renderer, surf_entities); + SDL_FreeSurface(surf_entities); SDL_Surface* surf_tiles = IMG_Load("tiles.png"); - level->surf_tiles = SDL_CreateTextureFromSurface(renderer, + level->tiles = SDL_CreateTextureFromSurface(renderer, surf_tiles); + SDL_FreeSurface(surf_tiles); SDL_Surface* surf_losing = IMG_Load("failscreen_01.png"); - level->surf_losingscreens[0] = SDL_ConvertSurfaceFormat(surf_losing, - SDL_PIXELFORMAT_ARGB8888, 0); + level->losingscreens[0] = SDL_CreateTextureFromSurface(renderer, + surf_losing); + SDL_FreeSurface(surf_losing); SDL_Surface* surf_win = IMG_Load("winningscreen.png"); - level->surf_winning_screen = SDL_ConvertSurfaceFormat(surf_win, - SDL_PIXELFORMAT_ARGB8888, 0); + level->winning_screen = SDL_CreateTextureFromSurface(renderer, + surf_win); + SDL_FreeSurface(surf_win); } diff --git a/main.c b/main.c index fbb130d..210fd3f 100644 --- a/main.c +++ b/main.c @@ -57,13 +57,6 @@ int main(int argc, char *argv[]) SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL ); SDL_Renderer *renderer = SDL_CreateRenderer(sdlWindow, -1, 0); - // Set surf_screen - SDL_Texture* surf_screen = SDL_CreateTexture(renderer, - SDL_PIXELFORMAT_ARGB8888, - SDL_TEXTUREACCESS_STREAMING, - 640, 480); - if ( surf_screen == NULL ) error_sdl("Could not set videomode"); - printf("done\n"); /// Menu @@ -76,9 +69,6 @@ int main(int argc, char *argv[]) if (!surf_title) { error_sdl("could not get title surface\n"); } - surf_title = SDL_ConvertSurfaceFormat(surf_title, - SDL_PIXELFORMAT_ARGB8888, 0); - SDL_Texture *surf_title_tex = SDL_CreateTextureFromSurface(renderer, surf_title); if (!surf_title_tex) { error_sdl("could not get title texture from surface\n"); @@ -106,7 +96,7 @@ int main(int argc, char *argv[]) if ( menuevent.type == SDL_KEYDOWN ) { // Menu choice selection - if ( menuevent.key.keysym.sym == SDLK_p ) game_loop(renderer, surf_screen); + if ( menuevent.key.keysym.sym == SDLK_p ) game_loop(renderer); else if ( menuevent.key.keysym.sym == SDLK_h ) help(renderer); else if ( menuevent.key.keysym.sym == SDLK_q || menuevent.key.keysym.sym == SDLK_ESCAPE ) exit(0); } diff --git a/render.c b/render.c index 04a3105..1e7fa63 100644 --- a/render.c +++ b/render.c @@ -49,7 +49,7 @@ void get_pixmap_level( char number, SDL_Rect * crop ) // render() paints everything onto the screen when called -void render(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Texture * surf_screen ) +void render(SDL_Renderer* renderer, struct level_struct *currentlevel) { /* The picture on-screen is made in three steps: * 1) Draw the level onto the screen surface @@ -87,11 +87,11 @@ 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 ); - int ret = SDL_RenderCopy(renderer, currentlevel->surf_tiles, + int ret = SDL_RenderCopy(renderer, currentlevel->tiles, &pixmap_crop, &destination); if (ret < 0 ) { - printf("Error (SDL): %s: %s\n", "could not render copy surf_tiles", SDL_GetError()); + printf("Error (SDL): %s: %s\n", "could not render copy tiles", SDL_GetError()); } } } @@ -103,7 +103,7 @@ void render(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Textu destination.y = currentlevel->py * TILE_SIZE; // Render onto screen - SDL_RenderCopy(renderer, currentlevel->surf_entities, + SDL_RenderCopy(renderer, currentlevel->entities, &pixmap_crop, &destination); @@ -123,7 +123,7 @@ void render(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Textu destination.y = current_echidna.ypos * TILE_SIZE; // Render it onscreen - SDL_RenderCopy(renderer, currentlevel->surf_entities, + SDL_RenderCopy(renderer, currentlevel->entities, &pixmap_crop, &destination); } @@ -133,16 +133,11 @@ void render(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Textu SDL_RenderPresent(renderer); } -void render_a_losingscreen(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Texture * surf_screen ) +void render_a_losingscreen(SDL_Renderer* renderer, struct level_struct *currentlevel) { // Render a losing screen - SDL_UpdateTexture(surf_screen, NULL, - currentlevel->surf_losingscreens[0]->pixels, - currentlevel->surf_losingscreens[0]->pitch); - - SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, surf_screen, NULL, NULL); + SDL_RenderCopy(renderer, currentlevel->winning_screen, NULL, NULL); SDL_RenderPresent(renderer); // Wait a second so the user can see the losing screen -- cgit v1.2.3