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.
 
 
 

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