system("cls") creates a lot of lag for my console game, how can I avoid that?

Sep 3, 2013 at 3:33pm
I think it won't be easy to answer this and simple for me to understand as well.
I'm using Windows 7.

Imagine this game - a figure (let's say it's an "X") walks through a field.
The figure amd the field look like this:
[X][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]

When you click the right arrow key, the field with the figure look like this:
[ ][X][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]

So you can imagine it now.
When the right arrow key is pressed, my program algorithm sees a system("cls") (and I know "system" functions are evil or something like that, so don't shout at me for that).
Clears the screen and types the stuff all over again.
When I want the 'X' to move faster, the lag is really noticeable.

It's not a huge problem for that kind of game, but I'm creating another one now that requires loads and loads of refreshes in a very short period of time.

The blinking of the screen would be unbearable.

I've seen a console game, created using C++ (as far as I know), that didn't have blinking at all. The "X" could move without a simple lag and it was incredibly smooth.

I think it wouldn't be easy to make it that way, but I wanna try. Make me an algorithm if you can that would me this possible. I might not understand it, but I'll use it on my games anyway.

I think I'd need to learn how to delete only the stuff that gets changed and not the whole foreground. Teach me a bit if you can.

Thanks in advance :D
Sep 3, 2013 at 3:37pm
It's a terrible waste of processing to clear the screen and redraw just to move a single char from one position to another. You'll be well advised to read up on ncurses, so that you just redraw the current char for x with a blank and redraw the next position.

HTH
Sep 3, 2013 at 3:37pm
Sep 3, 2013 at 4:38pm
I have a similar console game I wrote awhile back, my CLS function is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Map::cls() //windows h function to replace screen with nulls 
{ 
  DWORD n; 
  DWORD size; 
  COORD coord = {0}; 
  CONSOLE_SCREEN_BUFFER_INFO csbi; 
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE ); 
  GetConsoleScreenBufferInfo ( h, &csbi ); 
  size = csbi.dwSize.X * csbi.dwSize.Y; 
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n ); 
  GetConsoleScreenBufferInfo ( h, &csbi ); 
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n ); 
  SetConsoleCursorPosition ( h, coord ); 
} 


My whole code for the console based game can be found on my public dropbox link here:
https://dl.dropboxusercontent.com/u/78573135/BattleByte1.09a.rar

feel free to look at grab snippets of my code if you need any help, or just check it out!
Sep 3, 2013 at 5:43pm
Thanks everyone!

I've decided to try ncurses first. If that doesn't work, I'll just use the cls function Need4Sleep gave me. I know ncurses should work smoother, so I'll try it first.

So now I've decided to try it and I wanna know how to start! If some of you could bother answering my another asked question here:
http://www.cplusplus.com/forum/general/109857/
, that would be just great.

I have no idea what I should do :(
Thanks in advance!
Topic archived. No new replies allowed.