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.

87 lines
2.1 KiB

  1. //
  2. // Lol Engine — Triangle tutorial
  3. //
  4. // Copyright © 2012—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 <memory>
  16. #include <lol/engine.h>
  17. #include "loldebug.h"
  18. using namespace lol;
  19. LOLFX_RESOURCE_DECLARE(01_triangle);
  20. class Triangle : public WorldEntity
  21. {
  22. public:
  23. Triangle()
  24. : m_vertices({ vec2( 0.0f, 0.8f),
  25. vec2(-0.8f, -0.8f),
  26. vec2( 0.8f, -0.8f) }),
  27. m_ready(false)
  28. {
  29. }
  30. virtual void tick_draw(float seconds, Scene &scene)
  31. {
  32. WorldEntity::tick_draw(seconds, scene);
  33. if (!m_ready)
  34. {
  35. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(01_triangle));
  36. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  37. m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));
  38. m_vbo = std::make_shared<VertexBuffer>(m_vertices.bytes());
  39. void *vertices = m_vbo->Lock(0, 0);
  40. memcpy(vertices, &m_vertices[0], m_vertices.bytes());
  41. m_vbo->Unlock();
  42. m_ready = true;
  43. /* FIXME: this object never cleans up */
  44. }
  45. m_shader->Bind();
  46. m_vdecl->SetStream(m_vbo, m_coord);
  47. m_vdecl->Bind();
  48. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 3);
  49. m_vdecl->Unbind();
  50. }
  51. private:
  52. array<vec2> m_vertices;
  53. std::shared_ptr<Shader> m_shader;
  54. ShaderAttrib m_coord;
  55. std::shared_ptr<VertexDeclaration> m_vdecl;
  56. std::shared_ptr<VertexBuffer> m_vbo;
  57. bool m_ready;
  58. };
  59. int main(int argc, char **argv)
  60. {
  61. sys::init(argc, argv);
  62. Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
  63. new DebugFps(5, 5);
  64. new Triangle();
  65. app.Run();
  66. return EXIT_SUCCESS;
  67. }