Ver código fonte

Print to Console like a stream

Thomas Buck 9 anos atrás
pai
commit
240a80ab4f
4 arquivos alterados com 31 adições e 7 exclusões
  1. 1
    0
      ChangeLog.md
  2. 0
    1
      TODO.md
  3. 9
    0
      include/Console.h
  4. 21
    6
      src/Console.cpp

+ 1
- 0
ChangeLog.md Ver arquivo

@@ -12,6 +12,7 @@
12 12
     * Press dot key in Menu to see hidden files and folders
13 13
     * Console is now using std::string instead of char *
14 14
     * Added utf8-cpp dependency to allow Console to delete multi-byte chars
15
+    * Added stream-style printing to Console
15 16
 
16 17
     [ 20140809 ]
17 18
     * Script Unit Test brings it’s own scripts to test

+ 0
- 1
TODO.md Ver arquivo

@@ -8,7 +8,6 @@
8 8
     * Maybe replace loader with [VT](http://icculus.org/vt/), also used by OpenTomb.
9 9
 * Don't use C-Style code, try to replace with C++ lib
10 10
     * Use std::strings
11
-    * Rewrite Console and use operator << to write to the console?
12 11
 * Mesh has 2 different approaches of storing the same data (eg. mColors and mColorArray), but half of ‘em isn’t implemented. Unify this, probably even combining Mesh and StaticMesh...
13 12
 * Don’t use float everywhere just because (eg. float colors)
14 13
 * Add verbose command line flag for debug output also in release builds

+ 9
- 0
include/Console.h Ver arquivo

@@ -9,6 +9,7 @@
9 9
 #define _CONSOLE_H_
10 10
 
11 11
 #include <string>
12
+#include <sstream>
12 13
 #include <vector>
13 14
 
14 15
 /*!
@@ -26,6 +27,10 @@ public:
26 27
 
27 28
     bool isVisible();
28 29
 
30
+    template<typename T>
31
+    Console &operator<<(T t);
32
+
33
+    // Deprecated!
29 34
     void print(const char *s, ...) __attribute__((format(printf, 2, 3)));
30 35
 
31 36
     void display();
@@ -36,6 +41,8 @@ public:
36 41
 
37 42
     void handleMouseScroll(int xrel, int yrel);
38 43
 
44
+    const static char endl = '\n';
45
+
39 46
 private:
40 47
 
41 48
     void moveInHistory(bool up);
@@ -50,6 +57,8 @@ private:
50 57
     std::string mUnfinishedInput;
51 58
 
52 59
     unsigned int mLineOffset;
60
+
61
+    std::ostringstream printBuffer;
53 62
 };
54 63
 
55 64
 Console &getConsole();

+ 21
- 6
src/Console.cpp Ver arquivo

@@ -31,18 +31,30 @@ bool Console::isVisible() {
31 31
     return mVisible;
32 32
 }
33 33
 
34
+template<typename T>
35
+Console &Console::operator<<(T t) {
36
+    printBuffer << t;
37
+
38
+    if (printBuffer.str().back() == '\n') {
39
+        mHistory.push_back(printBuffer.str().substr(0, printBuffer.str().length() - 1));
40
+#ifdef DEBUG
41
+        std::cout << printBuffer.str().substr(0, printBuffer.str().length() - 1) << std::endl;
42
+#endif
43
+        printBuffer.str("");
44
+    }
45
+
46
+    return (*this);
47
+}
48
+
49
+// Deprecated!
34 50
 void Console::print(const char *s, ...) {
35 51
     va_list args;
36 52
     va_start(args, s);
37 53
     char *tmp = bufferString(s, args);
38 54
     va_end(args);
39 55
 
40
-    if (tmp != NULL) {
41
-        mHistory.push_back(std::string(tmp));
42
-#ifdef DEBUG
43
-        std::cout << tmp << std::endl;
44
-#endif
45
-    }
56
+    if (tmp != nullptr)
57
+        (*this) << tmp << endl;
46 58
 
47 59
     delete [] tmp;
48 60
 }
@@ -80,6 +92,7 @@ void Console::display() {
80 92
         mLineOffset = 0;
81 93
     }
82 94
 
95
+    // Draw status line
83 96
     getFont().drawText(10, 10, 0.70f, BLUE,
84 97
             "%s uptime %lus scroll %d%%", VERSION, systemTimerGet() / 1000, scrollIndicator);
85 98
 
@@ -93,6 +106,7 @@ void Console::display() {
93 106
     } else if (lineCount < mHistory.size()) {
94 107
         historyOffset = mHistory.size() - lineCount;
95 108
     }
109
+
96 110
     for (int i = 0; i < end; i++) {
97 111
         getFont().drawText(10, (unsigned int)((i + drawOffset) * lineSteps) + firstLine,
98 112
                 0.75f, BLUE, "%s", mHistory[i + historyOffset - mLineOffset].c_str());
@@ -122,6 +136,7 @@ void Console::handleKeyboard(KeyboardButton key, bool pressed) {
122 136
         mHistoryPointer = 0;
123 137
     }
124 138
 
139
+    // Delete last character
125 140
     if (pressed && (key == backspaceKey)) {
126 141
         if ((mPartialInput.length() == 0)
127 142
                 && (mInputBuffer.length() > 0)) {

Carregando…
Cancelar
Salvar