Advanced Color Text in console

Pages: 12
Is there any way to change the color in the console based on a boolean value?

What i want to do is for quest status

Make the string INACTIVE grey.
Make the string ACTIVE white.
Make the string COMPLETE green.


I can change the status with a boolean value, the question is how to toggle the colors??
To print a string in a specific color use SetConsoleTextAttribute (google it). If you want to change a color of some string without redrawing everything, I think you should take a look at ncurses lib.
I know how to change text color, im asking how to change it based on a variable.
If you want to redraw every time state changes write a setter function
1
2
3
4
void set_active(bool a){
    if(active != a) redraw();
    active = a;
}
1
2
// set text to dark green
mytextobject.SetColor( sf::Color( 0, 128, 0 ) );


oh wait... console...

=P



hint hint leave the console already http://cplusplus.com/forum/beginner/27978/#msg150950
@disch
I tried it and ran into nothing but trouble man i couldnt even get crap to compile! why mess with that when what i have is working fine, when this project is done maybe ill check it out but until then im stickin to my guns 3/4 of my main engine is in place soon its gonna be mostly all content and some fancy puzzles!

@hamsterman

Will that work with a tri-bool situtaton though and 3 colors
i have setup a switch system so that the player can activate a quest, cancel it or complete it. and it toggles the status based on 2 bools outa 3.

1
2
3
4
5
6
7
8
9
10
11
12
if ( (!IsQuest1Inactive) && (!IsQuest1Completed) )
	{
		IsQuest1VerifyState = QuestStateActive;
	}
	else if ( (!IsQuest1Inactive) && (!IsQuest1Active) )
	{
		IsQuest1VerifyState = QuestStateCompleted;
	}
	else if ( (!IsQuest1Active) && (!IsQuest1Completed) )
	{
		IsQuest1VerifyState = QuestStateInactive;
	}



VerifyState is the state of the quest, and each pre-defined string is QuestState
and the IsQuest's check the state
Last edited on
hello..if y understand correctly you're problem,y say u can use something like a switch case menu.
1
2
3
4
5
6
7
8
9
switch(Option){
	case 1:system("color 08");
		break;
	case 2:system("color 07");
		break;
	case 3:system("color 02");
		break;
	default: printf("Wrong option\n");
}


Now y am a beginner so if y am wrong or if you tried it before sry.it is just the best thing y could think of,to you're problem.
Last edited on
Snaef98 wrote:
I tried it and ran into nothing but trouble man i couldnt even get crap to compile!


I agree that getting it set up is a bit troublesome. But you only have to do it once, and it's well worth it.

why mess with that when what i have is working fine


Because what you're doing now is sinful and wretched. And in fact, the roundabout way you have to do things is ultimately making the overall process much more difficult for yourself than it would be to just take an hour or two to work out the kinks with getting SFML set up.

when this project is done maybe ill check it out


I suppose I can understand not wanting to abandon a project. But I would certainly hope that you would not do what you're doing again in any new project you start.
@Disch yeah i plan to try to get SFML working in the future. But since my program is pretty close to fully working other then adding more content im going to wait until its done, i wouldn't mind your assistance with setting up SFML though, i followed the tutorials to set it up and believe me i did it exactly as it said to, ive set up databases and ran private game servers so setting up a few settings for the compiler is childs play for me.

@hamsterman

not sure if you got my previous message, but won't what you described just reupdate the coloring when it changes, i want 3 seperate colors to be used on a single string which is altered based on which bool is currently set to true.
Last edited on
Little bit more about my system,


Right now i have a QuestLog display function, a QuestManager function and a QuestUpdater function the player loads the quest log to view the quests active, uses the manager to maintain, view the quest text and change stuff. The updater updates the status based on the actions in the manager. The only thing i need to figure out is how to change the string's color based on the bool. Its a 3 way switch using 2 strings and 3 bools. If anyone has any ideas id appreciate it. thanks
I accually have a really good idea, is there a way to specifically set a string to a certain color? i could use it in if else statement and add that to my updater function to update both the status and the color.
If you objectify it, yes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Text
{
private:
  std::string str;
  int color;

public:
  enum
  {
    Black = /*whatever code is black*/,
    White = /*whatever code is white*/,
    /*etc*/
  };

  Text(const std::string& s = "", int c = White)  // or instead of White, use whatever you want
                // the default color to be
    : str(s)
    , color(c)
  { }

  void SetColor(int clr)
  {
    color = clr;
  }

  void Print() const
  {
    /* insert whatever crazy code you have to change text color here*/
    std::cout << str;
  }
};


Then you can do stuff like this:

1
2
3
4
5
6
7
8
9
10
11
12
Text menu("Here are your menu options:\n"
          "1) Do something\n"
          "2) Do something else\n",
          Text::Blue
         );

menu.Print();

// then say you want to change the color to red:

menu.SetColor(Text::Red);
menu.Print();
Last edited on
I was thinking something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
bool IsQuest1Active = (true);
bool IsQuest1Inactive = (false);
bool IsQuest1Completed = (false);
string IsQuestVerifyState = "Unknown";
string QuestStateInactive = "INACTIVE";
string QuestStateActive = "ACTIVE";
string QuestStateCompleted = "COMPLETED";






if ( (!IsQuest1Inactive) && (!IsQuest1Completed) )
	{
		IsQuest1VerifyState = QuestStateActive;
	}
	else if ( (!IsQuest1Inactive) && (!IsQuest1Active) )
	{
		IsQuest1VerifyState = QuestStateCompleted;
	}
	else if ( (!IsQuest1Active) && (!IsQuest1Completed) )
	{
		IsQuest1VerifyState = QuestStateInactive;
	}

if (IsQuest1Active)
{
	CHANGE COLOR CODE HERE TO CHANGE STRING IsQuestVerifyState
}
else if (IsQuestInactive)
{
	CHANGE COLOR CODE HERE TO CHANGE STRING IsQuestVerifyState
}
else if (IsQuestCompleted)
{
	CHANGE COLOR CODE HERE TO CHANGE STRING IsQuestVerifyState
}
well strings don't have a color, they're just strings. So you can't assign a color to a specific strings.

If you want to do that, you'd need to create a new class which has both a string and a color, as I illustrated in my post.
I don't understand your code, ive just started working with arrays to a limited extent i still don't know structs and classes yet. Also how would that effect the strings because they have to be used to show the text state from the bool checks.
So if i understand what your saying, id have to create 3 classes 1 for each color then use the bool to call the class?
Ok im going to try to explain what im doing in simple words lol


I have 1 string. It represents the status of the quest.

I have 3 bools 1 for each of the 3 quest states.

I use the bools to check the state. Then i use if else to switch the single string = to 1 of 3 other strings ACTIVE, INACTIVE, COMPLETED..... now for each of the 3 quest states i want to be a different color.... Grey for inactive, white for active and green for completed.
Why do you have 3 bools here? Is there any case where two of them can be true?
Use one 'state' variable:
1
2
3
4
5
6
7
8
const int unknown_state = 0, inactive_state = 1, active_state = 2, completed_state = 3;
string state_string[] = {"unknown", "inactive", "active", "completed"};
char state_color[] = {0x07, 0x08, 0x0F, 0x0A};
int state = unknown_state;

//in print method:
SetColor(state_color[state]);
cout << state_string[state];

That would greatly simplify everything..
can you make the colors grey for unknown, red for inactive, white for active and green for complete? not sure what kind of color code you using, i am used to 1-15 using hconsole


and of course soon as u posted that, i already was working on a work around to do it, i realized the if else statements could be used since the console works on a line to line basis to hand write out all 3 states LOL
Last edited on
As for the states no there can only be 3 accually cause by default they will be set to inactive.
there is no unknown..... but each quest will have its own check/state
Pages: 12