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.
 
 
 

261 lines
8.2 KiB

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