From: Christoph Sommer Date: Thu, 6 Apr 2006 01:46:31 +0000 (+0000) Subject: Simple Console X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=9e5732a3c816561abfa93882860eb4a3e8e104c1;p=supertux.git Simple Console SVN-Revision: 3248 --- diff --git a/data/images/engine/console.jpg b/data/images/engine/console.jpg new file mode 100644 index 000000000..fe400bb6b Binary files /dev/null and b/data/images/engine/console.jpg differ diff --git a/src/console.cpp b/src/console.cpp new file mode 100644 index 000000000..d5e34d928 --- /dev/null +++ b/src/console.cpp @@ -0,0 +1,83 @@ +// $Id: worldmap.cpp 3209 2006-04-02 22:19:22Z sommer $ +// +// SuperTux - Console +// Copyright (C) 2006 Christoph Sommer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include "console.hpp" +#include "video/drawing_context.hpp" +#include "video/surface.hpp" +#include "player_status.hpp" +#include "main.hpp" +#include "resources.hpp" + +namespace { + int ticks; // TODO: use a clock? +} + +Console::Console(DrawingContext* context) : context(context) +{ + background = new Surface("images/engine/console.jpg"); +} + +Console::~Console() +{ + delete background; +} + +void +Console::flush() +{ + lines.push_front(outputBuffer.str()); + if (lines.size() >= 256) lines.pop_back(); + if (height < 64) { + if (height < 4) height=4; + height+=9; + } + ticks=120; + std::cerr << outputBuffer.str() << std::flush; + outputBuffer.str(std::string()); +} + +void +Console::draw() +{ + if (height == 0) return; + if (ticks-- < 0) { + height-=1; + ticks=0; + if (height < 0) height=0; + } + if (height == 0) return; + + context->draw_surface(background, Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), LAYER_FOREGROUND1+1); + + int lineNo = 0; + for (std::list::iterator i = lines.begin(); i != lines.end(); i++) { + lineNo++; + float py = height-4-lineNo*9; + if (py < -9) break; + context->draw_text(white_small_text, *i, Vector(BORDER_X, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1); + } +} + +int Console::height = 0; +std::list Console::lines; +ConsoleStreamBuffer Console::outputBuffer; +std::ostream Console::output(&Console::outputBuffer); + diff --git a/src/console.hpp b/src/console.hpp new file mode 100644 index 000000000..880ccb7fd --- /dev/null +++ b/src/console.hpp @@ -0,0 +1,66 @@ +// $Id: worldmap.hpp 3209 2006-04-02 22:19:22Z sommer $ +// +// SuperTux - Console +// Copyright (C) 2006 Christoph Sommer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef SUPERTUX_CONSOLE_H +#define SUPERTUX_CONSOLE_H + +#include +#include +#include +#include + +class Console; +class ConsoleStreamBuffer; +class DrawingContext; +class Surface; + +class Console +{ + public: + Console(DrawingContext* context); + ~Console(); + + static std::ostream output; + + static void flush(); + + void draw(); + + protected: + static std::list lines; + DrawingContext* context; + Surface* background; + static int height; + + static ConsoleStreamBuffer outputBuffer; +}; + +class ConsoleStreamBuffer : public std::stringbuf +{ + public: + int sync() + { + Console::flush(); + return std::stringbuf::sync(); + } +}; + +#endif + + diff --git a/src/game_session.cpp b/src/game_session.cpp index b38c8400d..cbad8be7d 100644 --- a/src/game_session.cpp +++ b/src/game_session.cpp @@ -89,6 +89,7 @@ GameSession::GameSession(const std::string& levelfile_, GameSessionMode mode, fps_fps = 0; context = new DrawingContext(); + console = new Console(context); restart_level(); } @@ -163,6 +164,7 @@ GameSession::~GameSession() delete end_sequence_controller; delete level; delete context; + delete console; current_ = NULL; } @@ -238,6 +240,7 @@ GameSession::levelintro() if(best_level_statistics != NULL) best_level_statistics->draw_message_info(context, _("Best Level Statistics")); + console->draw(); context.do_drawing(); wait_for_event(1.0, 3.0); @@ -490,6 +493,7 @@ GameSession::draw() Menu::current()->draw(*context); } + console->draw(); context->do_drawing(); } @@ -802,6 +806,7 @@ GameSession::drawresultscreen() sprintf(str, _("COINS: %d"), player_status->coins); context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1); + console->draw(); context.do_drawing(); wait_for_event(2.0, 5.0); diff --git a/src/game_session.hpp b/src/game_session.hpp index c5ed8259f..fced5d2d1 100644 --- a/src/game_session.hpp +++ b/src/game_session.hpp @@ -26,6 +26,7 @@ #include "timer.hpp" #include "statistics.hpp" #include "math/vector.hpp" +#include "console.hpp" /* GameLoop modes */ enum GameSessionMode { @@ -156,6 +157,7 @@ private: std::string capture_file; std::istream* playback_demo_stream; CodeController* demo_controller; + Console* console; }; std::string slotinfo(int slot); diff --git a/src/msg.hpp b/src/msg.hpp index 7f7113dd2..fa6cb0d9e 100644 --- a/src/msg.hpp +++ b/src/msg.hpp @@ -18,25 +18,29 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. -#ifndef __SUPERTUX_DEBUG_H__ -#define __SUPERTUX_DEBUG_H__ +#ifndef __SUPERTUX_MSG_H__ +#define __SUPERTUX_MSG_H__ #include #include +#include "console.hpp" + +// TODO: make macros more C++ish? + #ifdef DEBUG -#define msg_debug(message) std::cerr << "[DEBUG] " << __FILE__ << " l." << __LINE__ << ": " << message << std::endl -#define msg_info(message) std::cout << "[INFO] " << message << std::endl -#define msg_warning(message) std::cerr << "[WARNING] " << __FILE__ << " l." << __LINE__ << ": " << message << std::endl -#define msg_fatal(message) std::cerr << "[FATAL] " << __FILE__ << " l." << __LINE__ << ": " << message << std::endl +#define msg_debug(message) Console::output << "[DEBUG] " << __FILE__ << " l." << __LINE__ << ": " << message << std::endl +#define msg_info(message) Console::output << "[INFO] " << message << std::endl +#define msg_warning(message) Console::output << "[WARNING] " << __FILE__ << " l." << __LINE__ << ": " << message << std::endl +#define msg_fatal(message) Console::output << "[FATAL] " << __FILE__ << " l." << __LINE__ << ": " << message << std::endl #else #define msg_debug(message) -#define msg_info(message) std::cout << message << std::endl -#define msg_warning(message) std::cerr << "Warning: " << message << std::endl -#define msg_fatal(message) std::cerr << "Fatal: " << message << std::endl +#define msg_info(message) Console::output << message << std::endl +#define msg_warning(message) Console::output << "Warning: " << message << std::endl +#define msg_fatal(message) Console::output << "Fatal: " << message << std::endl #endif diff --git a/src/title.cpp b/src/title.cpp index f4d6ea084..e5e5aa58d 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -61,6 +61,7 @@ #include "main.hpp" #include "exceptions.hpp" #include "msg.hpp" +#include "console.hpp" static Surface* bkg_title; static Surface* logo; @@ -75,6 +76,8 @@ static std::vector contrib_subsets; static LevelSubset* current_contrib_subset = 0; static int current_subset = -1; +static Console* console; + /* If the demo was stopped - because game started, level editor was excuted, etc - call this when you get back to the title code. @@ -317,6 +320,9 @@ void title() Menu::set_current(main_menu); DrawingContext& context = *titlesession->context; + + console = new Console(&context); + bool running = true; while (running) { @@ -439,6 +445,8 @@ void title() Menu::set_current(main_menu); } + console->draw(); + context.do_drawing(); sound_manager->update(); @@ -453,5 +461,6 @@ void title() delete titlesession; delete bkg_title; delete logo; + delete console; //delete img_choose_subset; } diff --git a/src/worldmap.cpp b/src/worldmap.cpp index b667aec26..a2f3ac04a 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -1027,6 +1027,7 @@ WorldMap::display() Uint32 lastticks = SDL_GetTicks(); DrawingContext context; + Console* console = new Console(&context); while(!quit) { Uint32 ticks = SDL_GetTicks(); float elapsed_time = float(ticks - lastticks) / 1000; @@ -1063,8 +1064,11 @@ WorldMap::display() Menu::current()->draw(context); } + console->draw(); context.do_drawing(); } + + delete console; } void diff --git a/src/worldmap.hpp b/src/worldmap.hpp index 782c212f4..24db45750 100644 --- a/src/worldmap.hpp +++ b/src/worldmap.hpp @@ -31,6 +31,7 @@ #include "timer.hpp" #include "tile_manager.hpp" #include "game_object.hpp" +#include "console.hpp" class Sprite; class Menu; @@ -123,6 +124,8 @@ private: TileMap* solids; TileManager* tile_manager; + + Console* console; public: struct SpecialTile