Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

GLString.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. GLString::GLString() {
  20. _num_string_max = 0;
  21. _num_string = 0;
  22. _scale = 1.0;
  23. _string = NULL;
  24. }
  25. GLString::~GLString() {
  26. if (_string) {
  27. for (unsigned int i = 0; i < _num_string; ++i) {
  28. if (_string[i].text)
  29. delete [] _string[i].text;
  30. }
  31. delete [] _string;
  32. }
  33. }
  34. void GLString::Init(unsigned int max_strings) {
  35. if (!max_strings)
  36. return;
  37. _num_string_max = max_strings;
  38. _string = new gl_string_t[max_strings];
  39. }
  40. void GLString::SetChar(unsigned int string, unsigned int pos, char c) {
  41. gl_string_t *str = GetString(string);
  42. if (str && pos < str->len)
  43. str->text[pos] = c;
  44. }
  45. unsigned int GLString::GetStringLen(unsigned int string) {
  46. gl_string_t *str = GetString(string);
  47. if (str)
  48. return str->len;
  49. return 0;
  50. }
  51. char *GLString::GetBuffer(unsigned int string) {
  52. gl_string_t *str = GetString(string);
  53. if (str)
  54. return str->text;
  55. return 0;
  56. }
  57. void GLString::setActive(unsigned int string, bool active) {
  58. gl_string_t *str = GetString(string);
  59. if (str)
  60. str->active = active;
  61. }
  62. void GLString::SetString(unsigned int string, const char *s, ...) {
  63. va_list args;
  64. gl_string_t *str;
  65. str = GetString(string);
  66. if (s && s[0] && str) {
  67. str->active = true;
  68. va_start(args, s);
  69. vsnprintf(str->text, str->len-2, s, args);
  70. str->text[str->len-1] = 0;
  71. va_end(args);
  72. }
  73. }
  74. void GLString::Scale(float scale) {
  75. _scale = scale;
  76. }
  77. int GLString::glPrintf(int x, int y, const char *string, ...) {
  78. int sz = 60;
  79. int n;
  80. va_list args;
  81. // Mongoose 2002.01.01, Only allow valid strings
  82. // we must assume it's NULL terminated also if it passes...
  83. if (!string || !string[0])
  84. return -1;
  85. if (_num_string > _num_string_max)
  86. return -2;
  87. // xythobuz 2014.02.23, Activate, so we don't have to call SetString
  88. // directly after glPrintf, with the same text...
  89. _string[_num_string].active = true;
  90. // Mongoose 2002.01.01, Assume no longer than 'sz' wide lines
  91. // on first try
  92. _string[_num_string].text = new char[sz];
  93. // Mongoose 2002.01.03, Setup scale factor
  94. _string[_num_string].scale = _scale;
  95. // Mongoose 2002.01.01, Setup position
  96. _string[_num_string].x = x;
  97. _string[_num_string].y = y;
  98. va_start(args, string);
  99. // Mongoose 2002.01.01, Get exact size needed if the first try fails
  100. n = vsnprintf(_string[_num_string].text, sz, string, args);
  101. // Mongoose 2002.01.01, Realloc correct amount if truncated
  102. while (1) {
  103. if (n > -1 && n < sz)
  104. break;
  105. if (n > -1) { // glibc 2.1
  106. sz = n + 1;
  107. delete [] _string[_num_string].text;
  108. _string[_num_string].text = new char[sz];
  109. n = vsnprintf(_string[_num_string].text, sz, string, args);
  110. break;
  111. } else { // glibc 2.0
  112. sz *= 2;
  113. delete [] _string[_num_string].text;
  114. _string[_num_string].text = new char[sz];
  115. n = vsnprintf(_string[_num_string].text, sz, string, args);
  116. }
  117. }
  118. va_end(args);
  119. // Mongoose 2002.01.04, Remeber string size, for future rebuffering use
  120. _string[_num_string].len = (unsigned short)sz;
  121. // Mongoose 2002.01.01, Incement string counter, since we just
  122. // allocated a string
  123. ++_num_string;
  124. return 0;
  125. }
  126. void GLString::Render() {
  127. for (unsigned int i = 0; i < _num_string; ++i) {
  128. if (_string[i].active)
  129. glPrint2d(_string[i].x, _string[i].y, _string[i].scale, _string[i].text);
  130. }
  131. }
  132. gl_string_t *GLString::GetString(unsigned int id) {
  133. if (id < _num_string)
  134. return _string + id;
  135. return NULL;
  136. }