The easiest way to color text in C / C ++ DOS?

I made a basic snake game in DOS using turbo C ++ 3.0, and I am also a beginner myself. I was looking for some time for a very simple and perhaps elementary way to make text of different colors in a DOS window. I am not looking for complicated ways to color the text. Most of the programs that I write are extremely simple and straightforward, and complex code for colored text, which is larger than the program itself, will be confusing and inefficient.

My question is: what is the easiest way to colorize text in a DOS console in BOTH, C, and C ++?

+4
source share
4 answers

You can use only Turbo C / C ++ functions (i.e. non-standard) textcolor() , textbackground() and textattr() together with cprintf() , cputs() and putch() . See their description in the IDE help, all of which are in conio.h .

+5
source

If you activate ansi.sys , you can use ansi escape sequences.

I assume that you are using DOS on Windows because you are referring to the “DOS window”, so you need to enable ansi.sys before you can use it.

Turbo C ++ 3.0 does not come with Windows headers or libraries, so you won’t be able to use the Console API.

+7
source

Do you use DOS (e.g. DOSBox or DOS on Win 9x)?

If so, ANSI.SYS is very easy to use. You simply precede your text with control codes that set the color.

If not, (i.e. you are actually using the command prompt on Windows), use SetConsoleTextAttribute to set the foreground and background colors.

And I just remembered the 32-bit versions of Windows still support command.com , and this can load ANSI.SYS .

+2
source

If you write directly to the video text buffer, each screen symbol cell corresponds to a pair of bytes, one of which is a symbol to display, the other by its colors. See http://en.wikipedia.org/wiki/VGA_compatible_text_mode

It may seem a bit complicated at first, but it's actually quite simple. This is a bit unfamiliar. Using ANSI escape sequences requires a fairly small amount of output, while a text buffer is one 16-bit word per character.

Not that it mattered, but it was necessary to write a text buffer on the hardware of a past era to get the response changes. The transition through the ANSI interface took considerable time.

+1
source

Source: https://habr.com/ru/post/1392106/


All Articles