00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
#include <SDL.h>
00024
#include "guitypedefs.h"
00025
#ifdef _WIN32
00026
#pragma warning(push)
00027
00028
#include <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
00034
00035
00036
00037
#include <sge.h>
00038
00039
#pragma warning(pop)
00040
#else
00041
#include <sge.h>
00042
#endif
00043
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
00071 {
00072
00073
DEB(printf(
"read_config()\n");)
00074
read_config(argc,argv);
00075
00076
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
00087
DEB(printf(
"Set window title\n");)
00088 SDL_WM_SetCaption(
"Client Window",
"input");
00089
00090
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
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
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
00127 }
00128
00129
DEB(printf(
"SDLControl():\n");)
00130
status();
00131
00132
00133 }
00134
00135
00136 SDLControl::~SDLControl()
00137 {
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
if (
m_ok)
00148 {
00149 SDL_Quit();
00150 }
00151 }
00152
00153
00154 void SDLControl::read_config(
int argc,
char *argv[])
00155 {
00156
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
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 void SDLControl::Update8bitPalette()
00266 {
00267
00268 }
00269
00270
00271 void SDLControl::ToggleFullscreen()
00272 {
00273
m_fullscreen = !
m_fullscreen;
00274
m_videoflags ^= SDL_FULLSCREEN;
00275
00276
00277
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
00283 }
00284
00285
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
00325 }
00326
00327
00328 void SDLControl::UnlockMutex()
00329 {
00330
00331 }
00332
00333
00334 void SDLControl::lockSurface()
00335 {
00336
00337
00338
00339
00340
00341
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
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 void SDLControl::unlockSurface()
00379 {
00380
if ( SDL_MUSTLOCK(
m_screen) )
00381 {
00382 SDL_UnlockSurface(
m_screen);
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
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
00418
00419
00420
00421
00422
00423
00424 }
00425