What's the best way to add console to a game?

Started by
6 comments, last by Kristafarii 19 years ago
Hello, I've made a small game in OpenGL and just thinking what would be the best way to add a console to it where the user can type commands... for example in halflife when you press ~ (by default) you get a console, so what would be the best way to implement something similar into a game? One way I can think of would be to use OpenGL to display text in it and then add keyboard input checking, but that seems a little too much for something this basic. Is there a better way? Thank you.
Advertisement
I'm not sure how else you'd do it, beyond perhaps using one someone else already wrote.

I've one I wrote up myself, which follows me from project to project. Quite useful.
you can check out how I implemented the console feature in the dx utility engine (in my sig), source code fully included. The FuBi article written by Scott Bilas (can be found on his site and in game programming gems 1) came in REAL handy for the purpose of a console.
[size=2]aliak.net
I've created a really simple console that I've used and modified from game to game. I'm only using it for output but it could be easily modifed to accept input. Basically it started as a linked list of strings.

The console writes the last 'n' number of lines to the top half of the screen using GDI text. (Where 'n' is the number of lines that you want written to the screen.)

I added scroll up and down features using keypad - and +.

The whole thing is implemented as a class so all I need to do is code something like:

pMsgCon->add("What ever..");

or

pMsgCon->addError("Nasty");
How would you accept input?

Have a variable like:
char currentInput[64]

... and then when there's a keypress detected, append the character to the end of that string and display it in DrawGLScene() ? Also, I like the idea of using a separate class for it, thanks :)
You can use this article: Creating a Generic Object Factory as an example of the concept to making dyamic menus. As in, being able to type in a string and have a function called [smile] After that you can just get input by lines, then process it like that.
Quote:Original post by zerocide
How would you accept input?

Have a variable like:
char currentInput[64]

... and then when there's a keypress detected, append the character to the end of that string and display it in DrawGLScene() ? Also, I like the idea of using a separate class for it, thanks :)


Pretty much. And when enter is detected, clear the string and send it off to a seperate function to parse and deal with it.

Similar to Kristafarii, the console is a class which takes text in an ->add() sort of way. This allows you to use a different console class for non-windows apps which just pushes the text to cout.

[edit: and actually, my console is just a specialization of a (text)list and a textinput. The list class already defines the arrow keys and pgup/pgdown for scrolling. The textinput class sets the textinput and overrides some of the ins/del/left/right inputs. Code re-use is good.]
When you call the method that draws the message console.
(I put this after all the 3d stuff had been rendered to the backbuffer - so that the message console is drawn on top with a transparent background)
Check for key_down on the keys you want to scroll up or down.

If these keys are used for other things in the program you may need to do some trickery to let the main program know that these keystrokes have been handled.

If you want command line input into your console (something I haven't needed to bother yet but will have to do soon...) you will definately need to let the main program know the keystrokes have been handled.

I suggest a class called KeyBoardInput that is fed all the keystroke (in order) at the start of each game loop. These keystrokes can then be read by other classes if they need to, and can also be cleared by these classes if need be. This way you can read the keystrokes into the message console - only if you are in the process of entering a command (or if the message console visibale.)

This topic is closed to new replies.

Advertisement