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.

Console.cpp 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*!
  2. * \file src/Console.cpp
  3. * \brief Console 'overlay'
  4. *
  5. * \author xythobuz
  6. */
  7. #ifdef __APPLE__
  8. #include <OpenGL/gl.h>
  9. #include <OpenGL/glu.h>
  10. #else
  11. #include <GL/gl.h>
  12. #include <GL/glu.h>
  13. #endif
  14. #include "config.h"
  15. #include "Console.h"
  16. #include "main.h"
  17. #include "utils/strings.h"
  18. #include "utils/time.h"
  19. #define INPUT_BUFFER_SIZE 255
  20. Console::Console() {
  21. mVisible = false;
  22. mInputBuffer = new char[INPUT_BUFFER_SIZE + 1];
  23. mInputBuffer[INPUT_BUFFER_SIZE] = '\0';
  24. mInputBufferPointer = 0;
  25. mPartialInput = NULL;
  26. mHistoryPointer = 0;
  27. mUnfinishedInput = NULL;
  28. mLineOffset = 0;
  29. }
  30. Console::~Console() {
  31. if (mInputBuffer)
  32. delete [] mInputBuffer;
  33. if (mPartialInput)
  34. delete [] mPartialInput;
  35. if (mUnfinishedInput)
  36. delete [] mUnfinishedInput;
  37. while (mHistory.size() > 0) {
  38. char *tmp = mHistory.back();
  39. if (tmp != NULL)
  40. delete [] tmp;
  41. mHistory.pop_back();
  42. }
  43. while (mCommandHistory.size() > 0) {
  44. char *tmp = mCommandHistory.back();
  45. if (tmp != NULL)
  46. delete [] tmp;
  47. mCommandHistory.pop_back();
  48. }
  49. }
  50. void Console::setVisible(bool visible) {
  51. mVisible = visible;
  52. gOpenRaider->mWindow->setTextInput(mVisible);
  53. }
  54. bool Console::isVisible() {
  55. return mVisible;
  56. }
  57. void Console::print(const char *s, ...) {
  58. va_list args;
  59. va_start(args, s);
  60. char *tmp = bufferString(s, args);
  61. va_end(args);
  62. if (tmp != NULL) {
  63. mHistory.push_back(tmp);
  64. printf("%s\n", tmp);
  65. }
  66. }
  67. #define LINE_GEOMETRY(window) unsigned int firstLine = 35; \
  68. unsigned int lastLine = (window->mHeight / 2) - 55; \
  69. unsigned int inputLine = (window->mHeight / 2) - 30; \
  70. unsigned int lineSteps = 20; \
  71. unsigned int lineCount = (lastLine - firstLine + lineSteps) / lineSteps; \
  72. while (((lineCount * lineSteps) + firstLine) < inputLine) { \
  73. lineSteps++; \
  74. lineCount = (lastLine - firstLine + lineSteps) / lineSteps; \
  75. }
  76. void Console::display() {
  77. Window *window = gOpenRaider->mWindow;
  78. unsigned char color[4] = {0xFF, 0xFF, 0xFF, 0xFF};
  79. if (mVisible) {
  80. // Calculate line drawing geometry
  81. // Depends on window height, so recalculate every time
  82. LINE_GEOMETRY(window);
  83. // Draw half-transparent *overlay*
  84. glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
  85. glDisable(GL_TEXTURE_2D);
  86. glRecti(0, 0, window->mWidth, window->mHeight / 2);
  87. glEnable(GL_TEXTURE_2D);
  88. int scrollIndicator;
  89. if (mHistory.size() > lineCount) {
  90. scrollIndicator = (mHistory.size() - lineCount - mLineOffset) * 100 / (mHistory.size() - lineCount);
  91. } else {
  92. scrollIndicator = 100;
  93. }
  94. gOpenRaider->mWindow->drawText(10, 10, 0.70f, color,
  95. "%s uptime %lus scroll %d%%", VERSION, systemTimerGet() / 1000, scrollIndicator);
  96. // Draw output log
  97. int end = lineCount;
  98. int drawOffset = 0;
  99. int historyOffset = 0;
  100. if (mHistory.size() < lineCount) {
  101. end = mHistory.size();
  102. drawOffset = lineCount - mHistory.size();
  103. } else if (lineCount < mHistory.size()) {
  104. historyOffset = mHistory.size() - lineCount;
  105. }
  106. for (int i = 0; i < end; i++) {
  107. gOpenRaider->mWindow->drawText(10, ((i + drawOffset) * lineSteps) + firstLine,
  108. 0.75f, color, "%s", mHistory[i + historyOffset - mLineOffset]);
  109. }
  110. // Draw current input
  111. if ((mInputBufferPointer > 0) && (mInputBuffer[0] != '\0')) {
  112. gOpenRaider->mWindow->drawText(10, inputLine, 0.75f, color, "> %s", mInputBuffer);
  113. } else {
  114. gOpenRaider->mWindow->drawText(10, inputLine, 0.75f, color, ">");
  115. }
  116. //! \todo display the current mPartialInput. The UTF-8 segfaults SDL-TTF, somehow?
  117. }
  118. }
  119. void Console::handleKeyboard(KeyboardButton key, bool pressed) {
  120. if (pressed && (key == enter)) {
  121. // Execute entered command
  122. if ((mInputBufferPointer > 0) && (mInputBuffer[0] != '\0')) {
  123. mHistory.push_back(bufferString("> %s", mInputBuffer));
  124. mCommandHistory.push_back(bufferString("%s", mInputBuffer));
  125. gOpenRaider->command(mInputBuffer);
  126. } else {
  127. mHistory.push_back(bufferString("> "));
  128. }
  129. // Clear partial and input buffer
  130. mInputBufferPointer = 0;
  131. mInputBuffer[0] = '\0';
  132. if (mPartialInput != NULL) {
  133. delete [] mPartialInput;
  134. mPartialInput = NULL;
  135. }
  136. mHistoryPointer = 0;
  137. }
  138. //! \fixme only deleting the last byte is not valid for non-ASCII UTF-8 strings
  139. if (pressed && (key == backspace)) {
  140. if (mInputBufferPointer > 0) {
  141. mInputBufferPointer--;
  142. mInputBuffer[mInputBufferPointer] = '\0';
  143. }
  144. }
  145. if (pressed && ((key == up) || (key == down))) {
  146. moveInHistory(key == up);
  147. }
  148. }
  149. void Console::moveInHistory(bool up) {
  150. if (mCommandHistory.size() == 0)
  151. return;
  152. if (up) {
  153. if (mHistoryPointer < mCommandHistory.size()) {
  154. mHistoryPointer++;
  155. if (mHistoryPointer == 1) {
  156. mUnfinishedInput = bufferString("%s", mInputBuffer);
  157. }
  158. } else {
  159. return;
  160. }
  161. } else {
  162. if (mHistoryPointer > 0)
  163. mHistoryPointer--;
  164. else
  165. return;
  166. }
  167. if ((mHistoryPointer > 0) && (mHistoryPointer <= mCommandHistory.size())) {
  168. strcpy(mInputBuffer, mCommandHistory[mCommandHistory.size() - mHistoryPointer]);
  169. mInputBufferPointer = strlen(mInputBuffer);
  170. } else {
  171. if (mUnfinishedInput != NULL) {
  172. strcpy(mInputBuffer, mUnfinishedInput);
  173. mInputBufferPointer = strlen(mInputBuffer);
  174. delete [] mUnfinishedInput;
  175. mUnfinishedInput = NULL;
  176. } else {
  177. mInputBuffer[0] = '\0';
  178. mInputBufferPointer = 0;
  179. }
  180. }
  181. }
  182. void Console::handleText(char *text, bool notFinished) {
  183. //printf("Text: %s (%s)\n", text, (notFinished ? "not finished" : "finished"));
  184. // Always scroll to bottom when text input is received
  185. mLineOffset = 0;
  186. if (!notFinished) {
  187. // Finished entering character
  188. // delete previous partial character, if present
  189. if (mPartialInput != NULL) {
  190. delete [] mPartialInput;
  191. }
  192. //! \fixme Temporary hack filtering the console activation key
  193. if (text[0] == '`')
  194. return;
  195. // Append new input to buffer
  196. size_t length = strlen(text);
  197. if (length > 0) {
  198. if (((INPUT_BUFFER_SIZE - mInputBufferPointer) < length)) {
  199. printf("Console input buffer overflowed! (> %d)\n", INPUT_BUFFER_SIZE);
  200. return;
  201. }
  202. strcpy((mInputBuffer + mInputBufferPointer), text);
  203. mInputBufferPointer += length;
  204. mInputBuffer[mInputBufferPointer] = '\0';
  205. }
  206. } else {
  207. // Partial character received
  208. mPartialInput = bufferString("%s", text);
  209. }
  210. }
  211. void Console::handleMouseScroll(int xrel, int yrel) {
  212. LINE_GEOMETRY(gOpenRaider->mWindow);
  213. if (mHistory.size() > lineCount) {
  214. if (yrel > 0) {
  215. if (mLineOffset < (mHistory.size() - lineCount)) {
  216. mLineOffset++;
  217. }
  218. } else if (yrel < 0) {
  219. if (mLineOffset > 0) {
  220. mLineOffset--;
  221. }
  222. }
  223. }
  224. }