Back to index

extremetuxracer  0.5beta
videoconfig.cpp
Go to the documentation of this file.
00001 /* 
00002  * ETRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  * 
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (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 PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public 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 /*
00021  * Note:
00022  * Perhaps we should combine resolution and bpps in one list.
00023  * The only problem i see here is that the videomode autodetection
00024  * cannot detect what resolutions are supported in different 
00025  * bpp modes.
00026  *
00027  * Multisamples:
00028  * This is currently a little bit broken.
00029  * There is no test whether the hardware supports this and
00030  * the list of available multisamples is static. 
00031  */
00032 
00033 
00034  
00035 #include "videoconfig.h"
00036 
00037 #include "game_config.h"
00038 #include "ppgltk/ui_mgr.h"
00039 #include "winsys.h"
00040 
00042 resolution_t resolutions[] = {     {"1280 x 1024",1280,1024},
00043                                                         {"1152 x 864",1152,864},
00044                                                         {"1024 x 768",1024,768},
00045                                                         {"800 x 600",800,600},
00046                                                         {"640 x 480",640,480}};
00047 
00049 bpp_t bpps[] = {     {"Display",0},
00050                                           {"16",1},
00051                                           {"32",2}};
00052 
00054 multisample_t multisamples[] = {   {"1",1},
00055                                           {"2",2},
00056                                           {"4",4}};                                 
00057        
00058 VideoConfig::VideoConfig()
00059 {
00060        setTitle(_("Video Configuration"));       
00061        
00062        pp::Vec2d pos(0,0);
00063        
00064        // resolution listbox 
00065        
00066        bool found=false;
00067        #ifdef _WIN32
00068               //Skip the resolutionbox
00069        #else
00070        
00071        std::list<resolution_t>::iterator resit;
00072 
00073        
00074        initVideoModes();
00075        
00076        // check for the current resolution
00077        for (resit = m_resolutionList.begin();
00078                resit != m_resolutionList.end();
00079                resit++)
00080        {
00081               if ( (*resit).x==getparam_x_resolution() && (*resit).y==getparam_y_resolution())
00082                      break;                
00083        }
00084 
00085        if (resit == m_resolutionList.end()){
00086               // current resolution not in list
00087               // therefore we simply add this
00088               char buff[16];
00089               resolution_t resolution;
00090               sprintf(buff,"%d x %d",getparam_x_resolution(), getparam_y_resolution());
00091               resolution.name = buff;
00092               resolution.x=getparam_x_resolution();
00093               resolution.y=getparam_y_resolution();
00094               m_resolutionList.insert(m_resolutionList.begin(),resolution);
00095               resit = m_resolutionList.begin();
00096        }
00097        
00098 
00099        mp_resolutionListbox = new pp::Listbox<resolution_t>( pos,
00100                                pp::Vec2d(190, 32),
00101                                "listbox_item",
00102                                m_resolutionList);
00103      mp_resolutionListbox->setCurrentItem( resit );
00104        
00105        //bpp listbox
00106        std::list<bpp_t>::iterator bppit;
00107        for (unsigned int i=0; i < sizeof(bpps)/sizeof(bpp_t) ; i++) {
00108               m_bppList.push_back(bpps[i]);
00109               if(bpps[i].data==getparam_bpp_mode()){
00110                      bppit = --m_bppList.end();
00111               }
00112     }
00113     mp_bppListbox = new pp::Listbox<bpp_t>( pos,
00114                                    pp::Vec2d(190, 32),
00115                                    "listbox_item",
00116                                    m_bppList);
00117     mp_bppListbox->setCurrentItem( bppit );
00118        
00119        mp_fullscreenBox = new pp::CheckBox(pos, pp::Vec2d(32, 32) );
00120        mp_fullscreenBox->setState( getparam_fullscreen() );
00121 #endif
00122        
00123        mp_stencilBox = new pp::CheckBox(pos, pp::Vec2d(32, 32) );
00124        mp_stencilBox->setState( getparam_stencil_buffer() );
00125        
00126        //multisample listbox
00127        found=false;
00128        std::list<multisample_t>::iterator multiit;
00129        for (unsigned int i=0; i < sizeof(multisamples)/sizeof(multisample_t) ; i++) {
00130               m_multisampleList.push_back(multisamples[i]);
00131               if(multisamples[i].data==getparam_multisamples()){
00132                      multiit = --m_multisampleList.end();
00133                      found=true;
00134               }
00135     }
00136        if(!found) multiit = m_multisampleList.begin();
00137     mp_multisampleListbox = new pp::Listbox<multisample_t>( pos,
00138                                    pp::Vec2d(150, 32),
00139                                    "listbox_item",
00140                                    m_multisampleList);
00141     mp_multisampleListbox->setCurrentItem( multiit );
00142        
00143        mp_fsaaBox = new pp::CheckBox(pos, pp::Vec2d(32, 32) );
00144        mp_fsaaBox->setState( getparam_enable_fsaa());
00145 }
00146 
00147 VideoConfig::~VideoConfig()
00148 {
00149        #ifdef _WIN32
00150        #else
00151               delete mp_resolutionListbox;
00152               delete mp_bppListbox;
00153               delete mp_fullscreenBox;
00154        #endif
00155        
00156        delete mp_multisampleListbox;
00157        delete mp_stencilBox;
00158        delete mp_fsaaBox;
00159 }
00160 
00161 void
00162 VideoConfig::setWidgetPositions()
00163 {
00164        int width = 550;
00165        int height = 240;
00166        
00167        pp::Vec2d pos(getparam_x_resolution()/2 - width/2,
00168                               getparam_y_resolution()/2 + height/2);
00169        
00170        pp::Font *font = pp::Font::get("button_label");
00171        
00172        #ifdef _WIN32
00173               font->draw(_("To change the resolution, or switch into fullscreen mode"),pos);
00174               pos.y-=30;
00175               font->draw(_("use options.txt, located in the config folder."),pos);
00176        #else
00177               font->draw(_("Resolution:"),pos);
00178               mp_resolutionListbox->setPosition(pp::Vec2d(pos.x+width-190,pos.y));  
00179        
00180               pos.y-=40;
00181               font->draw(_("Bits Per Pixel:"),pos);
00182               mp_bppListbox->setPosition(pp::Vec2d(pos.x+width-190,pos.y));
00183               
00184               pos.y-=40;
00185               font->draw(_("Fullscreen:"),pos);
00186               mp_fullscreenBox->setPosition(pp::Vec2d(pos.x+width-32,pos.y));
00187        #endif
00188        pos.y-=80;
00189        font->draw(_("Experimental (needs restart)"),pos);
00190        
00191        pos.y-=40;
00192        font->draw(_("Stencil Buffer:"),pos);
00193        mp_stencilBox->setPosition(pp::Vec2d(pos.x+width-32,pos.y));
00194        
00195        pos.y-=40;
00196        font->draw(_("Enable FSAA:"),pos);
00197        mp_fsaaBox->setPosition(pp::Vec2d(pos.x+width-32,pos.y));
00198        
00199        pos.y-=40;
00200        font->draw(_("FSAA Multisamples:"),pos);
00201        mp_multisampleListbox->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
00202 
00203 }
00204 
00205 void
00206 VideoConfig::apply()
00207 {
00208        bool updatevideo = false;
00209        
00210        std::list<multisample_t>::iterator multiit = mp_multisampleListbox->getCurrentItem();
00211        
00212        #ifdef _WIN32
00213               //Skip the resolutionbox
00214        #else
00215               std::list<resolution_t>::iterator resit = mp_resolutionListbox->getCurrentItem();
00216               std::list<bpp_t>::iterator bppit = mp_bppListbox->getCurrentItem();
00217        
00218 
00219               if ( (*resit).x != getparam_x_resolution() ){
00220                      setparam_x_resolution((*resit).x);
00221                      setparam_y_resolution((*resit).y);
00222                      updatevideo=true;    
00223               }
00224        
00225        
00226               if ( (*bppit).data != getparam_bpp_mode() ){
00227                      setparam_bpp_mode((*bppit).data);
00228                      updatevideo=true;    
00229               }
00230               
00231               if (mp_fullscreenBox->getState() != getparam_fullscreen() ){
00232                      setparam_fullscreen(bool( mp_fullscreenBox->getState() ));
00233                      updatevideo=true;
00234               }
00235        #endif
00236        
00237        if (mp_stencilBox->getState() != getparam_stencil_buffer() ){
00238               setparam_stencil_buffer(bool( mp_stencilBox->getState() ));
00239               updatevideo=true;
00240        }
00241        
00242        if (mp_fsaaBox->getState() != getparam_enable_fsaa() ){
00243               setparam_enable_fsaa(bool( mp_fsaaBox->getState() ));
00244               updatevideo=true;
00245        }
00246        
00247        if ( (*multiit).data != getparam_multisamples() ){
00248               setparam_multisamples((*multiit).data);
00249               updatevideo=true;    
00250        }
00251        
00252        #ifdef _WIN32
00253               //Skip video update
00254        #else
00255        if (updatevideo==true){
00256               printf("Set new videomode:%dx%d bpp:%d \n", (*resit).x, (*resit).y,(*bppit).data);
00257               setup_sdl_video_mode();
00258        }
00259        #endif
00260        
00261        write_config_file();
00262        
00263        set_game_mode( GameMode::prevmode );
00264     UIMgr.setDirty();       
00265 }
00266 
00267 void
00268 VideoConfig::initVideoModes()
00274 {
00275        SDL_Rect **modes;
00276        int i;
00277 
00278        // get available fullscreen OpenGL modes
00279        if(!getparam_disable_videomode_autodetection()){ 
00280               modes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_OPENGL);
00281        }else{
00282               modes=(SDL_Rect **)-1;
00283        }
00284        // check if we cannot receive the modes
00285        if(    modes == (SDL_Rect **)-1 ||
00286               modes == (SDL_Rect **)0 )
00287        {
00288               // unable to find modes... fall back to standard modes
00289               for (unsigned int i=0; i<sizeof(resolutions)/sizeof(resolution_t); i++) {
00290                      m_resolutionList.push_back(resolutions[i]);
00291               }      
00292        }else{
00293               char buff[16];
00294               resolution_t resolution;
00295               
00296               // fill list with modes
00297               for(i=0; modes[i]; ++i){
00298                      sprintf(buff,"%d x %d", modes[i]->w, modes[i]->h);
00299                      resolution.name = buff;
00300                      resolution.x=modes[i]->w;
00301                      resolution.y=modes[i]->h;
00302                      m_resolutionList.push_back(resolution);   
00303               }      
00304        }
00305 }