OgreBase.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <Ogre.h>
  17. #include <OgreTextAreaOverlayElement.h>
  18. //#include <OgreOverlayManager.h>
  19. //#include <Overlay/OgreOverlayManager.h>
  20. #include <OgreOverlay.h>
  21. #include <OgreOverlayManager.h>
  22. #include <Bites/OgreWindowEventUtilities.h>
  23. #include <OIS/OIS.h>
  24. #include "common/FileSystem.h"
  25. #include "Logger.h"
  26. /**
  27. * The Ogre system root component.
  28. */
  29. extern Ogre::Root* root;
  30. /**
  31. * The Ogre render window.
  32. */
  33. extern Ogre::RenderWindow* window;
  34. /**
  35. * List of Ogre entities.
  36. */
  37. extern std::vector<Ogre::Entity*> entities;
  38. /**
  39. * The Ogre camera.
  40. */
  41. extern Ogre::Camera* camera;
  42. class DisplayFrameListener;
  43. /**
  44. * The display frame listener.
  45. */
  46. extern DisplayFrameListener* frame_listener;
  47. /**
  48. * The display frame listener.
  49. *
  50. * It listens and responds to input events, such as window changes or mouse and
  51. * keyboard inputs.
  52. */
  53. class DisplayFrameListener :
  54. public Ogre::FrameListener, public Ogre::WindowEventListener,
  55. public OIS::KeyListener, public OIS::MouseListener
  56. {
  57. public:
  58. // Constructor takes a RenderWindow because it uses that to determine input context
  59. /**
  60. * Constructor.
  61. *
  62. * @param win[in] The render window. Used to determine input context.
  63. */
  64. DisplayFrameListener(Ogre::RenderWindow* win):
  65. window_(win), input_manager_(0), keyboard_(0),
  66. mouse_(0), mouse_rotate_(false), exit_(false)
  67. {
  68. using namespace OIS;
  69. Ogre::LogManager::getSingletonPtr()->logMessage(
  70. "[OIS] Initializing..."
  71. );
  72. ParamList pl;
  73. size_t window_hnd = 0; // TODO What does HND stand for?
  74. std::ostringstream window_hnd_str;
  75. win->getCustomAttribute("WINDOW", &window_hnd);
  76. window_hnd_str << window_hnd;
  77. pl.insert(
  78. std::make_pair(std::string("WINDOW"), window_hnd_str.str())
  79. );
  80. input_manager_ = InputManager::createInputSystem(pl);
  81. keyboard_ = static_cast<Keyboard*> (
  82. input_manager_->createInputObject(OIS::OISKeyboard, true)
  83. );
  84. keyboard_->setEventCallback(this);
  85. mouse_ = static_cast<OIS::Mouse*> (
  86. input_manager_->createInputObject(OIS::OISMouse, true)
  87. );
  88. mouse_->setEventCallback(this);
  89. //Register as a Window listener.
  90. Ogre::WindowEventUtilities::addWindowEventListener(window_, this);
  91. // Debug info.
  92. info_text = new Ogre::TextAreaOverlayElement("DebugText");
  93. info_text->setCaption("");
  94. info_text->setMetricsMode(Ogre::GMM_PIXELS);
  95. info_text->setPosition(5, 5);
  96. // info_text->setFontName("BlueHighway");
  97. info_text->setCharHeight(18);
  98. info_overlay = Ogre::OverlayManager::getSingleton().create(
  99. "DebugOverlay"
  100. );
  101. info_overlay->setZOrder(1);
  102. info_overlay->add2D((Ogre::OverlayContainer*) info_text);
  103. info_overlay->show();
  104. }
  105. virtual ~DisplayFrameListener(){
  106. input_manager_->destroyInputObject(keyboard_);
  107. OIS::InputManager::destroyInputSystem(input_manager_);
  108. //Remove self as a Window listener.
  109. Ogre::WindowEventUtilities::removeWindowEventListener(
  110. window_, this
  111. );
  112. windowClosed(window_);
  113. }
  114. /**
  115. * Called on window closing.
  116. *
  117. * Detaches OIS before window shutdown (very important under Linux).
  118. */
  119. virtual void windowClosed(Ogre::RenderWindow* rw){exit_ = true;}
  120. /**
  121. * Called when a frame is about to begin rendering.
  122. *
  123. * This event happens before any render targets have begun updating.
  124. * It captures input events and checks the state of entities.
  125. *
  126. * @param evt[in] Triggering event. Unused.
  127. * @return True to go ahead, false to abort rendering and drop out of
  128. * the rendering loop.
  129. */
  130. bool frameStarted(const Ogre::FrameEvent& evt){
  131. // TODO: Move implementation to cpp file.
  132. if (exit_ == true) return false;
  133. if (keyboard_) keyboard_->capture();
  134. if (mouse_) mouse_->capture();
  135. for (unsigned int i = 0; i < entities.size(); ++ i){
  136. if (entities[i]->isVisible() == true){
  137. Ogre::AnimationStateIterator animations
  138. = entities[i]->getAllAnimationStates()
  139. ->getAnimationStateIterator();
  140. while (animations.hasMoreElements() == true){
  141. Ogre::AnimationState* state = animations.getNext();
  142. if (state->getEnabled() == true){
  143. state->addTime(evt.timeSinceLastFrame);
  144. info_text->setCaption(
  145. entities[i]->getName() + ": "
  146. + state->getAnimationName()
  147. );
  148. }
  149. }
  150. break;
  151. }
  152. }
  153. if (keyboard_->isKeyDown(OIS::KC_A)){
  154. assert(camera_);
  155. camera_->moveRelative(Ogre::Vector3(-0.005f, 0.0f, 0.0f));
  156. }
  157. if (keyboard_->isKeyDown(OIS::KC_D)){
  158. assert(camera_);
  159. camera_->moveRelative(Ogre::Vector3(0.005f, 0.0f, 0.0f));
  160. }
  161. if (keyboard_->isKeyDown(OIS::KC_W)){
  162. assert(camera_);
  163. camera_->moveRelative(Ogre::Vector3(0.0f, 0.0f, -0.005f));
  164. }
  165. if (keyboard_->isKeyDown(OIS::KC_S)){
  166. assert(camera_);
  167. camera_->moveRelative(Ogre::Vector3(0.0f, 0.0f, 0.005f));
  168. }
  169. if (mouse_rotate_ == true){
  170. assert(camera_);
  171. camera_->yaw(Ogre::Degree(-mouse_move_x_ * 0.13f));
  172. camera_->pitch(Ogre::Degree(-mouse_move_y_ * 0.13f));
  173. mouse_move_x_ = 0;
  174. mouse_move_y_ = 0;
  175. }
  176. return true;
  177. }
  178. /**
  179. * The key listener.
  180. *
  181. * Parses keyboard events.
  182. *
  183. * @param event[in] Triggering event.
  184. * @return Always true
  185. */
  186. bool keyPressed(const OIS::KeyEvent& e){
  187. switch(e.key){
  188. case OIS::KC_RIGHT:
  189. {
  190. bool change = false;
  191. for (unsigned int i = 0; i < entities.size(); ++ i){
  192. if (change == true){
  193. entities[i]->setVisible(true);
  194. break;
  195. }
  196. if (
  197. entities[i]->isVisible() == true
  198. && (i != entities.size() - 1)
  199. ){
  200. entities[i]->setVisible(false);
  201. change = true;
  202. }
  203. }
  204. }
  205. break;
  206. case OIS::KC_LEFT:
  207. {
  208. bool change = false;
  209. for(int i = entities.size() - 1; i >= 0; -- i){
  210. if (change == true){
  211. entities[i]->setVisible(true);
  212. break;
  213. }
  214. if (entities[i]->isVisible() == true && i != 0){
  215. entities[i]->setVisible(false);
  216. change = true;
  217. }
  218. }
  219. }
  220. break;
  221. case OIS::KC_UP:
  222. {
  223. bool change = false;
  224. for (unsigned int i = 0; i < entities.size(); ++i){
  225. if (entities[i]->isVisible() == true){
  226. Ogre::AnimationStateIterator animations
  227. = entities[i]->getAllAnimationStates()
  228. ->getAnimationStateIterator();
  229. Ogre::AnimationState* old_state;
  230. Ogre::AnimationState* new_state = animations.getNext();
  231. while (animations.hasMoreElements() == true){
  232. old_state = new_state;
  233. new_state = animations.getNext();
  234. if (new_state->getEnabled() == true){
  235. new_state->setEnabled(false);
  236. new_state->setLoop(false);
  237. old_state->setEnabled(true);
  238. old_state->setLoop(true);
  239. }
  240. }
  241. break;
  242. }
  243. }
  244. }
  245. break;
  246. case OIS::KC_DOWN:{
  247. bool change = false;
  248. for (unsigned int i = 0; i < entities.size(); ++ i){
  249. if (entities[i]->isVisible() == true){
  250. Ogre::AnimationStateIterator animations
  251. = entities[i]->getAllAnimationStates()
  252. ->getAnimationStateIterator();
  253. while (animations.hasMoreElements() == true){
  254. Ogre::AnimationState* state
  255. = animations.getNext();
  256. if (change == true){
  257. state->setEnabled(true);
  258. state->setLoop(true);
  259. break;
  260. }
  261. if (
  262. state->getEnabled() == true
  263. && animations.hasMoreElements() != false
  264. ){
  265. state->setEnabled(false);
  266. state->setLoop(false);
  267. change = true;
  268. }
  269. }
  270. break;
  271. }
  272. }
  273. }
  274. break;
  275. }
  276. return true;
  277. }
  278. /**
  279. * Handles key releasing events.
  280. *
  281. * It does nothing.
  282. *
  283. * @param e[in] Triggering event.
  284. * @return Always true.
  285. */
  286. bool keyReleased(const OIS::KeyEvent& e){return true;}
  287. /**
  288. * Handles mouse movement events.
  289. *
  290. * @param e[in] Triggering event.
  291. * @return Always true.
  292. */
  293. bool mouseMoved(const OIS::MouseEvent& e){
  294. mouse_move_x_ = float(e.state.X.rel);
  295. mouse_move_y_ = float(e.state.Y.rel);
  296. return true;
  297. }
  298. /**
  299. * Handles mouse button-down events.
  300. *
  301. * @param e[in] Triggering event.
  302. * @param id[in] Button ID.
  303. * @return Always true.
  304. */
  305. bool mousePressed(const OIS::MouseEvent& e, OIS::MouseButtonID id){
  306. if (id == OIS::MB_Right) mouse_rotate_ = true;
  307. return true;
  308. }
  309. /**
  310. * Handles mouse button-up events.
  311. *
  312. * @param e[in] Triggering event.
  313. * @param id[in] Button ID.
  314. * @return Always true.
  315. */
  316. bool mouseReleased(const OIS::MouseEvent& e, OIS::MouseButtonID id){
  317. if (id == OIS::MB_Right) mouse_rotate_ = false;
  318. return true;
  319. }
  320. /**
  321. * The information text.
  322. */
  323. Ogre::TextAreaOverlayElement* info_text;
  324. /**
  325. * The information overlay.
  326. */
  327. Ogre::Overlay* info_overlay;
  328. /**
  329. * Sets the viewport camera
  330. */
  331. virtual void setCamera(Ogre::Camera *camera){camera_ = camera;}
  332. private:
  333. /**
  334. * The render window.
  335. */
  336. Ogre::RenderWindow* window_;
  337. /**
  338. * The OIS input manager.
  339. */
  340. OIS::InputManager* input_manager_;
  341. /**
  342. * The OIS keyboard.
  343. */
  344. OIS::Keyboard* keyboard_;
  345. /**
  346. * The OIS mouse.
  347. */
  348. OIS::Mouse* mouse_;
  349. /**
  350. * The camera.
  351. */
  352. Ogre::Camera *camera_;
  353. /**
  354. * Mouse horizontal movement.
  355. */
  356. float mouse_move_x_;
  357. /**
  358. * Mouse vertical movement.
  359. */
  360. float mouse_move_y_;
  361. /**
  362. * Indicates if the mouse is being rotated.
  363. */
  364. bool mouse_rotate_;
  365. /**
  366. * Flag for application exit.
  367. */
  368. bool exit_;
  369. };
  370. /**
  371. * Initializes the Ogre system.
  372. *
  373. * @param name[in] Application title.
  374. */
  375. void InitializeOgreBase(const Ogre::String& name);
  376. /**
  377. * Destructs the Ogre system.
  378. */
  379. void DeinitializeOgreBase();