168 line
3.9 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 "lolgl.h"
  15. #include "loldebug.h"
  16. using namespace std;
  17. using namespace lol;
  18. #if defined _WIN32 && defined USE_D3D9
  19. # define FAR
  20. # define NEAR
  21. # include <d3d9.h>
  22. #endif
  23. #if USE_SDL && defined __APPLE__
  24. # include <SDL_main.h>
  25. #endif
  26. #if defined __native_client__
  27. # define main old_main
  28. #endif
  29. #if defined _WIN32
  30. # undef main /* FIXME: still needed? */
  31. # include <direct.h>
  32. #endif
  33. #if defined USE_D3D9
  34. extern IDirect3DDevice9 *g_d3ddevice;
  35. #elif defined _XBOX
  36. extern D3DDevice *g_d3ddevice;
  37. #endif
  38. class Triangle : public WorldEntity
  39. {
  40. public:
  41. Triangle()
  42. {
  43. m_vertices[0] = vec2( 0.0, 0.8);
  44. m_vertices[1] = vec2(-0.8, -0.8);
  45. m_vertices[2] = vec2( 0.8, -0.8);
  46. m_ready = false;
  47. }
  48. virtual void TickDraw(float deltams)
  49. {
  50. WorldEntity::TickDraw(deltams);
  51. if (!m_ready)
  52. {
  53. m_shader = Shader::Create(
  54. #if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
  55. "#version 120\n"
  56. "attribute vec2 in_Position;"
  57. "void main(void) {"
  58. " gl_Position = vec4(in_Position, 0.0, 1.0);"
  59. "}",
  60. "#version 120\n"
  61. "void main(void) {"
  62. " gl_FragColor = vec4(0.7, 0.5, 0.2, 1.0);"
  63. "}"
  64. #else
  65. "void main(float2 in_Position : POSITION,"
  66. " out float4 out_Position : POSITION) {"
  67. " out_Position = float4(in_Position, 0.0, 1.0);"
  68. "}",
  69. "void main(out float4 out_FragColor : COLOR) {"
  70. " out_FragColor = float4(0.7, 0.5, 0.2, 1.0);"
  71. "}"
  72. #endif
  73. );
  74. m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
  75. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  76. m_vbo = new VertexBuffer(sizeof(m_vertices));
  77. void *vertices = m_vbo->Lock(0, 0);
  78. memcpy(vertices, m_vertices, sizeof(m_vertices));
  79. m_vbo->Unlock();
  80. m_ready = true;
  81. /* FIXME: this object never cleans up */
  82. }
  83. m_shader->Bind();
  84. m_vdecl->Bind();
  85. m_vdecl->SetStream(m_vbo, m_coord);
  86. #if defined _XBOX || defined USE_D3D9
  87. g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
  88. #endif
  89. #if defined _XBOX || defined USE_D3D9
  90. g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
  91. #else
  92. glDrawArrays(GL_TRIANGLES, 0, 3);
  93. #endif
  94. #if defined _XBOX || defined USE_D3D9
  95. /* FIXME: do we need to unset anything here? */
  96. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  97. glDisableVertexAttribArray(m_attrib);
  98. glBindBuffer(GL_ARRAY_BUFFER, 0);
  99. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  100. /* Never used for now */
  101. glDisableVertexAttribArray(m_attrib);
  102. #else
  103. glDisableClientState(GL_VERTEX_ARRAY);
  104. #endif
  105. }
  106. private:
  107. vec2 m_vertices[3];
  108. Shader *m_shader;
  109. VertexDeclaration *m_vdecl;
  110. VertexBuffer *m_vbo;
  111. ShaderAttrib m_coord;
  112. bool m_ready;
  113. };
  114. int main(int argc, char **argv)
  115. {
  116. Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
  117. #if defined _MSC_VER && !defined _XBOX
  118. _chdir("..");
  119. #elif defined _WIN32 && !defined _XBOX
  120. _chdir("../..");
  121. #endif
  122. new DebugFps(5, 5);
  123. new Triangle();
  124. app.Run();
  125. return EXIT_SUCCESS;
  126. }