You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

266 lines
8.2 KiB

  1. //
  2. // Lol Engine - Input tutorial
  3. //
  4. // Copyright: (c) 2011-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <lol/engine.h>
  14. #include "loldebug.h"
  15. using namespace lol;
  16. LOLFX_RESOURCE_DECLARE(07_input);
  17. class InputTutorial : public WorldEntity
  18. {
  19. public:
  20. InputTutorial()
  21. {
  22. m_controller = new Controller("Default");
  23. # ifdef OLD_SCHOOL
  24. m_controller->SetInputCount(KEY_MAX, AXIS_MAX);
  25. m_keyboard = InputDevice::Get("Keyboard");
  26. if (m_keyboard)
  27. m_controller->GetKey(KEY_MANUAL_ROTATION).Bind("Keyboard", "Space");
  28. m_mouse = InputDevice::Get("Mouse");
  29. if (m_mouse)
  30. {
  31. m_controller->GetKey(KEY_DRAG_MESH).Bind("Mouse", "Left");
  32. m_controller->GetAxis(AXIS_DRAG_PITCH).Bind("Mouse", "Y");
  33. m_controller->GetAxis(AXIS_DRAG_YAW).Bind("Mouse", "X");
  34. }
  35. m_joystick = InputDevice::Get("Joystick1");
  36. if (m_joystick)
  37. {
  38. m_controller->GetAxis(AXIS_PITCH).Bind("Joystick1", "Axis2");
  39. m_controller->GetAxis(AXIS_YAW).Bind("Joystick1", "Axis1");
  40. }
  41. # else
  42. m_profile
  43. << InputProfile::Keyboard(KEY_MANUAL_ROTATION, "Space")
  44. << InputProfile::MouseKey(KEY_DRAG_MESH, "Left")
  45. << InputProfile::JoystickAxis(1, AXIS_PITCH, "Axis2")
  46. << InputProfile::JoystickAxis(1, AXIS_YAW, "Axis1")
  47. << InputProfile::MouseAxis(AXIS_DRAG_PITCH, "Y")
  48. << InputProfile::MouseAxis(AXIS_DRAG_YAW, "X");
  49. m_controller->Init(m_profile);
  50. m_keyboard = InputDevice::GetKeyboard();
  51. m_mouse = InputDevice::GetMouse();
  52. m_joystick = InputDevice::GetJoystick(1);
  53. # endif //OLD_SCHOOL
  54. m_pitch_angle = 0;
  55. m_yaw_angle = 0;
  56. m_autorot = true;
  57. /* Front vertices/colors */
  58. m_mesh.Push(vec3(-1.0, -1.0, 1.0), vec3(1.0, 0.0, 1.0));
  59. m_mesh.Push(vec3( 1.0, -1.0, 1.0), vec3(0.0, 1.0, 0.0));
  60. m_mesh.Push(vec3( 1.0, 1.0, 1.0), vec3(1.0, 0.5, 0.0));
  61. m_mesh.Push(vec3(-1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0));
  62. /* Back */
  63. m_mesh.Push(vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0));
  64. m_mesh.Push(vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0));
  65. m_mesh.Push(vec3( 1.0, 1.0, -1.0), vec3(0.0, 0.5, 1.0));
  66. m_mesh.Push(vec3(-1.0, 1.0, -1.0), vec3(0.0, 0.0, 1.0));
  67. m_faces_indices << 0 << 1 << 2 << 2 << 3 << 0;
  68. m_faces_indices << 1 << 5 << 6 << 6 << 2 << 1;
  69. m_faces_indices << 7 << 6 << 5 << 5 << 4 << 7;
  70. m_faces_indices << 4 << 0 << 3 << 3 << 7 << 4;
  71. m_faces_indices << 4 << 5 << 1 << 1 << 0 << 4;
  72. m_faces_indices << 3 << 2 << 6 << 6 << 7 << 3;
  73. m_lines_indices << 0 << 1 << 1 << 2 << 2 << 3 << 3 << 0;
  74. m_lines_indices << 4 << 5 << 5 << 6 << 6 << 7 << 7 << 4;
  75. m_lines_indices << 0 << 4 << 1 << 5 << 2 << 6 << 3 << 7;
  76. m_text = new Text("", "data/font/ascii.png");
  77. m_text->SetPos(vec3(5, 5, 1));
  78. Ticker::Ref(m_text);
  79. m_ready = false;
  80. }
  81. ~InputTutorial()
  82. {
  83. Ticker::Unref(m_text);
  84. }
  85. virtual void TickGame(float seconds)
  86. {
  87. WorldEntity::TickGame(seconds);
  88. /* Handle keyboard */
  89. if (m_keyboard)
  90. {
  91. if (m_controller->WasKeyPressedThisFrame(KEY_MANUAL_ROTATION))
  92. m_autorot = !m_autorot;
  93. }
  94. /* Handle joystick */
  95. if (m_joystick)
  96. {
  97. if (lol::abs(m_controller->GetAxisValue(AXIS_PITCH)) > 0.2f)
  98. m_pitch_angle += m_controller->GetAxisValue(AXIS_PITCH) * seconds * 100;
  99. if (lol::abs(m_controller->GetAxisValue(AXIS_YAW)) > 0.2f)
  100. m_yaw_angle += m_controller->GetAxisValue(AXIS_YAW) * seconds * 100;
  101. }
  102. /* Handle mouse */
  103. if (m_mouse)
  104. {
  105. if (m_controller->IsKeyPressed(KEY_DRAG_MESH))
  106. {
  107. InputDevice::CaptureMouse(true);
  108. m_pitch_angle -= m_controller->GetAxisValue(AXIS_DRAG_PITCH) * seconds * 100;
  109. m_yaw_angle += m_controller->GetAxisValue(AXIS_DRAG_YAW) * seconds * 100;
  110. }
  111. else
  112. {
  113. InputDevice::CaptureMouse(false);
  114. if (m_autorot)
  115. m_yaw_angle += seconds * 20;
  116. }
  117. m_text->SetText(String::Printf(
  118. "cursor: (%0.3f, %0.3f) - pixel (%d, %d)",
  119. m_mouse->GetCursor(0).x, m_mouse->GetCursor(0).y,
  120. m_mouse->GetCursorPixel(0).x, m_mouse->GetCursorPixel(0).y));
  121. }
  122. else
  123. {
  124. m_text->SetText("no mouse detected");
  125. }
  126. mat4 anim = mat4::fromeuler_yxz(m_yaw_angle, m_pitch_angle, 0.f);
  127. mat4 model = mat4::translate(vec3(0, 0, -4.5));
  128. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  129. mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
  130. m_matrix = proj * view * model * anim;
  131. }
  132. virtual void TickDraw(float seconds, Scene &scene)
  133. {
  134. WorldEntity::TickDraw(seconds, scene);
  135. if (!m_ready)
  136. {
  137. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(07_input));
  138. m_mvp = m_shader->GetUniformLocation("in_Matrix");
  139. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  140. m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);
  141. m_vdecl =
  142. new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
  143. VertexUsage::Color));
  144. m_vbo = new VertexBuffer(m_mesh.Bytes());
  145. void *mesh = m_vbo->Lock(0, 0);
  146. memcpy(mesh, &m_mesh[0], m_mesh.Bytes());
  147. m_vbo->Unlock();
  148. m_lines_ibo = new IndexBuffer(m_lines_indices.Bytes());
  149. void *indices = m_lines_ibo->Lock(0, 0);
  150. memcpy(indices, &m_lines_indices[0], m_lines_indices.Bytes());
  151. m_lines_ibo->Unlock();
  152. m_faces_ibo = new IndexBuffer(m_faces_indices.Bytes());
  153. indices = m_faces_ibo->Lock(0, 0);
  154. memcpy(indices, &m_faces_indices[0], m_faces_indices.Bytes());
  155. m_faces_ibo->Unlock();
  156. /* FIXME: this object never cleans up */
  157. m_ready = true;
  158. }
  159. g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  160. m_shader->Bind();
  161. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  162. m_vdecl->Bind();
  163. m_shader->SetUniform(m_mvp, m_matrix);
  164. m_lines_ibo->Bind();
  165. m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, 0, 0,
  166. (int)m_mesh.Count(), 0, (int)m_lines_indices.Count());
  167. m_lines_ibo->Unbind();
  168. m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
  169. m_faces_ibo->Bind();
  170. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0,
  171. (int)m_mesh.Count(), 0, (int)m_faces_indices.Count());
  172. m_faces_ibo->Unbind();
  173. m_vdecl->Unbind();
  174. }
  175. private:
  176. enum
  177. {
  178. KEY_MANUAL_ROTATION,
  179. KEY_DRAG_MESH,
  180. KEY_MAX
  181. };
  182. enum
  183. {
  184. AXIS_DRAG_PITCH,
  185. AXIS_DRAG_YAW,
  186. AXIS_PITCH,
  187. AXIS_YAW,
  188. AXIS_MAX
  189. };
  190. InputDevice *m_keyboard, *m_mouse, *m_joystick;
  191. Controller *m_controller;
  192. InputProfile m_profile;
  193. bool m_autorot;
  194. float m_pitch_angle;
  195. float m_yaw_angle;
  196. mat4 m_matrix;
  197. array<vec3,vec3> m_mesh;
  198. array<uint16_t> m_lines_indices, m_faces_indices;
  199. Shader *m_shader;
  200. ShaderAttrib m_coord, m_color;
  201. ShaderUniform m_mvp;
  202. VertexDeclaration *m_vdecl;
  203. VertexBuffer *m_vbo;
  204. IndexBuffer *m_lines_ibo, *m_faces_ibo;
  205. Text *m_text;
  206. bool m_ready;
  207. };
  208. int main(int argc, char **argv)
  209. {
  210. System::Init(argc, argv);
  211. Application app("Tutorial 7: Input", ivec2(640, 480), 60.0f);
  212. new DebugFps(5, 5);
  213. new InputTutorial();
  214. app.Run();
  215. return EXIT_SUCCESS;
  216. }