From 773c1bafe0f1d8b706e0f72e235f8466e7a9ccf5 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 11 Nov 2008 19:11:57 +0000 Subject: cleanups --- src/render/screenshot.cc | 109 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/render/screenshot.cc (limited to 'src/render/screenshot.cc') diff --git a/src/render/screenshot.cc b/src/render/screenshot.cc new file mode 100644 index 0000000..c2ee88a --- /dev/null +++ b/src/render/screenshot.cc @@ -0,0 +1,109 @@ +/* + 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 "auxiliary/functions.h" +#include "core/core.h" +#include "filesystem/filesystem.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" + +namespace render { + +core::Cvar *Screenshot::screenshotformat = 0; +core::Cvar *Screenshot::screenshotquality = 0; + +int Screenshot::number = 0; + +void Screenshot::save() +{ + bool available = false; + std::string shortname; + 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/"); + sys::mkdir(filename); + + aux::lowercase(screenshotformat->str()); + + if ((screenshotformat->str().compare("jpg") == 0) || (screenshotformat->str().compare("jpeg") == 0)) { + filetype = TYPEJPG; + 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"; + } + + // find the first available screenshotxxxx + do { + std::stringstream nstr; + nstr << number; + shortname.assign(nstr.str()); + + while(shortname.size() < 4) + shortname.insert(0, 1, '0'); + + shortname.insert(0, "screenshots/osirion"); + shortname.append("."); + shortname.append(screenshotformat->str()); + + filename.assign(filesystem::writedir()); + filename.append(shortname); + + FILE *handle = fopen(filename.c_str(), "r"); + if (handle) { + fclose(handle); + } else { + available = true; + } + number++; + } 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.data()); + + image.flip(); + + if (filetype == TYPEPNG) { +/* if ((Camera::width() % 8 != 0 ) || (Camera::height() % 8 != 0 )) { + image.pad(); + }*/ + render::PNG::save(filename.c_str(), image); + } else if (filetype == TYPEJPG) { +/* if ((Camera::width() % 8 != 0 ) || (Camera::height() % 8 != 0 )) { + image.pad(); + } +*/ + render::JPG::save(filename.c_str(), image, (int) screenshotquality->value()); + } else if (filetype == TYPETGA) { + render::TGA::save(filename.c_str(), image); + } +} + +} // namsepace render \ No newline at end of file -- cgit v1.2.3