X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fconsole.hpp;h=a128d607dbe0845188efa1cd89650ef105ac5b30;hb=d84d73b701cc7fa2bd74f3490b9be1bf8b6f705a;hp=1863309f85503d1877a50185f79f592041677469;hpb=f3e5e57c9996168a4889ae8e195be25f8b7e629b;p=supertux.git diff --git a/src/console.hpp b/src/console.hpp index 1863309f8..a128d607d 100644 --- a/src/console.hpp +++ b/src/console.hpp @@ -1,5 +1,5 @@ -// $Id: worldmap.hpp 3209 2006-04-02 22:19:22Z sommer $ -// +// $Id$ +// // SuperTux - Console // Copyright (C) 2006 Christoph Sommer // @@ -12,7 +12,7 @@ // 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. @@ -26,79 +26,104 @@ #include #include #include +#include class Console; class ConsoleStreamBuffer; class ConsoleCommandReceiver; class DrawingContext; class Surface; +class Font; class Console { - public: - Console(); - ~Console(); - - static std::ostream input; /**< stream of keyboard input to send to the console. Do not forget to send std::endl or to flush the stream. */ - static std::ostream output; /**< stream of characters to output to the console. Do not forget to send std::endl or to flush the stream. */ - - static void backspace(); /**< delete last character sent to the input stream */ - static void scroll(int offset); /**< scroll console text up or down by @c offset lines */ - static void autocomplete(); /**< autocomplete current command */ - - void draw(DrawingContext& context); /**< draw the console in a DrawingContext */ - static void show(); /**< display the console */ - static void hide(); /**< hide the console */ - static void toggle(); /**< display the console if hidden, hide otherwise */ - - static bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */ - static void registerCommand(std::string command, ConsoleCommandReceiver* ccr); /**< associate command with the given CCR */ - static void unregisterCommand(std::string command, ConsoleCommandReceiver* ccr); /**< dissociate command and CCR */ - static void unregisterCommands(ConsoleCommandReceiver* ccr); /**< dissociate all commands of given CCR */ - - template static bool string_is(std::string s) { - std::istringstream iss(s); - T i; - if ((iss >> i) && iss.eof()) { - return true; - } else { - return false; - } +public: + Console(); + ~Console(); + + static Console* instance; + + static std::ostream input; /**< stream of keyboard input to send to the console. Do not forget to send std::endl or to flush the stream. */ + static std::ostream output; /**< stream of characters to output to the console. Do not forget to send std::endl or to flush the stream. */ + + void init_graphics(); + + void backspace(); /**< delete last character sent to the input stream */ + void scroll(int offset); /**< scroll console text up or down by @c offset lines */ + void autocomplete(); /**< autocomplete current command */ + void show_history(int offset); /**< move @c offset lines forward through history; Negative offset moves backward */ + + void draw(DrawingContext& context); /**< draw the console in a DrawingContext */ + void update(float elapsed_time); + + void show(); /**< display the console */ + void hide(); /**< hide the console */ + void toggle(); /**< display the console if hidden, hide otherwise */ + + bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */ + void registerCommand(std::string command, ConsoleCommandReceiver* ccr); /**< associate command with the given CCR */ + void unregisterCommand(std::string command, ConsoleCommandReceiver* ccr); /**< dissociate command and CCR */ + void unregisterCommands(ConsoleCommandReceiver* ccr); /**< dissociate all commands of given CCR */ + + template static bool string_is(std::string s) { + std::istringstream iss(s); + T i; + if ((iss >> i) && iss.eof()) { + return true; + } else { + return false; } + } - template static T string_to(std::string s) { - std::istringstream iss(s); - T i; - if ((iss >> i) && iss.eof()) { - return i; - } else { - return T(); - } + template static T string_to(std::string s) { + std::istringstream iss(s); + T i; + if ((iss >> i) && iss.eof()) { + return i; + } else { + return T(); } + } - protected: - static std::list lines; /**< backbuffer of lines sent to the console */ - static std::map > commands; /**< map of console commands and a list of associated ConsoleCommandReceivers */ - Surface* background; /**< console background image */ - Surface* background2; /**< second, moving console background image */ - static int backgroundOffset; /**< current offset of scrolling background image */ - static int height; /**< height of the console in px */ - static int offset; /**< decrease to scroll text up */ - static bool focused; /**< true if console has input focus */ - - static ConsoleStreamBuffer inputBuffer; /**< stream buffer used by input stream */ - static ConsoleStreamBuffer outputBuffer; /**< stream buffer used by output stream */ - - static void addLine(std::string s); /**< display a line in the console */ - static void parse(std::string s); /**< react to a given command */ - - /** execute squirrel script and output result */ - static void execute_script(const std::string& s); +private: + std::list history; /**< command history. New lines get added to back. */ + std::list::iterator history_position; /**< item of command history that is currently displayed */ + std::list lines; /**< backbuffer of lines sent to the console. New lines get added to front. */ + std::map > commands; /**< map of console commands and a list of associated ConsoleCommandReceivers */ + + std::auto_ptr background; /**< console background image */ + std::auto_ptr background2; /**< second, moving console background image */ + + HSQUIRRELVM vm; /**< squirrel thread for the console (with custom roottable) */ + HSQOBJECT vm_object; + + int backgroundOffset; /**< current offset of scrolling background image */ + float height; /**< height of the console in px */ + float alpha; + int offset; /**< decrease to scroll text up */ + bool focused; /**< true if console has input focus */ + std::auto_ptr font; + float fontheight; /**< height of the font (this is a separate var, because the font could not be initialized yet but is needed in the addLine message */ + + float stayOpen; + + static ConsoleStreamBuffer inputBuffer; /**< stream buffer used by input stream */ + static ConsoleStreamBuffer outputBuffer; /**< stream buffer used by output stream */ + + void addLines(std::string s); /**< display a string of (potentially) multiple lines in the console */ + void addLine(std::string s); /**< display a line in the console */ + void parse(std::string s); /**< react to a given command */ + + /** ready a virtual machine instance, creating a new thread and loading default .nut files if needed */ + void ready_vm(); + + /** execute squirrel script and output result */ + void execute_script(const std::string& s); - static bool consoleCommand(std::string command, std::vector arguments); /**< process internal command; return false if command was unknown, true otherwise */ + bool consoleCommand(std::string command, std::vector arguments); /**< process internal command; return false if command was unknown, true otherwise */ - friend class ConsoleStreamBuffer; - static void flush(ConsoleStreamBuffer* buffer); /**< act upon changes in a ConsoleStreamBuffer */ + friend class ConsoleStreamBuffer; + void flush(ConsoleStreamBuffer* buffer); /**< act upon changes in a ConsoleStreamBuffer */ }; class ConsoleStreamBuffer : public std::stringbuf @@ -107,7 +132,8 @@ class ConsoleStreamBuffer : public std::stringbuf int sync() { int result = std::stringbuf::sync(); - Console::flush(this); + if(Console::instance != NULL) + Console::instance->flush(this); return result; } }; @@ -117,7 +143,7 @@ class ConsoleCommandReceiver public: virtual ~ConsoleCommandReceiver() { - Console::unregisterCommands(this); + Console::instance->unregisterCommands(this); } /**