Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

01_triangle.cpp 2.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // Lol Engine - Triangle tutorial
  3. //
  4. // Copyright: (c) 2012 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://sam.zoy.org/projects/COPYING.WTFPL 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. #if USE_SDL && defined __APPLE__
  18. # include <SDL_main.h>
  19. #endif
  20. #if defined _WIN32
  21. # undef main /* FIXME: still needed? */
  22. # include <direct.h>
  23. #endif
  24. extern char const *lolfx_01_triangle;
  25. class Triangle : public WorldEntity
  26. {
  27. public:
  28. Triangle()
  29. {
  30. m_vertices << vec2( 0.0, 0.8);
  31. m_vertices << vec2(-0.8, -0.8);
  32. m_vertices << vec2( 0.8, -0.8);
  33. m_ready = false;
  34. }
  35. virtual void TickDraw(float seconds)
  36. {
  37. WorldEntity::TickDraw(seconds);
  38. if (!m_ready)
  39. {
  40. m_shader = Shader::Create(lolfx_01_triangle);
  41. m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
  42. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  43. m_vbo = new VertexBuffer(m_vertices.Bytes());
  44. void *vertices = m_vbo->Lock(0, 0);
  45. memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
  46. m_vbo->Unlock();
  47. m_ready = true;
  48. /* FIXME: this object never cleans up */
  49. }
  50. m_shader->Bind();
  51. m_vdecl->SetStream(m_vbo, m_coord);
  52. m_vdecl->Bind();
  53. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 1);
  54. m_vdecl->Unbind();
  55. }
  56. private:
  57. Array<vec2> m_vertices;
  58. Shader *m_shader;
  59. ShaderAttrib m_coord;
  60. VertexDeclaration *m_vdecl;
  61. VertexBuffer *m_vbo;
  62. bool m_ready;
  63. };
  64. int main(int argc, char **argv)
  65. {
  66. Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
  67. #if defined _MSC_VER && !defined _XBOX
  68. _chdir("..");
  69. #elif defined _WIN32 && !defined _XBOX
  70. _chdir("../..");
  71. #endif
  72. new DebugFps(5, 5);
  73. new Triangle();
  74. app.Run();
  75. return EXIT_SUCCESS;
  76. }