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.
 
 
 

89 lines
2.0 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. virtual bool init_draw() override
  24. {
  25. array<vec2> vertices
  26. {
  27. vec2( 0.0f, 0.8f),
  28. vec2(-0.8f, -0.8f),
  29. vec2( 0.8f, -0.8f),
  30. };
  31. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(01_triangle));
  32. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  33. m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));
  34. m_vbo = std::make_shared<VertexBuffer>(vertices.bytes());
  35. m_vbo->set_data(vertices.data(), vertices.bytes());
  36. return true;
  37. }
  38. virtual void tick_draw(float seconds, Scene &scene) override
  39. {
  40. WorldEntity::tick_draw(seconds, scene);
  41. m_shader->Bind();
  42. m_vdecl->Bind();
  43. m_vdecl->SetStream(m_vbo, m_coord);
  44. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 3);
  45. m_vdecl->Unbind();
  46. }
  47. virtual bool release_draw() override
  48. {
  49. m_shader.reset();
  50. m_vdecl.reset();
  51. m_vbo.reset();
  52. return true;
  53. }
  54. private:
  55. std::shared_ptr<Shader> m_shader;
  56. ShaderAttrib m_coord;
  57. std::shared_ptr<VertexDeclaration> m_vdecl;
  58. std::shared_ptr<VertexBuffer> m_vbo;
  59. };
  60. int main(int argc, char **argv)
  61. {
  62. sys::init(argc, argv);
  63. Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
  64. new DebugFps(5, 5);
  65. new Triangle();
  66. app.Run();
  67. return EXIT_SUCCESS;
  68. }