Search code examples
c++terminalconsole

C++ Increasing while loop refresh rate speed?


So I want to make a graphic game in console. I display graphics simply std::couting array of colored empty string like this:

while (1 == 1) {
  for (int i = 0; i < height * width; i++) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), pixels[i]);
    cout << pixel;
  }
  system("CLS");
}

array 'pixels' stores colors of each pixel, and string pixel = " ";

The thing is fps is really low, and You can see pixels 'blinking' and process of drawing is also so slow that you can see how each pixel is drown into the screen. Is there a way to increasy fps, or a better way to draw pixels?


Solution

  • The FPS of this approach is very slow since every system("CLS"); launches a new process with a command interpreter and execute the OS instruction CLS to clear the screen. This is an extremely high overhead.

    Unfortunately, there is no standard C++ way to clear the screen. THis is platform dependent. You'd need to make it platform dependent using curses on linux platform and using console API of windows (see other question here with some more links to the API doc).