Sign in to follow this  
vecchia

Create a Game Console

Recommended Posts

Hi all,
I would create a game console that allow me to type commands to control the game and to have a log view of what the program is doing. I tried to search over the net witouth results, someone could explain me how could I create it and how could I bind it to a key so that if i press a keyboard button it opens the console? Also, how could I have the console like in a normal game binded at the top of the main window?

Share this post


Link to post
Share on other sites
1. Detect the appropriate keypress for opening the console in your input routine (window procedure, etc.).
2. Create a console window. Redirect appropriate keyboard input to a console input function.
3. Input commands and store in a std::string.
4. Add the input string to, for example, std::vector<std::string> consoleStrings.
5. Display consoleStrings entries in the console window.
6. Pass the input string to a command interpreter to parse and execute as desired.

Share this post


Link to post
Share on other sites
It depends on what rendering/UI system you're using. It is essentially a multiline textbox with some logic to auto-scroll on add and a textbox for input (or of course your UI system's equivalent). The input will need some sort of interpreter to parse the text and do stuff based on it. Some people wire up Lua, some Python, some just parse it and go. Often times it's pushed into the game-internal scripting system or translated into an internal messaging system.

If you're doing it in-game (which is common), then that UI element just needs to be set top most in the z-order. tilde (or whatever) would then toggle the hide/show of that element. That tends to be a lot cleaner with regards to keeping track of log messages and history while it's not active.

...and depending on what you're looking for, or how far along the game is, you might be better off with more traditional logging/debugging methods.

Share this post


Link to post
Share on other sites
I'm creating a Win32 C++ application and I have a main window. I would attach the console at the top left of the window. Should i use AllocConsole or should I create a new window with CreateWindowEx command?

Share this post


Link to post
Share on other sites
[quote name='Stefano A.' timestamp='1298302125' post='4777044']
I'm creating a Win32 C++ application and I have a main window. I would attach the console at the top left of the window. Should i use AllocConsole or should I create a new window with CreateWindowEx command?
[/quote]
First you'll need to decide whether you want a Windows console (the kind you get when you create a console application), or a typical drop-down, in-game console as is common in many games.

Which of these are you wanting?

Share this post


Link to post
Share on other sites
the typical drop-down, in-game console. I searched and someone says to use CreateWindow, someone else says to use AllocConsole so I'm really confused

Share this post


Link to post
Share on other sites
[quote name='Stefano A.' timestamp='1298306544' post='4777088']
the typical drop-down, in-game console. I searched and someone says to use CreateWindow, someone else says to use AllocConsole so I'm really confused
[/quote]
AllocConsole() creates a separate console window of the type you get in a console application, and CreateWindow() creates a completely new window, neither of which is what you want.

Typically, an in-game console will be handled by the same systems that handle the rest of your game; in other words, it's just 'something else to update and render'. Specifically, it'll usually be rendered over the top of everything else, using an orthographic projection (again, typically).

You should be able to use whatever text rendering method you're using elsewhere. As for the logical part of it, see the suggestions from earlier in the thread.

Share this post


Link to post
Share on other sites
[quote name='Stefano A.' timestamp='1298310077' post='4777116']
do you have some examples of how the console could be rendered?
[/quote]
Not off hand, no.

But, there's really nothing special about it. If you can render other stuff, you can render a console. A typical approach, for example, would be as follows:

- Render everything but the console (this isn't the only way to do it, but it's probably the easiest)
- Set up an orthographic projection
- Turn off depth testing
- Render a quad for the background
- Render the text using a quad for each character

It's not quite trivial (and can get fairly involved depending on what features you want to support), but it's not too difficult.

If you're still not sure how to proceed, perhaps you could describe which of the above steps you're unsure of. (Also, do you already have a rendering system in place? If so, how do you render the other objects in your game?)

Share this post


Link to post
Share on other sites
My application simply render a 3D model so I alredy have a little engine that allow me to draw objects and rotate them but I don't know how to set up an orthographic projection and the other steps. Maybe if you have a tutorial or you can post a really simple code example I will study it. At the moment, I load a .x file with DirectX 9 and i draw it on the screen...

Share this post


Link to post
Share on other sites
I would first say you a big "THANK YOU" for the help you are giving me, I'm a beginner of DirectX programming so sorry if my answers are stupids :) now, how could i see if I'm using the programmable pipeline or the fixed-pipeline? At the moment, I have only a 3D model loaded by a .x file and drawn on the screen, no textures or lights neither shaders

Share this post


Link to post
Share on other sites
[quote]I'm a beginner of DirectX programming so sorry if my answers are stupids[/quote]
No worries - we all had to learn this stuff at one point or other :)

It sounds like you're using the fixed-function pipeline. With the D3D fixed-function pipeline, you set transforms using the SetTransform() function, which can be used to set the current world, view, and projection transforms. If you've got something basic up and running already, then you're probably already setting one or more of these.

For something like a console, you typically want to set the world and view transforms to identity, and set the projection transform to an orthographic projection. For the orthographic transform, you can use the actual screen dimensions, or set up a 'virtual space' for the console, whichever you prefer.

For creating said transform, check the DirectX math library documentation (the DX math library includes functions for creating orthographic projection transforms).

For examples, try searching the forums for e.g. 'gui orthographic'; this has been discussed quite a bit in the past, so you should be able to find some examples and/or references in previous threads.

Share this post


Link to post
Share on other sites
For basic 2D rendering, including text, the D3D[b]X[/b] part of the DX 9 library offers some functionality. You wanna look at ID3DXSprite, ID3DXFont and maybe ID3DXLine. Normally you don't even need to bother about transformations a lot, these objects will handle them automatically when used.

Share this post


Link to post
Share on other sites
[quote name='Stefano A.' timestamp='1298317421' post='4777174']
I really don't understand how could I draw 2D objects. Using sprites?
[/quote]

Just texture a rectangle (two triangles in DX I believe) black. Then use DX's text methods to type onto that rectangle.

Share this post


Link to post
Share on other sites
ok but I have another question: I can draw the rectangle, draw the text, etc but how could I allow the user to type commands inside the console?

Share this post


Link to post
Share on other sites
[quote name='Stefano A.' timestamp='1298318519' post='4777180']
ok but I have another question: I can draw the rectangle, draw the text, etc but how could I allow the user to type commands inside the console?
[/quote]

You (essentially) get to write your own UI if you're not using an engine or something that makes it easier.

When the console is active, you'll need to capture the keyboard input from the windows message pump. WM_CHAR if I remember correctly is the message type for text input. That input will need to be handled in such a way that it effects what text is being rendered. You'll also likely need to get messages for the return key, and probably for the tilde (or whatever else you're using to toggle the console).

Share this post


Link to post
Share on other sites
so what you are saying is that every keyboard button pressed must be wrote with DirectX on the console? Also, I drawn a really simple sprite for test but I need a texture, is this right? If so, how could I make the texture looking % transparent?

Share this post


Link to post
Share on other sites
[quote name='Stefano A.' timestamp='1298322121' post='4777205']
Also, I drawn a really simple sprite for test but I need a texture, is this right? If so, how could I make the texture looking % transparent?
[/quote]
I was a bit sloppy here, I thought one could easily use ID3DXSprite for arbitrary rectangles. You need a texture in any case, so you either

- use a texture which has the right size already
- or use the sprites SetTransform to layout it arbitrarily

To enable transparency call sprite->Begin(D3DXSPRITE_ALPHABLEND). The color parameter in sprite->Draw() lets you tint your texture, e.g. using half transparent white (if your texture isn't providing a proper alpha already).

[i]Or[/i] you could actually abuse ID3DXLine for rectangles, using a very thick SetWidth().

[quote]so what you are saying is that every keyboard button pressed must be wrote with DirectX on the console?[/quote]
Basically. Use Buckeye's setup and collect the characters. For drawing, you can even collect all lines by concatenating the strings [i]including[/i] the newlines, ID3DXFont will do the wrap for you, so you can draw all your text in one go (given you don't need it to be too fancy, like different colors for different lines).

Share this post


Link to post
Share on other sites
Most graphical languages I've used allow you to stretch a texture. If you don't care about it being transparent, couldn't you just make a 1 pixel texture that's black and stretch over the entire rectangle?

Share this post


Link to post
Share on other sites
rofl...why doesn't someone just tell the guy that he needs to look into the Win32 API, MFC, or QT (or something else).

A "console" window in a game where you can input cheats/dev commands would be nothing but a regular window.

1. Pull a new window over your main window with tilde (~). Give it half the height of the main game window. Give it a black background.
2. Pause the game when the console window is not hidden or inactive.
3. When the user presses a key, write it to the "console" window while at the same time saving it into a buffer.
4. Execute the command when the user presses enter.
5. Put some error checking....etc, etc.

I gave you some quick theory to get your mind thinking on what needs to be done. But there are a TON of ways to do this. This is actually the least of your problems if you are making a 3d GAME.

Share this post


Link to post
Share on other sites
[quote name='Fl4sh' timestamp='1298325911' post='4777246']
rofl...why doesn't someone just tell the guy that he needs to look into the Win32 API, MFC, or QT (or something else).

A "console" window in a game where you can input cheats/dev commands would be nothing but a regular window.[/quote]
Are you sure that's how it's typically done? (The games I'm familiar with that have in-game consoles don't do it that way.)

Share this post


Link to post
Share on other sites
[quote name='jyk' timestamp='1298326899' post='4777252']
[quote name='Fl4sh' timestamp='1298325911' post='4777246']
rofl...why doesn't someone just tell the guy that he needs to look into the Win32 API, MFC, or QT (or something else).

A "console" window in a game where you can input cheats/dev commands would be nothing but a regular window.[/quote]
Are you sure that's how it's typically done? (The games I'm familiar with that have in-game consoles don't do it that way.)
[/quote]

Indeed. Mine was done via DX via the in-game ui.

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