/* 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 #include #include "auxiliary/functions.h" #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 using namespace render; namespace client { namespace video { float fullscreen = 0; int width = 0; int height = 0; int width_prev = 0; int height_prev = 0; int screenshot_number = 0; const int width_default = 1024; const int height_default = 768; //--- cvars ------------------------------------------------------- core::Cvar *r_width; core::Cvar *r_height; core::Cvar *r_fullscreen; core::Cvar *screenshotformat; core::Cvar *screenshotquality; void restart() { shutdown(); if (!init()) { client()->quit(1); } input::reset(); } void reset() { // setup our viewport. gl::viewport(0, 0, width, height ); // recalculate the video aspect render::Camera::set_aspect(width, height); // reset the view view::reset(); } 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"); screenshotformat = core::Cvar::get("screenshotformat", "jpg", core::Cvar::Archive); screenshotformat->set_info("[string] screenshot format: jpg png tga"); screenshotquality = core::Cvar::get("screenshotquality", "85", core::Cvar::Archive); screenshotquality->set_info("[int] screenshot jpg quality"); int bpp = 0; int flags = 0; 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; } 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" << 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 render::Camera::set_aspect(width, height); (*r_width) = width; (*r_height) = height; std::string version(core::name()); version += ' '; version.append(core::version()); SDL_WM_SetCaption(version.c_str(), 0); render::init(); video::reset(); view::init(); return true; } void frame(float seconds) { // detect fullscreen/windowed mode switch if (fullscreen != r_fullscreen->value()) restart(); // render a client frame view::frame(seconds); 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); } void screenshot() { 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 << screenshot_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; } screenshot_number++; } while (!available); render::Image image((unsigned int)video::width, (unsigned int)video::height, 3); glReadPixels(0, 0, (GLsizei) video::width, (GLsizei) video::height, GL_RGB, GL_UNSIGNED_BYTE, (void *) image.data()); image.flip(); 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); } } } // namespace video } // namespace client