Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

249 řádky
7.5 KiB

  1. //
  2. // Lol Engine - Input tutorial
  3. //
  4. // Copyright: (c) 2011-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include "core.h"
  14. #include "loldebug.h"
  15. using namespace std;
  16. using namespace lol;
  17. LOLFX_RESOURCE_DECLARE(07_input);
  18. class InputTutorial : public WorldEntity
  19. {
  20. public:
  21. InputTutorial()
  22. {
  23. m_controller = new Controller("Default", KEY_MAX, AXIS_MAX);
  24. m_keyboard = InputDevice::Get("Keyboard");
  25. if (m_keyboard)
  26. m_controller->GetKey(KEY_MANUAL_ROTATION).Bind("Keyboard", "Space");
  27. m_mouse = InputDevice::Get("Mouse");
  28. if (m_mouse)
  29. {
  30. m_controller->GetKey(KEY_DRAG_MESH).Bind("Mouse", "Left");
  31. m_controller->GetAxis(AXIS_DRAG_PITCH).Bind("Mouse", "Y");
  32. m_controller->GetAxis(AXIS_DRAG_YAW).Bind("Mouse", "X");
  33. }
  34. m_joystick = InputDevice::Get("Joystick1");
  35. if (m_joystick)
  36. {
  37. m_controller->GetAxis(AXIS_PITCH).Bind("Joystick1", "Axis2");
  38. m_controller->GetAxis(AXIS_YAW).Bind("Joystick1", "Axis1");
  39. }
  40. m_pitch_angle = 0;
  41. m_yaw_angle = 0;
  42. m_autorot = true;
  43. /* Front vertices/colors */
  44. m_mesh.Push(vec3(-1.0, -1.0, 1.0), vec3(1.0, 0.0, 1.0));
  45. m_mesh.Push(vec3( 1.0, -1.0, 1.0), vec3(0.0, 1.0, 0.0));
  46. m_mesh.Push(vec3( 1.0, 1.0, 1.0), vec3(1.0, 0.5, 0.0));
  47. m_mesh.Push(vec3(-1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0));
  48. /* Back */
  49. m_mesh.Push(vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0));
  50. m_mesh.Push(vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0));
  51. m_mesh.Push(vec3( 1.0, 1.0, -1.0), vec3(0.0, 0.5, 1.0));
  52. m_mesh.Push(vec3(-1.0, 1.0, -1.0), vec3(0.0, 0.0, 1.0));
  53. m_faces_indices << 0 << 1 << 2 << 2 << 3 << 0;
  54. m_faces_indices << 1 << 5 << 6 << 6 << 2 << 1;
  55. m_faces_indices << 7 << 6 << 5 << 5 << 4 << 7;
  56. m_faces_indices << 4 << 0 << 3 << 3 << 7 << 4;
  57. m_faces_indices << 4 << 5 << 1 << 1 << 0 << 4;
  58. m_faces_indices << 3 << 2 << 6 << 6 << 7 << 3;
  59. m_lines_indices << 0 << 1 << 1 << 2 << 2 << 3 << 3 << 0;
  60. m_lines_indices << 4 << 5 << 5 << 6 << 6 << 7 << 7 << 4;
  61. m_lines_indices << 0 << 4 << 1 << 5 << 2 << 6 << 3 << 7;
  62. m_text = new Text("", "data/font/ascii.png");
  63. m_text->SetPos(ivec3(5, 5, 1));
  64. Ticker::Ref(m_text);
  65. m_ready = false;
  66. }
  67. ~InputTutorial()
  68. {
  69. Ticker::Unref(m_text);
  70. }
  71. virtual void TickGame(float seconds)
  72. {
  73. WorldEntity::TickGame(seconds);
  74. /* Handle keyboard */
  75. if (m_keyboard)
  76. {
  77. if (m_controller->GetKey(KEY_MANUAL_ROTATION).IsPressed())
  78. m_autorot = !m_autorot;
  79. }
  80. /* Handle joystick */
  81. if (m_joystick)
  82. {
  83. if (lol::abs(m_controller->GetAxis(AXIS_PITCH).GetValue()) > 0.2f)
  84. m_pitch_angle += m_controller->GetAxis(AXIS_PITCH).GetValue() * seconds * 100;
  85. if (lol::abs(m_controller->GetAxis(AXIS_YAW).GetValue()) > 0.2f)
  86. m_yaw_angle += m_controller->GetAxis(AXIS_YAW).GetValue() * seconds * 100;
  87. }
  88. /* Handle mouse */
  89. if (m_mouse)
  90. {
  91. if (m_controller->GetKey(KEY_DRAG_MESH).IsDown())
  92. {
  93. InputDevice::CaptureMouse(true);
  94. m_pitch_angle -= m_controller->GetAxis(AXIS_DRAG_PITCH).GetValue() * seconds * 100;
  95. m_yaw_angle += m_controller->GetAxis(AXIS_DRAG_YAW).GetValue() * seconds * 100;
  96. }
  97. else
  98. {
  99. InputDevice::CaptureMouse(false);
  100. if (m_autorot)
  101. m_yaw_angle += seconds * 20;
  102. }
  103. m_text->SetText(String::Printf(
  104. "cursor: (%0.3f, %0.3f) - pixel (%d, %d)",
  105. m_mouse->GetCursor(0).x, m_mouse->GetCursor(0).y,
  106. m_mouse->GetCursorPixel(0).x, m_mouse->GetCursorPixel(0).y));
  107. }
  108. else
  109. {
  110. m_text->SetText("no mouse detected");
  111. }
  112. mat4 anim = mat4::fromeuler_yxz(m_yaw_angle, m_pitch_angle, 0.f);
  113. mat4 model = mat4::translate(vec3(0, 0, -4.5));
  114. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  115. mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
  116. m_matrix = proj * view * model * anim;
  117. }
  118. virtual void TickDraw(float seconds)
  119. {
  120. WorldEntity::TickDraw(seconds);
  121. if (!m_ready)
  122. {
  123. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(07_input));
  124. m_mvp = m_shader->GetUniformLocation("in_Matrix");
  125. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  126. m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);
  127. m_vdecl =
  128. new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
  129. VertexUsage::Color));
  130. m_vbo = new VertexBuffer(m_mesh.Bytes());
  131. void *mesh = m_vbo->Lock(0, 0);
  132. memcpy(mesh, &m_mesh[0], m_mesh.Bytes());
  133. m_vbo->Unlock();
  134. m_lines_ibo = new IndexBuffer(m_lines_indices.Bytes());
  135. void *indices = m_lines_ibo->Lock(0, 0);
  136. memcpy(indices, &m_lines_indices[0], m_lines_indices.Bytes());
  137. m_lines_ibo->Unlock();
  138. m_faces_ibo = new IndexBuffer(m_faces_indices.Bytes());
  139. indices = m_faces_ibo->Lock(0, 0);
  140. memcpy(indices, &m_faces_indices[0], m_faces_indices.Bytes());
  141. m_faces_ibo->Unlock();
  142. /* FIXME: this object never cleans up */
  143. m_ready = true;
  144. }
  145. g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  146. m_shader->Bind();
  147. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  148. m_vdecl->Bind();
  149. m_shader->SetUniform(m_mvp, m_matrix);
  150. m_lines_ibo->Bind();
  151. m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, 0, 0,
  152. m_mesh.Count(), 0, m_lines_indices.Count());
  153. m_lines_ibo->Unbind();
  154. m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
  155. m_faces_ibo->Bind();
  156. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0,
  157. m_mesh.Count(), 0, m_faces_indices.Count());
  158. m_faces_ibo->Unbind();
  159. m_vdecl->Unbind();
  160. }
  161. private:
  162. enum
  163. {
  164. KEY_MANUAL_ROTATION,
  165. KEY_DRAG_MESH,
  166. KEY_MAX
  167. };
  168. enum
  169. {
  170. AXIS_DRAG_PITCH,
  171. AXIS_DRAG_YAW,
  172. AXIS_PITCH,
  173. AXIS_YAW,
  174. AXIS_MAX
  175. };
  176. InputDevice *m_keyboard, *m_mouse, *m_joystick;
  177. Controller *m_controller;
  178. bool m_autorot;
  179. float m_pitch_angle;
  180. float m_yaw_angle;
  181. mat4 m_matrix;
  182. Array<vec3,vec3> m_mesh;
  183. Array<uint16_t> m_lines_indices, m_faces_indices;
  184. Shader *m_shader;
  185. ShaderAttrib m_coord, m_color;
  186. ShaderUniform m_mvp;
  187. VertexDeclaration *m_vdecl;
  188. VertexBuffer *m_vbo;
  189. IndexBuffer *m_lines_ibo, *m_faces_ibo;
  190. Text *m_text;
  191. bool m_ready;
  192. };
  193. int main(int argc, char **argv)
  194. {
  195. System::Init(argc, argv);
  196. Application app("Tutorial 7: Input", ivec2(640, 480), 60.0f);
  197. new DebugFps(5, 5);
  198. new InputTutorial();
  199. app.Run();
  200. return EXIT_SUCCESS;
  201. }