src/graphic/video.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A ARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU GeneralPublic License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  *****************************************************************************/
00019 
00020 #include "video.h"
00021 #include <string>
00022 #include <iostream>
00023 #include <SDL_endian.h>
00024 #include <SDL_image.h>
00025 #include "../game/config.h"
00026 #include "../tool/error.h"
00027 #include "../tool/i18n.h"
00028 #include "../include/app.h"
00029 #include "../include/constant.h"
00030 #include "../map/camera.h"
00031 
00032 Video::Video(){
00033   SetMaxFps (50);
00034   fullscreen = false;
00035   SDLReady = false;
00036 }
00037 
00038 Video::~Video(){
00039   if( SDLReady )
00040     SDL_Quit();
00041 }
00042 
00043 void Video::SetMaxFps(uint max_fps){
00044   m_max_fps = max_fps;
00045   if (0 < m_max_fps)
00046     m_sleep_max_fps = 1000/m_max_fps;
00047   else
00048     m_sleep_max_fps = 0;
00049 }
00050 
00051 uint Video::GetMaxFps(){
00052   return m_max_fps;
00053 }
00054 
00055 uint Video::GetSleepMaxFps(){
00056   return m_sleep_max_fps;
00057 }
00058 
00059 bool Video::IsFullScreen() const{
00060   return fullscreen;
00061 }
00062 
00063 bool CompareConfigs(const Point2i& a, const Point2i& b)
00064 {
00065   return  (a.x >= b.x) && (a.y >= b.y);
00066 }
00067 
00068 void Video::ComputeAvailableConfigs()
00069 {
00070   // Add the current resolution
00071   available_configs.push_back(Point2i(AppWormux::GetInstance()->video.window.GetWidth(),
00072                                       AppWormux::GetInstance()->video.window.GetHeight()));
00073 
00074   //Generate video mode list
00075   SDL_Rect **modes;
00076 
00077   // Get available fullscreen/hardware modes
00078   modes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
00079 
00080   // Check is there are any modes available
00081   if(modes != NULL){
00082     // We also had the current window resolution if it is not already in the list!
00083     for(int i=0;modes[i];++i) {
00084       // We accept only modes that are bigger than 800x600
00085       if (modes[i]->w < 800 || modes[i]->h < 600) break;
00086       available_configs.push_back(Point2i(modes[i]->w, modes[i]->h));
00087     }
00088   }
00089 
00090   // Sort the list
00091   available_configs.sort(CompareConfigs);
00092 
00093   // If biggest resolution is big enough, we propose standard resolution such as
00094   // 800x600, 1024x768, 1280x1024, 1600x1200
00095   Point2i a(800, 600);
00096   if ( CompareConfigs((*available_configs.begin()), a))
00097     available_configs.push_back(a);
00098   Point2i b(1024, 768);
00099   if ( CompareConfigs((*available_configs.begin()), b))
00100     available_configs.push_back(b);
00101   Point2i c(1280, 1024);
00102   if ( CompareConfigs((*available_configs.begin()), c))
00103     available_configs.push_back(c);
00104   Point2i d(1600, 1200);
00105   if ( CompareConfigs((*available_configs.begin()), d))
00106     available_configs.push_back(d);
00107 
00108   // Sort the list again...
00109   available_configs.sort(CompareConfigs);
00110 
00111   // Remove double items
00112   std::list<Point2i>::iterator prev = available_configs.begin(),
00113     it = available_configs.begin() ,
00114     end = available_configs.end();
00115 
00116   for (++it; it != end ; ++it) {
00117     if ((*prev)==(*it)) // the two items are equals
00118       prev = available_configs.erase(prev);
00119     else
00120       prev++;
00121   }
00122 }
00123 
00124 std::list<Point2i>& Video::GetAvailableConfigs()
00125 {
00126   return available_configs;
00127 }
00128 
00129 bool Video::SetConfig(int width, int height, bool _fullscreen){
00130   // initialize the main window
00131   if( window.IsNull() ||
00132      (width != window.GetWidth() ||
00133       height != window.GetHeight() ) ){
00134 
00135     window.SetSurface( SDL_SetVideoMode(width, height, 32,
00136                        SDL_HWSURFACE | SDL_HWACCEL | SDL_DOUBLEBUF ), false );
00137 
00138     if( window.IsNull() ) {
00139       window.SetSurface( SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE) );
00140       std::cerr << "WARNING: Video not using hardware acceleration!" << std::endl;
00141     }
00142 
00143     if( window.IsNull() )
00144       return false;
00145     fullscreen = false;
00146   }
00147 
00148   if(fullscreen != _fullscreen) {
00149     SDL_WM_ToggleFullScreen( window.GetSurface() );
00150     fullscreen = !fullscreen;
00151   }
00152 
00153   camera.SetSize(width, height);
00154 
00155   return true;
00156 }
00157 
00158 void Video::ToggleFullscreen()
00159 {
00160   SDL_WM_ToggleFullScreen( window.GetSurface() );
00161   fullscreen = !fullscreen;
00162 }
00163 
00164 void Video::InitWindow(){
00165   InitSDL();
00166 
00167   window.SetSurface( NULL , false );
00168   window.SetAutoFree( false );
00169 
00170   Config * config = Config::GetInstance();
00171   SetConfig(config->tmp.video.width,
00172             config->tmp.video.height,
00173             config->tmp.video.fullscreen);
00174 
00175   if( window.IsNull() )
00176     Error( "Unable to initialize SDL window.");
00177 
00178   SetWindowCaption( std::string("Wormux ") + Constants::VERSION );
00179   SetWindowIcon( config->GetDataDir() + PATH_SEPARATOR + "wormux-32.xpm" );
00180 
00181   ComputeAvailableConfigs();
00182 }
00183 
00184 void Video::SetWindowCaption(std::string caption){
00185   SDL_WM_SetCaption( caption.c_str(), NULL );
00186 }
00187 
00188 void Video::SetWindowIcon(std::string filename){
00189   SDL_WM_SetIcon( IMG_Load(filename.c_str()), NULL );
00190 }
00191 
00192 void Video::InitSDL(){
00193   if( SDLReady )
00194     return;
00195 
00196   if( SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO) < 0 )
00197     Error( Format( _("Unable to initialize SDL library: %s"), SDL_GetError() ) );
00198 
00199   SDL_EnableUNICODE(1);
00200   SDLReady = true;
00201 }
00202 
00203 void Video::Flip(){
00204   window.Flip();
00205 }
00206 

Generated on Mon Jan 1 13:10:56 2007 for Wormux by  doxygen 1.4.7