From a185c11f2397c0296a4b62cc266b4fa00a63c1e2 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Wed, 14 May 2008 21:07:10 +0000 Subject: console, camera & interpolation --- src/sys/consoleinterface.cc | 129 ++++++++++++++++++++++++++++++++++++++++++++ src/sys/consoleinterface.h | 39 +++++++++----- src/sys/sys.cc | 16 +++--- src/sys/sys.h | 4 +- 4 files changed, 164 insertions(+), 24 deletions(-) (limited to 'src/sys') diff --git a/src/sys/consoleinterface.cc b/src/sys/consoleinterface.cc index f80939d..a300ea1 100644 --- a/src/sys/consoleinterface.cc +++ b/src/sys/consoleinterface.cc @@ -17,11 +17,15 @@ ConsoleInterface *ConsoleInterface::consoleinterface_instance = 0; ConsoleInterface::ConsoleInterface() { + consoleinterface_ansi = true; if (consoleinterface_instance) { std::cerr << "multiple sys::ConsoleInterface instances!" << std::endl; sys::quit(2); } + consoleinterface_instance = this; + consoleinterface_text.clear(); + consoleinterface_buffer.clear(); } ConsoleInterface::~ConsoleInterface() @@ -34,4 +38,129 @@ ConsoleInterface *ConsoleInterface::instance() return consoleinterface_instance; } +std::ostream & ConsoleInterface::messagestream() +{ + flush(); + return (consoleinterface_buffer << "^N"); +} + +std::ostream & ConsoleInterface::warningstream() +{ + flush(); + return (consoleinterface_buffer << "^W"); +} + +std::ostream & ConsoleInterface::errorstream() +{ + flush(); + return (consoleinterface_buffer << "^R"); +} + +std::ostream & ConsoleInterface::debugstream() +{ + flush(); + return (consoleinterface_buffer << "^D"); +} + +void ConsoleInterface::flush() +{ + char line[MAXCMDSIZE]; + + while(consoleinterface_buffer.getline(line, MAXCMDSIZE-1)) { + + while (consoleinterface_text.size() >= MAXCONLINES) { + consoleinterface_text.pop_front(); + } + consoleinterface_text.push_back(std::string(line)); + + // print to stdout + print_ansi(line); + std::cout << std::endl; + } + + consoleinterface_buffer.clear(); +} + +void ConsoleInterface::print_ansi(const char *line) +{ + if (consoleinterface_ansi) + std::cout << "\033[0;39m"; + + const char *c = line; + while (*c) { + + if ((*c == '^')) { + bool is_code = true; + int bold = 0; + int color = 39; + + switch (*(c+1)) { + case '0': // black + color = 0; + bold = 1; + break; + case '1': // red + color = 31; + break; + case '2': // green + color = 32; + break; + case '3': // yellow + bold = 1; + color = 33; + break; + case '4': // blue + color = 34; + break; + case '5': // cyan + color = 36; + break; + case '6': // magenta + color = 35; + break; + case '7': // white is mapped to foreground color + bold = 1; + color = 39; + break; + + case 'N': // normal + bold = 0; + color = 39; + break; + case 'B': // bold + bold = 1; + color = 39; + break; + case 'D': // debug + bold = 0; + color = 39; + break; + case 'R': // error + bold = 0; + color = 31; + break; + case 'W': // warning + bold = 1; + color = 33; + break; + case 'F': // fancy + bold = 0; + color = 32; + break; + default: + is_code = false; + } + if (is_code) { + c++; + if (consoleinterface_ansi) + std::cout << "\033[" << bold << ";" << color << "m"; + } else + std::cout << *c; + } else { + std::cout << *c; + } + c++; + } +} + } // namespace sys diff --git a/src/sys/consoleinterface.h b/src/sys/consoleinterface.h index 33dc5b5..f874182 100644 --- a/src/sys/consoleinterface.h +++ b/src/sys/consoleinterface.h @@ -7,11 +7,11 @@ #ifndef __INCLUDED_SYS_CONSOLEINTERFACE_H__ #define __INCLUDED_SYS_CONSOLEINTERFACE_H__ -// project headers -#include "sys/sys.h" - -// C++ headers #include +#include +#include + +#include "sys/sys.h" /// global define to send a message to the system console #define con_print sys::ConsoleInterface::instance()->messagestream() @@ -30,6 +30,8 @@ namespace sys { +const size_t MAXCONLINES = 2048; + /// interface for the client and server Console classes class ConsoleInterface { @@ -40,27 +42,38 @@ public: /// default destructor virtual ~ConsoleInterface(); - /// stream to send normal messages too - virtual std::ostream & messagestream() = 0; + /// stream to send normal messages to + std::ostream & messagestream(); - /// stream to send warning messages too - virtual std::ostream & warningstream() = 0; + /// stream to send warning messages to + std::ostream & warningstream(); - /// stream to send error messages too - virtual std::ostream & errorstream() = 0; + /// stream to send error messages to + std::ostream & errorstream(); - /// stream to send debug messages too - virtual std::ostream & debugstream() = 0; + /// stream to send debug messages to + std::ostream & debugstream(); /// flush buffered messages - virtual void flush() = 0; + virtual void flush(); + + /// turn ANSI color codes on or off + inline void set_ansi(bool ansi) { consoleinterface_ansi = ansi; } /// a pointer to the current console instance static ConsoleInterface *instance(); +protected: + std::deque consoleinterface_text; + std::stringstream consoleinterface_buffer; + + /// print a string to stdout with ansi color codes + void print_ansi(const char *line); + private: /// console singleton static ConsoleInterface *consoleinterface_instance; + bool consoleinterface_ansi; }; } // namespace sys diff --git a/src/sys/sys.cc b/src/sys/sys.cc index 4c3f38f..4cb820d 100644 --- a/src/sys/sys.cc +++ b/src/sys/sys.cc @@ -20,25 +20,25 @@ #endif #include +#include #include "sys/sys.h" namespace sys { -void mkdir(const char *path) +void mkdir(std::string const &path) { #ifdef _WIN32 - mkdir(path); + string p(path); + for (size_t i = 0; i < p.lenght(); i++) + if (p[i] == '/') p[i] = '\\'; + ::mkdir(p.cstr()); #else - ::mkdir(path, 0777); + + ::mkdir(path.c_str(), 0777); #endif } -void mkdir(const std::string &path) -{ - mkdir(path.c_str()); -} - void signal(int signum, signalfunc handler) { #ifndef _WIN32 diff --git a/src/sys/sys.h b/src/sys/sys.h index 178fe60..e455b82 100644 --- a/src/sys/sys.h +++ b/src/sys/sys.h @@ -22,9 +22,7 @@ namespace sys typedef void (* signalfunc)(int signum); /// create a directory -void mkdir(const char *path); - -void mkdir(const std::string &path); +void mkdir(std::string const &path); /// intercept OS signals void signal(int signum, signalfunc handler); -- cgit v1.2.3