Main.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // $Id: Main.cpp 147 2007-02-24 06:13:17Z super_gb $
  2. #include <Ogre.h>
  3. #include <OIS/OIS.h>
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include "Define.h"
  7. #include "Main.h"
  8. #include "common/TypeDefine.h"
  9. #include "common/filesystem/GameFileSystem.h"
  10. #include "common/filesystem/RealFileSystem.h"
  11. #include "common/utilites/Logger.h"
  12. #include "common/utilites/StdString.h"
  13. #include "common/utilites/Utilites.h"
  14. #include "core/input/InputFilter.h"
  15. #include "game/field/Walkmesh.h"
  16. #include "game/field/WalkmeshFile.h"
  17. Uint8 state;
  18. class DisplayFrameListener : public Ogre::FrameListener, public Ogre::WindowEventListener, public OIS::KeyListener, public OIS::MouseListener
  19. {
  20. public:
  21. // Constructor takes a RenderWindow because it uses that to determine input context
  22. DisplayFrameListener(Ogre::RenderWindow* win):
  23. m_Window(win),
  24. m_InputManager(0),
  25. m_Keyboard(0),
  26. m_CameraFreeRotate(false)
  27. {
  28. using namespace OIS;
  29. Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
  30. ParamList pl;
  31. size_t windowHnd = 0;
  32. std::ostringstream windowHndStr;
  33. win->getCustomAttribute("WINDOW", &windowHnd);
  34. windowHndStr << windowHnd;
  35. pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
  36. m_InputManager = InputManager::createInputSystem(pl);
  37. m_Keyboard = static_cast<Keyboard*>(m_InputManager->createInputObject(OISKeyboard, true));
  38. m_Keyboard->setEventCallback(this);
  39. m_Mouse = static_cast<OIS::Mouse*>(m_InputManager->createInputObject(OIS::OISMouse, true));
  40. m_Mouse->setEventCallback(this);
  41. //Register as a Window listener
  42. Ogre::WindowEventUtilities::addWindowEventListener(m_Window, this);
  43. }
  44. virtual
  45. ~DisplayFrameListener(void)
  46. {
  47. m_InputManager->destroyInputObject(m_Keyboard);
  48. m_InputManager->destroyInputObject(m_Mouse);
  49. OIS::InputManager::destroyInputSystem(m_InputManager);
  50. //Remove ourself as a Window listener
  51. Ogre::WindowEventUtilities::removeWindowEventListener(m_Window, this);
  52. windowClosed(m_Window);
  53. }
  54. //Unattach OIS before window shutdown (very important under Linux)
  55. virtual void
  56. windowClosed(Ogre::RenderWindow* rw)
  57. {
  58. state = EXIT;
  59. }
  60. bool
  61. frameStarted(const Ogre::FrameEvent& evt)
  62. {
  63. if (state == EXIT)
  64. {
  65. return false;
  66. }
  67. if (m_Keyboard)
  68. {
  69. m_Keyboard->capture();
  70. }
  71. if (m_Mouse)
  72. {
  73. m_Mouse->capture();
  74. }
  75. Ogre::SceneManager* scene_manager;
  76. scene_manager = Ogre::Root::getSingleton().getSceneManager("Scene");
  77. m_InputFilter.Update();
  78. static InputEventArray input_event_array;
  79. input_event_array.clear();
  80. m_InputFilter.GetInputEvents(input_event_array);
  81. for (size_t i = 0; i < input_event_array.size(); ++i)
  82. {
  83. if (input_event_array[i].name == "quit" && input_event_array[i].type == IE_PRESSED)
  84. {
  85. Ogre::LogManager::getSingletonPtr()->logMessage("Quit Pressed");
  86. state = EXIT;
  87. continue;
  88. }
  89. if (input_event_array[i].name == "camera_forward" && (input_event_array[i].type == IE_PRESSED || input_event_array[i].type == IE_REPEATED))
  90. {
  91. scene_manager->getCamera("Camera")->moveRelative(Ogre::Vector3( 0, 0, -2));
  92. }
  93. if (input_event_array[i].name == "camera_left" && (input_event_array[i].type == IE_PRESSED || input_event_array[i].type == IE_REPEATED))
  94. {
  95. scene_manager->getCamera("Camera")->moveRelative(Ogre::Vector3(-2, 0, 0));
  96. }
  97. if (input_event_array[i].name == "camera_backward" && (input_event_array[i].type == IE_PRESSED || input_event_array[i].type == IE_REPEATED))
  98. {
  99. scene_manager->getCamera("Camera")->moveRelative(Ogre::Vector3( 0, 0, 2));
  100. }
  101. if (input_event_array[i].name == "camera_right" && (input_event_array[i].type == IE_PRESSED || input_event_array[i].type == IE_REPEATED))
  102. {
  103. scene_manager->getCamera("Camera")->moveRelative(Ogre::Vector3( 2, 0, 0));
  104. }
  105. if (input_event_array[i].name == "camera_rotate" && input_event_array[i].type == IE_PRESSED)
  106. {
  107. m_CameraFreeRotate = true;
  108. }
  109. else if (input_event_array[i].name == "camera_rotate" && input_event_array[i].type == IE_RELEASED)
  110. {
  111. m_CameraFreeRotate = false;
  112. }
  113. if (m_CameraFreeRotate && input_event_array[i].name == "mouse_move")
  114. {
  115. scene_manager->getCamera("Camera")->rotate(Ogre::Vector3::UNIT_Y, Ogre::Radian(Ogre::Degree(input_event_array[i].x * 0.13f)));
  116. scene_manager->getCamera("Camera")->pitch(Ogre::Degree(-input_event_array[i].y * 0.13f));
  117. }
  118. }
  119. return true;
  120. }
  121. // KeyListener
  122. bool
  123. keyPressed(const OIS::KeyEvent& e)
  124. {
  125. m_InputFilter.ButtonPressed(e.key, true);
  126. return true;
  127. }
  128. bool
  129. keyReleased(const OIS::KeyEvent& e)
  130. {
  131. m_InputFilter.ButtonPressed(e.key, false);
  132. return true;
  133. }
  134. bool
  135. mouseMoved(const OIS::MouseEvent &e)
  136. {
  137. m_InputFilter.MouseMoved(e.state.X.rel, e.state.Y.rel);
  138. return true;
  139. }
  140. bool
  141. mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
  142. {
  143. m_InputFilter.ButtonPressed(id + 256, true);
  144. return true;
  145. }
  146. bool
  147. mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
  148. {
  149. m_InputFilter.ButtonPressed(id + 256, false);
  150. return true;
  151. }
  152. protected:
  153. Ogre::RenderWindow* m_Window;
  154. InputFilter m_InputFilter;
  155. //OIS Input devices
  156. OIS::InputManager* m_InputManager;
  157. OIS::Keyboard* m_Keyboard;
  158. OIS::Mouse* m_Mouse;
  159. bool m_CameraFreeRotate;
  160. };
  161. int
  162. main(int argc, char *argv[])
  163. {
  164. Ogre::Root* root;
  165. Ogre::RenderWindow* window;
  166. Ogre::SceneManager* scene_manager;
  167. Ogre::Camera* camera;
  168. Ogre::Viewport* viewport;
  169. root = new Ogre::Root("plugins.cfg", "q-gears.cfg", "ogre q-gears.log");
  170. if (root->restoreConfig() == true || root->showConfigDialog() == true)
  171. {
  172. window = root->initialise(true, "FFVII Model Exporter 0.00 alpha3");
  173. }
  174. else
  175. {
  176. return 0;
  177. }
  178. DisplayFrameListener* frame_listener = new DisplayFrameListener(window);
  179. root->addFrameListener(frame_listener);
  180. scene_manager = root->createSceneManager(Ogre::ST_GENERIC, "Scene");
  181. scene_manager->setAmbientLight(Ogre::ColourValue(1.0, 1.0, 1.0));
  182. camera = scene_manager->createCamera("Camera");
  183. camera->setNearClipDistance(5);
  184. viewport = window->addViewport(camera);
  185. viewport->setBackgroundColour(Ogre::ColourValue(0, 0, 0));
  186. camera->setAspectRatio(Ogre::Real(viewport->getActualWidth()) / Ogre::Real(viewport->getActualHeight()));
  187. Ogre::ResourceGroupManager::getSingleton().addResourceLocation("./data/", "FileSystem", "General");
  188. Ogre::ResourceGroupManager::getSingleton().addResourceLocation("./data/OgreCore.zip", "Zip", "Bootstrap");
  189. Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
  190. REALFILESYSTEM = new RealFileSystem();
  191. LOGGER = new Logger(DEFAULT_LOG, true, true);
  192. LOGGER->Log(LOGGER_ERROR, "ModuleManager created.");
  193. GAMEFILESYSTEM = new GameFileSystem();
  194. state = GAME;
  195. scene_manager->clearScene();
  196. scene_manager->setAmbientLight(Ogre::ColourValue(1.0, 1.0, 1.0));
  197. camera = scene_manager->getCamera("Camera");
  198. camera->setPosition(300, -500, 300);
  199. camera->lookAt(0, 0, 0);
  200. Walkmesh* walkmesh = new Walkmesh();
  201. WalkmeshFile* file = new WalkmeshFile("field/1_walkmesh");
  202. file->GetWalkmesh(walkmesh);
  203. delete file;
  204. walkmesh->SetUpWalkmesh();
  205. walkmesh->setVisible(true);
  206. Ogre::SceneNode* node = Ogre::Root::getSingleton().getSceneManager("Scene")->getRootSceneNode()->createChildSceneNode();
  207. node->attachObject(walkmesh);
  208. Ogre::Root::getSingleton().startRendering();
  209. LOGGER->Log(LOGGER_INFO, "===================== Stop the game!!!");
  210. delete GAMEFILESYSTEM;
  211. SAFE_DELETE(LOGGER)
  212. SAFE_DELETE(REALFILESYSTEM)
  213. delete root;
  214. delete frame_listener;
  215. return 0;
  216. }