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.
 
 
 

248 line
7.5 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", KEY_MAX, AXIS_MAX);
  23. m_keyboard = InputDevice::Get("Keyboard");
  24. if (m_keyboard)
  25. m_controller->GetKey(KEY_MANUAL_ROTATION).Bind("Keyboard", "Space");
  26. m_mouse = InputDevice::Get("Mouse");
  27. if (m_mouse)
  28. {
  29. m_controller->GetKey(KEY_DRAG_MESH).Bind("Mouse", "Left");
  30. m_controller->GetAxis(AXIS_DRAG_PITCH).Bind("Mouse", "Y");
  31. m_controller->GetAxis(AXIS_DRAG_YAW).Bind("Mouse", "X");
  32. }
  33. m_joystick = InputDevice::Get("Joystick1");
  34. if (m_joystick)
  35. {
  36. m_controller->GetAxis(AXIS_PITCH).Bind("Joystick1", "Axis2");
  37. m_controller->GetAxis(AXIS_YAW).Bind("Joystick1", "Axis1");
  38. }
  39. m_pitch_angle = 0;
  40. m_yaw_angle = 0;
  41. m_autorot = true;
  42. /* Front vertices/colors */
  43. m_mesh.Push(vec3(-1.0, -1.0, 1.0), vec3(1.0, 0.0, 1.0));
  44. m_mesh.Push(vec3( 1.0, -1.0, 1.0), vec3(0.0, 1.0, 0.0));
  45. m_mesh.Push(vec3( 1.0, 1.0, 1.0), vec3(1.0, 0.5, 0.0));
  46. m_mesh.Push(vec3(-1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0));
  47. /* Back */
  48. m_mesh.Push(vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0));
  49. m_mesh.Push(vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0));
  50. m_mesh.Push(vec3( 1.0, 1.0, -1.0), vec3(0.0, 0.5, 1.0));
  51. m_mesh.Push(vec3(-1.0, 1.0, -1.0), vec3(0.0, 0.0, 1.0));
  52. m_faces_indices << 0 << 1 << 2 << 2 << 3 << 0;
  53. m_faces_indices << 1 << 5 << 6 << 6 << 2 << 1;
  54. m_faces_indices << 7 << 6 << 5 << 5 << 4 << 7;
  55. m_faces_indices << 4 << 0 << 3 << 3 << 7 << 4;
  56. m_faces_indices << 4 << 5 << 1 << 1 << 0 << 4;
  57. m_faces_indices << 3 << 2 << 6 << 6 << 7 << 3;
  58. m_lines_indices << 0 << 1 << 1 << 2 << 2 << 3 << 3 << 0;
  59. m_lines_indices << 4 << 5 << 5 << 6 << 6 << 7 << 7 << 4;
  60. m_lines_indices << 0 << 4 << 1 << 5 << 2 << 6 << 3 << 7;
  61. m_text = new Text("", "data/font/ascii.png");
  62. m_text->SetPos(vec3(5, 5, 1));
  63. Ticker::Ref(m_text);
  64. m_ready = false;
  65. }
  66. ~InputTutorial()
  67. {
  68. Ticker::Unref(m_text);
  69. }
  70. virtual void TickGame(float seconds)
  71. {
  72. WorldEntity::TickGame(seconds);
  73. /* Handle keyboard */
  74. if (m_keyboard)
  75. {
  76. if (m_controller->GetKey(KEY_MANUAL_ROTATION).IsPressed())
  77. m_autorot = !m_autorot;
  78. }
  79. /* Handle joystick */
  80. if (m_joystick)
  81. {
  82. if (lol::abs(m_controller->GetAxis(AXIS_PITCH).GetValue()) > 0.2f)
  83. m_pitch_angle += m_controller->GetAxis(AXIS_PITCH).GetValue() * seconds * 100;
  84. if (lol::abs(m_controller->GetAxis(AXIS_YAW).GetValue()) > 0.2f)
  85. m_yaw_angle += m_controller->GetAxis(AXIS_YAW).GetValue() * seconds * 100;
  86. }
  87. /* Handle mouse */
  88. if (m_mouse)
  89. {
  90. if (m_controller->GetKey(KEY_DRAG_MESH).IsDown())
  91. {
  92. InputDevice::CaptureMouse(true);
  93. m_pitch_angle -= m_controller->GetAxis(AXIS_DRAG_PITCH).GetValue() * seconds * 100;
  94. m_yaw_angle += m_controller->GetAxis(AXIS_DRAG_YAW).GetValue() * seconds * 100;
  95. }
  96. else
  97. {
  98. InputDevice::CaptureMouse(false);
  99. if (m_autorot)
  100. m_yaw_angle += seconds * 20;
  101. }
  102. m_text->SetText(String::Printf(
  103. "cursor: (%0.3f, %0.3f) - pixel (%d, %d)",
  104. m_mouse->GetCursor(0).x, m_mouse->GetCursor(0).y,
  105. m_mouse->GetCursorPixel(0).x, m_mouse->GetCursorPixel(0).y));
  106. }
  107. else
  108. {
  109. m_text->SetText("no mouse detected");
  110. }
  111. mat4 anim = mat4::fromeuler_yxz(m_yaw_angle, m_pitch_angle, 0.f);
  112. mat4 model = mat4::translate(vec3(0, 0, -4.5));
  113. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  114. mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
  115. m_matrix = proj * view * model * anim;
  116. }
  117. virtual void TickDraw(float seconds, Scene &scene)
  118. {
  119. WorldEntity::TickDraw(seconds, scene);
  120. if (!m_ready)
  121. {
  122. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(07_input));
  123. m_mvp = m_shader->GetUniformLocation("in_Matrix");
  124. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  125. m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);
  126. m_vdecl =
  127. new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
  128. VertexUsage::Color));
  129. m_vbo = new VertexBuffer(m_mesh.Bytes());
  130. void *mesh = m_vbo->Lock(0, 0);
  131. memcpy(mesh, &m_mesh[0], m_mesh.Bytes());
  132. m_vbo->Unlock();
  133. m_lines_ibo = new IndexBuffer(m_lines_indices.Bytes());
  134. void *indices = m_lines_ibo->Lock(0, 0);
  135. memcpy(indices, &m_lines_indices[0], m_lines_indices.Bytes());
  136. m_lines_ibo->Unlock();
  137. m_faces_ibo = new IndexBuffer(m_faces_indices.Bytes());
  138. indices = m_faces_ibo->Lock(0, 0);
  139. memcpy(indices, &m_faces_indices[0], m_faces_indices.Bytes());
  140. m_faces_ibo->Unlock();
  141. /* FIXME: this object never cleans up */
  142. m_ready = true;
  143. }
  144. g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  145. m_shader->Bind();
  146. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  147. m_vdecl->Bind();
  148. m_shader->SetUniform(m_mvp, m_matrix);
  149. m_lines_ibo->Bind();
  150. m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, 0, 0,
  151. m_mesh.Count(), 0, m_lines_indices.Count());
  152. m_lines_ibo->Unbind();
  153. m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
  154. m_faces_ibo->Bind();
  155. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0,
  156. m_mesh.Count(), 0, m_faces_indices.Count());
  157. m_faces_ibo->Unbind();
  158. m_vdecl->Unbind();
  159. }
  160. private:
  161. enum
  162. {
  163. KEY_MANUAL_ROTATION,
  164. KEY_DRAG_MESH,
  165. KEY_MAX
  166. };
  167. enum
  168. {
  169. AXIS_DRAG_PITCH,
  170. AXIS_DRAG_YAW,
  171. AXIS_PITCH,
  172. AXIS_YAW,
  173. AXIS_MAX
  174. };
  175. InputDevice *m_keyboard, *m_mouse, *m_joystick;
  176. Controller *m_controller;
  177. bool m_autorot;
  178. float m_pitch_angle;
  179. float m_yaw_angle;
  180. mat4 m_matrix;
  181. array<vec3,vec3> m_mesh;
  182. array<uint16_t> m_lines_indices, m_faces_indices;
  183. Shader *m_shader;
  184. ShaderAttrib m_coord, m_color;
  185. ShaderUniform m_mvp;
  186. VertexDeclaration *m_vdecl;
  187. VertexBuffer *m_vbo;
  188. IndexBuffer *m_lines_ibo, *m_faces_ibo;
  189. Text *m_text;
  190. bool m_ready;
  191. };
  192. int main(int argc, char **argv)
  193. {
  194. System::Init(argc, argv);
  195. Application app("Tutorial 7: Input", ivec2(640, 480), 60.0f);
  196. new DebugFps(5, 5);
  197. new InputTutorial();
  198. app.Run();
  199. return EXIT_SUCCESS;
  200. }