00001
00027 #ifndef _CAPP_H_
00028 #define _CAPP_H_
00029
00030 #include "main.h"
00031 #include "GameStateManager.h"
00032
00033 #include "Singleton.h"
00034 #include "Cube.h"
00035
00036 #include "Collection.h"
00037 #include "TextureManager.h"
00038 #include "GameObject3DManager.h"
00039 #include "GameObject2DManager.h"
00040 #include "FontManager.h"
00041 #include "FontPrinter.h"
00042 #include "Renderer.h"
00043
00044 template<class T>
00045 class CApplication: public CSingleton<T> {
00046
00047 public:
00048
00054 void init(int *argc, char **argv);
00055
00061 bool initDevIL(void);
00062
00066 void initManagers();
00067
00071 void run(void);
00072
00076 static void clear(void);
00077
00082 void changeState(int state);
00083
00087 CApplication(void);
00088
00092 ~CApplication(void);
00093
00098 void setTitle(string str) {
00099 m_title = str;
00100 }
00101
00106 void setTitle(const char *str) {
00107 m_title = str;
00108 }
00109
00114 string getTitle() {
00115 return m_title;
00116 }
00117
00121 virtual void terminate();
00122
00123 protected:
00124
00128 void loadResources(void);
00129
00133 virtual void loadTextures(void) = 0;
00134
00138 virtual void loadFonts(void) = 0;
00139
00143 virtual void createScenes() = 0;
00144
00145
00151 void mouseMove(int x, int y);
00152
00160 void mouseButtons(int button, int state, int x, int y);
00161
00169 void keyboard(unsigned char key, int x, int y);
00170
00177 void keyboardSpecial(int key, int x, int y);
00178
00183 void timer(int value);
00184
00188 void renderScene(void);
00189
00195 void changeSize(int width, int height);
00196
00202 bool isCallTerminate() {
00203 return m_terminate;
00204 }
00205
00209 void cleanUp();
00210
00211 static void mouseMoveWrapper(int x, int y);
00212 static void mouseButtonsWrapper(int button, int state, int x, int y);
00213 static void keyboardWrapper(unsigned char key, int x, int y);
00214 static void keyboardSpecialWrapper(int key, int x, int y);
00215 static void renderSceneWrapper(void);
00216 static void changeSizeWrapper(int width, int height);
00217 static void timerWrapper(int value);
00218
00219
00224 string m_title;
00225
00230 bool m_terminate;
00231
00236 CGameStateManager* m_gameStateManager;
00237
00238 };
00239
00244 template<class T>
00245 CApplication<T>::CApplication(void) {
00246 m_terminate = false;
00247 m_gameStateManager = new CGameStateManager();
00248 }
00249
00253 template<class T>
00254 CApplication<T>::~CApplication(void) {
00255 if (m_gameStateManager != NULL) {
00256 delete m_gameStateManager;
00257 }
00258 }
00259
00265 template<class T>
00266 void CApplication<T>::init(int *argc, char **argv) {
00267
00268 cout << "* Core initialization" << endl;
00269
00270 glutInit(argc, argv);
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_STENCIL);
00283 glutInitWindowSize(800, 600);
00284 glutCreateWindow(getTitle().c_str());
00285
00286 glutSetCursor(GLUT_CURSOR_NONE);
00287
00288
00289
00290
00291 glutDisplayFunc(renderSceneWrapper);
00292 glutReshapeFunc(changeSizeWrapper);
00293 glutKeyboardFunc(keyboardWrapper);
00294 glutSpecialFunc(keyboardSpecialWrapper);
00295 glutPassiveMotionFunc(mouseMoveWrapper);
00296 glutMouseFunc(mouseButtonsWrapper);
00297 glutTimerFunc(10, timerWrapper, 0);
00298
00299 initDevIL();
00300 loadResources();
00301 createScenes();
00302
00303
00304 CCamera* camera = CCamera::getInstance();
00305
00306
00307 CRenderer* renderer = CRenderer::getInstance();
00308 renderer->blendingEnable();
00309 renderer->setViewport(800, 600);
00310 renderer->setCamera(camera);
00311
00312
00313 CFontPrinter* fontPrinter = CFontPrinter::getInstance();
00314 fontPrinter->setFontManager(CFontManager::getInstance());
00315 }
00316
00317 template<class T>
00318 void CApplication<T>::initManagers() {
00319 m_gameStateManager = new CGameStateManager();
00320 }
00321
00328 template<class T>
00329 bool CApplication<T>::initDevIL() {
00330
00331
00332
00333
00334
00335 if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION || iluGetInteger(
00336 ILU_VERSION_NUM) < ILU_VERSION || ilutGetInteger(ILUT_VERSION_NUM)
00337 < ILUT_VERSION) {
00338 cerr << "DevIL version is different...exiting!\n" << endl;
00339 return false;
00340 }
00341
00342 cout << "DevIL initialization ... ";
00343
00344
00345
00346
00347
00348
00349 ilInit();
00350 iluInit();
00351 ilutRenderer(ILUT_OPENGL);
00352
00353 cout << "succeed" << endl;
00354
00355 return true;
00356 }
00357
00361 template<class T>
00362 void CApplication<T>::run(void) {
00363 glutMainLoop();
00364 }
00365
00366
00367
00368 template<class T>
00369 void CApplication<T>::changeState(int state) {
00370 if (m_gameStateManager != NULL) {
00371 m_gameStateManager->switchStateTo(state);
00372 }
00373 }
00374
00378 template<class T>
00379 void CApplication<T>::loadResources(void) {
00380 cout << "*Resources loading" << endl;
00381 cout << "**Textures loading" << endl;
00382 loadTextures();
00383 cout << "**Font loading" << endl;
00384 loadFonts();
00385 }
00386
00387
00388
00393 template<class T>
00394 void CApplication<T>::timer(int value) {
00395 glutPostRedisplay();
00396 glutTimerFunc(1000 / 60, timerWrapper, 1);
00397 }
00398
00405 template<class T>
00406 void CApplication<T>::keyboard(unsigned char key, int x, int y) {
00407 if (m_gameStateManager != NULL) {
00408 this->m_gameStateManager->getCurrentState()->keyboard(key, x, y);
00409 }
00410 }
00411
00418 template<class T>
00419 void CApplication<T>::keyboardSpecial(int key, int x, int y) {
00420 if (m_gameStateManager != NULL) {
00421 this->m_gameStateManager->getCurrentState()->keyboardSpecial(key, x, y);
00422 }
00423 }
00424
00430 template<class T>
00431 void CApplication<T>::mouseMove(int x, int y) {
00432 if (m_gameStateManager != NULL) {
00433 this->m_gameStateManager->getCurrentState()->mouseMove(x, y);
00434 }
00435 }
00436
00444 template<class T>
00445 void CApplication<T>::mouseButtons(int button, int state, int x, int y) {
00446 if (m_gameStateManager != NULL) {
00447 this->m_gameStateManager->getCurrentState()->mouseButtons(button,
00448 state, x, y);
00449 }
00450 }
00451
00456 template<class T>
00457 void CApplication<T>::renderScene(void) {
00458
00459 if (this->isCallTerminate()) {
00460 this->cleanUp();
00461 }
00462
00463 if (m_gameStateManager != NULL) {
00464 m_gameStateManager->update();
00465 m_gameStateManager->render();
00466 }
00467 }
00468
00474 template<class T>
00475 void CApplication<T>::changeSize(int width, int height) {
00476
00477 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
00478
00479 if (height == 0)
00480 height = 1;
00481
00482 GLfloat ratio = (GLfloat) width / (GLfloat) height;
00483
00484 glViewport(0, 0, (GLsizei) width, (GLsizei) height);
00485 glMatrixMode(GL_PROJECTION);
00486 glLoadIdentity();
00487 gluPerspective(60, ratio, 0.1, 1000);
00488 glMatrixMode(GL_MODELVIEW);
00489
00490 glEnable(GL_DEPTH_TEST);
00491 glDepthFunc(GL_LEQUAL);
00492
00493
00494 }
00495
00496
00497
00501 template<class T>
00502 void CApplication<T>::clear(void) {
00503
00504 cout << "Exit application" << endl;
00505
00506 if (CSingleton<T>::m_instance != NULL)
00507 delete CSingleton<T>::m_instance;
00508
00509 exit(0);
00510
00511 }
00512
00513
00514
00521 template<class T>
00522 void CApplication<T>::keyboardWrapper(unsigned char key, int x, int y) {
00523
00524 if (CSingleton<T>::m_instance != NULL) {
00525 CSingleton<T>::m_instance->keyboard(key, x, y);
00526 }
00527
00528 }
00529
00537 template<class T>
00538 void CApplication<T>::keyboardSpecialWrapper(int key, int x, int y) {
00539 if (CSingleton<T>::m_instance != NULL) {
00540 CSingleton<T>::m_instance->keyboardSpecial(key, x, y);
00541 }
00542 }
00543
00550 template<class T>
00551 void CApplication<T>::mouseMoveWrapper(int x, int y) {
00552
00553 if (CSingleton<T>::m_instance != NULL)
00554 CSingleton<T>::m_instance->mouseMove(x, y);
00555
00556 }
00557
00565 template<class T>
00566 void CApplication<T>::mouseButtonsWrapper(int button, int state, int x, int y) {
00567
00568 if (CSingleton<T>::m_instance != NULL)
00569 CSingleton<T>::m_instance->mouseButtons(button, state, x, y);
00570
00571 }
00572
00576 template<class T>
00577 void CApplication<T>::renderSceneWrapper(void) {
00578
00579 if (CSingleton<T>::m_instance != NULL)
00580 CSingleton<T>::m_instance->renderScene();
00581
00582 }
00583
00589 template<class T>
00590 void CApplication<T>::changeSizeWrapper(int width, int height) {
00591
00592 if (CSingleton<T>::m_instance != NULL)
00593 CSingleton<T>::m_instance->changeSize(width, height);
00594
00595 }
00596
00601 template<class T>
00602 void CApplication<T>::timerWrapper(int value) {
00603
00604 if (CSingleton<T>::m_instance != NULL)
00605 CSingleton<T>::m_instance->timer(value);
00606
00607 }
00608
00609 template<class T>
00610 void CApplication<T>::terminate() {
00611 this->m_terminate = true;
00612 }
00613
00614 template<class T>
00615 void CApplication<T>::cleanUp() {
00616
00617 cout << "DISCONNECT GAME STATE MANAGER" << endl;
00618 if (m_gameStateManager != NULL) {
00619 CGameStateManager* gm = m_gameStateManager;
00620 gm->clean();
00621 m_gameStateManager = NULL;
00622 }
00623
00624 cout << "DISCONNECT RENDERS" << endl;
00625 CRenderer* renderer = CRenderer::getInstance();
00626 if (renderer != NULL) {
00627 renderer->disconnect();
00628 delete renderer;
00629 }
00630
00631 cout << "DELETE 2D MANAGER OBJECTS" << endl;
00632 CGameObject2DManager* object2DManager = CGameObject2DManager::getInstance();
00633 if (object2DManager != NULL) {
00634 object2DManager->clean();
00635 delete object2DManager;
00636 }
00637
00638 cout << "DELETE 3D MANAGER OBJECTS" << endl;
00639 CGameObject3DManager* object3DManager = CGameObject3DManager::getInstance();
00640 if (object3DManager != NULL) {
00641 object3DManager->clean();
00642 delete object3DManager;
00643 }
00644
00645 cout << "DELETE TEXTURE MANAGER" << endl;
00646
00647 CTextureManager* textureManager = CTextureManager::getInstance();
00648 if (textureManager != NULL)
00649 ;
00650 {
00651 textureManager->clean();
00652 delete textureManager;
00653 }
00654
00655 cout << "DELETE FONT MANAGER" << endl;
00656 CFontManager::deleteInstance();
00657 CCamera::deleteInstance();
00658
00659 clear();
00660
00661 }
00662
00663 #endif