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;
00042 m_flags |= SDL_GL_DOUBLEBUFFER;
00043 m_flags |= SDL_HWPALETTE;
00044 m_flags |= SDL_RESIZABLE;
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
00111 SDL_GL_SwapBuffers( );
00112
00113
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
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207