Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

218 rindas
6.5 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. m_text = new Text("", "data/font/ascii.png");
  28. m_text->SetPos(vec3(5, 30, 1));
  29. Ticker::Ref(m_text);
  30. }
  31. ~InputTutorial()
  32. {
  33. Ticker::Unref(m_text);
  34. }
  35. virtual void tick_game(float seconds) override
  36. {
  37. WorldEntity::tick_game(seconds);
  38. auto mouse = input::mouse();
  39. auto keyboard = input::keyboard();
  40. /* Handle keyboard */
  41. if (keyboard->key_pressed(input::key::SC_Space))
  42. m_autorot = !m_autorot;
  43. /* Handle joystick */
  44. auto joystick = input::joystick(0);
  45. if ((bool)joystick)
  46. {
  47. if (lol::abs(joystick->axis(input::axis::LeftY)) > 0.2f)
  48. m_pitch_angle += joystick->axis(input::axis::LeftY) * seconds;
  49. if (lol::abs(joystick->axis(input::axis::LeftX)) > 0.2f)
  50. m_yaw_angle += joystick->axis(input::axis::LeftX) * seconds;
  51. }
  52. /* Handle mouse */
  53. if (true)
  54. {
  55. if (mouse->button(input::button::BTN_Left))
  56. {
  57. mouse->capture(true);
  58. m_pitch_angle += mouse->axis(input::axis::MoveY) * seconds * 0.1f;
  59. m_yaw_angle += mouse->axis(input::axis::MoveX) * seconds * 0.1f;
  60. }
  61. else
  62. {
  63. mouse->capture(false);
  64. if (m_autorot)
  65. m_yaw_angle += seconds * 0.2f;
  66. }
  67. m_text->SetText(lol::format(
  68. "cursor: (%0.3f,%0.3f) - pixel (%d,%d)\n"
  69. " move: (%0.3f,%0.3f) - pixel (%d,%d)",
  70. mouse->axis(input::axis::X), mouse->axis(input::axis::Y),
  71. (int)mouse->axis(input::axis::ScreenX),
  72. (int)mouse->axis(input::axis::ScreenY),
  73. mouse->axis(input::axis::MoveX), mouse->axis(input::axis::MoveY),
  74. (int)mouse->axis(input::axis::ScreenMoveX),
  75. (int)mouse->axis(input::axis::ScreenMoveY)));
  76. }
  77. else
  78. {
  79. m_text->SetText("no mouse detected");
  80. }
  81. mat4 anim = mat4::fromeuler_yxz(m_yaw_angle, m_pitch_angle, 0.f);
  82. mat4 model = mat4::translate(vec3(0, 0, -4.5));
  83. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  84. mat4 proj = mat4::perspective(radians(45.0f), 640.0f, 480.0f, 0.1f, 10.0f);
  85. m_matrix = proj * view * model * anim;
  86. }
  87. virtual bool init_draw() override
  88. {
  89. array<vec3, vec3> mesh
  90. {
  91. // Front vertices/colors
  92. { vec3(-1.0, -1.0, 1.0), vec3(1.0, 0.0, 1.0) },
  93. { vec3( 1.0, -1.0, 1.0), vec3(0.0, 1.0, 0.0) },
  94. { vec3( 1.0, 1.0, 1.0), vec3(1.0, 0.5, 0.0) },
  95. { vec3(-1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0) },
  96. // Back
  97. { vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0) },
  98. { vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0) },
  99. { vec3( 1.0, 1.0, -1.0), vec3(0.0, 0.5, 1.0) },
  100. { vec3(-1.0, 1.0, -1.0), vec3(0.0, 0.0, 1.0) },
  101. };
  102. array<uint16_t> faces_indices
  103. {
  104. 0, 1, 2, 2, 3, 0, 1, 5, 6, 6, 2, 1,
  105. 7, 6, 5, 5, 4, 7, 4, 0, 3, 3, 7, 4,
  106. 4, 5, 1, 1, 0, 4, 3, 2, 6, 6, 7, 3,
  107. };
  108. array<uint16_t> lines_indices
  109. {
  110. 0, 1, 1, 2, 2, 3, 3, 0,
  111. 4, 5, 5, 6, 6, 7, 7, 4,
  112. 0, 4, 1, 5, 2, 6, 3, 7,
  113. };
  114. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(07_input));
  115. m_mvp = m_shader->GetUniformLocation("u_matrix");
  116. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  117. m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);
  118. m_vdecl = std::make_shared<VertexDeclaration>(
  119. VertexStream<vec3,vec3>(VertexUsage::Position,
  120. VertexUsage::Color));
  121. m_vbo = std::make_shared<VertexBuffer>(mesh.bytes());
  122. m_vbo->set_data(mesh.data(), mesh.bytes());
  123. m_lines_ibo = std::make_shared<IndexBuffer>(lines_indices.bytes());
  124. m_lines_ibo->set_data(lines_indices.data(), lines_indices.bytes());
  125. m_faces_ibo = std::make_shared<IndexBuffer>(faces_indices.bytes());
  126. m_faces_ibo->set_data(faces_indices.data(), faces_indices.bytes());
  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()->clear_color(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  133. m_shader->Bind();
  134. m_vdecl->Bind();
  135. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  136. m_shader->SetUniform(m_mvp, m_matrix);
  137. m_lines_ibo->Bind();
  138. m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, m_lines_ibo->size() / sizeof(uint16_t));
  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_ibo->size() / sizeof(uint16_t));
  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. std::shared_ptr<Shader> m_shader;
  161. ShaderAttrib m_coord, m_color;
  162. ShaderUniform m_mvp;
  163. std::shared_ptr<VertexDeclaration> m_vdecl;
  164. std::shared_ptr<VertexBuffer> m_vbo;
  165. std::shared_ptr<IndexBuffer> m_lines_ibo, m_faces_ibo;
  166. Text *m_text;
  167. };
  168. int main(int argc, char **argv)
  169. {
  170. sys::init(argc, argv);
  171. Application app("Tutorial 7: Input", ivec2(640, 480), 60.0f);
  172. new DebugFps(5, 5);
  173. new InputTutorial();
  174. app.Run();
  175. return EXIT_SUCCESS;
  176. }