Open Source Tomb Raider Engine
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.

GLString.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*!
  2. * \file src/GLString.cpp
  3. * \brief Open GL rendering font/string class
  4. *
  5. * \author Mongoose
  6. */
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #ifdef __APPLE__
  12. #include <OpenGL/gl.h>
  13. #else
  14. #include <GL/gl.h>
  15. #endif
  16. #include <Texture.h>
  17. #include <GLString.h>
  18. #ifdef DEBUG_MEMORY
  19. #include <memory_test.h>
  20. #endif
  21. GLString::GLString()
  22. {
  23. _num_string_max = 0;
  24. _num_string = 0;
  25. _scale = 1.0;
  26. _string = NULL;
  27. }
  28. GLString::~GLString()
  29. {
  30. unsigned int i;
  31. if (_string)
  32. {
  33. for (i = 0; i < _num_string; ++i)
  34. {
  35. if (_string[i].text)
  36. {
  37. delete [] _string[i].text;
  38. }
  39. }
  40. delete [] _string;
  41. }
  42. }
  43. void GLString::Init(unsigned int max_strings)
  44. {
  45. if (!max_strings)
  46. {
  47. return;
  48. }
  49. _num_string_max = max_strings;
  50. _string = new gl_string_t[max_strings];
  51. }
  52. void GLString::SetChar(unsigned int string, unsigned int pos, char c)
  53. {
  54. gl_string_t *str = GetString(string);
  55. if (str && pos < str->len)
  56. {
  57. str->text[pos] = c;
  58. }
  59. }
  60. unsigned int GLString::GetStringLen(unsigned int string)
  61. {
  62. gl_string_t *str = GetString(string);
  63. if (str)
  64. {
  65. return str->len;
  66. }
  67. return 0;
  68. }
  69. char *GLString::GetBuffer(unsigned int string)
  70. {
  71. gl_string_t *str = GetString(string);
  72. if (str)
  73. {
  74. return str->text;
  75. }
  76. return 0;
  77. }
  78. void GLString::setActive(unsigned int string, bool active)
  79. {
  80. gl_string_t *str;
  81. str = GetString(string);
  82. if (str)
  83. {
  84. str->active = active;
  85. }
  86. }
  87. void GLString::SetString(unsigned int string, const char *s, ...)
  88. {
  89. va_list args;
  90. gl_string_t *str;
  91. unsigned int len;
  92. str = GetString(string);
  93. if (s && s[0] && str)
  94. {
  95. str->active = true;
  96. len = strlen(s);
  97. if (len > str->len)
  98. {
  99. len = str->len - 1;
  100. }
  101. va_start(args, s);
  102. vsnprintf(str->text, str->len-2, s, args);
  103. str->text[str->len-1] = 0;
  104. va_end(args);
  105. }
  106. }
  107. void GLString::Scale(float scale)
  108. {
  109. _scale = scale;
  110. }
  111. int GLString::glPrintf(int x, int y, const char *string, ...)
  112. {
  113. int sz = 60;
  114. int n;
  115. va_list args;
  116. // Mongoose 2002.01.01, Only allow valid strings
  117. // we must assume it's NULL terminated also if it passes...
  118. if (!string || !string[0])
  119. {
  120. return -1;
  121. }
  122. if (_num_string > _num_string_max)
  123. {
  124. return -2;
  125. }
  126. // Mongoose 2002.01.01, Assume no longer than 'sz' wide lines
  127. // on first try
  128. _string[_num_string].text = new char[sz];
  129. // Mongoose 2002.01.03, Setup scale factor
  130. _string[_num_string].scale = _scale;
  131. // Mongoose 2002.01.01, Setup position
  132. _string[_num_string].x = x;
  133. _string[_num_string].y = y;
  134. va_start(args, string);
  135. // Mongoose 2002.01.01, Get exact size needed if the first try fails
  136. n = vsnprintf(_string[_num_string].text, sz, string, args);
  137. // Mongoose 2002.01.01, Realloc correct amount if truncated
  138. while (1)
  139. {
  140. if (n > -1 && n < sz)
  141. {
  142. break;
  143. }
  144. // Mongoose 2002.01.01, For glibc 2.1
  145. if (n > -1)
  146. {
  147. sz = n + 1;
  148. delete [] _string[_num_string].text;
  149. _string[_num_string].text = new char[sz];
  150. n = vsnprintf(_string[_num_string].text, sz, string, args);
  151. break;
  152. }
  153. else // glibc 2.0
  154. {
  155. sz *= 2;
  156. delete [] _string[_num_string].text;
  157. _string[_num_string].text = new char[sz];
  158. n = vsnprintf(_string[_num_string].text, sz, string, args);
  159. }
  160. }
  161. va_end(args);
  162. // Mongoose 2002.01.04, Remeber string size, for future rebuffering use
  163. _string[_num_string].len = sz;
  164. // Mongoose 2002.01.01, Incement string counter, since we just
  165. // allocated a string
  166. ++_num_string;
  167. return 0;
  168. }
  169. void GLString::Render(int width, int height)
  170. {
  171. unsigned int i;
  172. for (i = 0; i < _num_string; ++i)
  173. {
  174. if (_string[i].active)
  175. {
  176. glPrint2d(_string[i].x, _string[i].y,
  177. _string[i].scale,
  178. _string[i].text);
  179. }
  180. }
  181. }
  182. gl_string_t *GLString::GetString(unsigned int id)
  183. {
  184. if (id < _num_string)
  185. {
  186. return _string + id;
  187. }
  188. return NULL;
  189. }