GameFrameListner.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include <OgreStringConverter.h>
  2. #include "QGearsGameState.h"
  3. #include "core/CameraManager.h"
  4. #include "core/ConfigVar.h"
  5. #include "core/Console.h"
  6. #include "core/DebugDraw.h"
  7. #include "core/EntityManager.h"
  8. #include "core/GameFrameListner.h"
  9. #include "core/InputManager.h"
  10. #include "core/Logger.h"
  11. #include "core/ScriptManager.h"
  12. #include "core/Timer.h"
  13. #include "core/UiManager.h"
  14. ConfigVar cv_debug_fps("debug_fps", "Debug FPS", "false");
  15. GameFrameListener::GameFrameListener(Ogre::RenderWindow* win):
  16. m_Window(win),
  17. m_InputManager(0),
  18. m_Keyboard(0)
  19. {
  20. OIS::ParamList pl;
  21. size_t windowHnd = 0;
  22. std::ostringstream windowHndStr;
  23. win->getCustomAttribute("WINDOW", &windowHnd);
  24. windowHndStr << windowHnd;
  25. pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
  26. #if defined __WIN32__
  27. pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND")));
  28. pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
  29. #else
  30. pl.insert(std::make_pair(std::string("x11_mouse_grab"), std::string("false")));
  31. pl.insert(std::make_pair(std::string("x11_mouse_hide"), std::string("false")));
  32. // pl.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("false")));
  33. #endif
  34. m_InputManager = OIS::InputManager::createInputSystem(pl);
  35. m_Keyboard = static_cast< OIS::Keyboard* >(m_InputManager->createInputObject(OIS::OISKeyboard, true));
  36. m_Keyboard->setEventCallback(this);
  37. m_Mouse = static_cast< OIS::Mouse* >(m_InputManager->createInputObject(OIS::OISMouse, true));
  38. m_Mouse->setEventCallback(this);
  39. windowResized(m_Window);
  40. //Register as a Window listener
  41. Ogre::WindowEventUtilities::addWindowEventListener(m_Window, this);
  42. }
  43. GameFrameListener::~GameFrameListener()
  44. {
  45. m_InputManager->destroyInputObject(m_Keyboard);
  46. m_InputManager->destroyInputObject(m_Mouse);
  47. OIS::InputManager::destroyInputSystem(m_InputManager);
  48. //Remove ourself as a Window listener
  49. Ogre::WindowEventUtilities::removeWindowEventListener(m_Window, this);
  50. windowClosed(m_Window);
  51. }
  52. bool
  53. GameFrameListener::frameStarted(const Ogre::FrameEvent& evt)
  54. {
  55. Timer::getSingleton().AddTime(evt.timeSinceLastFrame);
  56. if(QGears::g_ApplicationState == QGears::G_EXIT)
  57. {
  58. return false;
  59. }
  60. if(m_Keyboard)
  61. {
  62. m_Keyboard->capture();
  63. }
  64. if(m_Mouse)
  65. {
  66. m_Mouse->capture();
  67. }
  68. InputManager::getSingleton().Update();
  69. static InputEventArray input_event_array;
  70. input_event_array.clear();
  71. InputManager::getSingleton().GetInputEvents(input_event_array);
  72. bool console_active = Console::getSingleton().IsVisible();
  73. for(size_t i = 0; i < input_event_array.size(); ++i)
  74. {
  75. Console::getSingleton().Input(input_event_array[ i ]);
  76. if(console_active != true)
  77. {
  78. EntityManager::getSingleton().Input(input_event_array[ i ]);
  79. ScriptManager::getSingleton().Input(input_event_array[ i ]);
  80. CameraManager::getSingleton().Input(input_event_array[ i ], evt.timeSinceLastFrame);
  81. }
  82. }
  83. Console::getSingleton().Update();
  84. ScriptManager::getSingleton().Update(ScriptManager::SYSTEM);
  85. UiManager::getSingleton().Update();
  86. CameraManager::getSingleton().Update();
  87. EntityManager::getSingleton().Update();
  88. return true;
  89. }
  90. bool
  91. GameFrameListener::frameEnded(const Ogre::FrameEvent& evt)
  92. {
  93. if(cv_debug_fps.GetB() == true)
  94. {
  95. const Ogre::RenderTarget::FrameStats& stats = m_Window->getStatistics();
  96. DEBUG_DRAW.SetTextAlignment(DEBUG_DRAW.LEFT);
  97. DEBUG_DRAW.SetScreenSpace(true);
  98. DEBUG_DRAW.SetColour(Ogre::ColourValue(1, 1, 1, 1));
  99. DEBUG_DRAW.Text(10, 10, "Current FPS:" + Ogre::StringConverter::toString(stats.lastFPS));
  100. }
  101. return true;
  102. }
  103. void
  104. GameFrameListener::windowMoved(Ogre::RenderWindow* rw)
  105. {
  106. }
  107. void
  108. GameFrameListener::windowResized(Ogre::RenderWindow *rw)
  109. {
  110. if(m_Mouse)
  111. {
  112. const OIS::MouseState& ms = m_Mouse->getMouseState();
  113. ms.width = (int)rw->getWidth();
  114. ms.height = (int)rw->getHeight();
  115. }
  116. Console::getSingleton().OnResize();
  117. UiManager::getSingleton().OnResize();
  118. CameraManager::getSingleton().OnResize();
  119. EntityManager::getSingleton().OnResize();
  120. }
  121. void
  122. GameFrameListener::windowClosed(Ogre::RenderWindow* rw)
  123. {
  124. QGears::g_ApplicationState = QGears::G_EXIT;
  125. }
  126. void
  127. GameFrameListener::windowFocusChange(Ogre::RenderWindow* rw)
  128. {
  129. }
  130. bool
  131. GameFrameListener::keyPressed(const OIS::KeyEvent& event)
  132. {
  133. InputManager::getSingleton().ButtonPressed(event.key, event.text, true);
  134. return true;
  135. }
  136. bool
  137. GameFrameListener::keyReleased(const OIS::KeyEvent& event)
  138. {
  139. InputManager::getSingleton().ButtonPressed(event.key, event.text, false);
  140. return true;
  141. }
  142. bool
  143. GameFrameListener::mouseMoved(const OIS::MouseEvent& e)
  144. {
  145. if(e.state.Z.rel != 0)
  146. {
  147. InputManager::getSingleton().MouseScrolled(e.state.Z.rel);
  148. }
  149. else
  150. {
  151. InputManager::getSingleton().MouseMoved(e.state.X.rel, e.state.Y.rel);
  152. }
  153. return true;
  154. }
  155. bool
  156. GameFrameListener::mousePressed(const OIS::MouseEvent& e, OIS::MouseButtonID id)
  157. {
  158. InputManager::getSingleton().MousePressed(id, true);
  159. return true;
  160. }
  161. bool
  162. GameFrameListener::mouseReleased(const OIS::MouseEvent& e, OIS::MouseButtonID id)
  163. {
  164. InputManager::getSingleton().MousePressed(id, false);
  165. return true;
  166. }