[SOLVED] Is it possible to change this code to stop the flickering?

For a game I been working on for someone they need to be able to display a map in a console 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
void DisplayMap(Character *teamOne, Character *teamTwo, int soldiers)
{
			char* map[24][78];
			for(int x = 0; x < 24; ++x)
			{
				for(int y = 0; y < 78; ++y)
				{
					map[x][y] = " ";
				}
			}

			for(int i = 0; i < soldiers; ++i)
			{
				map[teamOne[i].iX][teamOne[i].iY] = "@"; // Was @ using " " as Temp
				map[teamTwo[i].iX][teamTwo[i].iY] = "%";
			}

			for(int x = 0; x < 24; ++x)
			{
				for(int y = 0; y < 78; ++y)
				{
					std::cout << map[x][y];
				}
				std::cout << std::endl;
			}
			system("Cls");

}


I know this isn't perfect but because of the way it needs to be displayed i'm not sure of how else I can do it. It works fine just the bottem right of the screen flickers a heck of a lot due to the constant amount of loops.
Last edited on
The only way is to not clear the screen each time through the loop.
Any idea of what else i can use other than a screen clear to keep the refreshing half decent or?
Consoles are not made for that. :)

Unless your console supports something like double-buffering or n-buffering. You can also search console frame buffering.
I'm searching it up now. I understand what it is in a sence as a mate of mine used it once for the same time of thing - sadly we are no longer in touch so i'm unable to ask him for help heh. Anyway main point being. How do I know if my console supports it?
Well I did find a site for the buffer and all.. Only problem is - it's the only one i could find that fit what i wanted to do.. Yet all there links are broken. Oh well not to important.

Thanks for the help though :)
If you can move the cursor back to the top left and then redraw every character on the screen...
Wow - you can do that? I'll go see what i can dig up. If you have any idea how it's done please list it off :D
For a game I suggest you look at NCurses.
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
Get NCurses for Linux.
Get PDCurses for Windows.

If you only intend this application to work on windows, then you can use the Windows Console buffer directly. Check out the API
http://msdn.microsoft.com/en-us/library/ms682073(VS.85).aspx

Good luck!
You're a legend mate - thanks for that reply looking into it now. Cheers!
Quick question - say im using:
1
2
3
4
5
6
BOOL WINAPI SetConsoleCursorPosition(
  __in  HANDLE hConsoleOutput,
  __in  COORD dwCursorPosition
);

SetConsoleCursorPosition();


What do i put inside the SetConsoleCursorPosition()? Haha and yeah don't worry I didn't include the above in my code - just not sure what to put inside - say if i want the cursor to go back to the top left at position 0, 0?
1
2
COORD newpos = {0,0}; //position to 0, 0
SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), newpos );
Last edited on
Sweet i just tried
1
2
3
4
COORD coord;
coord.X = 0;
coord.Y = 0;
SetConsoleCursorPosition(0, coord);

But it scrolls like mad
Perfect heh, just tried yours and it works a charm. Cheers guys!
Now you have the ability to move the cursor, it might (depending on the number of items on the map) be quicker to update the map rather than redraw the whole map
IE Just update the points that change.
The balance is the overhead of moving the cursor vs the reduced number of screen writes. Note that you will have to write to both the old (to remove it) and new positions for each 'thing'.
All is good i re-rote some of the display and changed my other functions - no flickering or lag if anything its to fast now ahah. But its sweet now nice and smooth :D Thanks Guys
You probably had scrolling because by default the console screen buffer tends to be larger than the window can display (giving those nice scrollbars on the side and/or bottom).

When your game starts, you should resize the screen buffer down to what your program expects to use somewhere in the initialization. You can use GetLargestConsoleWindowSize() to see what your current constraints are. Then use CreateConsoleScreenBuffer() to create a couple of buffers that are the size you need. Set one of them to the currently displayed buffer with SetConsoleActiveScreenBuffer().

Thereafter, draw in the currently inactive buffer, and when you are ready to update, switch the active/inactive buffer. (This is called double-buffering, and makes things look sweet.)

Before terminating, restore the original screen buffer to the active state, and destroy the two you created with CloseHandle().

Hope this helps.
Thanks a heap for that man, very nice explanation - cheers :D
Topic archived. No new replies allowed.