Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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