00001
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "K3dEngine.h"
00033
00034 K3dEngine::K3dEngine()
00035 {
00036 cout << "K3dEngine::K3dEngine()" << endl;
00037 }
00038
00039 K3dEngine::~K3dEngine()
00040 {
00041 cout << "K3dEngine::~K3dEngine()" << endl;
00042 }
00043
00045 bool K3dEngine::Init(int _argc, char *_argv[], bool _bReinit)
00046 {
00047 if(!InitEngine(_argc,_argv,_bReinit))
00048 {
00049 Destroy();
00050 return false;
00051 }
00052
00053 printf("\n");
00054 m_pScene->PrintGraphicDriverName();
00055 m_pScene->PrintGraphicDriverInfo();
00056 m_pScene->PrintModes();
00057 return true;
00058 }
00059
00061 bool K3dEngine::InitEngine(int _argc, char *_argv[], bool _bReinit)
00062 {
00063 cout << "bool K3dEngine::Init(int _argc, char *_argv[], bool _bReinit=false)" << endl;
00064
00065 if(SDL_Init(SDL_SUBSYSTEMS) == -1)
00066 {
00067 fprintf(stderr, "Unable to initialize SDL: %s\n",
00068 SDL_GetError());
00069 return false;
00070 }
00071
00072
00073 m_pGameData = new K3dGameData();
00074
00075 m_pScene = new K3dScene(m_pGameData, _argc, _argv);
00076
00077 m_pVM = new K3dVM(m_pGameData, m_pScene);
00078
00079
00080 TGraphicOption *ptGOption = m_pGameData->GetGraphicOption();
00081
00082 int iWidth = ptGOption->iWidth;
00083 int iHeight = ptGOption->iHeight;
00084 int iBpp = ptGOption->iColorBits;
00085 Uint32 uiFlags = ptGOption->uiFlags;
00086 if(_bReinit)
00087 {
00088 uiFlags = m_uiFlags;
00089 }
00090 else
00091 {
00092 m_bIsFullscreen = ptGOption->bFullscreen;
00093 }
00094 printf("SDL was initialized\n");
00095 m_pScene->PrintParams(iWidth, iHeight, iBpp, true);
00096 printf("Checking window parameters...\n");
00097 iBpp = SDL_VideoModeOK(iWidth, iHeight, iBpp, uiFlags);
00098
00099 if(iBpp == 0)
00100 {
00101
00102 iBpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
00103 printf("SDL_VideoModeOK(): Parameters are not valid, "
00104 "checking new color depth...\n");
00105 printf("SDL_GetVideoInfo()->vfmt->BitsPerPixel: %d\n", iBpp);
00106 iBpp = SDL_VideoModeOK(iWidth, iHeight, iBpp, uiFlags);
00107 if(iBpp == 0)
00108 {
00109 printf("SDL_VideoModeOK(): Parameters are not "
00110 "valid, finding new dimensions\n");
00111
00112 iBpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
00113
00114 SDL_Rect **pptModes;
00115 pptModes = SDL_ListModes(NULL, uiFlags);
00116
00117 if(pptModes == (SDL_Rect **)0)
00118 {
00119 printf("SDL_ListModes(): No modes available\n");
00120 return false;
00121 }
00122 else if(pptModes == (SDL_Rect **)-1)
00123 {
00124 printf("SDL_ListModes(): All resolutions "
00125 "available, using 640x480.\n");
00126 iWidth = 640;
00127 iHeight = 480;
00128 }
00129 else
00130 {
00131 printf("SDL_ListModes(): Using maximal "
00132 "available dimensions\n");
00133 iWidth = pptModes[0]->w;
00134 iHeight = pptModes[0]->h;
00135 }
00136 printf("Everything should be OK now, but some "
00137 "other check would be great :]\n");
00138 m_pScene->PrintParams(iWidth, iHeight, iBpp, true);
00139 iBpp = SDL_VideoModeOK(iWidth, iHeight, iBpp, uiFlags);
00140 if(iBpp == 0)
00141 {
00142 printf("SDL_VideoModeOK(): Something is still "
00143 "not valid :-(((((\n");
00144 m_pScene->PrintParams(iWidth, iHeight, iBpp, false);
00145 return false;
00146 }
00147 }
00148 }
00149 printf("SDL_VideoModeOK(): Parameters are valid\n");
00150 printf("Creating window...\n");
00151
00152 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
00153 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 24);
00154 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
00155 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
00156 SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 0);
00157 SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 0);
00158 SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 0);
00159 SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 0);
00160 #ifdef FSAA
00161 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
00162 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, FSAA);
00163 #endif
00164 m_pScreen = SDL_SetVideoMode(iWidth, iHeight, iBpp, uiFlags);
00165 if(m_pScreen == NULL)
00166 {
00167 printf("Window creation failed\n");
00168 m_pScene->PrintParams(iWidth, iHeight, iBpp, false);
00169 fprintf(stderr, "SDL_GetError(): %s\n", SDL_GetError());
00170 cerr << SDL_GetError() << endl;
00171 return false;
00172 }
00173 printf("Window was successfuly created\n");
00174 m_pScene->PrintParams(iWidth, iHeight, iBpp, true);
00175
00176 if(!m_pScene->InitGL())
00177 {
00178 return false;
00179 }
00180
00181 SDL_WM_SetCaption(WIN_TITLE, NULL);
00182
00183 cout << "LoadScript()" << endl;
00184 TScriptOption *ptOption = m_pGameData->GetScriptOption();
00185
00186 m_pVM->LoadScript(ptOption->strDefault);
00187
00188 m_pVM->Engine_Init();
00189
00190 m_pScene->ResizeGL(ptGOption->iWidth, ptGOption->iHeight);
00191 return true;
00192 }
00193
00195 void K3dEngine::Destroy()
00196 {
00197
00198 if(m_pVM)
00199 {
00200 m_pVM->Engine_Delete();
00201 }
00202 if(m_pScene)
00203 {
00204 m_pScene->Delete();
00205 SDL_Quit();
00206 m_pScene->DeleteOptions();
00207 delete m_pScene;
00208 m_pScene = NULL;
00209 }
00210 if(m_pGameData)
00211 {
00212 delete m_pGameData;
00213 m_pGameData = NULL;
00214 }
00215 if(m_pVM)
00216 {
00217 delete m_pVM;
00218 m_pVM = NULL;
00219 }
00220 }
00221
00223 bool K3dEngine::ToggleFullscreen()
00224 {
00225
00226 m_bIsFullscreen = !m_bIsFullscreen;
00227 if(!m_bIsFullscreen)
00228 {
00229 m_uiFlags = SDL_OPENGL|SDL_RESIZABLE;
00230 }
00231 else
00232 {
00233 m_uiFlags = SDL_OPENGL|SDL_RESIZABLE|SDL_FULLSCREEN;
00234 }
00235
00236 if(SDL_WM_ToggleFullScreen(m_pScreen) == 0)
00237 {
00238 fprintf(stderr, "Unable to toggle fullscreen,""Trying to recreate window\n");
00239 cerr << "SDL_WM_ToggleFullScreen doesn`t work in win32. I must reinit engine..." << endl;
00240 SDL_FreeSurface(m_pScreen);
00241 cout << "SDL_FreeSurface(m_pScreen); -- OK" << endl;
00242 Destroy();
00243
00244 if(!Init(m_argc, m_argv, true))
00245 {
00246 Destroy();
00247 return false;
00248 }
00249 }
00250
00251 return true;
00252 }
00253
00255 bool K3dEngine::ProcessEvent()
00256 {
00257 SDL_Event event;
00258 while(SDL_PollEvent(&event))
00259 {
00260 switch(event.type)
00261 {
00262 case SDL_KEYDOWN:
00263
00264 switch(event.key.keysym.sym)
00265 {
00266 case SDLK_ESCAPE:
00267
00268 break;
00269
00270
00271 case SDLK_F1:
00272 if(!ToggleFullscreen())
00273 return false;
00274 break;
00275
00276 case SDL_BUTTON_WHEELUP:
00277 printf("Wheel Up\n");
00278 break;
00279
00280 default:
00281 break;
00282 }
00283 break;
00284
00285 case SDL_VIDEORESIZE:
00286 {
00287 TGraphicOption *ptGOption = m_pGameData->GetGraphicOption();
00288 int iBpp = ptGOption->iColorBits;
00289 unsigned int uiFlags = ptGOption->uiFlags;
00290 m_pScreen = SDL_SetVideoMode(event.resize.w, event.resize.h, iBpp, uiFlags);
00291 if(m_pScreen == NULL)
00292 {
00293 printf("Window creation failed\n");
00294 m_pScene->PrintParams(event.resize.w, event.resize.h, iBpp, false);
00295 fprintf(stderr, "SDL_GetError(): %s\n", SDL_GetError());
00296 cerr << SDL_GetError() << endl;
00297 }
00298
00299 ptGOption->ptScreen = m_pScreen;
00300
00301 m_pScene->ResizeGL(event.resize.w, event.resize.h);
00302
00303 m_pScene->SetTargetPlane(event.resize.w, event.resize.h);
00304 }
00305 break;
00306
00307 case SDL_QUIT:
00308 return false;
00309 break;
00310
00311 case SDL_MOUSEMOTION:
00312
00313 m_pGameData->GetMouse()->MousePositionRel()[0] = event.motion.xrel;
00314 m_pGameData->GetMouse()->MousePositionRel()[1] = event.motion.yrel;
00315
00316 m_pGameData->GetMouse()->MousePosition()[0] = event.motion.x;
00317 m_pGameData->GetMouse()->MousePosition()[1] = event.motion.y;
00318 break;
00319
00320 case SDL_MOUSEBUTTONDOWN:
00321 if(event.button.button == SDL_BUTTON_WHEELUP)
00322 {
00323 m_pGameData->GetMouse()->MouseWheelButtonUp() = true;
00324
00325 }
00326 if(event.button.button == SDL_BUTTON_WHEELDOWN)
00327 {
00328 m_pGameData->GetMouse()->MouseWheelButtonDown() = true;
00329
00330 }
00331 break;
00332
00333 case SDL_MOUSEBUTTONUP:
00334 if(event.button.button == SDL_BUTTON_WHEELUP)
00335 {
00336
00337 }
00338 else if(event.button.button == SDL_BUTTON_WHEELDOWN)
00339 {
00340
00341 }
00342 break;
00343
00344 default:
00345 break;
00346 }
00347 if(m_pScene->GetExit() == true)
00348 {
00349 return false;
00350 }
00351
00352
00353
00354
00355
00356 gcn::SDLInput* pGuiInput = m_pScene->GetInput();
00357 if(pGuiInput)
00358 {
00359 pGuiInput->pushInput(event);
00360 }
00361 }
00362 return true;
00363 }
00364
00366 int K3dEngine::Start()
00367 {
00368
00369 bool done = false;
00370 while(!done)
00371 {
00372 done = !ProcessEvent();
00373
00374 m_pScene->Update();
00375
00376 m_pVM->Engine_Update();
00377
00378 m_pScene->Draw();
00379
00380 glFlush();
00381 SDL_GL_SwapBuffers();
00382 }
00383 Destroy();
00384 return 0;
00385 }