GameFrameListner.cpp 5.5 KB

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