Browse Source

Print to Console like a stream

Thomas Buck 9 years ago
parent
commit
240a80ab4f
4 changed files with 31 additions and 7 deletions
  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 View File

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

+ 0
- 1
TODO.md View File

8
     * Maybe replace loader with [VT](http://icculus.org/vt/), also used by OpenTomb.
8
     * Maybe replace loader with [VT](http://icculus.org/vt/), also used by OpenTomb.
9
 * Don't use C-Style code, try to replace with C++ lib
9
 * Don't use C-Style code, try to replace with C++ lib
10
     * Use std::strings
10
     * Use std::strings
11
-    * Rewrite Console and use operator << to write to the console?
12
 * 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...
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
 * Don’t use float everywhere just because (eg. float colors)
12
 * Don’t use float everywhere just because (eg. float colors)
14
 * Add verbose command line flag for debug output also in release builds
13
 * Add verbose command line flag for debug output also in release builds

+ 9
- 0
include/Console.h View File

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

+ 21
- 6
src/Console.cpp View File

31
     return mVisible;
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
 void Console::print(const char *s, ...) {
50
 void Console::print(const char *s, ...) {
35
     va_list args;
51
     va_list args;
36
     va_start(args, s);
52
     va_start(args, s);
37
     char *tmp = bufferString(s, args);
53
     char *tmp = bufferString(s, args);
38
     va_end(args);
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
     delete [] tmp;
59
     delete [] tmp;
48
 }
60
 }
80
         mLineOffset = 0;
92
         mLineOffset = 0;
81
     }
93
     }
82
 
94
 
95
+    // Draw status line
83
     getFont().drawText(10, 10, 0.70f, BLUE,
96
     getFont().drawText(10, 10, 0.70f, BLUE,
84
             "%s uptime %lus scroll %d%%", VERSION, systemTimerGet() / 1000, scrollIndicator);
97
             "%s uptime %lus scroll %d%%", VERSION, systemTimerGet() / 1000, scrollIndicator);
85
 
98
 
93
     } else if (lineCount < mHistory.size()) {
106
     } else if (lineCount < mHistory.size()) {
94
         historyOffset = mHistory.size() - lineCount;
107
         historyOffset = mHistory.size() - lineCount;
95
     }
108
     }
109
+
96
     for (int i = 0; i < end; i++) {
110
     for (int i = 0; i < end; i++) {
97
         getFont().drawText(10, (unsigned int)((i + drawOffset) * lineSteps) + firstLine,
111
         getFont().drawText(10, (unsigned int)((i + drawOffset) * lineSteps) + firstLine,
98
                 0.75f, BLUE, "%s", mHistory[i + historyOffset - mLineOffset].c_str());
112
                 0.75f, BLUE, "%s", mHistory[i + historyOffset - mLineOffset].c_str());
122
         mHistoryPointer = 0;
136
         mHistoryPointer = 0;
123
     }
137
     }
124
 
138
 
139
+    // Delete last character
125
     if (pressed && (key == backspaceKey)) {
140
     if (pressed && (key == backspaceKey)) {
126
         if ((mPartialInput.length() == 0)
141
         if ((mPartialInput.length() == 0)
127
                 && (mInputBuffer.length() > 0)) {
142
                 && (mInputBuffer.length() > 0)) {

Loading…
Cancel
Save