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.0KB

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