154 line
3.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-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. #if defined _WIN32 && defined USE_D3D9
  16. # define FAR
  17. # define NEAR
  18. # include <d3d9.h>
  19. #endif
  20. using namespace std;
  21. #if defined USE_D3D9
  22. extern IDirect3DDevice9 *g_d3ddevice;
  23. #elif defined _XBOX
  24. extern D3DDevice *g_d3ddevice;
  25. #endif
  26. namespace lol
  27. {
  28. //
  29. // The IndexBufferData class
  30. // --------------------------
  31. //
  32. class IndexBufferData
  33. {
  34. friend class IndexBuffer;
  35. size_t m_size;
  36. #if defined USE_D3D9
  37. IDirect3DIndexBuffer9 *m_ibo;
  38. #elif defined _XBOX
  39. D3DIndexBuffer *m_ibo;
  40. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  41. GLuint m_ibo;
  42. uint8_t *m_memory;
  43. #endif
  44. };
  45. //
  46. // The IndexBuffer class
  47. // ----------------------
  48. //
  49. IndexBuffer::IndexBuffer(size_t size)
  50. : m_data(new IndexBufferData)
  51. {
  52. m_data->m_size = size;
  53. if (!size)
  54. return;
  55. #if defined USE_D3D9 || defined _XBOX
  56. if (FAILED(g_d3ddevice->CreateIndexBuffer(size, D3DUSAGE_WRITEONLY,
  57. D3DFMT_INDEX16, D3DPOOL_MANAGED,
  58. &m_data->m_ibo, NULL)))
  59. Abort();
  60. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  61. glGenBuffers(1, &m_data->m_ibo);
  62. m_data->m_memory = new uint8_t[size];
  63. #endif
  64. }
  65. IndexBuffer::~IndexBuffer()
  66. {
  67. if (m_data->m_size)
  68. {
  69. #if defined USE_D3D9 || defined _XBOX
  70. if (FAILED(m_data->m_ibo->Release()))
  71. Abort();
  72. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  73. glDeleteBuffers(1, &m_data->m_ibo);
  74. delete[] m_data->m_memory;
  75. #endif
  76. }
  77. delete m_data;
  78. }
  79. void *IndexBuffer::Lock(size_t offset, size_t size)
  80. {
  81. if (!m_data->m_size)
  82. return NULL;
  83. #if defined USE_D3D9 || defined _XBOX
  84. void *ret;
  85. if (FAILED(m_data->m_ibo->Lock(offset, size, (void **)&ret, 0)))
  86. Abort();
  87. return ret;
  88. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  89. return m_data->m_memory + offset;
  90. #endif
  91. }
  92. void IndexBuffer::Unlock()
  93. {
  94. if (!m_data->m_size)
  95. return;
  96. #if defined USE_D3D9 || defined _XBOX
  97. if (FAILED(m_data->m_ibo->Unlock()))
  98. Abort();
  99. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  100. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
  101. glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_data->m_size, m_data->m_memory,
  102. GL_STATIC_DRAW);
  103. #endif
  104. }
  105. void IndexBuffer::Bind()
  106. {
  107. if (!m_data->m_size)
  108. return;
  109. #if defined USE_D3D9 || defined _XBOX
  110. if (FAILED(g_d3ddevice->SetIndices(m_data->m_ibo)))
  111. Abort();
  112. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  113. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
  114. /* XXX: not necessary because we kept track of the size */
  115. //int size;
  116. //glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
  117. #endif
  118. }
  119. void IndexBuffer::Unbind()
  120. {
  121. if (!m_data->m_size)
  122. return;
  123. #if defined USE_D3D9 || defined _XBOX
  124. if (FAILED(g_d3ddevice->SetIndices(NULL)))
  125. Abort();
  126. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  127. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  128. #endif
  129. }
  130. } /* namespace lol */