gcsx_game.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* GCSx
  2. ** GAME.CPP
  3. **
  4. ** Game playing module- basic init, other top-level functionality
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #include "all.h"
  24. WorldPlay* gameWorld = NULL;
  25. Uint8* keystate = NULL;
  26. // FPS Calculation (running averages)
  27. Uint32 firstTicks = 0;
  28. Uint32 frames = 0;
  29. Uint32 lastTicks = 0;
  30. double actualFPS;
  31. double totalFPS;
  32. #define COUNT_FPS 10
  33. int inFPS;
  34. double cumFPS[COUNT_FPS];
  35. void calcActualFPS() { start_func
  36. // FPS this frame
  37. Uint32 ticks = SDL_GetTicks();
  38. if (ticks == lastTicks) --lastTicks;
  39. actualFPS = (double)1000.0 / (ticks - lastTicks);
  40. lastTicks = ticks;
  41. // Update running average
  42. totalFPS -= cumFPS[inFPS];
  43. totalFPS += actualFPS;
  44. cumFPS[inFPS] = actualFPS;
  45. if (++inFPS >= COUNT_FPS) inFPS = 0;
  46. // Place in titlebar
  47. string title = PRODUCT_NAME " - ";
  48. title += gameWorld->getTitle();
  49. title += formatString(" (%4.2lf FPS avg of 10 / %2.4lf ms avg overall)", totalFPS / COUNT_FPS, (ticks - firstTicks) / (double)(++frames));
  50. SDL_WM_SetCaption(title.c_str(), title.c_str());
  51. }
  52. void gcsxGameplay(const string& gameFile) throw_File { start_func
  53. keystate = SDL_GetKeyState(NULL);
  54. gameWorld = new WorldPlay(gameFile.c_str());
  55. string title = PRODUCT_NAME " - ";
  56. title += gameWorld->getTitle();
  57. SDL_WM_SetCaption(title.c_str(), title.c_str());
  58. // @TODO: This should be configurable per game
  59. // @TODO: throw_Video
  60. selectVideoMode(-1, -1, -1, -1, 0, 1);
  61. // @TODO: optimize enabling these modes-
  62. // per layer (based on tileset and presence of layer alpha)
  63. // per layer sprites (?)
  64. // for desktop (simply on)
  65. oglAlphaMode(1, 1);
  66. gameWorld->gameStart();
  67. // Init FPS
  68. firstTicks = lastTicks = SDL_GetTicks();
  69. frames = 0;
  70. inFPS = 0;
  71. totalFPS = (double)0.0;
  72. for (int p = 0; p < COUNT_FPS; ++p)
  73. cumFPS[p] = (double)0.0;
  74. }
  75. void gcsxEndgame() { start_func
  76. if (gameWorld) {
  77. interpreterCleanup();
  78. delete gameWorld;
  79. gameWorld = NULL;
  80. initDefaultVideo();
  81. SDL_WM_SetCaption(PRODUCT_NAME, PRODUCT_NAME);
  82. }
  83. }
  84. int handleGameEvents(int (*eventFunc)(SDL_Event*)) { start_func
  85. // TEMPORARY SAMPLE EVENT HANDLING
  86. Scene* scene = gameWorld->getCurrentScene();
  87. SDL_Event event;
  88. while (eventFunc(&event)) {
  89. if (event.type == SDL_QUIT)
  90. return GAME_EXIT;
  91. if (event.type == SDL_KEYDOWN) {
  92. int pos = 0;
  93. SDLMod mod = KMOD_NONE;
  94. if (event.key.keysym.mod & KMOD_ALT) mod = (SDLMod)(mod | KMOD_ALT);
  95. if (event.key.keysym.mod & KMOD_CTRL) mod = (SDLMod)(mod | KMOD_CTRL);
  96. if (event.key.keysym.mod & KMOD_SHIFT) mod = (SDLMod)(mod | KMOD_SHIFT);
  97. while (int command = config->readShortcut(event.key.keysym.sym, mod, ++pos)) {
  98. switch (command) {
  99. case VIEW_CONSOLE:
  100. return GAME_CONSOLE;
  101. case FILE_CLOSEGAME:
  102. return GAME_EXIT;
  103. case FILE_RETURNGAME:
  104. return GAME_MENU;
  105. default:
  106. break;
  107. }
  108. }
  109. }
  110. }
  111. if (scene) {
  112. if (keystate[SDLK_LEFT])
  113. scene->viewX(scene->viewX() + 1);
  114. if (keystate[SDLK_RIGHT])
  115. scene->viewX(scene->viewX() - 1);
  116. if (keystate[SDLK_UP])
  117. scene->viewY(scene->viewY() + 1);
  118. if (keystate[SDLK_DOWN])
  119. scene->viewY(scene->viewY() - 1);
  120. }
  121. return 0;
  122. }
  123. int gcsxGameloop() { start_func
  124. calcActualFPS();
  125. preGLScreenFlip();
  126. gameWorld->cycle();
  127. gameWorld->draw();
  128. postGLScreenFlip();
  129. // @TODO: actual proper delay/vsync wait
  130. SDL_Delay(1);
  131. return handleGameEvents(grabEvent);
  132. }
  133. void gcsxGameloopGui(int keysOk) { start_func
  134. calcActualFPS();
  135. preGLScreenFlip();
  136. gameWorld->cycle();
  137. gameWorld->draw();
  138. if (keysOk) {
  139. int result = handleGameEvents(grabEventGame);
  140. if (result == GAME_EXIT)
  141. desktop->broadcastEvent(SDL_SPECIAL, SDL_CLOSEGAME);
  142. if (result == GAME_MENU)
  143. desktop->broadcastEvent(SDL_SPECIAL, SDL_QUITTOGAME);
  144. if (result == GAME_CONSOLE)
  145. desktop->broadcastEvent(SDL_COMMAND, VIEW_CONSOLE);
  146. }
  147. }