Creating a Scalable Console System with STL – Part 1
UsageOverloading the classThis system is only useful if extended, it is only a base and it must be used as a new class, it must be completed with new functions and a new context. Now I will briefly explain how to do this but we'll focus on this topic in the next part of this article so check this site periodically to see if its online ;) class text_console : public console { text_console(); ~text_console(); virtual void render(); }; void text_console::render() { ... // use the text-buffers to render or print some text to the screen print(m_textBuffer); ... } Passing keysWhen you detect a keypress by using any means that you want (could be DirectInput, SDL or whatever) you have to pass it to the console for it to act properly, here's a pseudo-code: char c = get_keypress(); switch(c) { case BACKSPACE: console->passBackspace(); break; case INTRO: console->passIntro(); break; default: console->passKey(c); break; } This is just an example of how to switch the key input and send it to the console. Adding VariablesIf you want the user to be able to change or query a memory variable by writing its name in the console then you can add it to the list in the following way: static std::string user_name; console->addItem("user_name", &user_name, CTYPE_STRING); That's all ;) Adding CommandsOne of the strong points of a console is that it lets the user execute commands, by adding them to the list you can easily make the console pass a list of arguments to the hook function. void print_args(const std::vector<std::string> & args) { for(int i = 0; i < args.size(); ++i) { console->print(args[i]); console->print(", "); } console->print("\n"); } void initialize() { ... console->addItem("/print_args", print_args, CTYPE_FUNCTION); ... } After adding the command when the user types "/print_args 1 2 hello" the console would output "1, 2, hello". This is just a simple example of how to access the arguments vector. ConclusionWell well, what have we learned? Now you can design, code and use an extensible and complex console system that uses STL containers for efficiency and stability. In this part of the article we created the base class for the console system and in further articles we will discuss how to create a *REAL* text-console system and compile it. We'll also probably create the typical "Quake" style console that we all love… and want. The uses of this systems are infinite, the only limit is your imagination (*wink*). You can check the attached code here to help you understand the system we tried to design. NEVER copy-paste this code or any code because it will be no good for you, the best you can do is to understand it, understand how and why it works and rewrite it or copy the example and adjust it to your needs. Thank you very much for reading this article and I hope it is helpful to you and you use your new knowledge to make amazing new games to have fun, for hobby, or for money… You have the power, use it wisely… Facundo Matias Carreiro
ReferenceIf you had a hard time reading this article then I recommend you to read a good C/C++ book and some articles/tutorials on the topics discussed in this article. I will now provide you of some links, they may not be the best way to learn this but they are free and they are online. I strongly recommend buying some books if you can afford them, for those who can't (like me) here are the links… Thinking in C++ (e-book)
C++ Reference
C++ Polymorphism
C++ Virtual Functions
Virtual Destructors
Unions and Data Types
Function Pointers
Standard Template Library (please buy a book for this!)
Passing by Reference
Constructor Initializer Lists
|
|