您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

266 行
8.2 KiB

  1. //
  2. // Lol Engine — Input tutorial
  3. //
  4. // Copyright © 2011—2015 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. # ifdef OLD_SCHOOL
  26. m_controller->SetInputCount(KEY_MAX, AXIS_MAX);
  27. m_keyboard = InputDevice::Get("Keyboard");
  28. if (m_keyboard)
  29. m_controller->GetKey(KEY_MANUAL_ROTATION).Bind("Keyboard", "Space");
  30. m_mouse = InputDevice::Get("Mouse");
  31. if (m_mouse)
  32. {
  33. m_controller->GetKey(KEY_DRAG_MESH).Bind("Mouse", "Left");
  34. m_controller->GetAxis(AXIS_DRAG_PITCH).Bind("Mouse", "Y");
  35. m_controller->GetAxis(AXIS_DRAG_YAW).Bind("Mouse", "X");
  36. }
  37. m_joystick = InputDevice::Get("Joystick1");
  38. if (m_joystick)
  39. {
  40. m_controller->GetAxis(AXIS_PITCH).Bind("Joystick1", "Axis2");
  41. m_controller->GetAxis(AXIS_YAW).Bind("Joystick1", "Axis1");
  42. }
  43. # else
  44. m_profile
  45. << InputProfile::Keyboard(KEY_MANUAL_ROTATION, "Space")
  46. << InputProfile::MouseKey(KEY_DRAG_MESH, "Left")
  47. << InputProfile::JoystickAxis(1, AXIS_PITCH, "Axis2")
  48. << InputProfile::JoystickAxis(1, AXIS_YAW, "Axis1")
  49. << InputProfile::MouseAxis(AXIS_DRAG_PITCH, "Y")
  50. << InputProfile::MouseAxis(AXIS_DRAG_YAW, "X");
  51. m_controller->Init(m_profile);
  52. m_keyboard = InputDevice::GetKeyboard();
  53. m_mouse = InputDevice::GetMouse();
  54. m_joystick = InputDevice::GetJoystick(1);
  55. # endif //OLD_SCHOOL
  56. m_pitch_angle = 0;
  57. m_yaw_angle = 0;
  58. m_autorot = true;
  59. /* Front vertices/colors */
  60. m_mesh.push(vec3(-1.0, -1.0, 1.0), vec3(1.0, 0.0, 1.0));
  61. m_mesh.push(vec3( 1.0, -1.0, 1.0), vec3(0.0, 1.0, 0.0));
  62. m_mesh.push(vec3( 1.0, 1.0, 1.0), vec3(1.0, 0.5, 0.0));
  63. m_mesh.push(vec3(-1.0, 1.0, 1.0), vec3(1.0, 1.0, 0.0));
  64. /* Back */
  65. m_mesh.push(vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0));
  66. m_mesh.push(vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0));
  67. m_mesh.push(vec3( 1.0, 1.0, -1.0), vec3(0.0, 0.5, 1.0));
  68. m_mesh.push(vec3(-1.0, 1.0, -1.0), vec3(0.0, 0.0, 1.0));
  69. m_faces_indices << 0 << 1 << 2 << 2 << 3 << 0;
  70. m_faces_indices << 1 << 5 << 6 << 6 << 2 << 1;
  71. m_faces_indices << 7 << 6 << 5 << 5 << 4 << 7;
  72. m_faces_indices << 4 << 0 << 3 << 3 << 7 << 4;
  73. m_faces_indices << 4 << 5 << 1 << 1 << 0 << 4;
  74. m_faces_indices << 3 << 2 << 6 << 6 << 7 << 3;
  75. m_lines_indices << 0 << 1 << 1 << 2 << 2 << 3 << 3 << 0;
  76. m_lines_indices << 4 << 5 << 5 << 6 << 6 << 7 << 7 << 4;
  77. m_lines_indices << 0 << 4 << 1 << 5 << 2 << 6 << 3 << 7;
  78. m_text = new Text("", "data/font/ascii.png");
  79. m_text->SetPos(vec3(5, 5, 1));
  80. Ticker::Ref(m_text);
  81. m_ready = false;
  82. }
  83. ~InputTutorial()
  84. {
  85. Ticker::Unref(m_text);
  86. }
  87. virtual void TickGame(float seconds)
  88. {
  89. WorldEntity::TickGame(seconds);
  90. /* Handle keyboard */
  91. if (m_keyboard)
  92. {
  93. if (m_controller->WasKeyPressedThisFrame(KEY_MANUAL_ROTATION))
  94. m_autorot = !m_autorot;
  95. }
  96. /* Handle joystick */
  97. if (m_joystick)
  98. {
  99. if (lol::abs(m_controller->GetAxisValue(AXIS_PITCH)) > 0.2f)
  100. m_pitch_angle += m_controller->GetAxisValue(AXIS_PITCH) * seconds;
  101. if (lol::abs(m_controller->GetAxisValue(AXIS_YAW)) > 0.2f)
  102. m_yaw_angle += m_controller->GetAxisValue(AXIS_YAW) * seconds;
  103. }
  104. /* Handle mouse */
  105. if (m_mouse)
  106. {
  107. if (m_controller->IsKeyPressed(KEY_DRAG_MESH))
  108. {
  109. InputDevice::CaptureMouse(true);
  110. m_pitch_angle -= m_controller->GetAxisValue(AXIS_DRAG_PITCH) * seconds * 0.1f;
  111. m_yaw_angle += m_controller->GetAxisValue(AXIS_DRAG_YAW) * seconds * 0.1f;
  112. }
  113. else
  114. {
  115. InputDevice::CaptureMouse(false);
  116. if (m_autorot)
  117. m_yaw_angle += seconds * 0.2f;
  118. }
  119. m_text->SetText(String::format(
  120. "cursor: (%0.3f, %0.3f) - pixel (%d, %d)",
  121. m_mouse->GetCursor(0).x, m_mouse->GetCursor(0).y,
  122. m_mouse->GetCursorPixel(0).x, m_mouse->GetCursorPixel(0).y));
  123. }
  124. else
  125. {
  126. m_text->SetText("no mouse detected");
  127. }
  128. mat4 anim = mat4::fromeuler_yxz(m_yaw_angle, m_pitch_angle, 0.f);
  129. mat4 model = mat4::translate(vec3(0, 0, -4.5));
  130. mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
  131. mat4 proj = mat4::perspective(radians(45.0f), 640.0f, 480.0f, 0.1f, 10.0f);
  132. m_matrix = proj * view * model * anim;
  133. }
  134. virtual void TickDraw(float seconds, Scene &scene)
  135. {
  136. WorldEntity::TickDraw(seconds, scene);
  137. if (!m_ready)
  138. {
  139. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(07_input));
  140. m_mvp = m_shader->GetUniformLocation("u_matrix");
  141. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  142. m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);
  143. m_vdecl =
  144. new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
  145. VertexUsage::Color));
  146. m_vbo = new VertexBuffer(m_mesh.bytes());
  147. void *mesh = m_vbo->Lock(0, 0);
  148. memcpy(mesh, &m_mesh[0], m_mesh.bytes());
  149. m_vbo->Unlock();
  150. m_lines_ibo = new IndexBuffer(m_lines_indices.bytes());
  151. void *indices = m_lines_ibo->Lock(0, 0);
  152. memcpy(indices, &m_lines_indices[0], m_lines_indices.bytes());
  153. m_lines_ibo->Unlock();
  154. m_faces_ibo = new IndexBuffer(m_faces_indices.bytes());
  155. indices = m_faces_ibo->Lock(0, 0);
  156. memcpy(indices, &m_faces_indices[0], m_faces_indices.bytes());
  157. m_faces_ibo->Unlock();
  158. /* FIXME: this object never cleans up */
  159. m_ready = true;
  160. }
  161. Renderer::Get()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  162. m_shader->Bind();
  163. m_vdecl->SetStream(m_vbo, m_coord, m_color);
  164. m_vdecl->Bind();
  165. m_shader->SetUniform(m_mvp, m_matrix);
  166. m_lines_ibo->Bind();
  167. m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, m_lines_indices.count());
  168. m_lines_ibo->Unbind();
  169. m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
  170. m_faces_ibo->Bind();
  171. m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, m_faces_indices.count());
  172. m_faces_ibo->Unbind();
  173. m_vdecl->Unbind();
  174. }
  175. private:
  176. enum
  177. {
  178. KEY_MANUAL_ROTATION,
  179. KEY_DRAG_MESH,
  180. KEY_MAX
  181. };
  182. enum
  183. {
  184. AXIS_DRAG_PITCH,
  185. AXIS_DRAG_YAW,
  186. AXIS_PITCH,
  187. AXIS_YAW,
  188. AXIS_MAX
  189. };
  190. InputDevice *m_keyboard, *m_mouse, *m_joystick;
  191. Controller *m_controller;
  192. InputProfile m_profile;
  193. bool m_autorot;
  194. float m_pitch_angle;
  195. float m_yaw_angle;
  196. mat4 m_matrix;
  197. array<vec3,vec3> m_mesh;
  198. array<uint16_t> m_lines_indices, m_faces_indices;
  199. Shader *m_shader;
  200. ShaderAttrib m_coord, m_color;
  201. ShaderUniform m_mvp;
  202. VertexDeclaration *m_vdecl;
  203. VertexBuffer *m_vbo;
  204. IndexBuffer *m_lines_ibo, *m_faces_ibo;
  205. Text *m_text;
  206. bool m_ready;
  207. };
  208. int main(int argc, char **argv)
  209. {
  210. sys::init(argc, argv);
  211. Application app("Tutorial 7: Input", ivec2(640, 480), 60.0f);
  212. new DebugFps(5, 5);
  213. new InputTutorial();
  214. app.Run();
  215. return EXIT_SUCCESS;
  216. }