demos/SdlDemo.cpp

Go to the documentation of this file.
00001 
00002 #include "SDL.h"
00003 #include "DemoConfig.h"
00004 
00005 
00006 DemoEventHandler handler;
00007 GuiWrapper gui(&handler);
00008 unsigned int oldTime = 0;
00009 
00010 bool InitSDL();
00011 void RenderScene();
00012 void ResizeWindow(int width, int height);
00013 
00014  
00015 
00016 int main (int argc, char *argv[]) {
00017 
00018         bool done = false;
00019         int videoFlags;
00020         int SCREEN_WIDTH  = 700;
00021         int SCREEN_HEIGHT = 500;
00022         int SCREEN_BPP    = 32;
00023 
00024         const char APP_NAME[] = "SDL Demo";
00025 
00026         std::string DataDir = DEMO_DATA_DIR;
00027         if (argc == 2) DataDir = argv[1];
00028 
00029         SDL_Event event;
00030         SDL_Surface *surface;
00031 
00032         //init sdl and sdl_net
00033         if ( !InitSDL() ) {
00034                 printf("Video initialization failed.\n");
00035                 exit(0);
00036         }
00037 
00038         //call SDL_Quit if program exits
00039         atexit(SDL_Quit);
00040 
00041 
00042         //set openGL attribute
00043         SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
00044 
00045         //set window title
00046         SDL_WM_SetCaption(APP_NAME, APP_NAME);
00047 
00048         //set video flags
00049         videoFlags = SDL_OPENGL | SDL_RESIZABLE;    
00050 
00051         // get a SDL surface
00052         surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
00053                                 videoFlags );
00054 
00055         //check that we got a surface
00056         if ( !surface ) {
00057                 printf("Video mode set failed: %s\n", SDL_GetError( ) );
00058                 exit(0);
00059         }
00060 
00061 
00062         //initialize gui
00063         if ( !gui.Init(DataDir.c_str()) ) {
00064                 printf("Failed to initialize gui.\n");
00065                 exit(0);
00066         }
00067 
00068         //initialize window size
00069         ResizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
00070 
00071         //--- main loop ----
00072 
00073         while ( !done ) {
00074 
00075             //handle the events in the queue 
00076             while ( SDL_PollEvent( &event ) )
00077                 {
00078                     switch( event.type )
00079                         {
00080                             
00081                         case SDL_VIDEORESIZE:
00082                                 surface = SDL_SetVideoMode( event.resize.w, event.resize.h, SCREEN_BPP, videoFlags );
00083                                 if ( !surface ) {
00084                                         printf("Could not get a surface after resize: %s\n", SDL_GetError() );
00085                                         exit(0);
00086                                 }
00087 
00088                                 ResizeWindow( event.resize.w, event.resize.h );
00089                                 RenderScene();
00090                                 break;
00091                         case SDL_KEYDOWN:
00092                                 gui.AsciiKeyDown( event.key.keysym.sym );
00093                                 break;
00094                         case SDL_MOUSEMOTION:
00095                                 gui.MouseMoved(event.motion.x, event.motion.y); //event.motion.xrel, event.motion.yrel
00096                                 break;
00097                         case SDL_MOUSEBUTTONDOWN:
00098                                 if (event.button.button == SDL_BUTTON_LEFT)
00099                                         gui.MouseClicked(1, true, event.button.x, event.button.y);
00100                                 else if (event.button.button == SDL_BUTTON_RIGHT)
00101                                         gui.MouseClicked(2, true, event.button.x, event.button.y);
00102                                 break;
00103                         case SDL_MOUSEBUTTONUP:
00104                                 if (event.button.button == SDL_BUTTON_LEFT)
00105                                         gui.MouseClicked(1, false, event.button.x, event.button.y);
00106                                 else if (event.button.button == SDL_BUTTON_RIGHT)
00107                                         gui.MouseClicked(2, false, event.button.x, event.button.y);
00108                                 break;
00109                         case SDL_QUIT:
00110                                 done = true;
00111                                 break;
00112                         default:
00113                                 break;
00114                         }
00115                 }
00116 
00117                 if (!done) {
00118 
00119                         //render
00120                         RenderScene();
00121                 }
00122         }
00123 
00124         SDL_Quit();
00125 
00126         return 0;
00127 }
00128 
00129 bool InitSDL() {
00130 
00131         // initialize SDL
00132         if(SDL_Init( SDL_INIT_VIDEO ) < 0)
00133         {
00134                 printf("SDL_Init: %s\n",SDL_GetError());
00135                 return false;
00136         }
00137 
00138         //turn on keyboard key repeat
00139         if ( ( SDL_EnableKeyRepeat( 500, SDL_DEFAULT_REPEAT_INTERVAL ) ) ) {
00140                 printf("Setting keyboard repeat failed: %s\n", SDL_GetError( ) );
00141                 return false;
00142         }
00143 
00144         return true;
00145 }
00146 
00147 void ResizeWindow(int width, int height) {
00148 
00149   float aspectRatio = 1.33f;
00150   height            = height <= 0 ? 1 : height;
00151   aspectRatio       = (float)width/height;
00152 
00153   glViewport(0, 0, width, height);
00154   glMatrixMode(GL_PROJECTION);
00155 
00156   glLoadIdentity();
00157   gluPerspective(90.0f, aspectRatio, 1.0f, 1500.0f);
00158 
00159   glMatrixMode(GL_MODELVIEW);
00160   glLoadIdentity();
00161 
00162   gui.SetDimensions(width, height);
00163 
00164 }
00165 
00166 void RenderScene()
00167 {
00168         glClearColor(handler.red, handler.green, handler.blue, 0.0f);
00169         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00170 
00171         //get elapsed milliseconds
00172         int time = SDL_GetTicks();
00173         gui.Update((time-oldTime)/1000.0f);
00174         oldTime = time;
00175 
00176         if (gui.IsActive()) gui.RenderScene();
00177         SDL_GL_SwapBuffers( );
00178 
00179 }
00180 
00181 
00182 
00183 
00184 

Generated on Wed Dec 5 20:32:03 2007 for GLWX by  doxygen 1.5.3