Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: d8ba3169a1fcefd8b3b5448ef428be7a3fcb44e2 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
   client/video.cc
   This file is part of the Osirion project and is distributed under 
   the terms and conditions of the GNU General Public License version 2 
*/

#include "client/video.h"
#include "client/input.h"
#include "client/view.h"
#include "client/client.h"
#include "render/camera.h"
#include "render/render.h"
#include "render/tga.h"
#include "render/pngfile.h"
#include "render/jpgfile.h"
#include "core/core.h"
#include "filesystem/filesystem.h"
#include "sys/sys.h"
#include "ui/ui.h"

#include <SDL/SDL.h>

using namespace render;

namespace client {

namespace video {

float fullscreen = 0;

int bpp = 0;
int flags = 0;

int width =  0;
int height = 0;

int width_prev = 0;
int height_prev = 0;

const int width_default = 1024;
const int height_default = 768;

/* -- engine variables --------------------------------------------- */

core::Cvar *r_width;
core::Cvar *r_height;
core::Cvar *r_fullscreen;

bool init() 
{
	con_print << "^BInitializing video..." << std::endl;

	// initialize cvars
	r_width = core::Cvar::get("r_width", width_default, core::Cvar::Archive);
	r_width->set_info("[int] video resolution width");

	r_height = core::Cvar::get("r_height", height_default, core::Cvar::Archive);
	r_height->set_info("[int] video resolution height");

	r_fullscreen = core::Cvar::get("r_fullscreen", "0", core::Cvar::Archive);
	r_fullscreen->set_info("[bool] enable or disable fullscreen video");

	if( SDL_InitSubSystem(SDL_INIT_VIDEO) < 0 ) {
		con_error << "SDL_InitSubSystem() failed: " << SDL_GetError() << std::endl;
		return false;
	}

	const SDL_VideoInfo* sdl_videoinfo = SDL_GetVideoInfo();
	if( !sdl_videoinfo) {
		con_error << "SDL_GetVideoInfo() failed: " << SDL_GetError() << std::endl;
		return false;
	}

	width_prev = width;
	height_prev = height;
	width = (int) r_width->value();
	height = (int) r_height->value();

	bpp = sdl_videoinfo->vfmt->BitsPerPixel;

	if (bpp == 32) {
		SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8);
		SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
		SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8);
		SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);	
	} else if (bpp == 24) {
		SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 6);
		SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 6);
		SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 6);
		SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 6);	
	} else if (bpp == 16) {
		SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 4);
		SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 4);
		SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 4);
		SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 4);
	} else {
		con_warn << "Display depth " << bpp << " is not supported!" << std::endl;
	}

	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);

	fullscreen = r_fullscreen->value();
	if (r_fullscreen->value()) {
		flags = SDL_OPENGL | SDL_FULLSCREEN;
	} else  {
		flags = SDL_OPENGL | SDL_RESIZABLE;
	}

	if(!SDL_SetVideoMode(width, height, bpp, flags )) {
		con_warn << "Failed to set video mode " << width << "x" << height << "x" << bpp << "bpp" << std::endl;
		if (width_prev && height_prev) {
			width = width_prev;
			height = height_prev;
			if(!SDL_SetVideoMode(width, height, bpp, flags )) {
				con_error << "Failed to restore video mode " << width << "x" << height << "x" << bpp << "bpp" << std::endl;
				return false;
			}
		} else
			return false;
	}
	con_print << "  video mode " << width << "x" << height << "x" << bpp << "bpp " << (fullscreen ? "fullscreen " : "window") << std::endl;

#ifdef HAVE_DEBUG_MESSAGES

	int red, green, blue, alpha, depth;

	SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &red);
	SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &green);
	SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &blue);
	SDL_GL_GetAttribute( SDL_GL_ALPHA_SIZE, &alpha);
	SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &depth);

	con_debug << "  visual r: " << red << " g: " << green << " blue: " << blue << " alpha: " << alpha << " depth: " << depth << std::endl;

#endif // HAVE_DEBUG_MESSAGES

	// save r_width and r_height variables
	(*r_width) = width;
	(*r_height) = height;

	// set window caption
	std::string version(core::name());
	version += ' ';
	version.append(core::version());
	SDL_WM_SetCaption(version.c_str(), 0);

	// resize user interface
	ui::root()->set_size((float) width, (float) height);
	ui::root()->event_resize();
	
	// to grab or not to grab
	if (client()->console()->visible()) {
		SDL_WM_GrabInput(SDL_GRAB_OFF);
		SDL_ShowCursor(SDL_ENABLE);
	} else {
		SDL_WM_GrabInput(SDL_GRAB_ON);
		SDL_ShowCursor(SDL_DISABLE);
	}

	// initialize renderer
	render::init();
	render::resize(width,  height);
	
	view::init();

	return true;
}

void resize(float w, float h)
{
	if (fullscreen)
		return;

	if (h < 64) h = 64;
	if (w < 64) w = 64;
		
	if (SDL_SetVideoMode(w, h, bpp, flags )) {
		render::resize(w,  h);
		ui::root()->set_size(w, h);
		ui::root()->event_resize();
	} else {
		con_warn << "Could not resize window!" << std::endl;
	}
}

void restart()
{
	shutdown();
	if (!init()) {
		client()->quit(1);
	}

	input::reset();
}

void frame(float elapsed)
{
	// detect fullscreen/windowed mode switch
	if (fullscreen != r_fullscreen->value())
		restart();

	// render a client frame
	view::frame(elapsed);
	
	SDL_GL_SwapBuffers();
}

void shutdown()
{
	con_print << "^BShutting down video..." << std::endl;
	
	view::shutdown();

	render::shutdown();

	width = 0;
	height = 0;

	SDL_QuitSubSystem(SDL_INIT_VIDEO);
}

} // namespace video

} // namespace client