d5e34d9287ed34d62602cfac1290314a19e564d8
[supertux.git] / src / console.cpp
1 //  $Id: worldmap.cpp 3209 2006-04-02 22:19:22Z sommer $
2 //
3 //  SuperTux - Console
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21 #include <iostream>
22 #include "console.hpp"
23 #include "video/drawing_context.hpp"
24 #include "video/surface.hpp"
25 #include "player_status.hpp"
26 #include "main.hpp"
27 #include "resources.hpp"
28
29 namespace {
30   int ticks; // TODO: use a clock?
31 }
32
33 Console::Console(DrawingContext* context) : context(context) 
34 {
35   background = new Surface("images/engine/console.jpg");
36 }
37
38 Console::~Console() 
39 {
40   delete background;
41 }
42
43 void 
44 Console::flush() 
45 {
46  lines.push_front(outputBuffer.str());
47  if (lines.size() >= 256) lines.pop_back();
48  if (height < 64) {
49    if (height < 4) height=4;
50    height+=9;
51  }
52  ticks=120;
53  std::cerr << outputBuffer.str() << std::flush;
54  outputBuffer.str(std::string());
55 }
56
57 void 
58 Console::draw() 
59 {
60   if (height == 0) return;
61   if (ticks-- < 0) {
62     height-=1;
63     ticks=0;
64     if (height < 0) height=0;
65   }
66   if (height == 0) return;
67
68   context->draw_surface(background, Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), LAYER_FOREGROUND1+1);
69
70   int lineNo = 0;
71   for (std::list<std::string>::iterator i = lines.begin(); i != lines.end(); i++) {
72     lineNo++;
73     float py = height-4-lineNo*9;
74     if (py < -9) break;
75     context->draw_text(white_small_text, *i, Vector(BORDER_X, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1);
76   }
77 }
78
79 int Console::height = 0;
80 std::list<std::string> Console::lines;
81 ConsoleStreamBuffer Console::outputBuffer;
82 std::ostream Console::output(&Console::outputBuffer);
83