MercuryWindowSDL.cpp

Go to the documentation of this file.
00001 #include "MercuryWindowSDL.h"
00002 #include "MercuryLog.h"
00003 #include "MercuryDisplay.h"
00004 #include "GL/glx.h"
00005 #include <SDL/SDL.h>
00006     
00007 MercuryWindowSDL::MercuryWindowSDL()
00008 :MercuryWindow(0,0,false)
00009 {
00010     LOG.Log("Video render: SDL");
00011 
00012     if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
00013     {
00014         LOG.Warn(MString("SDL initialization failed: ") + SDL_GetError());
00015         throw;
00016     }
00017     
00018     m_info = SDL_GetVideoInfo( );
00019     
00020     if ( !m_info )
00021     {
00022         LOG.Warn(MString("Video query failed: ") + SDL_GetError());
00023         throw;
00024     }
00025 }
00026 
00027 MercuryWindowSDL::~MercuryWindowSDL()
00028 {
00029     DestroyWindowAndRender();
00030 }
00031 
00032 bool MercuryWindowSDL::MakeWindow(const char* title, int width, int height, int bits, bool fullscreenflag)
00033 {
00034     SetTitle(title);
00035     m_width = width;
00036     m_height  = height;
00037     m_bits = bits;
00038 
00039     if( m_RT == RT_OGL )
00040     {
00041         m_flags  = SDL_OPENGL;          /* Enable OpenGL in SDL */
00042         m_flags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */
00043         m_flags |= SDL_HWPALETTE;       /* Store the palette in hardware */
00044         m_flags |= SDL_RESIZABLE;       /* Enable window resizing */       
00045     
00046         if (fullscreenflag)
00047             m_flags |= SDL_FULLSCREEN;
00048 
00049         if ( m_info->hw_available )
00050         {
00051             m_flags |= SDL_HWSURFACE;
00052             LOG.Log("Using hardware surface");
00053         }
00054         else
00055         {
00056             m_flags |= SDL_SWSURFACE;
00057             LOG.Log("Using software surface");
00058         }
00059     
00060         if ( m_info->blit_hw )
00061         {
00062             m_flags |= SDL_HWACCEL;
00063         }
00064         else
00065         {
00066             LOG.Log("No hardware acceleration");
00067         }
00068 
00069         SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); 
00070         MakeOGL();
00071         return true;
00072     }
00073     if( m_RT == RT_SW )
00074     {
00075         m_flags  = SDL_SWSURFACE;
00076         if( fullscreenflag )
00077             m_flags |= SDL_FULLSCREEN;
00078         m_BackBuffer = new unsigned char[m_width*m_height*m_bits/8];
00079         m_surface = SDL_SetVideoMode( m_width, m_height, m_bits, m_flags );
00080         if( !m_surface )
00081         {
00082             LOG.Warn( MString("SDL Set Video Mode FAILED: ") + SDL_GetError() );
00083             return false;
00084         }
00085         return true;
00086     }
00087     return false;
00088 }
00089 
00090 void MercuryWindowSDL::DestroyWindow()
00091 {
00092 }
00093 
00094 bool MercuryWindowSDL::MakeOGL()
00095 {
00096     m_surface = SDL_SetVideoMode( m_width, m_height, m_bits, m_flags );
00097 
00098     if ( !m_surface )
00099     {
00100         LOG.Log(MString("Video mode set failed: ") +  SDL_GetError());
00101         return false;
00102     }
00103     return true;
00104 }
00105 
00106 void MercuryWindowSDL::SwapBuffersInternal() 
00107 {
00108     if( m_RT == RT_OGL )
00109     {
00110         //glXWaitGL();
00111         SDL_GL_SwapBuffers( );
00112         //glXWaitX();
00113         //glXSwapBuffers(m_surface, true);
00114     }
00115     if( m_RT == RT_SW )
00116     {
00117         SDL_UpdateRect( m_surface,0,0,m_width,m_height);    
00118     }
00119 }
00120 
00121 bool MercuryWindowSDL::Update(const float dTime)
00122 {
00123     if (SDL_PollEvent(&m_event))
00124     {
00125         switch(m_event.type)
00126         {
00127             case SDL_ACTIVEEVENT:
00128                 if ( m_event.active.state & SDL_APPACTIVE )
00129                     if ( m_event.active.gain )
00130                         m_minimized = false;
00131                     else
00132                         m_minimized = true;
00133                 break;
00134             case SDL_VIDEORESIZE:
00135                 DISPLAY->Resize((int)m_event.resize.w, (int)m_event.resize.h);
00136                 SDL_SetVideoMode( m_width, m_height, m_bits, m_flags );
00137                 break;
00138             case SDL_QUIT:
00139                 return false;
00140         }
00141     }
00142     return true;
00143 }
00144 
00145 bool MercuryWindowSDL::HasFocus()
00146 {
00147     int state = SDL_GetAppState();
00148 
00149     if (state & SDL_APPINPUTFOCUS)
00150         return true;
00151 
00152     return false;
00153 }
00154 
00155 void MercuryWindowSDL::DestroyWindowAndRender()
00156 {
00157     SDL_Quit( );
00158 }
00159 
00160 bool MercuryWindowSDL::RestoreDevice()
00161 {
00162     return true;
00163 }
00164 
00165 bool MercuryWindowSDL::MakeRenderCurrent()
00166 {
00167     return true;
00168 }
00169 
00170 unsigned char * MercuryWindowSDL::GetBackDirectBuffer()
00171 {
00172     return (unsigned char*)m_surface->pixels;
00173 }
00174 
00175 bool MercuryWindowSDL::MakeRender(const char* title, int width, int height, int bits, bool fullscreenflag, RendererType rt )
00176 {
00177     m_RT = rt;
00178     return MakeWindow(title, width, height,bits, fullscreenflag);
00179 }
00180 
00181 /* 
00182  * Copyright (c) 2005-2006, Joshua Allen
00183  * All rights reserved.
00184  *
00185  * Redistribution and use in source and binary forms, with or
00186  * without modification, are permitted provided that the following
00187  * conditions are met:
00188  *  -   Redistributions of source code must retain the above
00189  *      copyright notice, this list of conditions and the following disclaimer.
00190  *  -   Redistributions in binary form must reproduce the above copyright
00191  *      notice, this list of conditions and the following disclaimer in
00192  *      the documentation and/or other materials provided with the distribution.
00193  *  -   Neither the name of the Mercury Engine nor the names of its
00194  *      contributors may be used to endorse or promote products derived from
00195  *      this software without specific prior written permission.
00196  *
00197  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00198  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00199  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00200  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00201  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00202  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00203  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00204  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00205  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00206  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00207  */

Hosted by SourceForge.net Logo