Sign in to follow this  
zerocide

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

Recommended Posts

zerocide    122
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.

Share this post


Link to post
Share on other sites
Telastyn    3777
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.

Share this post


Link to post
Share on other sites
IFooBar    906
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.

Share this post


Link to post
Share on other sites
Kristafarii    172
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");

Share this post


Link to post
Share on other sites
zerocide    122
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 :)

Share this post


Link to post
Share on other sites
Telastyn    3777
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.]

Share this post


Link to post
Share on other sites
Kristafarii    172
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.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this