main.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /** Example 019 Mouse and Joystick
  2. This tutorial builds on example 04.Movement which showed how to
  3. handle keyboard events in Irrlicht. Here we'll handle mouse events
  4. and joystick events, if you have a joystick connected and a device
  5. that supports joysticks. These are currently Windows, Linux and SDL
  6. devices.
  7. */
  8. #ifdef _MSC_VER
  9. // We'll define this to stop MSVC complaining about sprintf().
  10. #define _CRT_SECURE_NO_WARNINGS
  11. #pragma comment(lib, "Irrlicht.lib")
  12. #endif
  13. #include <irrlicht.h>
  14. #include "driverChoice.h"
  15. using namespace irr;
  16. /*
  17. Just as we did in example 04.Movement, we'll store the latest state of the
  18. mouse and the first joystick, updating them as we receive events.
  19. */
  20. class MyEventReceiver : public IEventReceiver
  21. {
  22. public:
  23. // We'll create a struct to record info on the mouse state
  24. struct SMouseState
  25. {
  26. core::position2di Position;
  27. bool LeftButtonDown;
  28. SMouseState() : LeftButtonDown(false) { }
  29. } MouseState;
  30. // This is the one method that we have to implement
  31. virtual bool OnEvent(const SEvent& event)
  32. {
  33. // Remember the mouse state
  34. if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
  35. {
  36. switch(event.MouseInput.Event)
  37. {
  38. case EMIE_LMOUSE_PRESSED_DOWN:
  39. MouseState.LeftButtonDown = true;
  40. break;
  41. case EMIE_LMOUSE_LEFT_UP:
  42. MouseState.LeftButtonDown = false;
  43. break;
  44. case EMIE_MOUSE_MOVED:
  45. MouseState.Position.X = event.MouseInput.X;
  46. MouseState.Position.Y = event.MouseInput.Y;
  47. break;
  48. default:
  49. // We won't use the wheel
  50. break;
  51. }
  52. }
  53. // The state of each connected joystick is sent to us
  54. // once every run() of the Irrlicht device. Store the
  55. // state of the first joystick, ignoring other joysticks.
  56. // This is currently only supported on Windows and Linux.
  57. if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT
  58. && event.JoystickEvent.Joystick == 0)
  59. {
  60. JoystickState = event.JoystickEvent;
  61. }
  62. return false;
  63. }
  64. const SEvent::SJoystickEvent & GetJoystickState(void) const
  65. {
  66. return JoystickState;
  67. }
  68. const SMouseState & GetMouseState(void) const
  69. {
  70. return MouseState;
  71. }
  72. MyEventReceiver()
  73. {
  74. }
  75. private:
  76. SEvent::SJoystickEvent JoystickState;
  77. };
  78. /*
  79. The event receiver for keeping the pressed keys is ready, the actual responses
  80. will be made inside the render loop, right before drawing the scene. So lets
  81. just create an irr::IrrlichtDevice and the scene node we want to move. We also
  82. create some other additional scene nodes, to show that there are also some
  83. different possibilities to move and animate scene nodes.
  84. */
  85. int main()
  86. {
  87. // ask user for driver
  88. video::E_DRIVER_TYPE driverType=driverChoiceConsole();
  89. if (driverType==video::EDT_COUNT)
  90. return 1;
  91. // create device
  92. MyEventReceiver receiver;
  93. IrrlichtDevice* device = createDevice(driverType,
  94. core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver);
  95. if (device == 0)
  96. return 1; // could not create selected driver.
  97. core::array<SJoystickInfo> joystickInfo;
  98. if(device->activateJoysticks(joystickInfo))
  99. {
  100. std::cout << "Joystick support is enabled and " << joystickInfo.size() << " joystick(s) are present." << std::endl;
  101. for(u32 joystick = 0; joystick < joystickInfo.size(); ++joystick)
  102. {
  103. std::cout << "Joystick " << joystick << ":" << std::endl;
  104. std::cout << "\tName: '" << joystickInfo[joystick].Name.c_str() << "'" << std::endl;
  105. std::cout << "\tAxes: " << joystickInfo[joystick].Axes << std::endl;
  106. std::cout << "\tButtons: " << joystickInfo[joystick].Buttons << std::endl;
  107. std::cout << "\tHat is: ";
  108. switch(joystickInfo[joystick].PovHat)
  109. {
  110. case SJoystickInfo::POV_HAT_PRESENT:
  111. std::cout << "present" << std::endl;
  112. break;
  113. case SJoystickInfo::POV_HAT_ABSENT:
  114. std::cout << "absent" << std::endl;
  115. break;
  116. case SJoystickInfo::POV_HAT_UNKNOWN:
  117. default:
  118. std::cout << "unknown" << std::endl;
  119. break;
  120. }
  121. }
  122. }
  123. else
  124. {
  125. std::cout << "Joystick support is not enabled." << std::endl;
  126. }
  127. core::stringw tmp = L"Irrlicht Joystick Example (";
  128. tmp += joystickInfo.size();
  129. tmp += " joysticks)";
  130. device->setWindowCaption(tmp.c_str());
  131. video::IVideoDriver* driver = device->getVideoDriver();
  132. scene::ISceneManager* smgr = device->getSceneManager();
  133. /*
  134. We'll create an arrow mesh and move it around either with the joystick axis/hat,
  135. or make it follow the mouse pointer. */
  136. scene::ISceneNode * node = smgr->addMeshSceneNode(
  137. smgr->addArrowMesh( "Arrow",
  138. video::SColor(255, 255, 0, 0),
  139. video::SColor(255, 0, 255, 0),
  140. 16,16,
  141. 2.f, 1.3f,
  142. 0.1f, 0.6f
  143. )
  144. );
  145. node->setMaterialFlag(video::EMF_LIGHTING, false);
  146. scene::ICameraSceneNode * camera = smgr->addCameraSceneNode();
  147. camera->setPosition(core::vector3df(0, 0, -10));
  148. // As in example 04, we'll use framerate independent movement.
  149. u32 then = device->getTimer()->getTime();
  150. const f32 MOVEMENT_SPEED = 5.f;
  151. while(device->run())
  152. {
  153. // Work out a frame delta time.
  154. const u32 now = device->getTimer()->getTime();
  155. const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
  156. then = now;
  157. bool movedWithJoystick = false;
  158. core::vector3df nodePosition = node->getPosition();
  159. if(joystickInfo.size() > 0)
  160. {
  161. f32 moveHorizontal = 0.f; // Range is -1.f for full left to +1.f for full right
  162. f32 moveVertical = 0.f; // -1.f for full down to +1.f for full up.
  163. const SEvent::SJoystickEvent & joystickData = receiver.GetJoystickState();
  164. // We receive the full analog range of the axes, and so have to implement our
  165. // own dead zone. This is an empirical value, since some joysticks have more
  166. // jitter or creep around the center point than others. We'll use 5% of the
  167. // range as the dead zone, but generally you would want to give the user the
  168. // option to change this.
  169. const f32 DEAD_ZONE = 0.05f;
  170. moveHorizontal =
  171. (f32)joystickData.Axis[SEvent::SJoystickEvent::AXIS_X] / 32767.f;
  172. if(fabs(moveHorizontal) < DEAD_ZONE)
  173. moveHorizontal = 0.f;
  174. moveVertical =
  175. (f32)joystickData.Axis[SEvent::SJoystickEvent::AXIS_Y] / -32767.f;
  176. if(fabs(moveVertical) < DEAD_ZONE)
  177. moveVertical = 0.f;
  178. // POV hat info is only currently supported on Windows, but the value is
  179. // guaranteed to be 65535 if it's not supported, so we can check its range.
  180. const u16 povDegrees = joystickData.POV / 100;
  181. if(povDegrees < 360)
  182. {
  183. if(povDegrees > 0 && povDegrees < 180)
  184. moveHorizontal = 1.f;
  185. else if(povDegrees > 180)
  186. moveHorizontal = -1.f;
  187. if(povDegrees > 90 && povDegrees < 270)
  188. moveVertical = -1.f;
  189. else if(povDegrees > 270 || povDegrees < 90)
  190. moveVertical = +1.f;
  191. }
  192. if(!core::equals(moveHorizontal, 0.f) || !core::equals(moveVertical, 0.f))
  193. {
  194. nodePosition.X += MOVEMENT_SPEED * frameDeltaTime * moveHorizontal;
  195. nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime * moveVertical;
  196. movedWithJoystick = true;
  197. }
  198. }
  199. // If the arrow node isn't being moved with the joystick, then have it follow the mouse cursor.
  200. if(!movedWithJoystick)
  201. {
  202. // Create a ray through the mouse cursor.
  203. core::line3df ray = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(
  204. receiver.GetMouseState().Position, camera);
  205. // And intersect the ray with a plane around the node facing towards the camera.
  206. core::plane3df plane(nodePosition, core::vector3df(0, 0, -1));
  207. core::vector3df mousePosition;
  208. if(plane.getIntersectionWithLine(ray.start, ray.getVector(), mousePosition))
  209. {
  210. // We now have a mouse position in 3d space; move towards it.
  211. core::vector3df toMousePosition(mousePosition - nodePosition);
  212. const f32 availableMovement = MOVEMENT_SPEED * frameDeltaTime;
  213. if(toMousePosition.getLength() <= availableMovement)
  214. nodePosition = mousePosition; // Jump to the final position
  215. else
  216. nodePosition += toMousePosition.normalize() * availableMovement; // Move towards it
  217. }
  218. }
  219. node->setPosition(nodePosition);
  220. // Turn lighting on and off depending on whether the left mouse button is down.
  221. node->setMaterialFlag(video::EMF_LIGHTING, receiver.GetMouseState().LeftButtonDown);
  222. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255,113,113,133));
  223. smgr->drawAll(); // draw the 3d scene
  224. driver->endScene();
  225. }
  226. /*
  227. In the end, delete the Irrlicht device.
  228. */
  229. device->drop();
  230. return 0;
  231. }
  232. /*
  233. **/