| 253 | } |
| 254 | |
| 255 | bool Window::takeScreenshot( std::string filepath, const Image::SaveType& format ) { |
| 256 | GlobalBatchRenderer::instance()->draw(); |
| 257 | |
| 258 | bool createNewFile = false; |
| 259 | std::string file; |
| 260 | std::string ext; |
| 261 | |
| 262 | if ( filepath.size() ) { |
| 263 | file = filepath.substr( filepath.find_last_of( "/\\" ) + 1 ); |
| 264 | ext = file.substr( file.find_last_of( "." ) + 1 ); |
| 265 | if ( FileSystem::isDirectory( filepath ) || !ext.size() ) |
| 266 | createNewFile = true; |
| 267 | } else { |
| 268 | filepath = Sys::getProcessPath(); |
| 269 | createNewFile = true; |
| 270 | } |
| 271 | |
| 272 | std::string finalPath; |
| 273 | |
| 274 | if ( createNewFile ) { // Search if file path is given, and if have and extension |
| 275 | bool find = false; |
| 276 | int fileNum = 1; |
| 277 | std::string tmpPath = filepath; |
| 278 | |
| 279 | if ( !FileSystem::isDirectory( filepath ) ) |
| 280 | FileSystem::makeDir( filepath ); |
| 281 | |
| 282 | ext = "." + Image::saveTypeToExtension( format ); |
| 283 | |
| 284 | while ( !find && fileNum < 10000 ) { |
| 285 | tmpPath = String::format( "%s%05d%s", filepath.c_str(), fileNum, ext.c_str() ); |
| 286 | fileNum++; |
| 287 | |
| 288 | if ( !FileSystem::fileExists( tmpPath ) ) |
| 289 | find = true; |
| 290 | |
| 291 | if ( fileNum == 10000 && find == false ) |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | finalPath = std::move( tmpPath ); |
| 296 | } else { |
| 297 | std::string Direc = FileSystem::fileRemoveFileName( filepath ); |
| 298 | if ( !FileSystem::isDirectory( Direc ) ) |
| 299 | FileSystem::makeDir( Direc ); |
| 300 | finalPath = filepath; |
| 301 | } |
| 302 | |
| 303 | return getFrontBufferImage().saveToFile( finalPath, format ); |
| 304 | } |
| 305 | |
| 306 | Image Window::getFrontBufferImage() { |
| 307 | GlobalBatchRenderer::instance()->draw(); |