Back to index

extremetuxracer  0.5beta
winsys.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  * 
00005  * Copyright (C) 1999-2001 Jasmin F. Patry
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00021 
00022 #include "winsys.h"
00023 #include "game_config.h"
00024 #include "loop.h"
00025 #include "render_util.h"
00026 #include "highscore.h"
00027 #include "player.h"
00028 
00029 #include "ppgltk/audio/audio.h"
00030 
00031 /* Windowing System Abstraction Layer */
00032 /* Abstracts creation of windows, handling of events, etc. */
00033 
00034 #if defined( HAVE_SDL_MIXER )
00035 #   include "SDL_mixer.h"
00036 #endif
00037 
00038 static SDL_Surface *screen = NULL;
00039 
00040 static bool redisplay = false;
00041 
00042 
00043 /*---------------------------------------------------------------------------*/
00050 void winsys_post_redisplay() 
00051 {
00052     redisplay = true;
00053 }
00054 
00055 
00056 /*---------------------------------------------------------------------------*/
00063 void winsys_swap_buffers()
00064 {
00065     SDL_GL_SwapBuffers();
00066 }
00067 
00068 
00069 /*---------------------------------------------------------------------------*/
00076 void winsys_warp_pointer( int x, int y )
00077 {
00078     SDL_WarpMouse( x, y );
00079 }
00080 
00081 
00082 /*---------------------------------------------------------------------------*/
00089 void setup_sdl_video_mode()
00090 {
00091     Uint32 video_flags = SDL_OPENGL; 
00092     int bpp = 0;
00093     int width, height;
00094 
00095     if ( getparam_fullscreen() ) {
00096               video_flags |= SDL_FULLSCREEN;
00097     } else {
00098               video_flags |= SDL_RESIZABLE;
00099     }
00100        
00101 #if SDL_VERSION_ATLEAST(1,2,6)
00102        if(getparam_enable_fsaa()){
00103               //enable FSAA
00104               SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1);
00105               SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, getparam_multisamples());
00106        }
00107 #endif
00108        
00109        if(getparam_stencil_buffer()){
00110               SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8 );
00111        }
00112 
00113     switch ( getparam_bpp_mode() ) {
00114     case 0:
00115               /* Use current bpp */
00116               bpp = 0;
00117        break;
00118 
00119     case 1:
00120               /* 16 bpp */
00121               bpp = 16;
00122        break;
00123 
00124     case 2:
00125               /* 32 bpp */
00126               bpp = 32;
00127        break;
00128 
00129     default:
00130               setparam_bpp_mode( 0 );
00131               bpp = getparam_bpp_mode();
00132     }
00133 
00134     width = getparam_x_resolution();
00135        
00136        if(getparam_x_resolution_half_width()){
00137               width /=2;           
00138        }
00139        
00140     height = getparam_y_resolution();
00141     
00142     if(width < 800) width = 800;
00143     if(height < 600) height = 600;
00144 
00145     if ( ( screen = SDL_SetVideoMode( width, height, bpp, video_flags ) ) ==  NULL ){
00146        handle_system_error( 1, "Couldn't initialize video: %s", 
00147                           SDL_GetError() );
00148     }
00149        
00150        glViewport(0,0,width,height);
00151 
00152 }
00153 
00154 
00155 /*---------------------------------------------------------------------------*/
00163 void winsys_init( int *argc, char **argv, char *window_title, 
00164                 char *icon_title )
00165 {
00166     Uint32 sdl_flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
00167 
00168     /*
00169      * Initialize SDL
00170      */
00171     if ( SDL_Init( sdl_flags ) < 0 ) {
00172        handle_error( 1, "Couldn't initialize SDL: %s", SDL_GetError() );
00173     }
00174 
00175 
00176     /* 
00177      * Init video 
00178      */
00179     SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
00180 
00181     setup_sdl_video_mode();
00182 
00183     SDL_WM_SetCaption( window_title, icon_title );
00184 
00185 }
00186 
00187 
00188 /*---------------------------------------------------------------------------*/
00195 void winsys_shutdown()
00196 {
00197     SDL_Quit();
00198 }
00199 
00200 /*---------------------------------------------------------------------------*/
00208 void winsys_enable_key_repeat( bool enabled )
00209 {
00210     if ( enabled ) {
00211        SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY,
00212                           SDL_DEFAULT_REPEAT_INTERVAL );
00213     } else {
00214        SDL_EnableKeyRepeat( 0, 0 );
00215     }
00216 }
00217 
00218 
00219 /*---------------------------------------------------------------------------*/
00226 void winsys_show_cursor( bool visible )
00227 {
00228     SDL_ShowCursor( visible );
00229 }
00230 
00231 /*---------------------------------------------------------------------------*/
00239 void winsys_process_events()
00240 {
00241        SDL_Event event; 
00242     int x, y;
00243 
00244     while (true) {
00245 
00246        SDL_LockAudio();
00247        SDL_UnlockAudio();
00248 
00249        while ( SDL_PollEvent( &event ) ) {
00250            
00251            switch ( event.type ) {
00252            case SDL_KEYDOWN:
00253                   SDL_GetMouseState( &x, &y );
00254                      loop_keyboard_func( event.key.keysym.sym,
00255                                           event.key.keysym.mod,
00256                                    false,
00257                                    x, y );
00258                      break;
00259 
00260            case SDL_KEYUP:
00261                   SDL_GetMouseState( &x, &y );
00262                   loop_keyboard_func( event.key.keysym.sym,
00263                                    event.key.keysym.mod,
00264                                    true,
00265                                    x, y );
00266               break;
00267 
00268            case SDL_MOUSEBUTTONDOWN:
00269            case SDL_MOUSEBUTTONUP:
00270                      loop_mouse_func( event.button.button,
00271                                event.button.state,
00272                                event.button.x,
00273                                event.button.y );
00274               break;
00275 
00276            case SDL_MOUSEMOTION:
00277               if ( event.motion.state ) {
00278                   /* buttons are down */
00279                      loop_mouse_motion_func( event.motion.x,
00280                                    event.motion.y );
00281               } else {
00282                   /* no buttons are down */
00283                      loop_mouse_motion_func( event.motion.x,
00284                                           event.motion.y );
00285               }
00286               break;
00287 
00288            case SDL_VIDEORESIZE:
00289               setup_sdl_video_mode();
00290                   reshape( event.resize.w,
00291                                  event.resize.h );
00292               break;
00293                      
00294               case SDL_QUIT:
00295                      winsys_exit(0);             
00296                      break;
00297               
00298            }
00299 
00300            SDL_LockAudio();
00301            SDL_UnlockAudio();
00302        }
00303 
00304        if ( redisplay ) {
00305            redisplay = false;
00306        }
00307        main_loop();
00308 
00309        /* Delay for 1 ms.  This allows the other threads to do some
00310           work (otherwise the audio thread gets starved). */
00311        SDL_Delay(1);
00312 
00313     }
00314 
00315     /* Never exits */
00316     code_not_reached();
00317 }
00318 
00319 
00320 /*---------------------------------------------------------------------------*/
00327 extern void cleanup(void);
00328 
00329 void winsys_exit( int code )
00330 {
00331     players[0].saveData();
00332     Highscore->saveData();
00333     cleanup();
00334     
00335     exit( code );
00336 }