Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

/Users/blackie/Documents/myRepository/phobosengine-vc2005/phobosengine/phobosengine/SDLApplication.cpp

Go to the documentation of this file.
00001 
00002 
00003 #include "SDLApplication.h"
00004 
00005 #include <windows.h>
00006 
00007 typedef void (APIENTRY *PFNWGLEXTSWAPCONTROLPROC) (int);
00008 typedef int (*PFNWGLEXTGETSWAPINTERVALPROC) (void);
00009 
00010 PFNWGLEXTSWAPCONTROLPROC wglSwapIntervalEXT = NULL;
00011 PFNWGLEXTGETSWAPINTERVALPROC wglGetSwapIntervalEXT = NULL;
00012 
00013 bool initVSync() {
00014         char* extensions = (char*)glGetString(GL_EXTENSIONS);
00015 
00016         if (strstr(extensions,"WGL_EXT_swap_control")) {
00017                 wglSwapIntervalEXT = (PFNWGLEXTSWAPCONTROLPROC)wglGetProcAddress("wglSwapIntervalEXT");
00018                 wglGetSwapIntervalEXT = (PFNWGLEXTGETSWAPINTERVALPROC)wglGetProcAddress("wglGetSwapIntervalEXT");
00019                 return true;
00020         } else {
00021                 return false;
00022         }
00023 }
00024 
00025 bool vsyncEnabled() {
00026         return (wglGetSwapIntervalEXT() > 0);
00027 }
00028 
00029 void setVSyncEnabled(bool enable) {
00030         if (enable) {
00031                 wglSwapIntervalEXT(1);
00032         } else {
00033                 wglSwapIntervalEXT(0);
00034         }
00035 }
00036 
00037 
00038 namespace pge {
00039 
00040 
00041         //****************************************************************************
00042         //
00043         //
00044         //
00045         //****************************************************************************
00046         SDLApplication::~SDLApplication(void) {
00047                 m_surface = NULL;
00048                 m_resX = 0;
00049                 m_resY = 0;
00050                 m_bits = 0;
00051                 m_fullscreen = false;
00052                 m_title = "";
00053                 m_videoFlags = 0;
00054                 m_running = false;
00055         }
00056 
00057 
00058         //****************************************************************************
00059         //
00060         //
00061         //
00062         //****************************************************************************
00063         bool SDLApplication::openWindow(int resX, int resY, int bits, bool fullscreen, const std::string &title) {
00064                 //
00065                 // Variables
00066                 //
00067                 const SDL_VideoInfo *videoInfo;
00068                 std::string logMessage;
00069 
00070 
00071                 m_resX = resX;
00072                 m_resY = resY;
00073                 m_bits = bits;
00074                 m_fullscreen = fullscreen;
00075                 m_title = title;
00076                 m_videoFlags = 0;
00077                 m_surface = NULL;
00078                 m_running = false;
00079 
00080                 /*pLogger::getInstance()->printLog("pSDLApplication: Trying to open window:");
00081                 if(m_fullscreen) {
00082                 pLogger::getInstance()->printLog("fullscreen.");
00083                 } else {
00084                 pLogger::getInstance()->printLog("window mode.");
00085                 }*/
00086 
00087                 // initialize SDL video mode and timer settings
00088                 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
00089                         //pLogger::getInstance()->printError("pSDLApplication: Could not initialize SDL.");
00090                         return false;
00091                 }
00092 
00093                 // set the video flags needed by SDL
00094                 m_videoFlags  = SDL_OPENGL;     // flag for opengl
00095                 m_videoFlags |= SDL_HWPALETTE;  // flag for access to hardware palette
00096                 m_videoFlags |= SDL_DOUBLEBUF;
00097                 m_videoFlags |= SDL_ANYFORMAT;
00098                 m_videoFlags |= SDL_OPENGLBLIT;
00099                 m_videoFlags |= SDL_RESIZABLE;  // enable the window to be resize-able;
00100                 if(m_fullscreen) {
00101                         m_videoFlags |= SDL_FULLSCREEN; // fullscreen
00102                 }
00103 
00104                 // get some hardware information
00105                 videoInfo = SDL_GetVideoInfo();
00106 
00107                 if(videoInfo == NULL) {
00108                         // receiving the info failed
00109                         //pLogger::getInstance()->printError("pSDLApplication: Could not get video info.");
00110                         return false;
00111                 }
00112 
00113                 // now set the available flags just tested
00114                 if(videoInfo->hw_available) {
00115                         m_videoFlags |= SDL_HWSURFACE;
00116                 } else {
00117                         m_videoFlags |= SDL_SWSURFACE;
00118                 }
00119                 // see if blitting is available
00120                 if(videoInfo->blit_hw) {
00121                         m_videoFlags |= SDL_HWACCEL;
00122                 }
00123 
00124                 // create an OpenGL double buffered rendering context
00125                 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
00126 
00127                 // the size of the OpenGL depth buffer
00128                 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, m_bits);
00129 
00130                 // size of the OpenGL stencil buffer
00131                 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
00132 
00133                 // size of the OpenGL accumulation buffer
00134                 SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 0);
00135                 SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 0);
00136                 SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 0);
00137                 SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 0);
00138 
00139                 m_surface = SDL_SetVideoMode(m_resX, m_resY, m_bits, m_videoFlags);
00140 
00141                 if(m_surface == NULL) {
00142                         //pLogger::getInstance()->printError("pSDLApplication: Could not set SDL video mode.");
00143                         return false;
00144                 }
00145 
00146                 // Hide the mouse pointer
00147                 SDL_ShowCursor(SDL_DISABLE);
00148 
00149                 // This is very important for smooth mouse moving.
00150                 SDL_WM_GrabInput(SDL_GRAB_ON);
00151 
00152                 // Set the window caption ( first arg for window, second one for the iconified mode)
00153                 SDL_WM_SetCaption(m_title.c_str(), m_title.c_str());
00154 
00155                 //pLogger::getInstance()->printLog("pSDLApplication: SDL ok.");
00156 
00157                 if(initVSync()) {
00158                         setVSyncEnabled(false);
00159                 }
00160 
00161                 return true;
00162         }
00163 
00164 
00165         //****************************************************************************
00166         //
00167         //
00168         //
00169         //****************************************************************************
00170         void SDLApplication::execute(void) {
00171                 //
00172                 // Variables
00173                 //
00174                 SDL_Event event;
00175 
00176 
00177                 //pLogger::getInstance()->printLog("pSDLApplication: Trying to execute application ...");
00178 
00179                 // check for initialization
00180                 if(m_surface == NULL) {
00181                         //pLogger::getInstance()->printError("pSDLApplication: Initialization error. Be sure to run openWindow(...) before executing!");
00182                         return;
00183                 } else {
00184                         //pLogger::getInstance()->printLog("pSDLApplication: Init done. Entering main loop!");
00185                 }
00186 
00187                 // Do the only global init just before entering the mainloop.
00188                 initCall();
00189 
00190                 m_running = true;
00191 
00192                 // SDL main loop.
00193                 // Continue main loop until running is false.
00194                 while(m_running) {
00195 
00196                         // get the SDL events
00197                         while(SDL_PollEvent(&event)) {
00198 
00199                                 // handle event
00200                                 switch (event.type) {
00201 
00202                                         // if the program received the SDL_QUIT event, then quit
00203                                 case SDL_QUIT:
00204                                         m_running = false;
00205                                         break;
00206 
00207                                         // if the program received a keystroke event
00208                                 case SDL_KEYDOWN:
00209                                         keyDownCall(event.key.keysym.sym);
00210                                         break;
00211 
00212                                 case SDL_KEYUP:
00213                                         keyUpCall(event.key.keysym.sym);
00214                                         break;
00215 
00216                                 case SDL_MOUSEMOTION:
00217                                         // Do not call mouseMotion(x, y); here because warping the pointer creates
00218                                         // a new mousemotion event -> the system gets caught in an endless loop
00219                                         break;
00220 
00221                                 case SDL_MOUSEBUTTONDOWN:
00222                                         mousePressCall(event.button.button, event.button.x, event.button.y);
00223                                         break;
00224 
00225                                 case SDL_MOUSEBUTTONUP:
00226                                         mouseReleaseCall(event.button.button, event.button.x, event.button.y);
00227                                         break;
00228 
00229                                         // if the program received a resize event
00230                                 case SDL_VIDEORESIZE:
00231                                         // request SDL to resize the window to the size and
00232                                         // depth etc. that we specify
00233                                         m_surface = SDL_SetVideoMode(event.resize.w, event.resize.h, m_bits, m_videoFlags);
00234                                         // reshape the OpenGL window
00235                                         reshapeCall(event.resize.w, event.resize.h);
00236                                         if(m_surface == NULL) {
00237                                                 //pLogger::getInstance()->printError("pSDLApplication: Could not reshape OpenGL window.");
00238                                                 return;
00239                                         }
00240                                         break;
00241                                 // The default action
00242                                 default:
00243                                         break;
00244                                 }
00245                         }
00246                         mainLoopCall();
00247                 }
00248 
00249                 // program has run, shutdown now
00250                 //pLogger::getInstance()->printLog("pSDLApplication: Shutting down window.");
00251                 //pLogger::getInstance()->printLog("pSDLApplication: Bye Bye!");
00252 
00253                 SDL_FreeSurface(m_surface);
00254                 SDL_ShowCursor(SDL_ENABLE);
00255                 SDL_Quit();
00256         }
00257 
00258 
00259         //****************************************************************************
00260         //
00261         //
00262         //
00263         //****************************************************************************
00264         void SDLApplication::shutdown(void) {
00265                 m_running = false;
00266         }
00267 
00268 
00269         //****************************************************************************
00270         //
00271         //
00272         //
00273         //****************************************************************************
00274         void SDLApplication::sleep(unsigned int delay) {
00275                 SDL_Delay(delay);
00276         }
00277 
00278 
00279         //****************************************************************************
00280         //
00281         //
00282         //
00283         //****************************************************************************
00284         void SDLApplication::swapBuffer() {
00285                 SDL_GL_SwapBuffers();
00286         }
00287 
00288 
00289         //****************************************************************************
00290         //
00291         //
00292         //
00293         //****************************************************************************
00294         unsigned int SDLApplication::getTicks() {
00295                 return SDL_GetTicks();
00296         }
00297 
00298 
00299         //****************************************************************************
00300         //
00301         //
00302         //
00303         //****************************************************************************
00304         void SDLApplication::setMousePointer(int x, int y) {
00305                 SDL_WarpMouse(x, y);
00306         }
00307 
00308 
00309         //****************************************************************************
00310         //
00311         //
00312         //
00313         //****************************************************************************
00314         void SDLApplication::setMousePointer(const MouseCoordinate &coord) {
00315                 SDL_WarpMouse(coord.x, coord.y);
00316         }
00317 
00318 
00319         //****************************************************************************
00320         //
00321         //
00322         //
00323         //****************************************************************************
00324         SDLApplication::MouseCoordinate SDLApplication::getMousePointer(void) {
00325                 MouseCoordinate c;
00326 
00327 
00328                 SDL_GetMouseState(&c.x, &c.y);
00329                 return c;
00330         }
00331 
00332 
00333         //****************************************************************************
00334         //
00335         //
00336         //
00337         //****************************************************************************
00338         int SDLApplication::getResolutionWidth(void) {
00339                 return m_resX;
00340         }
00341 
00342 
00343         //****************************************************************************
00344         //
00345         //
00346         //
00347         //****************************************************************************
00348         int SDLApplication::getResolutionHeight(void) {
00349                 return m_resY;
00350         }
00351 
00352 
00353         //****************************************************************************
00354         //
00355         //
00356         //
00357         //****************************************************************************
00358         int SDLApplication::getDisplayDepth(void) {
00359                 return m_bits;
00360         }
00361 
00362 
00363         //****************************************************************************
00364         //
00365         //
00366         //
00367         //****************************************************************************
00368         bool SDLApplication::isFullscreen(void) {
00369                 return m_fullscreen;
00370         }
00371 
00372 
00373         //****************************************************************************
00374         //
00375         //
00376         //
00377         //****************************************************************************
00378         std::string SDLApplication::getTitle(void) {
00379                 return m_title;
00380         }
00381 };

Generated on Mon Oct 16 12:08:11 2006 for Phobosengine by doxygen 1.3.4