C++: Can I print different colors simultaneously?

For school, I decided to simulate Conway's Game of Life. Things went very smoothly until I decided that the game needed to be in color.
Using information found on this board (post #3),
http://www.cplusplus.com/forum/beginner/5830/
I was able to add a different color to each generation of cells

The problem I am having is that output is occurring too slowly. Whereas before, I would append all of the cells together into one big string, now I am forced to print each cell individually. This causes the display to flicker horribly at high speeds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//This setup prints the entire board to the screen at once, eliminating any scrolling effect
void printGameBoard(int board[][ROWS])
{
    string boardString = "";
    for(int x=0;x<COLS;x++)
    {
        for(int y=0;y<ROWS;y++)
        {
            if(board[x][y] == 1)
                boardString.append("* ");
            else if(board[x][y] == 0)
                boardString.append("  ");
        }
        boardString.append("\n");
    }
    cout << boardString;
}


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
//this setup effectively adds color to the board, but it also means that the cells need to be printed one at a time
void printGameBoard(int board[][ROWS][2])
{
    string boardString = "";
    for(int x=0;x<COLS;x++)
    {
        for(int y=0;y<ROWS;y++)
        {
            if(board[x][y][0] == 1)
                addColorToCell(board[x][y][1]);//send cell's age to addColorToCell()
            else if(board[x][y][0] == 0)
                cout << "  ";
        }
        cout << "\n";
    }
    cout << boardString;
}

void addColorToCell(int cellAge)
{
    const WORD colors[12] ={0x0F, 0x0E, 0x06, 0x0A, 0x02, 0x0B, 0x03, 0x01, 0x0D, 0x05, 0x0C, 0x04};
    if(cellAge < 12)
        SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), colors[cellAge-1]);
    else
        SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), colors[11]);
    cout << "*";
}


My question is, is there any way to store every cell along with its corresponding color and print them to the screen all at the same time? How would YOU solve a problem like this?
Honestly, I would just not use color on the console and instead use a graphics lib like SFML or something to do it. It wouldn't be that hard, really.
closed account (Dy7SLyTq)
What os are you using? becuase each has its own way
@DTSCode Judging by the Windows API calls in his code, he could be using any OS - I'll just guess he's on a *nix platform.
Sorry if you're not familiar with Windows API function names.

I'll have to agree with firedraco - at this point you should abandon the console and use a graphics library.
Printing everything "at the same time" won't speed it up any or reduce flickering - it's the console for Stroustrup's sake.
Last edited on
take a look at this:

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

you can write into a buffer and make it active
closed account (Dy7SLyTq)
sorry my mistake i didnt look at the code which now that I think about it wasn't posted originally. (S?)He edited it so before I thought it was just printing in colors
You may have skimmed it, because the OP has not been edited. Look at the bottom of this post, which has, and then look at the bottom of the OP.
(Just for future reference, I'm not heckling you)
Last edited on
closed account (Dy7SLyTq)
yeah i understand. thanks. i must have been more tired than i thought this morning and missed it.
use a graphics lib like SFML
That would appear to be the simplest solution right now. I think I'll end up doing that.

What os are you using?
Windows 7, but I'm attempting to keep things as universal as I can.

Printing everything "at the same time" won't speed it up any or reduce flickering
Well, perhaps not, but when I added all the cells into one big string and printed it, there was almost no visible flickering. One I tried to print the cells with a separate cout statement, it printed much more slowly.

you can write into a buffer and make it active
That is also a valid option. I'll have to look into learning that technique.
so at each stage do all cells have same age, or same color? If so, you can simply SetConsoleTextAttribute with the cellAge color before outputting the boardString.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
  int cellAge = 0;
  const WORD colors[12] ={0x0F, 0x0E, 0x06, 0x0A, 0x02, 0x0B, 0x03, 0x01, 0x0D, 0x05, 0x0C, 0x04};

  while (cellAge < 100)
  {
    ++cellAge;
    // Process board . . .

    if(cellAge < 12)
      SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), colors[cellAge-1]);
    else
      SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), colors[11]);

    printGameBoard(board);
  }

}


if each cell has different colors then... don't color them or use a graphics lib instead (:
closed account (Dy7SLyTq)
correct me if im wrong, but isn't that all win32 code? so it can't be portable
Topic archived. No new replies allowed.