Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

video_sdl.cpp

00001 //FILE:         video_sdl.cpp
00002 //AUTHOR:       Nathan Cournia <nathan@cournia.com>
00003 
00004 #ifdef WIN32
00005 #define WIN32_LEAN_AND_MEAN
00006 #include <windows.h>
00007 #endif
00008 
00009 #include <GL/gl.h>
00010 #include <GL/glu.h>
00011 #include "gl.h"
00012 #include "video_sdl.h"
00013 
00015 video_sdl::video_sdl( void ): m_shutdown( false )
00016 { }
00017 
00019 video_sdl::~video_sdl( void ) 
00020 { 
00021         shutdown( );
00022 }
00023 
00025 bool
00026 video_sdl::init( unsigned int w, unsigned int h, unsigned int bpp, bool fs )
00027 {
00028         //make sure passed in vars are sane
00029         if( w < 1 ) w = 1;
00030         if( h < 1 ) h = 1;
00031         if( bpp < 16 ) bpp = 16;
00032 
00033         //copy vars to internal vars
00034         m_width = w;
00035         m_height = h;
00036         m_bpp = bpp;
00037         m_fullscreen = fs;
00038         
00039         //initialize sdl
00040         if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
00041                 set_error( SDL_GetError( ) );
00042                 return false;
00043         }
00044         //atexit( SDL_Quit );
00045 
00046         //fullscreen
00047         int flags = SDL_OPENGL;
00048         if( m_fullscreen ) {
00049                 flags |= SDL_FULLSCREEN;
00050         }
00051         
00052         //initialize video
00053         SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
00054         m_screen = SDL_SetVideoMode( m_width, m_height, m_bpp, flags);
00055         if( m_screen == NULL ) {
00056                 set_error( SDL_GetError( ) );
00057                 return false;
00058         }
00059 
00060         //initialize gl
00061         gl::init( m_width, m_height );
00062 
00063         //initalize the clock
00064         m_clock.reset( );
00065 
00066         //success
00067         return true;
00068 }
00069 
00071 void 
00072 video_sdl::set_title( const std::string& title )
00073 {
00074         SDL_WM_SetCaption( title.c_str( ), title.c_str( ) );
00075 }
00076 
00078 void 
00079 video_sdl::resize( unsigned int w, unsigned int h )
00080 { }
00081 
00083 void
00084 video_sdl::start_frame( void )
00085 { 
00086         //update the clock
00087         m_clock.update( );
00088 
00089         //update instant fps
00090         m_fps = 1.0 / m_clock.elapsed( );
00091 
00092         //inc frame count;
00093         ++m_frame_count;
00094 
00095         //update avg fps
00096         m_fps_sum += m_clock.elapsed( );
00097         if( m_fps_sum > 1.0 ) {
00098                 m_avg_fps = m_fps_sum * static_cast<double>( m_frame_count );
00099                 m_fps_sum = 0.0;
00100                 m_frame_count = 0;
00101         }
00102 
00103         //clear OpenGL buffers
00104         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00105 }
00106 
00108 void
00109 video_sdl::end_frame( void )
00110 { 
00111         //swap buffers
00112         SDL_GL_SwapBuffers( );
00113 }
00114 
00116 void 
00117 video_sdl::toggle_fullscreen( void )
00118 {
00119         SDL_WM_ToggleFullScreen( m_screen );
00120 
00121         if( m_screen->flags & SDL_FULLSCREEN ) {
00122                 m_fullscreen = true;
00123         } else {
00124                 m_fullscreen = false;
00125         }
00126 }
00127 
00129 void 
00130 video_sdl::shutdown( void ) 
00131 { 
00132         if( !m_shutdown ) {
00133                 SDL_Quit( );
00134                 m_shutdown = true;
00135         }
00136 }

Generated on Tue Feb 11 18:49:42 2003 for uber by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002