Skip to main content
GameDev.net gamedev.net
🔒 Locked

[redacted anon]

Started by Jemburula Nov 21, 2005 at 1:29 AM 8 replies 1.9k views
Original Post
Jemburula
Jemburula

[redacted anon]

What we do in life... Echoes in eternity
CraZeE
CraZeE
u didnt mention the OS you're running on so its a gamble. If u're using windows (highly likely), then there's a collection of functions on console formatting. However, these use the Win32 API and might be a bit complicated at first.

For exact details, check out MSDN.
To learn, we share... Give some to take some -
Jemburula
Jemburula
Yes Windows XP. Thanks for the info, keep it coming everyone but I will look into it. Thanks :)
What we do in life... Echoes in eternity
cignox1
cignox1
unders dos/windows there is (there was) a non-standard library called conio.h that let you handle coloured texts (as well as other things). I don't know if it is providen with all compilers, nor if it is supported anymore.
Other than that (if you can) you could use win32 functions.

EDIT: if you want to skip win32 functions and you're using mingw, then you may try this conio library.
Undeadlnsanity
Undeadlnsanity
You can use SetConsoleTextAttribute() to change the colour of console text in Win32, but you'll have to include the window.h header.

Take a look at this quick example I made:
#include <iostream>#include <windows.h>using namespace std;int main(){    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);    cout<<"Moo\n";        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);    cout<<"Moo\n";        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);    cout<<"Moo\n";        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);    cout<<"Moo\n";        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED);    cout<<"Moo\n";        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);    cout<<"Moo\n";        cin.get();        return 0;}

As you can see, changing the 2nd parameter changes the colour of the text. Use an RGB colour mixing chart to see which colours used together will make what.

Good luck!
Jemburula
Jemburula
Ah ha! that was perfect. Much appreciated Undead my friend, thank you all for your help but that was fairly simple, I've always tended to avoid things like that in C++ because many I've heard believe such Windows functions are slow, over complicated, etc. But this suits my purpose nicely :) Thankyou!
What we do in life... Echoes in eternity
GDKnight
GDKnight
More colors!!
#include <iostream>#include <windows.h>using namespace std;enum ConsoleTextColor{	GREY,	WHITE,	RED,	GREEN,	BLUE,	PURPLE,	CYAN,	BRIGHT_RED,	BRIGHT_GREEN,	BRIGHT_BLUE,	LAVANDER,	AQUA,	YELLOW,	GOLD};void SetConsoleTextColor(ConsoleTextColor cl){	WORD newColor;	HANDLE ScreenBuffer = GetStdHandle(STD_OUTPUT_HANDLE);	switch(cl)	{	  case GREY:		  newColor = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;		  break;	  case WHITE:		  newColor = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY;		  break;	  case RED:		  newColor = FOREGROUND_RED;		  break;  	  case GREEN:		  newColor = FOREGROUND_GREEN;		  break;  	  case BLUE:		  newColor = FOREGROUND_BLUE;		  break;	  case PURPLE:		  newColor = FOREGROUND_RED | FOREGROUND_BLUE;		  break;	  case CYAN:		  newColor = FOREGROUND_GREEN | FOREGROUND_BLUE;		  break;	  case BRIGHT_RED:		  newColor = FOREGROUND_RED | FOREGROUND_INTENSITY;		  break;	  case BRIGHT_GREEN:		  newColor = FOREGROUND_GREEN | FOREGROUND_INTENSITY;		  break;	  case BRIGHT_BLUE:		  newColor = FOREGROUND_BLUE | FOREGROUND_INTENSITY;		  break;	  case LAVANDER:		  newColor = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;		  break;	  case AQUA:		  newColor = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;		  break;	  case YELLOW:		  newColor = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;		  break;	  case GOLD:		  newColor = FOREGROUND_RED | FOREGROUND_GREEN;		  break;	}	SetConsoleTextAttribute(ScreenBuffer, newColor);}int main(){	ConsoleTextColor textColor;	for(int i = 0; i<=GOLD; i++)	{		textColor = (ConsoleTextColor)i;		SetConsoleTextColor(textColor);		cout << "Moo." << endl;	}    return 0;}
GDKnight
Jemburula
Jemburula
Awesome! BTW, does anyone have any thoughts on a LOCATE statement equivelent? If you've never used QBASIC before it essentially just sets the "cursor" or "pointer" to which ever Row and Column you want on screen and then when you next print it writes from there?
What we do in life... Echoes in eternity
tychon
tychon
This link is to the complete reference of the Win32 console API. It should be able to handle any such questions.

Very handy stuff for Windows ASCII games.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.