From eec31e5ade7a91f19323627ffb4fd91abba9c6c9 Mon Sep 17 00:00:00 2001 From: BevapDin Date: Tue, 12 Dec 2017 17:38:33 +0100 Subject: [PATCH] Change create_tile_surface to throw if something fails. Releases the caller from explicitly checking the returned pointer. Inserts `assert` statements in the callers just because. Change some `SDL_Surface_Ptr` instances to be const as they don't get changed at all. --- src/cata_tiles.cpp | 18 ++++++++++-------- src/cata_tiles.h | 3 ++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/cata_tiles.cpp b/src/cata_tiles.cpp index 28bbc7f517962..aa3feda545d20 100644 --- a/src/cata_tiles.cpp +++ b/src/cata_tiles.cpp @@ -62,6 +62,8 @@ extern int fontwidth, fontheight; extern bool tile_iso; SDL_Color cursesColorToSDL( const nc_color &color ); +///@throws std::exception upon errors. +///@returns Always a valid pointer. static SDL_Surface_Ptr create_tile_surface( int w, int h ); static const std::string empty_string; @@ -351,7 +353,7 @@ static SDL_Surface_Ptr apply_color_filter( const SDL_Surface_Ptr &original, { assert( original ); SDL_Surface_Ptr surf = create_tile_surface( original->w, original->h ); - throwErrorIf( !surf, "Unable to create alternate colored tilesets." ); + assert( surf ); throwErrorIf( SDL_BlitSurface( original.get(), NULL, surf.get(), NULL ) != 0, "SDL_BlitSurface failed" ); for (int y = 0; y < surf->h; y++) { for (int x = 0; x < surf->w; x++) { @@ -467,9 +469,7 @@ void tileset_loader::load_tileset( std::string img_path ) const int w = std::min( tile_atlas->w - sub_rect.x, sub_rect.w ); const int h = std::min( tile_atlas->h - sub_rect.y, sub_rect.h ); smaller_surf = ::create_tile_surface( w, h ); - if( !smaller_surf ) { - throw std::runtime_error( std::string( "Unable to create smaller tilesets." ) ); - } + assert( smaller_surf ); const SDL_Rect inp{ sub_rect.x, sub_rect.y, w, h }; throwErrorIf( SDL_BlitSurface( tile_atlas.get(), &inp, smaller_surf.get(), NULL ) != 0, "SDL_BlitSurface failed" ); } @@ -1126,7 +1126,8 @@ static tripoint convert_tripoint_to_abs_submap(const tripoint& p) //the surface is needed to determine the color format needed by the texture SDL_Texture_Ptr cata_tiles::create_minimap_cache_texture(int tile_width, int tile_height) { - SDL_Surface_Ptr temp = create_tile_surface(); + const SDL_Surface_Ptr temp = create_tile_surface(); + assert( temp ); SDL_Texture_Ptr tex ( SDL_CreateTexture(renderer, temp->format->format, SDL_TEXTUREACCESS_TARGET, tile_width, tile_height) ); throwErrorIf( !tex, "SDL_CreateTexture failed to create minimap texture" ); @@ -2286,7 +2287,8 @@ SDL_Surface_Ptr create_tile_surface( const int w, const int h ) #else surface.reset( SDL_CreateRGBSurface( 0, w, h, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 ) ); #endif - printErrorIf( !surface, "Failed to create surface" ); + throwErrorIf( !surface, "Failed to create surface" ); + assert( surface ); return surface; } @@ -2305,8 +2307,8 @@ void tileset_loader::ensure_default_item_highlight() std::string key = ITEM_HIGHLIGHT; int index = ts.tile_values.size(); - SDL_Surface_Ptr surface = create_tile_surface( ts.tile_width, ts.tile_height ); - throwErrorIf( !surface, "Failed to create surface for default item highlight" ); + const SDL_Surface_Ptr surface = create_tile_surface( ts.tile_width, ts.tile_height ); + assert( surface ); throwErrorIf( SDL_FillRect( surface.get(), NULL, SDL_MapRGBA( surface->format, 0, 0, 127, highlight_alpha ) ) != 0, "SDL_FillRect failed" ); SDL_Texture_Ptr texture( SDL_CreateTextureFromSurface( renderer, surface.get() ) ); throwErrorIf( !texture, "Failed to create texture for default item highlight" ); diff --git a/src/cata_tiles.h b/src/cata_tiles.h index e55fd7bae01da..7691d21d05313 100644 --- a/src/cata_tiles.h +++ b/src/cata_tiles.h @@ -455,7 +455,8 @@ class cata_tiles bool draw_tile_at( const tile_type &tile, int x, int y, unsigned int loc_rand, int rota, lit_level ll, bool apply_night_vision_goggles, int &height_3d ); - /** Surface/Sprite rotation specifics */ + ///@throws std::exception upon errors. + ///@returns Always a valid pointer. SDL_Surface_Ptr create_tile_surface(); /* Tile Picking */