Here is the header file Console.h that has been included in the Engine project:
class Console { private: bool showing; Sprite *panel; Font *font; int currentLine; std::vector<std::string> textlines; std::vector<std::string>::iterator iter; public: Console(); virtual ~Console(); bool init(); void draw(); void clear(); void print(std::string text, int line = -1); bool isShowing() { return this->showing; } void show() { this->showing = true; } void hide() { this->showing = false; } void setShowing(bool value) { this->showing = value; } };
The implementation file Console.cpp is next. Note how the console automatically loads an image that is rendered with alpha transparency onto the game screen. This adds a dependency to all games that use the engine—the panel.tga file must be included. But that is to be expected; once an engine begins to incorporate new features, additional dependencies must be provided (such as font image and data files).
#include "Advanced2D.h" namespace Advanced2D { Console::Console() { showing = false; currentLine = 0; clear(); } Console::~Console() { delete font; delete panel; } bool Console::init() { //load the panel image panel = new Sprite(); if (!panel->loadImage("panel.tga")) return false; double scale = g_engine->getScreenWidth() / 640.0f; panel->setScale(scale); panel->setColor(0x99FFFFFF); //load the font font = new Font(); if (!font->loadImage("system12.tga")) return false; font->setColumns(16); font->setCharSize(14,16); if (!font->loadWidthData("system12.dat")) return false; return true; } void Console::draw() { int x = 5, y = 0; if (!showing) return; //draw panel background panel->draw(); //draw text lines for (unsigned int n = 0; n < textlines.size(); n++) { font->Print(x,y*14, textlines[n], 0xFF000000); y += 1; if (y > 26) { if (x > 10) break; x = g_engine->getScreenWidth()/2 + 5; y = 0; } } } void Console::print(std::string text, int line) { if (line > -1) currentLine = line; textlines[currentLine] = text; if (currentLine++ > 52) currentLine = 0; } void Console::clear() { for (int n=0; n<55; n++) textlines.push_back(""); } };