00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "video.h"
00021 #include <string>
00022 #include <iostream>
00023 #include <SDL_endian.h>
00024 #include <SDL_image.h>
00025 #include "../game/config.h"
00026 #include "../tool/error.h"
00027 #include "../tool/i18n.h"
00028 #include "../include/app.h"
00029 #include "../include/constant.h"
00030 #include "../map/camera.h"
00031
00032 Video::Video(){
00033 SetMaxFps (50);
00034 fullscreen = false;
00035 SDLReady = false;
00036 }
00037
00038 Video::~Video(){
00039 if( SDLReady )
00040 SDL_Quit();
00041 }
00042
00043 void Video::SetMaxFps(uint max_fps){
00044 m_max_fps = max_fps;
00045 if (0 < m_max_fps)
00046 m_sleep_max_fps = 1000/m_max_fps;
00047 else
00048 m_sleep_max_fps = 0;
00049 }
00050
00051 uint Video::GetMaxFps(){
00052 return m_max_fps;
00053 }
00054
00055 uint Video::GetSleepMaxFps(){
00056 return m_sleep_max_fps;
00057 }
00058
00059 bool Video::IsFullScreen() const{
00060 return fullscreen;
00061 }
00062
00063 bool CompareConfigs(const Point2i& a, const Point2i& b)
00064 {
00065 return (a.x >= b.x) && (a.y >= b.y);
00066 }
00067
00068 void Video::ComputeAvailableConfigs()
00069 {
00070
00071 available_configs.push_back(Point2i(AppWormux::GetInstance()->video.window.GetWidth(),
00072 AppWormux::GetInstance()->video.window.GetHeight()));
00073
00074
00075 SDL_Rect **modes;
00076
00077
00078 modes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
00079
00080
00081 if(modes != NULL){
00082
00083 for(int i=0;modes[i];++i) {
00084
00085 if (modes[i]->w < 800 || modes[i]->h < 600) break;
00086 available_configs.push_back(Point2i(modes[i]->w, modes[i]->h));
00087 }
00088 }
00089
00090
00091 available_configs.sort(CompareConfigs);
00092
00093
00094
00095 Point2i a(800, 600);
00096 if ( CompareConfigs((*available_configs.begin()), a))
00097 available_configs.push_back(a);
00098 Point2i b(1024, 768);
00099 if ( CompareConfigs((*available_configs.begin()), b))
00100 available_configs.push_back(b);
00101 Point2i c(1280, 1024);
00102 if ( CompareConfigs((*available_configs.begin()), c))
00103 available_configs.push_back(c);
00104 Point2i d(1600, 1200);
00105 if ( CompareConfigs((*available_configs.begin()), d))
00106 available_configs.push_back(d);
00107
00108
00109 available_configs.sort(CompareConfigs);
00110
00111
00112 std::list<Point2i>::iterator prev = available_configs.begin(),
00113 it = available_configs.begin() ,
00114 end = available_configs.end();
00115
00116 for (++it; it != end ; ++it) {
00117 if ((*prev)==(*it))
00118 prev = available_configs.erase(prev);
00119 else
00120 prev++;
00121 }
00122 }
00123
00124 std::list<Point2i>& Video::GetAvailableConfigs()
00125 {
00126 return available_configs;
00127 }
00128
00129 bool Video::SetConfig(int width, int height, bool _fullscreen){
00130
00131 if( window.IsNull() ||
00132 (width != window.GetWidth() ||
00133 height != window.GetHeight() ) ){
00134
00135 window.SetSurface( SDL_SetVideoMode(width, height, 32,
00136 SDL_HWSURFACE | SDL_HWACCEL | SDL_DOUBLEBUF ), false );
00137
00138 if( window.IsNull() ) {
00139 window.SetSurface( SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE) );
00140 std::cerr << "WARNING: Video not using hardware acceleration!" << std::endl;
00141 }
00142
00143 if( window.IsNull() )
00144 return false;
00145 fullscreen = false;
00146 }
00147
00148 if(fullscreen != _fullscreen) {
00149 SDL_WM_ToggleFullScreen( window.GetSurface() );
00150 fullscreen = !fullscreen;
00151 }
00152
00153 camera.SetSize(width, height);
00154
00155 return true;
00156 }
00157
00158 void Video::ToggleFullscreen()
00159 {
00160 SDL_WM_ToggleFullScreen( window.GetSurface() );
00161 fullscreen = !fullscreen;
00162 }
00163
00164 void Video::InitWindow(){
00165 InitSDL();
00166
00167 window.SetSurface( NULL , false );
00168 window.SetAutoFree( false );
00169
00170 Config * config = Config::GetInstance();
00171 SetConfig(config->tmp.video.width,
00172 config->tmp.video.height,
00173 config->tmp.video.fullscreen);
00174
00175 if( window.IsNull() )
00176 Error( "Unable to initialize SDL window.");
00177
00178 SetWindowCaption( std::string("Wormux ") + Constants::VERSION );
00179 SetWindowIcon( config->GetDataDir() + PATH_SEPARATOR + "wormux-32.xpm" );
00180
00181 ComputeAvailableConfigs();
00182 }
00183
00184 void Video::SetWindowCaption(std::string caption){
00185 SDL_WM_SetCaption( caption.c_str(), NULL );
00186 }
00187
00188 void Video::SetWindowIcon(std::string filename){
00189 SDL_WM_SetIcon( IMG_Load(filename.c_str()), NULL );
00190 }
00191
00192 void Video::InitSDL(){
00193 if( SDLReady )
00194 return;
00195
00196 if( SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO) < 0 )
00197 Error( Format( _("Unable to initialize SDL library: %s"), SDL_GetError() ) );
00198
00199 SDL_EnableUNICODE(1);
00200 SDLReady = true;
00201 }
00202
00203 void Video::Flip(){
00204 window.Flip();
00205 }
00206