Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
114 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Creating a Scalable Console System with STL – Part 1


Usage

Overloading the class

This 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 keys

When 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 Variables

If 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 Commands

One 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.

Conclusion

Well 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
fcarreiro@fibertel.com.ar

Reference

If 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)
http://www.planetpdf.com/mainpage.asp?WebPageID=315

C++ Reference
http://www.cppreference.com

C++ Polymorphism
http://cplus.about.com/library/weekly/aa120602b.htm

C++ Virtual Functions
http://www.glenmccl.com/virt_cmp.htm

Virtual Destructors
http://cpptips.hyperformix.com/cpptips/why_virt_dtor2
http://cpptips.hyperformix.com/Ctordtor.html

Unions and Data Types
http://www.cplusplus.com/doc/tutorial/tut3-6.html
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/class_29.asp

Function Pointers
http://www.function-pointer.org/

Standard Template Library (please buy a book for this!)
http://www.cs.brown.edu/people/jak/proglang/cpp/stltut/tut.html
http://www.xraylith.wisc.edu/~khan/software/stl/STL.newbie.html
http://www.yrl.co.uk/~phil/stl/stl.htmlx

Passing by Reference
http://www.hermetic.ch/cfunlib/ast_amp.htm
http://www.cs.iastate.edu/~leavens/larchc++manual/lcpp_64.html#SEC64

Constructor Initializer Lists
http://www.blueturnip.com/projects/edu/cs/cpp/initializer-lists.html





Contents
  Introduction
  Planning the Console
  Console Core
  Usage & Conclusion

  Source code
  Printable version
  Discuss this article