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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. int error = gOpenRaider->command(mInputBuffer);
  126. if (error != 0) {
  127. print("Error Code: %d", error);
  128. }
  129. } else {
  130. mHistory.push_back(bufferString("> "));
  131. }
  132. // Clear partial and input buffer
  133. mInputBufferPointer = 0;
  134. mInputBuffer[0] = '\0';
  135. if (mPartialInput != NULL) {
  136. delete [] mPartialInput;
  137. mPartialInput = NULL;
  138. }
  139. mHistoryPointer = 0;
  140. }
  141. //! \fixme only deleting the last byte is not valid for non-ASCII UTF-8 strings
  142. if (pressed && (key == backspace)) {
  143. if (mInputBufferPointer > 0) {
  144. mInputBufferPointer--;
  145. mInputBuffer[mInputBufferPointer] = '\0';
  146. }
  147. }
  148. if (pressed && ((key == up) || (key == down))) {
  149. moveInHistory(key == up);
  150. }
  151. }
  152. void Console::moveInHistory(bool up) {
  153. if (mCommandHistory.size() == 0)
  154. return;
  155. if (up) {
  156. if (mHistoryPointer < mCommandHistory.size()) {
  157. mHistoryPointer++;
  158. if (mHistoryPointer == 1) {
  159. mUnfinishedInput = bufferString("%s", mInputBuffer);
  160. }
  161. } else {
  162. return;
  163. }
  164. } else {
  165. if (mHistoryPointer > 0)
  166. mHistoryPointer--;
  167. else
  168. return;
  169. }
  170. if ((mHistoryPointer > 0) && (mHistoryPointer <= mCommandHistory.size())) {
  171. strcpy(mInputBuffer, mCommandHistory[mCommandHistory.size() - mHistoryPointer]);
  172. mInputBufferPointer = strlen(mInputBuffer);
  173. } else {
  174. if (mUnfinishedInput != NULL) {
  175. strcpy(mInputBuffer, mUnfinishedInput);
  176. mInputBufferPointer = strlen(mInputBuffer);
  177. delete [] mUnfinishedInput;
  178. mUnfinishedInput = NULL;
  179. } else {
  180. mInputBuffer[0] = '\0';
  181. mInputBufferPointer = 0;
  182. }
  183. }
  184. }
  185. void Console::handleText(char *text, bool notFinished) {
  186. //printf("Text: %s (%s)\n", text, (notFinished ? "not finished" : "finished"));
  187. // Always scroll to bottom when text input is received
  188. mLineOffset = 0;
  189. if (!notFinished) {
  190. // Finished entering character
  191. // delete previous partial character, if present
  192. if (mPartialInput != NULL) {
  193. delete [] mPartialInput;
  194. }
  195. //! \fixme Temporary hack filtering the console activation key
  196. if (text[0] == '`')
  197. return;
  198. // Append new input to buffer
  199. size_t length = strlen(text);
  200. if (length > 0) {
  201. if (((INPUT_BUFFER_SIZE - mInputBufferPointer) < length)) {
  202. printf("Console input buffer overflowed! (> %d)\n", INPUT_BUFFER_SIZE);
  203. return;
  204. }
  205. strcpy((mInputBuffer + mInputBufferPointer), text);
  206. mInputBufferPointer += length;
  207. mInputBuffer[mInputBufferPointer] = '\0';
  208. }
  209. } else {
  210. // Partial character received
  211. mPartialInput = bufferString("%s", text);
  212. }
  213. }
  214. void Console::handleMouseScroll(int xrel, int yrel) {
  215. LINE_GEOMETRY(gOpenRaider->mWindow);
  216. if (mHistory.size() > lineCount) {
  217. if (yrel > 0) {
  218. if (mLineOffset < (mHistory.size() - lineCount)) {
  219. mLineOffset++;
  220. }
  221. } else if (yrel < 0) {
  222. if (mLineOffset > 0) {
  223. mLineOffset--;
  224. }
  225. }
  226. }
  227. }