00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "mxwnd.h"
00017 #include<ctime>
00018
00019
00020
00021 namespace mx
00022 {
00023
00024 mxWnd *mxWnd::mxwnd = 0;
00025
00026 mxWnd::mxWnd( Size window, Size s, Uint32 flags, SDL_Surface *ico)
00027 {
00028 initWnd(window.width,window.height,flags,ico,false);
00029 createSizedBackground( s );
00030 }
00031
00032
00033 mxWnd::mxWnd(int w, int h, Uint32 flags, SDL_Surface *ico, bool opengl_init)
00034 {
00035 initWnd(w,h,flags,ico,opengl_init);
00036 }
00037
00038 void mxWnd::initWnd(int w, int h, Uint32 flags, SDL_Surface *ico, bool opengl_init)
00039 {
00040 err_Type = 0;
00041 active = false;
00042 mxwnd = this;
00043 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK) < 0) {
00044 err_Type = 1;
00045 return;
00046 }
00047
00048 if(ico != 0)
00049 SDL_WM_SetIcon(ico, 0);
00050
00051 opengl_ = false;
00052
00053 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
00054
00055 Uint8 bpp=0;
00056
00057 bpp = vi->vfmt->BitsPerPixel;
00058
00059 bool hw_ = true;
00060
00061 if(vi->hw_available != 0)
00062 flags |= SDL_HWSURFACE;
00063 else {
00064 flags |= SDL_SWSURFACE;
00065 hw_ = false;
00066 }
00067
00068 if(vi->blit_hw)
00069 flags |= SDL_HWACCEL;
00070
00071
00072 if(opengl_init == true)
00073 {
00074 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
00075 d_type = ORTHO;
00076 }
00077
00078
00079 if(w == -1)
00080 {
00081 w = vi->current_w;
00082 h = vi->current_h;
00083 flags |= SDL_FULLSCREEN;
00084 }
00085
00086 if(opengl_init == true) {
00087 flags |= SDL_OPENGL;
00088 opengl_ = true;
00089 }
00090
00091
00092 SDL_Surface *temp = SDL_SetVideoMode(w,h,32,flags);
00093
00094 if(!temp) {
00095 err_Type = 2;
00096 return;
00097 }
00098
00099 front = temp;
00100 vid_flags = flags;
00101 front.noZero(true);
00102
00103
00104
00105 std::cout << "application activated @ ";
00106
00107 time_t the_time;
00108 struct tm *time_info;
00109
00110 time(&the_time);
00111
00112 time_info = localtime(&the_time);
00113 std::cout << asctime(time_info);
00114 std::cout << "SDL: subsystem initalized\n";
00115 std::cout << "Video " << front.width() << "x" << front.height() << "x" << int(front.getSurface()->format->BitsPerPixel) << "\n";
00116 std::cout << "Render Mode: " << ((hw_ == true) ? "Direct Rendering" : "Software Rendering") << "\n";
00117 std::cout << "Hardware Blit Acceleration: " << ((vi->blit_hw == 0) ? "No": "Yes") << "\n";
00118 sizevar = Size(front.width(), front.height());
00119 }
00120
00121 void mxWnd::createSizedBackground ( Size &s )
00122 {
00123 background.createSurface( s );
00124 }
00125
00126 mxWnd::~mxWnd()
00127 {
00128
00129 SDL_Quit();
00130 std::cout << "application shutdown @ ";
00131 time_t the_time;
00132 struct tm *time_info;
00133 time(&the_time);
00134 time_info = localtime(&the_time);
00135 std::cout << asctime(time_info);
00136 std::cout << "SDL: subsystem shutdown\n";
00137
00138 }
00139
00140
00141 int mxWnd::messageLoop()
00142 {
00143
00144 if(err_Type != 0) {
00145
00146 switch(err_Type) {
00147 case 1:
00148 throw mx::mxException<std::string>("Error could not init SDL: " + std::string(SDL_GetError()) + "\n");
00149 break;
00150 case 2:
00151 throw mx::mxException<std::string>("Error could not set video mode: " + std::string(SDL_GetError()) + "\n");
00152 break;
00153 }
00154
00155 }
00156
00157 static SDL_Event e;
00158
00159 active = true;
00160
00161 while(active == true)
00162 {
00163
00164 while(SDL_PollEvent(&e))
00165 {
00166
00167
00168 switch(e.type) {
00169 case SDL_VIDEORESIZE:
00170 {
00171 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
00172 Uint8 bpp = vi->vfmt->BitsPerPixel;
00173 front = SDL_SetVideoMode( e.resize.w, e.resize.h, bpp, vid_flags );
00174 resizeWindow(e.resize.w, e.resize.h);
00175
00176 }
00177 break;
00178 }
00179
00180
00181 eventPassed(e);
00182 }
00183
00184 renderScreen();
00185
00186 }
00187
00188 return EXIT_SUCCESS;
00189 }
00190
00191 void mxWnd::quit()
00192 {
00193 active = false;
00194 std::cout << "System: A Quit Signal has been sent\n\n";
00195 }
00196
00197
00198 void mxWnd::eventPassed(SDL_Event &e)
00199 {
00200 if(e.type == SDL_QUIT) active = false;
00201 else if(e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE) active = false;
00202 }
00203
00204
00205
00206 void mxWnd::renderScreen()
00207 {
00208
00209 SDL_FillRect(front, 0, SDL_MapRGB(front.getSurface()->format, 255, 255, 255));
00210 front.Flip();
00211
00212
00213 if(opengl_) {
00214
00215 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00216 SDL_GL_SwapBuffers();
00217 }
00218
00219 }
00220
00221 void mxWnd::setTitle(string title) const
00222 {
00223 SDL_WM_SetCaption(title.c_str(), 0);
00224 }
00225
00226 void mxWnd::minimizeWindow() const
00227 {
00228 SDL_WM_IconifyWindow();
00229 }
00230
00231 void mxWnd::pumpEvents() const
00232 {
00233 SDL_PumpEvents();
00234 }
00235
00236
00237 void mxWnd::enableKeyRepeat(int code, int interval)
00238 {
00239 SDL_EnableKeyRepeat(code, interval);
00240 }
00241
00242 Size &mxWnd::size()
00243 {
00244
00245 return sizevar;
00246
00247 }
00248
00249 void mxWnd::sizedFlip() {
00250 front.copyResizeSurface( background );
00251 front.Flip();
00252 }
00253
00254 void mxWnd::resizeWindow(int w, int h)
00255 {
00256 if(opengl_ == false) return;
00257
00258
00259 if(d_type == ORTHO) {
00260
00261 GLfloat range = 100.0f;
00262
00263 if(h == 0) h = 1;
00264
00265 glViewport(0,0,w,h);
00266
00267 glMatrixMode(GL_PROJECTION);
00268 glLoadIdentity();
00269
00270 if(w<=h)
00271 glOrtho( -range, range, -range*h/w, range*h/w, -range, range);
00272 else
00273 glOrtho( -range*w/h, range*w/h, -range, range, -range, range);
00274
00275 glMatrixMode(GL_MODELVIEW);
00276 glLoadIdentity();
00277
00278 }
00279 else {
00280
00281 if(h == 0)
00282 h = 1;
00283
00284 GLfloat ratio = 0.0f;
00285 ratio = static_cast<GLfloat>( w / h );
00286 glViewport( 0, 0, static_cast<GLsizei>(w), static_cast<GLsizei>(h));
00287 glMatrixMode(GL_PROJECTION);
00288 glLoadIdentity();
00289 gluPerspective(45.0f, ratio, 0.1f, 200.0f);
00290 glMatrixMode(GL_MODELVIEW);
00291 glLoadIdentity();
00292 }
00293
00294 }
00295
00296 void mxWnd::setMode(enum lookType type) {
00297 this->d_type = type;
00298 }
00299
00300 }
00301
00302
00303