DirectRenderer.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "DirectRenderer.h"
  2. #include "GameEngine.h"
  3. #include <cmath>
  4. DirectRenderer::DirectRenderer()
  5. {
  6. glGenVertexArrays(1, &vba);
  7. glGenBuffers(1, &vbo);
  8. glBindVertexArray(vba);
  9. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  10. glEnableVertexAttribArray(0);
  11. glVertexAttribPointer(0, 3, GL_FLOAT, false, 24, (GLvoid*) 0);
  12. glEnableVertexAttribArray(1);
  13. glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, true, 24, (GLvoid*) 12);
  14. glEnableVertexAttribArray(2);
  15. glVertexAttribPointer(2, 2, GL_FLOAT, false, 24, (GLvoid*) 16);
  16. }
  17. DirectRenderer::DirectRenderer(const DirectRenderer& orig)
  18. {
  19. }
  20. DirectRenderer::~DirectRenderer()
  21. {
  22. glDeleteVertexArrays(1, &vba);
  23. glDeleteBuffers(1, &vbo);
  24. }
  25. void DirectRenderer::drawRectangle(float minX, float minY, float maxX, float maxY, float tMinX, float tMinY, float tMaxX, float tMaxY, IntColor c)
  26. {
  27. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  28. if(offset + OBJECT_LENGTH >= BUFFER_LENGTH)
  29. {
  30. offset = 0;
  31. glBufferData(GL_ARRAY_BUFFER, BUFFER_LENGTH, NULL, GL_STREAM_DRAW);
  32. }
  33. float* buffer = (float*) glMapBufferRange(GL_ARRAY_BUFFER, offset, OBJECT_LENGTH, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
  34. if(buffer == NULL)
  35. {
  36. return;
  37. }
  38. buffer[0] = minX;
  39. buffer[1] = maxY;
  40. buffer[2] = 0.0f;
  41. buffer[3] = *((float*) (&c));
  42. buffer[4] = tMinX;
  43. buffer[5] = tMaxY;
  44. buffer[6] = maxX;
  45. buffer[7] = maxY;
  46. buffer[8] = 0.0f;
  47. buffer[9] = *((float*) (&c));
  48. buffer[10] = tMaxX;
  49. buffer[11] = tMaxY;
  50. buffer[12] = minX;
  51. buffer[13] = minY;
  52. buffer[14] = 0.0f;
  53. buffer[15] = *((float*) (&c));
  54. buffer[16] = tMinX;
  55. buffer[17] = tMinY;
  56. buffer[18] = maxX;
  57. buffer[19] = minY;
  58. buffer[20] = 0.0f;
  59. buffer[21] = *((float*) (&c));
  60. buffer[22] = tMaxX;
  61. buffer[23] = tMinY;
  62. glUnmapBuffer(GL_ARRAY_BUFFER);
  63. glBindVertexArray(vba);
  64. glDrawArrays(GL_TRIANGLE_STRIP, offset / (OBJECT_LENGTH / 4), 4);
  65. offset += OBJECT_LENGTH;
  66. }
  67. void DirectRenderer::drawRectangle(float minX, float minY, float maxX, float maxY, IntColor c)
  68. {
  69. drawRectangle(minX, minY, maxX, maxY, 0.0f, 0.0f, 0.0f, 0.0f, c);
  70. }
  71. void DirectRenderer::drawRectangle(float minX, float minY, float maxX, float maxY, float tMinX, float tMinY, float tMaxX, float tMaxY)
  72. {
  73. drawRectangle(minX, minY, maxX, maxY, tMinX, tMinY, tMaxX, tMaxY, 0);
  74. }
  75. float DirectRenderer::drawString(float x, float y, bool shadow, string& s)
  76. {
  77. int size = s.length() * OBJECT_LENGTH * (shadow + 1);
  78. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  79. if(offset + size >= BUFFER_LENGTH)
  80. {
  81. offset = 0;
  82. glBufferData(GL_ARRAY_BUFFER, BUFFER_LENGTH, NULL, GL_STREAM_DRAW);
  83. }
  84. float* buffer = (float*) glMapBufferRange(GL_ARRAY_BUFFER, offset, size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
  85. if(buffer == NULL)
  86. {
  87. return y;
  88. }
  89. int index = 0;
  90. if(shadow)
  91. {
  92. addString(x + SHADOW_STEP, y + SHADOW_STEP, index, buffer, Color::DARK_COLORS, s);
  93. }
  94. y = addString(x, y, index, buffer, Color::COLORS, s);
  95. glUnmapBuffer(GL_ARRAY_BUFFER);
  96. glBindVertexArray(vba);
  97. FONTS[min(GameEngine::get()->getScale() - 1, FONTS_LENGTH - 1)].bind();
  98. glDrawArrays(GL_TRIANGLE_STRIP, offset / (OBJECT_LENGTH / 4), (index + 1) / 6);
  99. offset += size;
  100. return y;
  101. }
  102. float DirectRenderer::addString(float x, float y, int& index, float* data, const IntColor* color, string& s)
  103. {
  104. int l = min((int) s.length(), static_cast<int>(MAX_STRING_LENGTH));
  105. //x = roundf(x / GameEngine::scale) * GameEngine::scale;
  106. //y = roundf(y / GameEngine::scale) * GameEngine::scale;
  107. float oldX = x;
  108. float currentColor = *((float*) (&(color[Color::COLOR_AMOUNT - 1])));
  109. for(int pos = 0; pos < l; pos++)
  110. {
  111. char c = s[pos];
  112. if(c == COLOR_CHAR)
  113. {
  114. pos++;
  115. if(pos < l)
  116. {
  117. int index = s[pos] - 'a';
  118. if(index >= 0 && index < Color::COLOR_AMOUNT)
  119. {
  120. currentColor = *((float*) (&(color[index])));
  121. }
  122. }
  123. continue;
  124. }
  125. else if(c == '\n')
  126. {
  127. y += FONT_SIZE + LINE_STEP;
  128. x = oldX;
  129. continue;
  130. }
  131. float tMinX = (c & 0xF) * 0.0625f;
  132. float tMinY = (c >> 4) * 0.0625f;
  133. float tMaxX = tMinX + 0.0625f;
  134. float tMaxY = tMinY + 0.0625f;
  135. float maxX = x + FONT_SIZE;
  136. float maxY = y + FONT_SIZE;
  137. data[index++] = x;
  138. data[index++] = maxY;
  139. data[index++] = 0.0f;
  140. data[index++] = currentColor;
  141. data[index++] = tMinX;
  142. data[index++] = tMaxY;
  143. data[index++] = maxX;
  144. data[index++] = maxY;
  145. data[index++] = 0.0f;
  146. data[index++] = currentColor;
  147. data[index++] = tMaxX;
  148. data[index++] = tMaxY;
  149. data[index++] = x;
  150. data[index++] = y;
  151. data[index++] = 0.0f;
  152. data[index++] = currentColor;
  153. data[index++] = tMinX;
  154. data[index++] = tMinY;
  155. data[index++] = maxX;
  156. data[index++] = y;
  157. data[index++] = 0.0f;
  158. data[index++] = currentColor;
  159. data[index++] = tMaxX;
  160. data[index++] = tMinY;
  161. x += FONT_SIZE;
  162. }
  163. return y + FONT_SIZE + LINE_STEP;
  164. }
  165. /*
  166. public Rectangle getSize(String s)
  167. {
  168. int length = 0;
  169. int counter = 0;
  170. for(int i = 0; i < s.length(); i++)
  171. {
  172. switch(s.charAt(i))
  173. {
  174. case '\n':
  175. counter++;
  176. break;
  177. case COLOR_CHAR:
  178. i++;
  179. break;
  180. default:
  181. length++;
  182. }
  183. }
  184. return new Rectangle(FONT_SIZE * length, (FONT_SIZE + LINE_STEP) * counter);
  185. }
  186. public Rectangle getSize(int w, int h)
  187. {
  188. return new Rectangle(FONT_SIZE * w, (FONT_SIZE + LINE_STEP) * h);
  189. }
  190. public float getHeight()
  191. {
  192. return FONT_SIZE + LINE_STEP;
  193. }
  194. public float getWidth()
  195. {
  196. return FONT_SIZE;
  197. }
  198. */