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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include <assert.h>
  13. #ifdef __APPLE__
  14. #include <OpenGL/gl.h>
  15. #else
  16. #include <GL/gl.h>
  17. #endif
  18. #include "Texture.h"
  19. #include "utils/strings.h"
  20. #include "GLString.h"
  21. GLString::GLString() {
  22. _num_string_max = 0;
  23. _num_string = 0;
  24. _scale = 1.0;
  25. _string = NULL;
  26. }
  27. GLString::~GLString() {
  28. if (_string) {
  29. for (unsigned int i = 0; i < _num_string; ++i) {
  30. if (_string[i].text)
  31. delete [] _string[i].text;
  32. }
  33. delete [] _string;
  34. }
  35. }
  36. void GLString::Init(unsigned int max_strings) {
  37. assert(max_strings > 0);
  38. _num_string_max = max_strings;
  39. _string = new gl_string_t[max_strings];
  40. }
  41. void GLString::SetChar(unsigned int string, unsigned int pos, char c) {
  42. gl_string_t *str = GetString(string);
  43. assert(str != NULL);
  44. if (pos < str->len)
  45. str->text[pos] = c;
  46. }
  47. unsigned int GLString::GetStringLen(unsigned int string) {
  48. gl_string_t *str = GetString(string);
  49. assert(str != NULL);
  50. return str->len;
  51. }
  52. char *GLString::GetBuffer(unsigned int string) {
  53. gl_string_t *str = GetString(string);
  54. assert(str != NULL);
  55. return str->text;
  56. }
  57. void GLString::setActive(unsigned int string, bool active) {
  58. gl_string_t *str = GetString(string);
  59. assert(str != NULL);
  60. str->active = active;
  61. }
  62. void GLString::SetString(unsigned int string, const char *s, ...) {
  63. va_list args;
  64. gl_string_t *str = GetString(string);
  65. assert(s != NULL);
  66. assert(s[0] != '\0');
  67. assert(str != NULL);
  68. delete [] str->text;
  69. va_start(args, s);
  70. str->text = bufferString(s, args);
  71. va_end(args);
  72. str->active = true;
  73. str->len = (unsigned short)strlen(str->text);
  74. }
  75. void GLString::Scale(float scale) {
  76. _scale = scale;
  77. }
  78. int GLString::glPrintf(int x, int y, const char *string, ...) {
  79. va_list args;
  80. assert(string != NULL);
  81. assert(string[0] != '\0');
  82. if (_num_string > _num_string_max)
  83. return -2;
  84. va_start(args, string);
  85. _string[_num_string].active = true;
  86. _string[_num_string].scale = _scale;
  87. _string[_num_string].x = x;
  88. _string[_num_string].y = y;
  89. _string[_num_string].text = bufferString(string, args);
  90. //! \fixme All this is really unnecessary complex!
  91. _string[_num_string].len = (unsigned short)strlen(_string[_num_string].text);
  92. ++_num_string;
  93. va_end(args);
  94. return 0;
  95. }
  96. void GLString::Render() {
  97. for (unsigned int i = 0; i < _num_string; ++i) {
  98. if (_string[i].active)
  99. glPrint2d(_string[i].x, _string[i].y, _string[i].scale, _string[i].text);
  100. }
  101. }
  102. gl_string_t *GLString::GetString(unsigned int id) {
  103. assert(id < _num_string);
  104. return _string + id;
  105. }