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.
 
 
 

121 lines
3.0 KiB

  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 __native_client__
  21. # define main old_main
  22. #endif
  23. #if defined _WIN32
  24. # undef main /* FIXME: still needed? */
  25. # include <direct.h>
  26. #endif
  27. class Triangle : public WorldEntity
  28. {
  29. public:
  30. Triangle()
  31. {
  32. m_vertices << vec2( 0.0, 0.8);
  33. m_vertices << vec2(-0.8, -0.8);
  34. m_vertices << vec2( 0.8, -0.8);
  35. m_ready = false;
  36. }
  37. virtual void TickDraw(float seconds)
  38. {
  39. WorldEntity::TickDraw(seconds);
  40. if (!m_ready)
  41. {
  42. m_shader = Shader::Create(
  43. #if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
  44. "#version 120\n"
  45. "attribute vec2 in_Position;"
  46. "void main(void) {"
  47. " gl_Position = vec4(in_Position, 0.0, 1.0);"
  48. "}",
  49. "#version 120\n"
  50. "void main(void) {"
  51. " gl_FragColor = vec4(0.7, 0.2, 0.5, 1.0);"
  52. "}"
  53. #else
  54. "void main(float2 in_Position : POSITION,"
  55. " out float4 out_Position : POSITION) {"
  56. " out_Position = float4(in_Position, 0.0, 1.0);"
  57. "}",
  58. "void main(out float4 out_FragColor : COLOR) {"
  59. " out_FragColor = float4(0.7, 0.2, 0.5, 1.0);"
  60. "}"
  61. #endif
  62. );
  63. m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
  64. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  65. m_vbo = new VertexBuffer(m_vertices.Bytes());
  66. void *vertices = m_vbo->Lock(0, 0);
  67. memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
  68. m_vbo->Unlock();
  69. m_ready = true;
  70. /* FIXME: this object never cleans up */
  71. }
  72. m_shader->Bind();
  73. m_vdecl->SetStream(m_vbo, m_coord);
  74. m_vdecl->Bind();
  75. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 1);
  76. m_vdecl->Unbind();
  77. }
  78. private:
  79. Array<vec2> m_vertices;
  80. Shader *m_shader;
  81. ShaderAttrib m_coord;
  82. VertexDeclaration *m_vdecl;
  83. VertexBuffer *m_vbo;
  84. bool m_ready;
  85. };
  86. int main(int argc, char **argv)
  87. {
  88. Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
  89. #if defined _MSC_VER && !defined _XBOX
  90. _chdir("..");
  91. #elif defined _WIN32 && !defined _XBOX
  92. _chdir("../..");
  93. #endif
  94. new DebugFps(5, 5);
  95. new Triangle();
  96. app.Run();
  97. return EXIT_SUCCESS;
  98. }