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.
 
 
 

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