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.
 
 
 

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