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.
 
 
 

220 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. }
  50. ~InputTutorial()
  51. {
  52. Ticker::Unref(m_text);
  53. }
  54. virtual void tick_game(float seconds) override
  55. {
  56. WorldEntity::tick_game(seconds);
  57. auto mouse = input::mouse();
  58. auto keyboard = input::keyboard();
  59. /* Handle keyboard */
  60. if (keyboard->key_pressed(input::key::SC_Space))
  61. m_autorot = !m_autorot;
  62. /* Handle joystick */
  63. auto joystick = input::joystick(0);
  64. if ((bool)joystick)
  65. {
  66. if (lol::abs(joystick->axis(input::axis::LeftY)) > 0.2f)
  67. m_pitch_angle += joystick->axis(input::axis::LeftY) * seconds;
  68. if (lol::abs(joystick->axis(input::axis::LeftX)) > 0.2f)
  69. m_yaw_angle += joystick->axis(input::axis::LeftX) * seconds;
  70. }
  71. /* Handle mouse */
  72. if (true)
  73. {
  74. if (mouse->button(input::button::BTN_Left))
  75. {
  76. mouse->capture(true);
  77. m_pitch_angle += mouse->axis(input::axis::MoveY) * seconds * 0.1f;
  78. m_yaw_angle += mouse->axis(input::axis::MoveX) * seconds * 0.1f;
  79. }
  80. else
  81. {
  82. mouse->capture(false);
  83. if (m_autorot)
  84. m_yaw_angle += seconds * 0.2f;
  85. }
  86. m_text->SetText(lol::format(
  87. "cursor: (%0.3f,%0.3f) - pixel (%d,%d)\n"
  88. " move: (%0.3f,%0.3f) - pixel (%d,%d)",
  89. mouse->axis(input::axis::X), mouse->axis(input::axis::Y),
  90. (int)mouse->axis(input::axis::ScreenX),
  91. (int)mouse->axis(input::axis::ScreenY),
  92. mouse->axis(input::axis::MoveX), mouse->axis(input::axis::MoveY),
  93. (int)mouse->axis(input::axis::ScreenMoveX),
  94. (int)mouse->axis(input::axis::ScreenMoveY)));
  95. }
  96. else
  97. {
  98. m_text->SetText("no mouse detected");
  99. }
  100. mat4 anim = mat4::fromeuler_yxz(m_yaw_angle, m_pitch_angle, 0.f);
  101. mat4 model = mat4::translate(vec3(0, 0, -4.5));
  102. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  103. mat4 proj = mat4::perspective(radians(45.0f), 640.0f, 480.0f, 0.1f, 10.0f);
  104. m_matrix = proj * view * model * anim;
  105. }
  106. virtual bool init_draw() override
  107. {
  108. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(07_input));
  109. m_mvp = m_shader->GetUniformLocation("u_matrix");
  110. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  111. m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);
  112. m_vdecl = std::make_shared<VertexDeclaration>(
  113. VertexStream<vec3,vec3>(VertexUsage::Position,
  114. VertexUsage::Color));
  115. m_vbo = std::make_shared<VertexBuffer>(m_mesh.bytes());
  116. void *mesh = m_vbo->Lock(0, 0);
  117. memcpy(mesh, &m_mesh[0], m_mesh.bytes());
  118. m_vbo->Unlock();
  119. m_lines_ibo = std::make_shared<IndexBuffer>(m_lines_indices.bytes());
  120. void *indices = m_lines_ibo->Lock(0, 0);
  121. memcpy(indices, &m_lines_indices[0], m_lines_indices.bytes());
  122. m_lines_ibo->Unlock();
  123. m_faces_ibo = std::make_shared<IndexBuffer>(m_faces_indices.bytes());
  124. indices = m_faces_ibo->Lock(0, 0);
  125. memcpy(indices, &m_faces_indices[0], m_faces_indices.bytes());
  126. m_faces_ibo->Unlock();
  127. return WorldEntity::init_draw();
  128. }
  129. virtual void tick_draw(float seconds, Scene &scene) override
  130. {
  131. WorldEntity::tick_draw(seconds, scene);
  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. virtual bool release_draw() override
  147. {
  148. m_shader.reset();
  149. m_vdecl.reset();
  150. m_vbo.reset();
  151. m_lines_ibo.reset();
  152. m_faces_ibo.reset();
  153. return true;
  154. }
  155. private:
  156. bool m_autorot;
  157. float m_pitch_angle;
  158. float m_yaw_angle;
  159. mat4 m_matrix;
  160. array<vec3,vec3> m_mesh;
  161. array<uint16_t> m_lines_indices, m_faces_indices;
  162. std::shared_ptr<Shader> m_shader;
  163. ShaderAttrib m_coord, m_color;
  164. ShaderUniform m_mvp;
  165. std::shared_ptr<VertexDeclaration> m_vdecl;
  166. std::shared_ptr<VertexBuffer> m_vbo;
  167. std::shared_ptr<IndexBuffer> m_lines_ibo, m_faces_ibo;
  168. Text *m_text;
  169. };
  170. int main(int argc, char **argv)
  171. {
  172. sys::init(argc, argv);
  173. Application app("Tutorial 7: Input", ivec2(640, 480), 60.0f);
  174. new DebugFps(5, 5);
  175. new InputTutorial();
  176. app.Run();
  177. return EXIT_SUCCESS;
  178. }