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.
 
 
 

268 lines
8.3 KiB

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