DirectRenderer.cpp 6.1 KB

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