简体   繁体   中英

Output text to a window console. C++

So at the moment I'm creating a basic game.

It has a background and a player character (which are both bmp's) which is being outputted to the window console successfully using the wc.Display function call.

I am curious about the method of displaying text to a window console, ie health, score, or how many lives the player has left, eg Pac-man, Pong etc. Much like how my sprite is on my game window, how would I do it for text?

I know the method of cout, but I don't think it's the same method used to display text to a game screen.

Any help would be much appreciated.

Assuming you don't actually mean console since you already know how to do a sprite but not text:

RECT where; // populate this
HGDIOBJ OldFont;
BeginPaint(hWnd, &hDC);
OldFont = SelectObject(hDC, yourfont);
DrawText(hDC, "text", 4, &where, 0);
SelectObject(hDC, OldFont);
EndPaint(hWnd, &hDC);

Screen oriented text i/o is not covered by the standard library's functionality.

It's very system dependent.

But, you can use a fairly portable library such as ncurses .


Old text terminals supported escape sequences for cursor positioning etc., and these were standardized at one point, “ANSI escape sequences”.

In Unix-land and with Windows 10 and later you can use them, but that doesn't cover single character (unbuffered) input.

So you need something like ncurses anyway.


For Windows you should perhaps consider wchar_t based direct console i/o via Windows API functions, in order to be able to output non-ASCII characters in general.

There is a specific function added into my header files which allows me to output text to the screen. I didn't not know it existed until I went searching for it. Thanks for the help guys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM