/* render/screenshot.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include #include "auxiliary/functions.h" #include "core/core.h" #include "core/application.h" #include "filesystem/filesystem.h" #include "render/camera.h" #include "render/draw.h" #include "render/state.h" #include "render/screenshot.h" #include "render/image.h" #include "render/jpgfile.h" #include "render/pngfile.h" #include "render/tgafile.h" #include "render/gl.h" #include namespace render { core::Cvar *Screenshot::screenshotformat = 0; core::Cvar *Screenshot::screenshotquality = 0; int Screenshot::current_number = 0; int Screenshot::current_date = 0; void Screenshot::savegameshot(const std::string & filename) { int w = State::width(); int h = State::height(); // round image size down to a multiplier of 16 w -= w % 16; h -= h % 16; if ((w > 0) && (h > 0)) { // read pixels into an image instance Image image(w, h, 3); glReadPixels( (GLsizei) ((State::width() - w) / 2), (GLsizei) ((State::height() - h) / 2), w, h, GL_RGB, GL_UNSIGNED_BYTE, (void *) image.ptr() ); image.flip_vertical(); con_debug << "Saving " << filename << std::endl; JPG::save(filename.c_str(), image, 85); } } void Screenshot::save() { std::string filename; const int TYPETGA = 0; const int TYPEPNG = 1; const int TYPEJPG = 2; int filetype = TYPETGA; // make sure the screenshots folder exists filename.assign(filesystem::writedir()); filename.append("screenshots"); if (!sys::directory_exists(filename)) { sys::mkdir(filename); } filename += '/'; aux::lowercase(screenshotformat->str()); if ((screenshotformat->str().compare("jpg") == 0) || (screenshotformat->str().compare("jpeg") == 0)) { filetype = TYPEJPG; if (screenshotquality->value() == 0) { (*screenshotquality) = 85; } else if (screenshotquality->value() < 10) { (*screenshotquality) = 10; } else if (screenshotquality->value() > 100) { (*screenshotquality) = 100; } } else if (screenshotformat->str().compare("png") == 0) { filetype = TYPEPNG; } else if (screenshotformat->str().compare("tga") == 0) { filetype = TYPETGA; } else { filetype = TYPETGA; (*screenshotformat) = "tga"; } // check if the date has changed since the previous screenshot; std::stringstream filenamestr; int day, month, year, hour, min, sec, msec; bool available = false; do { sys::get_localtime(year, month, day, hour, min, sec, msec); std::stringstream filenamestr; // screenshots directory filenamestr << filesystem::writedir() << "screenshots/"; // date filenamestr << std::setw(4) << std::setfill('0') << year << '-'; filenamestr << std::setw(2) << month << '-'; filenamestr << std::setw(2) << day << '-'; //time filenamestr << std::setw(2) << hour << '-'; filenamestr << std::setw(2) << min << '-'; filenamestr << std::setw(2) << sec << '-'; filenamestr << std::setw(2) << msec / 10; // extension filenamestr << "." << screenshotformat->str(); // try to open the file filename.assign(filenamestr.str().c_str()); FILE *handle = fopen(filename.c_str(), "r"); if (handle) { fclose(handle); } else { available = true; } } while (!available); render::Image image(State::width(), State::height(), 3); glReadPixels(0, 0, (GLsizei) State::width(), (GLsizei) State::height(), GL_RGB, GL_UNSIGNED_BYTE, (void *) image.ptr()); image.flip_vertical(); if (filetype == TYPEPNG) { render::PNG::save(filename.c_str(), image); } else if (filetype == TYPEJPG) { render::JPG::save(filename.c_str(), image, (int) screenshotquality->value()); } else if (filetype == TYPETGA) { render::TGA::save(filename.c_str(), image); } // send a notification std::string message; message.assign("Wrote "); message.append(filename); core::application()->notify_message(core::Message::Info, message); } } // namsepace render