pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] CVS: Games/Pingus/src console.cxx,1.5,1.6 console.hxx,1.5,1


From: torangan
Subject: [Pingus-CVS] CVS: Games/Pingus/src console.cxx,1.5,1.6 console.hxx,1.5,1.6 debug_stream.cxx,1.3,1.4 debug_stream.hxx,1.6,1.7
Date: 17 Aug 2002 17:21:27 -0000

Update of /usr/local/cvsroot/Games/Pingus/src
In directory dark:/tmp/cvs-serv27775

Modified Files:
        console.cxx console.hxx debug_stream.cxx debug_stream.hxx 
Log Message:
- made console work again
- fixed some warnings
- added a missing include


Index: console.cxx
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/console.cxx,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- console.cxx 26 Jun 2002 16:49:33 -0000      1.5
+++ console.cxx 17 Aug 2002 17:21:25 -0000      1.6
@@ -25,12 +25,11 @@
 #include "math.hxx"
 
 using namespace Pingus;
-using namespace std;
 
 // Globale console
 Console console;
 
-ConsoleBuffer::ConsoleBuffer () 
+ConsoleBuffer::ConsoleBuffer () : buffer(NUM_LINES) 
 {
   // Set the output buffer
   setp (char_buffer, char_buffer + CONSOLE_BUFFER_SIZE - 1);
@@ -42,37 +41,17 @@
 ConsoleBuffer::~ConsoleBuffer () 
 {
   sync ();
-
-  /* disabled, due to uselessness
-  int c = 1;
-  std::cout << "----------- Debugging Output ------------" << std::endl;
-  for (std::vector<std::string>::iterator i = buffer.begin ();
-       i != buffer.end (); ++i)
-    {
-      std::cout << "pingus:" << c++ << ": " << *i << std::endl;
-    }
-  */
 }
 
 int
 ConsoleBuffer::overflow (int c) 
 {
-  std::string str;
+  std::string str = fill_buffer(true);
     
-  for (char* ptr = pbase (); ptr != pptr (); ++ptr)
-    {
-      if (*ptr != '\n') 
-       str += *ptr;
-      else
-       {
-         std::cout << str << std::endl;
-         buffer.push_back (str);
-         str = "";
-       }
-    }
   str += c;
-  buffer.push_back (str);    
-    
+  buffer.push_back(str);
+  buffer.pop_front();
+
   setp (char_buffer, char_buffer + CONSOLE_BUFFER_SIZE - 1);
   return 0;
 }
@@ -80,34 +59,62 @@
 int 
 ConsoleBuffer::sync () 
 {
-  std::string str;
+  std::string str = fill_buffer(false);
+
+  if (!str.empty())
+    {
+      buffer.push_back(str);
+      buffer.pop_front();
+    }
     
+  setp(char_buffer, char_buffer + CONSOLE_BUFFER_SIZE - 1);
+  return 0;
+}
+
+std::string
+ConsoleBuffer::fill_buffer (bool append)
+{
+  std::string str;
+  if (append)
+    {
+      str = *(--buffer.end());
+      buffer.pop_back();
+      buffer.push_front("");
+    }
+
   for (char* c = pbase (); c != pptr (); ++c)
     {
       if (*c != '\n') 
        str += *c;
       else
        {
-         std::cout << str << std::endl;
-         buffer.push_back (str);
-         str = "";
+         if (str.size() > MAX_LINE_LENGTH)
+           {
+             std::string::size_type pos = str.rfind(' ');
+             if (pos == std::string::npos)
+               pos = MAX_LINE_LENGTH;
+
+             buffer.push_back(str.substr(0, pos));
+             buffer.pop_front();
+             
+             str = str.substr(pos, str.size());
+           }
+           
+          buffer.push_back(str);
+          buffer.pop_front();
+          str = "";
        }
     }
     
-  if (!str.empty ())
-    buffer.push_back (str);
-
-  setp (char_buffer, char_buffer + CONSOLE_BUFFER_SIZE - 1);
-  return 0;
+  return str;
 }
 
-std::vector<std::string>* 
+const std::list<std::string>&
 ConsoleBuffer::get_buffer () {
-  return &buffer;
+  return buffer;
 }
 
-Console::Console()
-  : ostream (&streambuf)
+Console::Console() : std::ostream (&streambuf)
 {
   is_init = false;
   is_visible = false;
@@ -148,18 +155,24 @@
                        CL_Display::get_height(),
                        0.0, 0.0, 0.0, 0.5);
 
-  std::vector<std::string>* buffer = streambuf.get_buffer ();
+  const std::list<std::string>& buffer = streambuf.get_buffer ();
 
-  int window_start = Math::max(0, (int)(buffer->size ()) - number_of_lines);
+  unsigned int window_start = Math::max(0, static_cast<int>(buffer.size() - 
number_of_lines - current_pos));
 
-  for (int i = 0; 
-       i < number_of_lines
-        && i + window_start < int(buffer->size ()); 
-       ++i)
+  std::list<std::string>::const_iterator it = buffer.begin();
+  
+  // move iterator to the first line to be displayed
+  for (unsigned int i = 0; i < window_start; ++i)
+    ++it;
+  
+  for (unsigned int i = 0; 
+       i < number_of_lines && i + window_start < buffer.size(); 
+       ++it, ++i)
     {
       font->print_left(10, 
                       start_y_pos + (i * (font->get_height() + 2)), 
-                      (*buffer)[i + window_start].c_str());
+                      it->c_str()
+                     );
     }
 }
 
@@ -179,15 +192,15 @@
 void 
 Console::scroll_up()
 {
-  if (current_pos - number_of_lines > 0)
-    --current_pos;
+  if (current_pos + number_of_lines < streambuf.get_buffer().size())
+    ++current_pos;
 }
 
 void
 Console::scroll_down()
 {
-  if (current_pos - number_of_lines < (int)streambuf.get_buffer ()->size())
-    ++current_pos;
+  if (current_pos)
+    --current_pos;
 }
 
 void

Index: console.hxx
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/console.hxx,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- console.hxx 26 Jun 2002 16:49:33 -0000      1.5
+++ console.hxx 17 Aug 2002 17:21:25 -0000      1.6
@@ -20,20 +20,23 @@
 #ifndef HEADER_PINGUS_CONSOLE_HXX
 #define HEADER_PINGUS_CONSOLE_HXX
 
+#include <list>
 #include <string>
-#include <vector>
 #include <iostream>
 #include "display.hxx"
 
 class CL_Font;
 
-#define CONSOLE_BUFFER_SIZE 1024
-
 class ConsoleBuffer :
   public std::streambuf
 {
 private:
-  std::vector<std::string> buffer;
+
+  enum { CONSOLE_BUFFER_SIZE = 200 };
+  enum { NUM_LINES           = 100 };
+  enum { MAX_LINE_LENGTH     = 80  };
+
+  std::list<std::string> buffer;
   char char_buffer[CONSOLE_BUFFER_SIZE];
   
 public:
@@ -41,7 +44,11 @@
   virtual ~ConsoleBuffer ();
   int overflow (int c);  
   int sync ();
-  std::vector<std::string>* get_buffer ();
+  const std::list<std::string>& get_buffer ();
+  
+private:
+  /// helper function used by overflow and sync
+  std::string fill_buffer (bool append);
 };
 
 /** A "Quake" like console, but it can just handle output, you can't
@@ -52,15 +59,14 @@
 {
 private:
   ConsoleBuffer streambuf;
-  std::vector<std::string>* buffer;
 
 public:
   CL_Font* font;
   bool is_init;
-  int  current_pos;
+  unsigned int current_pos;
 
   /** number of lines which will get displayed on the screen */
-  int number_of_lines;
+  unsigned int number_of_lines;
   void draw();
 public:
   Console ();
@@ -103,7 +109,3 @@
 #endif
 
 /* EOF */
-
-
-
-

Index: debug_stream.cxx
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/debug_stream.cxx,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- debug_stream.cxx    21 Jun 2002 08:56:45 -0000      1.3
+++ debug_stream.cxx    17 Aug 2002 17:21:25 -0000      1.4
@@ -20,7 +20,6 @@
 #include "debug_stream.hxx"
 #include "globals.hxx"
 
-using namespace std;
 
 NilStream DebugStream::nilstream;
 
@@ -66,7 +65,7 @@
 {
   if (!out_streams.empty ())
     {
-      for (std::vector<ostream*>::iterator i = out_streams.begin ();
+      for (std::vector<std::ostream*>::iterator i = out_streams.begin ();
            i != out_streams.end (); ++i)
        {
          *(*i) << line;
@@ -95,17 +94,15 @@
        }
     }
     
-  if (!str.empty ()) // Why to we check this?
-    {
-      put_line (str);
-    }
+  if (!str.empty ())
+    put_line (str);
 
   setp (char_buffer, char_buffer + buffersize - 1);
   return 0;
 }
 
 void
-DebugStream::Buffer::add (ostream& s)
+DebugStream::Buffer::add (std::ostream& s)
 {
   out_streams.push_back (&s);
 }
@@ -122,7 +119,7 @@
 
 
 DebugStream::DebugStream (const std::string& prefix)
-  : ostream (&buffer),
+  : std::ostream (&buffer),
     buffer (prefix)
 {
 }
@@ -132,7 +129,7 @@
 }
 
 /// returns self if the debug flag is set, else nilstream
-ostream & DebugStream::operator () (int component) {
+std::ostream & DebugStream::operator () (int component) {
 
   if (pingus_debug_flags & component) {
     return *this;
@@ -142,7 +139,7 @@
 }
 
 void
-DebugStream::add (ostream& s)
+DebugStream::add (std::ostream& s)
 {
   buffer.add (s);
 }
@@ -155,7 +152,7 @@
 
 
 NilStream::NilStream ()
-  : ostream(&buffer) 
+  : std::ostream(&buffer) 
 { 
 }
 

Index: debug_stream.hxx
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/debug_stream.hxx,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- debug_stream.hxx    16 Aug 2002 13:03:34 -0000      1.6
+++ debug_stream.hxx    17 Aug 2002 17:21:25 -0000      1.7
@@ -36,13 +36,11 @@
   {
   private:
 
-    // unnessecary complex in this case
-    // static const int buffersize;
-    enum { buffersize = 200 };
+    enum { buffersize = 1024 };
 
     std::vector<std::ostream*> out_streams;
 
-    char char_buffer[200];
+    char char_buffer[buffersize];
     std::string prefix;
   
   public:
@@ -69,7 +67,7 @@
   DebugStream (const std::string& prefix);
   virtual ~DebugStream ();
 
-  std::ostream & operator () (int component);
+  std::ostream & operator() (int component);
 
   void add (std::ostream& s);
   void set_prefix (const std::string & prefix);
@@ -94,8 +92,8 @@
          NilBuffer () { setp(char_buffer, char_buffer + 3); setg(0,0,0); }
         ~NilBuffer () { }
         
-        int overflow () { return 0; }
-        int sync     () { return 0; }
+        int overflow (int) { return 0; }
+        int sync     ()    { return 0; }
     } buffer;
 
    NilStream ();





reply via email to

[Prev in Thread] Current Thread [Next in Thread]