Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 7b401562b1c884978530579c066890f134fef511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
   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 <iomanip>

#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::current_number = 0;
int Screenshot::current_date = 0;

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;
	int date_serial;

	sys::get_datetime(year, month, day, hour, min);
	date_serial = (day - 1) + (month - 1) * 31 + (year - 2000) * 31 * 12;
	if (current_date != date_serial) {
		current_number = 0;
		current_date = date_serial;
	}

	bool available = false;
	do {
		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 << '-';
		// sequence number
		filenamestr << std::setw(4) << std::setfill('0') << current_number;
		// 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;
		}
		current_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.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);
	}
}

} // namsepace render