Google
Web alhem.net
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

SDLControl.cpp

Go to the documentation of this file.
00001 00006 /* 00007 Copyright (C) 2004 Anders Hedstrom 00008 00009 This program is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU General Public License 00011 as published by the Free Software Foundation; either version 2 00012 of the License, or (at your option) any later version. 00013 00014 This program is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 You should have received a copy of the GNU General Public License 00020 along with this program; if not, write to the Free Software 00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 */ 00023 #include <SDL.h> 00024 #include "guitypedefs.h" 00025 #ifdef _WIN32 00026 #pragma warning(push) 00027 00028 #include <yvals.h> // warning numbers get enabled in yvals.h 00029 00030 #pragma warning(disable: 4251) 00031 #pragma warning(disable: 4786) // identifier was truncated to 'number' characters in the debug information 00032 00033 // BUG: C4786 Warning Is Not Disabled with #pragma Warning 00034 // STATUS: Microsoft has confirmed this to be a bug in the Microsoft product. This warning can be ignored. 00035 // This occured only in the <map> container. 00036 00037 #include <sge.h> 00038 00039 #pragma warning(pop) 00040 #else 00041 #include <sge.h> 00042 #endif 00043 //#include <iotm/Mutex.h> 00044 00045 #include "SDLControl.h" 00046 00047 #ifdef _DEBUG 00048 #define DEB(x) x 00049 #else 00050 #define DEB(x) 00051 #endif 00052 00053 #define NUM_COLORS 256 00054 00055 namespace gui 00056 { 00057 00058 00059 SDLControl::SDLControl(int argc,char *argv[],int x,int y,int b) 00060 :m_ok(false) 00061 ,m_screen(NULL) 00062 ,m_width(x) 00063 ,m_height(y) 00064 ,m_bpp(b) 00065 ,m_hw(false) 00066 ,m_hwpalette(false) 00067 ,m_noframe(false) 00068 ,m_fullscreen(false) 00069 ,m_videoflags(SDL_SWSURFACE) 00070 //,m_timerThread(NULL) 00071 { 00072 // Command line 00073 DEB(printf("read_config()\n");) 00074 read_config(argc,argv); 00075 00076 // Init SDL 00077 DEB(printf("Init_SDL()\n");) 00078 Init_SDL(); 00079 00080 if (!m_ok) 00081 { 00082 DEB(printf("Ej OK\n");) 00083 return; 00084 } 00085 00086 // Set window title 00087 DEB(printf("Set window title\n");) 00088 SDL_WM_SetCaption("Client Window", "input"); 00089 00090 // check flags 00091 if (m_hw) 00092 { 00093 m_videoflags |= SDL_HWSURFACE; 00094 } 00095 if (m_hwpalette) 00096 { 00097 m_videoflags |= SDL_HWPALETTE; 00098 } 00099 if (m_noframe) 00100 { 00101 m_videoflags |= SDL_NOFRAME; 00102 } 00103 if (m_fullscreen) 00104 { 00105 m_videoflags |= SDL_FULLSCREEN; 00106 } 00107 00108 // Set a video mode 00109 int bpp = m_bpp; 00110 if ( (m_bpp = SDL_VideoModeOK(m_width, m_height, m_bpp, m_videoflags) ) == 0) 00111 { 00112 fprintf(stderr,"Video mode not supported: %s\n", SDL_GetError()); 00113 m_ok = false; 00114 // return NULL; 00115 } 00116 else 00117 { 00118 DEB(printf("SDL is recommending %dbpp. Using %dbpp.\n", m_bpp, m_bpp);) 00119 } 00120 00121 m_bpp = bpp; 00122 if ( (m_screen = SDL_SetVideoMode(m_width, m_height, m_bpp, m_videoflags) ) == NULL) 00123 { 00124 fprintf(stderr,"Could not initialize SDL: %s.\n", SDL_GetError()); 00125 m_ok = false; 00126 // return NULL; 00127 } 00128 00129 DEB(printf("SDLControl():\n");) 00130 status(); 00131 00132 // m_timerThread = new TimerThread; 00133 } 00134 00135 00136 SDLControl::~SDLControl() 00137 { 00138 /* 00139 if (m_timerThread) 00140 { 00141 m_timerThread -> SetRunning(false); 00142 SDL_Delay(250); 00143 delete m_timerThread; 00144 } 00145 */ 00146 // DeInit SDL 00147 if (m_ok) 00148 { 00149 SDL_Quit(); 00150 } 00151 } 00152 00153 00154 void SDLControl::read_config(int argc,char *argv[]) 00155 { 00156 // See if we try to get a hardware colormap 00157 for (int loop = 1; loop < argc; loop++) 00158 { 00159 if (!strcmp(argv[loop],"-width") && loop < argc - 1) 00160 { 00161 m_width = atoi(argv[loop + 1]); 00162 loop++; 00163 } 00164 else 00165 if (!strcmp(argv[loop],"-height") && loop < argc - 1) 00166 { 00167 m_height = atoi(argv[loop + 1]); 00168 loop++; 00169 } 00170 else 00171 if (!strcmp(argv[loop],"-bpp") && loop < argc - 1) 00172 { 00173 m_bpp = atoi(argv[loop + 1]); 00174 loop++; 00175 } 00176 else 00177 if (!strcmp(argv[loop],"-hw")) 00178 { 00179 m_hw = true; 00180 } 00181 else 00182 if (!strcmp(argv[loop],"-hwpalette")) 00183 { 00184 m_hwpalette = true; 00185 } 00186 else 00187 if (!strcmp(argv[loop],"-noframe")) 00188 { 00189 m_noframe = true; 00190 } 00191 else 00192 if (!strcmp(argv[loop],"-fullscreen")) 00193 { 00194 m_fullscreen = true; 00195 } 00196 else 00197 { 00198 fprintf(stderr,"'%s'?\n",argv[loop]); 00199 fprintf(stderr,"Usage: %s [-width xx] [-height xx] [-bpp xx]\n",*argv); 00200 fprintf(stderr," [-hw] [-hwpalette] [-noframe] [-fullscreen]\n"); 00201 } 00202 } 00203 } 00204 00205 00206 void SDLControl::Init_SDL() 00207 { 00208 DEB(printf("SDL_Init()\n");) 00209 if (SDL_Init( SDL_INIT_TIMER|SDL_INIT_VIDEO ) < 0) 00210 { 00211 DEB(printf("SDL_Init() ej ok\n");) 00212 fprintf(stderr,"SDL error: %s\n",SDL_GetError() ); 00213 } 00214 else 00215 { 00216 DEB(printf("SDL_Init() OK\n");) 00217 m_ok = true; 00218 } 00219 } 00220 00221 00222 /* 00223 void SDLControl::CreateScreen() 00224 { 00225 int i; 00226 SDL_Color palette[NUM_COLORS]; 00227 Uint8 *buffer; 00228 00229 // Set the video mode 00230 m_screen = SDL_SetVideoMode(m_width, m_height, m_bpp, m_videoflags); 00231 if ( m_screen == NULL ) 00232 { 00233 fprintf(stderr, "Couldn't set display mode: %s\n",SDL_GetError()); 00234 return; 00235 } 00236 DEB(printf("Screen is in %s mode\n",(m_screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");) 00237 00238 // Set a gray colormap, reverse order from white to black 00239 for ( i=0; i<NUM_COLORS; ++i ) 00240 { 00241 palette[i].r = (NUM_COLORS-1)-i * (256 / NUM_COLORS); 00242 palette[i].g = (NUM_COLORS-1)-i * (256 / NUM_COLORS); 00243 palette[i].b = (NUM_COLORS-1)-i * (256 / NUM_COLORS); 00244 } 00245 SDL_SetColors(m_screen, palette, 0, NUM_COLORS); 00246 00247 // Set the surface pixels and refresh! 00248 if ( SDL_LockSurface(m_screen) < 0 ) 00249 { 00250 fprintf(stderr, "Couldn't lock display surface: %s\n",SDL_GetError()); 00251 return; 00252 } 00253 buffer = (Uint8 *)m_screen->pixels; 00254 for ( i=0; i<m_screen->h; ++i ) 00255 { 00256 memset(buffer,(i*(NUM_COLORS-1))/m_screen->h, m_screen->w); 00257 buffer += m_screen->pitch; 00258 } 00259 SDL_UnlockSurface(m_screen); 00260 SDL_UpdateRect(m_screen, 0, 0, 0, 0); 00261 } 00262 */ 00263 00264 00265 void SDLControl::Update8bitPalette() 00266 { 00267 // SDL_SetColors(m_screen, gui::Color::m_palette, 0, gui::Color::m_qcolors); 00268 } 00269 00270 00271 void SDLControl::ToggleFullscreen() 00272 { 00273 m_fullscreen = !m_fullscreen; 00274 m_videoflags ^= SDL_FULLSCREEN; 00275 // m_mutex.Lock(); 00276 00277 // CreateScreen(); 00278 if ( (m_screen = SDL_SetVideoMode(m_width, m_height, m_bpp, m_videoflags) ) == NULL) 00279 { 00280 fprintf(stderr,"Could not initialize SDL: %s.\n", SDL_GetError()); 00281 m_ok = false; 00282 // return NULL; 00283 } 00284 00285 // m_mutex.Unlock(); 00286 } 00287 00288 00289 SDL_Surface *SDLControl::GetScreen() 00290 { 00291 return m_screen; 00292 } 00293 00294 00295 short SDLControl::GetBpp() 00296 { 00297 return m_bpp; 00298 } 00299 00300 00301 bool SDLControl::IsOK() 00302 { 00303 DEB( if (m_ok) 00304 { 00305 printf("m_ok is true\n"); 00306 } 00307 else 00308 { 00309 printf("m_ok is false\n"); 00310 }) 00311 return m_ok; 00312 } 00313 00314 00315 void SDLControl::SetOK(bool ok) 00316 { 00317 DEB(printf("SetOK = %s\n",ok ? "TRUE" : "FALSE");) 00318 m_ok = ok; 00319 } 00320 00321 00322 void SDLControl::LockMutex() 00323 { 00324 // m_mutex.Lock(); 00325 } 00326 00327 00328 void SDLControl::UnlockMutex() 00329 { 00330 // m_mutex.Unlock(); 00331 } 00332 00333 00334 void SDLControl::lockSurface() //bool bClearRects) 00335 { 00336 /* 00337 if (bClearRects) 00338 { 00339 while (m_rects.size()) 00340 { 00341 m_rects.erase(m_rects.begin()); 00342 } 00343 } 00344 */ 00345 if ( SDL_MUSTLOCK(m_screen) ) 00346 { 00347 if ( SDL_LockSurface(m_screen) < 0 ) 00348 { 00349 fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError()); 00350 return; 00351 } 00352 } 00353 } 00354 00355 00356 /* 00357 void SDLControl::AddRect(SDL_Rect &rect) 00358 { 00359 // check outer bounds here 00360 coord_t xmax = m_screen -> w; 00361 coord_t ymax = m_screen -> h; 00362 00363 // kontroll utanfor ytan 00364 if (rect.x + rect.w > xmax) 00365 { 00366 rect.w -= rect.x + rect.w - xmax; 00367 } 00368 if (rect.y + rect.h > ymax) 00369 { 00370 rect.h -= rect.y + rect.h - ymax; 00371 } 00372 // 00373 m_rects.insert(m_rects.end(), rect); 00374 } 00375 */ 00376 00377 00378 void SDLControl::unlockSurface() //bool bUpdateRects) 00379 { 00380 if ( SDL_MUSTLOCK(m_screen) ) 00381 { 00382 SDL_UnlockSurface(m_screen); 00383 } 00384 /* 00385 if (bUpdateRects && m_rects.size()) 00386 { 00387 SDL_Rect *rects = new SDL_Rect[m_rects.size()]; 00388 int i = 0; 00389 rectvector_t::iterator it; 00390 00391 for (it = m_rects.begin(); it != m_rects.end(); it++) 00392 { 00393 rects[i] = (*it); 00394 i++; 00395 } 00396 SDL_UpdateRects(m_screen, i, rects); 00397 delete rects; 00398 } 00399 */ 00400 } 00401 00402 00403 void SDLControl::status() 00404 { 00405 DEB( printf("m_ok: %s\n", m_ok ? "true" : "false"); 00406 printf("m_width: %d\n", m_width); 00407 printf("m_height: %d\n", m_height); 00408 printf("m_bpp: %d\n", m_bpp); 00409 printf("m_hw: %s\n", m_hw ? "true" : "false"); 00410 printf("m_hwpalette: %s\n", m_hwpalette ? "true" : "false"); 00411 printf("m_noframe: %s\n", m_noframe ? "true" : "false"); 00412 printf("m_fullscreen: %s\n", m_fullscreen ? "true" : "false");) 00413 } 00414 00415 00416 /* 00417 TimerThread *SDLControl::GetTimerThread() 00418 { 00419 return m_timerThread; 00420 } 00421 */ 00422 00423 00424 } // namespace 00425

Generated for My SDL C++ Gui by doxygen 1.3.6

www.TV-friendship.com
The matchmaking service with an all new twist.

Quantum 'Teleportation'
Some thoughts
Page, code, and content Copyright (C) 2004 by Anders Hedström