src/tools/tConsole.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #include "tConsole.h"
00029 #include "tLocale.h"
00030 #include <iostream>
00031 
00032 // anchor for the linked list of filters; filters are added automatically from the constructor.
00033 static tConsoleFilter* st_filterAnchor=0;
00034 
00035 class tConsoleFilterComparator
00036 {
00037 public:
00038     static int Compare( const tConsoleFilter* A, const tConsoleFilter*B )
00039     {
00040         tASSERT( A && B );
00041         return B->GetPriority() - A->GetPriority();
00042     }
00043 };
00044 
00045 // *******************************************************************************************
00046 // *
00047 // *    tConsoleFilter
00048 // *
00049 // *******************************************************************************************
00052 // *******************************************************************************************
00053 
00054 tConsoleFilter::tConsoleFilter( void )
00055         : tListItem< tConsoleFilter >( st_filterAnchor )
00056 {
00057 }
00058 
00059 // *******************************************************************************************
00060 // *
00061 // *    ~tConsoleFilter
00062 // *
00063 // *******************************************************************************************
00066 // *******************************************************************************************
00067 
00068 tConsoleFilter::~tConsoleFilter( void )
00069 {
00070 }
00071 
00072 // *******************************************************************************************
00073 // *
00074 // *    DoFilterOutput
00075 // *
00076 // *******************************************************************************************
00081 // *******************************************************************************************
00082 
00083 void tConsoleFilter::DoFilterLine( tString & line )
00084 {
00085     // no filtering
00086 }
00087 
00088 // *******************************************************************************************
00089 // *
00090 // *    DoFilterElement
00091 // *
00092 // *******************************************************************************************
00097 // *******************************************************************************************
00098 
00099 void tConsoleFilter::DoFilterElement( tString & element )
00100 {
00101     // no filtering
00102 }
00103 
00104 // *******************************************************************************************
00105 // *
00106 // *    DoGetPriority
00107 // *
00108 // *******************************************************************************************
00112 // *******************************************************************************************
00113 
00114 int tConsoleFilter::DoGetPriority( void ) const
00115 {
00116     return 0;
00117 }
00118 
00119 static void FilterLine( tString& line )
00120 {
00121     // no filtering to do
00122     if ( !st_filterAnchor )
00123         return;
00124 
00125 #ifndef WIN32 // MSVC++ does not eat this code, but that's not tragic right now.
00126     // sort static filters according to priority
00127     static bool sorted = false;
00128     if ( !sorted )
00129         st_filterAnchor->Sort< tConsoleFilterComparator >();
00130 #endif
00131 
00132     // remove end of line
00133     bool hasEOL = false;
00134     int eol = line.Size()-1;
00135     while ( eol >= 0 && line[eol] == 0 )
00136         eol--;
00137     if ( eol >= 0 && line[eol] == '\n' )
00138     {
00139         hasEOL = true;
00140         line = line.SubStr( 0, eol );
00141     }
00142 
00143     // filter string
00144     tConsoleFilter* filter = st_filterAnchor;
00145     while ( filter )
00146     {
00147         filter->FilterLine( line );
00148         filter = filter->Next();
00149     }
00150 
00151     // add end of line
00152     if ( hasEOL )
00153         line += '\n';
00154 }
00155 
00156 tConsole *tConsole::s_betterConsole=NULL;
00157 
00158 tConsole::~tConsole(){
00159     if (s_betterConsole == this)
00160         s_betterConsole = NULL;
00161 }
00162 
00163 // stored line
00164 static tString line_("");
00165 
00166 tConsole & tConsole::Print(tString s)
00167 {
00168     // append to line
00169     line_ += s;
00170 
00171     // determine if a newline was received
00172     bool newline = false;
00173     for ( int i = s.Size()-1; i>=0; --i )
00174         if ( s(i) == '\n' )
00175             newline = true;
00176 
00177     if ( newline )
00178     {
00179         // filter
00180         FilterLine( line_ );
00181 
00182         // print
00183         if (s_betterConsole)
00184             s_betterConsole->DoPrint( line_ );
00185         else
00186             DoPrint( line_ );
00187 
00188         line_ = "";
00189     }
00190 
00191     return *this;
00192 }
00193 
00194 void tConsole::CenterDisplay(tString s,REAL timeout,REAL r,REAL g,REAL b)
00195 {
00196     FilterLine(s);
00197 
00198     if (s_betterConsole)
00199         s_betterConsole->DoCenterDisplay(s,timeout,r,g,b);
00200     else
00201         DoCenterDisplay(s,timeout,r,g,b);
00202 }
00203 
00204 tConsole & tConsole::DoPrint(const tString& s){
00205     std::cout << s;
00206     std::cout.flush();
00207     return *this;
00208 }
00209 
00210 void tConsole::DoCenterDisplay(const tString& s,REAL timeout,REAL r,REAL g,REAL b){
00211     std::cout << s;
00212     std::cout.flush();
00213 }
00214 
00215 void tConsole::RegisterBetterConsole(tConsole *better){
00216     s_betterConsole = better;
00217 }
00218 
00219 tString tConsole::ColorString(REAL r, REAL g, REAL b) const{
00220     if (s_betterConsole)
00221         return s_betterConsole->ColorString(r,g,b);
00222     else
00223         return tString("");
00224 }
00225 
00226 
00227 static tConsole::MessageCallback *s_messageCallback = NULL;
00228 void tConsole::RegisterMessageCallback(MessageCallback *a_callback)
00229 {
00230     s_messageCallback = a_callback;
00231 }
00232 
00233 bool tConsole::Message(const tOutput& message, const tOutput& interpretation, REAL timeout){
00234     if (s_messageCallback)
00235         return (*s_messageCallback)(message, interpretation, timeout);
00236     else
00237     {
00238         con << tString(message) << ":\n";
00239         con << tString(interpretation) << '\n';
00240         return true;
00241     }
00242 }
00243 
00244 static tConsole::IdleCallback *s_idleCallback = NULL;
00245 void tConsole::RegisterIdleCallback(IdleCallback *a_callback)
00246 {
00247     s_idleCallback = a_callback;
00248 }
00249 
00250 bool tConsole::Idle(){
00251     if (s_idleCallback)
00252     {
00253         return (*s_idleCallback)();
00254     }
00255     else
00256     {
00257         return false;
00258     }
00259 }
00260 
00261 tConsole con;
00262 
00263 

Generated on Sat Mar 15 22:55:59 2008 for Armagetron Advanced by  doxygen 1.5.4