Skip to content

Commit

Permalink
display: Return error status from display::screenshot() instead of fi…
Browse files Browse the repository at this point in the history
…le size

This makes it so errors in image::save_image() are printed to stderr
using our log facilities before returning, and also propagates the
function's result to screenshot()'s callers instead of a computed
screenshot size that doesn't hold true for PNG images.

Additionally, a couple of existing error messages have been reworded and
are now emitted through the log facilities instead of being sent
directly to stderr.
  • Loading branch information
irydacea committed Apr 10, 2015
1 parent db28dca commit a62914e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
34 changes: 20 additions & 14 deletions src/display.cpp
Expand Up @@ -762,31 +762,32 @@ map_location display::minimap_location_on(int x, int y)
return loc;
}

int display::screenshot(std::string filename, bool map_screenshot)
bool display::screenshot(const std::string& filename, bool map_screenshot)
{
int size = 0;
bool res = false;

if (!map_screenshot) {
surface screenshot_surf = screen_.getSurface();

image::save_image(screenshot_surf, filename);
res = image::save_image(screenshot_surf, filename);

size = screenshot_surf->w * screenshot_surf->h;
if (!res) {
ERR_DP << "Screenshot failed: " << SDL_GetError() << '\n';
}
} else {
if (get_map().empty()) {
// Map Screenshot are big, abort and warn the user if he does strange things
std::cerr << "No map, can't do a Map Screenshot. If it was not wanted, check your hotkey.\n";
return -1;
ERR_DP << "No map loaded, cannot create a map screenshot.\n";
return false;
}

SDL_Rect area = max_map_area();
map_screenshot_surf_ = create_compatible_surface(screen_.getSurface(), area.w, area.h);

if (map_screenshot_surf_ == NULL) {
// Memory problem ?
std::cerr << "Can't create the screenshot surface. Maybe too big, try dezooming.\n";
return -1;
ERR_DP << "Could not create screenshot surface, try zooming out.\n";
return false;
}
size = map_screenshot_surf_->w * map_screenshot_surf_->h;

// back up the current map view position and move to top-left
int old_xpos = xpos_;
Expand All @@ -801,7 +802,14 @@ int display::screenshot(std::string filename, bool map_screenshot)
draw(true,true);

// finally save the image on disk
image::save_image(map_screenshot_surf_,filename);
res = image::save_image(map_screenshot_surf_,filename);

if (!res) {
// Need to do this ASAP or spurious messages result due to
// redraw_everything calling SDL too (e.g. "SDL_UpperBlit: passed
// a NULL surface")
ERR_DP << "Map screenshot failed: " << SDL_GetError() << '\n';
}

//NOTE: need to be sure that we free this huge surface (is it enough?)
map_screenshot_surf_ = NULL;
Expand All @@ -815,9 +823,7 @@ int display::screenshot(std::string filename, bool map_screenshot)
redraw_everything();
}

// convert pixel size to BMP size
size = (2048 + size*3);
return size;
return res;
}

gui::button* display::find_action_button(const std::string& id)
Expand Down
4 changes: 2 additions & 2 deletions src/display.hpp
Expand Up @@ -373,8 +373,8 @@ class display : public filter_context
/** Setter for the terrain code debug overlay on tiles */
void set_draw_terrain_codes(bool value) { draw_terrain_codes_ = value; }

/** Save a (map-)screenshot and return the estimated file size */
int screenshot(std::string filename, bool map_screenshot = false);
/** Save a (map-)screenshot and return whether the operation succeeded. */
bool screenshot(const std::string& filename, bool map_screenshot = false);

/** Invalidates entire screen, including all tiles and sidebar. Calls redraw observers. */
void redraw_everything();
Expand Down

0 comments on commit a62914e

Please sign in to comment.