Change color of specific characters in console pt.2

Hello! Im brand new on this forum (and sorry for my poor English, but I'll do my best), expect a many questions from me as Im the beginner in C++! ;-D . But I like programming and I am making progress every day. But...last topic about this was closed and I am really sorry, but I dont understand.
The closed topic is here http://www.cplusplus.com/forum/beginner/134678/

Q
I know *SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)0x0A);* is for font colors... but I dont really know, how to implement this into his map. The page pointed by the answerer is very useless for a beginners in this (I am weird, eh?). I have similiar map in which you can move around, but I dont know how to make "T" - green, "W"-blue and so on (examples).
Would you be so nice and show me, how the code is done for this, please? I am that person who learn from copied codes (and learning from tutorials on this web in meantime ofc).
Thank you!

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  char mapa[100][100] =
    {
     "###############################################",
     "#@                                          WW#",
     "#            WWW                            WW#",
     "#            WWWW           a              WW #",
     "#           WWW                   #        WW #",
     "#                                # #      WW  #",
     "#                     T         #   #    WW   #",
     "#   T            f              #  B#   WW    #",
     "#                               ## ##  WW     #",
     "#                                       WW    #",
     "#                                  T    WW    #",
     "#                   T            T       WW   #",
     "#     T                       T       T  WW   #",
     "#               T              T T T    WW    #",
     "#                                  T  TWW T   #",
     "#  p                             T     TWW    #",
     "#                              T    T T WWT   #",
     "#      T                     T     T WWWWWW   #",
     "# +                           T   WWWWWWWWWWWW#",
     "#                                WWWWWWWWWWWWW#",
     "###############################################"
     };
bool stop=false;
//AND HERE IS SOME BASIC EXAMPLE WHICH DIDNT WORK
const int rows = 100 , columns = 100;
for( int i = 0; i < rows; ++i )
{
    for( int j = 0; j < columns; ++j )
    {
         if( mapa[i][j] == 'T' )
         {
             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
            cout << mapa[i] [j];
        }
        else
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
            cout << mapa[i][j] ;
        }
    }
}
// IT WOULD BE NICE TO HAVE IT IMPLEMENTED INTO THIS MOVEMENT 

//(its from my different game - so different names)
while (stop==false){
    system("cls");
    for (int y=0; y<20; y++)
        {
                cout<<mapa[y]<<endl;
        }
        for (int y=0;y<20;y++)
    {
        for (int x=0;x<20;x++)
        {
            switch(mapa[y][x])
            {
            case '@':
                {
                   system("pause>nul"); if (GetAsyncKeyState(VK_UP)!=0)
                    {
                        int y2=(y-1);
                        switch (mapa[y2][x])
                        {
                             case ' ':
                                 {
                                     mapa[y][x]= ' ';
                                     y-=1;
                                     mapa[y2][x]= '@';
                                 }break;
                        }
                    }

                    if(GetAsyncKeyState(VK_DOWN)!=0)
                    {
                         int y2=(y+1);

                         switch(mapa[y2][x])
                         {
                             case ' ':
                                 {
                                     mapa[y][x]= ' ';
                                     y+=1;
                                     mapa[y2][x]= '@';
                                 }break;
                         }
                    }
                    if(GetAsyncKeyState(VK_RIGHT)!=0)
                    {
                         int x2=(x+1);

                         switch(mapa[y][x2])
                         {
                             case ' ':
                                 {
                                     mapa[y][x]= ' ';
                                     x+=1;
                                     mapa[y][x2]= '@';
                                 }break;
                         }
                    }

                    if(GetAsyncKeyState(VK_LEFT)!=0)
                    {
                         int x2=(x-1);

                         switch(mapa[y][x2])
                         {
                             case ' ':
                                 {
                                     mapa[y][x]= ' ';
                                     x-=1;
                                     mapa[y][x2]= '@';
                                 }break;}}}}}}}
Last edited on
The attribute for SetConsoleTextAttribute are explainded here:

http://msdn.microsoft.com/en-us/library/ms682088%28v=vs.85%29.aspx#_win32_character_attributes

Sorry, but you need learn reading (msdn) documentation. Otherwise you won't get far.


Why do you use 10 as an attribute and what happens if you do so?

I checked it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

#include <windows.h>


using namespace std;

int main()
{
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
    cout << "Hello world!" << endl;
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE);
    cout << "Hello world!" << endl;
    return 0;
}
I get red and blue output
hello coder777.
If you write SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xf0); you can change two font attributes at once (f-white background, 0-black text). So it seems better for me since I have document about these numbers/letters and their colors...
But, if you doesnt mind, my question was targeting somewhere else. I would like to know, how to extract the letter 'T' from the char map and color it into (example) f0 and anyhing else will be in the default color and still be able to use the code of movement I posted above(or it can be completly different code..). I am already trying for two days, googling over the whole internet, reading tutorials, trying by success-error ... but I am just too poor yet and thats why I need to see how it can be done, to learn :-) thank you for reply :-)
Last edited on
Well, I'm not sure whether I understand it correctly. This changes the color of just a single character (in this case the 'o' of "Hello world!":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

#include <windows.h>


using namespace std;

int main()
{
  WORD write = 10;
  COORD c = { 4, 0 };
  DWORD written;
//  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
    cout << "Hello world!" << endl;
  WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), &write, 1, c, &written);
//  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
//    cout << "Hello world!" << endl;
    return 0;
}
In your context it would probably:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for( int i = 0; i < rows; ++i )
{
    for( int j = 0; j < columns; ++j )
    {
         if( mapa[i][j] == 'T' )
         {
//             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
            cout << mapa[i] [j]; // Note that WriteConsoleOutputAttribute has to be called after the character is written to the console
  WORD write = 10;
  COORD c = { j, i };
  DWORD written;
  WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), &write, 1, c, &written);
        }
        else
        {
//            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
            cout << mapa[i][j] ;
        }
    }
}


For WriteConsoleOutputAttribute read this:
http://msdn.microsoft.com/en-us/library/ms687407%28v=vs.85%29.aspx

Note that with WriteConsoleOutputCharacter you can write the whole map at once:
http://msdn.microsoft.com/en-us/library/ms687410%28v=vs.85%29.aspx
hey, thanks! The second code is something like I wanted! It has some minor bugs but only because I tryed to implement it into my movement code. I will definitely look into this and try to understand. Thank you!

EDIT: I was able to fill (thanks to *WORD write = 0xAA;* *COORD c = { 10, 15 };)* the selected space in the console with green color which is exactly what I wanted to do. I probably must do it this way, because in the case of *COORD j,i* the character lose its color as soon as my moving letter (in my case '@') got above it (the line above).
I put a character on that filled space (10,15) so it serves as a block for my moving letter and everything is working now. Thank you once more mr. Coder.

Last edited on
Topic archived. No new replies allowed.