Skip to main content
added 1 character in body
Source Link
Joel
  • 1.8k
  • 4
  • 24

If you just want to stop the flickering without modifying anything else, you could just hide the cursor using SetConsoleCursorInfo at the start of your application:

const CONSOLE_CURSOR_INFO info{ .dwSize = 1, .bVisible = truefalse }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); 

As the other answers and comments already mentioned, to remove flickering and as bonus even speed up the draw time you should use a buffer to which you write your characters first, and then output the whole buffer at once. This could be achieved by something like the following:

void board() { std::array<char, HEIGHT* (WIDTH + 1)> buffer; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { 0, 0 }); for (int y = 0; y < HEIGHT; ++y) { for (int x = 0; x < WIDTH; ++x) { if (x == 0 || y == 0 || x == WIDTH - 1 || y == HEIGHT - 1) { buffer[y * (WIDTH + 1) + x] = '#'; } else { buffer[y * (WIDTH + 1) + x] = ' '; } } buffer[y * (WIDTH + 1) + WIDTH] = '\n'; } std::cout.write(buffer.data(), buffer.size()); } 

This sped up drawing by over 300 times on my machine.


If you would like to speed up your drawing even more, you could have two buffers and swap them back and forth with a second thread drawing them.

If you just want to stop the flickering without modifying anything else, you could just hide the cursor using SetConsoleCursorInfo at the start of your application:

const CONSOLE_CURSOR_INFO info{ .dwSize = 1, .bVisible = true }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); 

As the other answers and comments already mentioned, to remove flickering and as bonus even speed up the draw time you should use a buffer to which you write your characters first, and then output the whole buffer at once. This could be achieved by something like the following:

void board() { std::array<char, HEIGHT* (WIDTH + 1)> buffer; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { 0, 0 }); for (int y = 0; y < HEIGHT; ++y) { for (int x = 0; x < WIDTH; ++x) { if (x == 0 || y == 0 || x == WIDTH - 1 || y == HEIGHT - 1) { buffer[y * (WIDTH + 1) + x] = '#'; } else { buffer[y * (WIDTH + 1) + x] = ' '; } } buffer[y * (WIDTH + 1) + WIDTH] = '\n'; } std::cout.write(buffer.data(), buffer.size()); } 

This sped up drawing by over 300 times on my machine.


If you would like to speed up your drawing even more, you could have two buffers and swap them back and forth with a second thread drawing them.

If you just want to stop the flickering without modifying anything else, you could just hide the cursor using SetConsoleCursorInfo at the start of your application:

const CONSOLE_CURSOR_INFO info{ .dwSize = 1, .bVisible = false }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); 

As the other answers and comments already mentioned, to remove flickering and as bonus even speed up the draw time you should use a buffer to which you write your characters first, and then output the whole buffer at once. This could be achieved by something like the following:

void board() { std::array<char, HEIGHT* (WIDTH + 1)> buffer; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { 0, 0 }); for (int y = 0; y < HEIGHT; ++y) { for (int x = 0; x < WIDTH; ++x) { if (x == 0 || y == 0 || x == WIDTH - 1 || y == HEIGHT - 1) { buffer[y * (WIDTH + 1) + x] = '#'; } else { buffer[y * (WIDTH + 1) + x] = ' '; } } buffer[y * (WIDTH + 1) + WIDTH] = '\n'; } std::cout.write(buffer.data(), buffer.size()); } 

This sped up drawing by over 300 times on my machine.


If you would like to speed up your drawing even more, you could have two buffers and swap them back and forth with a second thread drawing them.

added 36 characters in body
Source Link
Joel
  • 1.8k
  • 4
  • 24

If you just want to stop the flickering without modifying anything else, you could just hide the cursor using SetConsoleCursorInfo at the start of your application:

const CONSOLE_CURSOR_INFO info{ .dwSize = 1, .bVisible = true }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); 

As the other answers and comments already mentioned, to remove flickering and as bonus even speed up the draw time you should use a buffer to which you write your characters first, and then output the whole buffer at once. This could be achieved by something like the following:

void board() { std::array<char, HEIGHT* (WIDTH + 1)> buffer; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { 0, 0 }); for (int y = 0; y < HEIGHT; ++y) { for (int x = 0; x < WIDTH; ++x) { if (x == 0 || y == 0 || x == WIDTH - 1 || y == HEIGHT - 1) { buffer[y * (WIDTH + 1) + x] = '#'; } else { buffer[y * (WIDTH + 1) + x] = ' '; } } buffer[y * (WIDTH + 1) + WIDTH] = '\n'; } std::cout.write(buffer.data(), buffer.size()); } 

This sped up drawing by over 300 times on my machine.


If you would like to speed up your drawing even more, you could have two buffers and swap them back and forth with a second thread drawing them.

If you just want to stop the flickering without modifying anything else, you could just hide the cursor using SetConsoleCursorInfo at the start of your application:

const CONSOLE_CURSOR_INFO info{ .dwSize = 1, .bVisible = true }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); 

As the other answers and comments already mentioned, to speed up the draw time you should use a buffer to which you write your characters first, and then output the whole buffer at once. This could be achieved by something like the following:

void board() { std::array<char, HEIGHT* (WIDTH + 1)> buffer; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { 0, 0 }); for (int y = 0; y < HEIGHT; ++y) { for (int x = 0; x < WIDTH; ++x) { if (x == 0 || y == 0 || x == WIDTH - 1 || y == HEIGHT - 1) { buffer[y * (WIDTH + 1) + x] = '#'; } else { buffer[y * (WIDTH + 1) + x] = ' '; } } buffer[y * (WIDTH + 1) + WIDTH] = '\n'; } std::cout.write(buffer.data(), buffer.size()); } 

This sped up drawing by over 300 times on my machine.


If you would like to speed up your drawing even more, you could have two buffers and swap them back and forth with a second thread drawing them.

If you just want to stop the flickering without modifying anything else, you could just hide the cursor using SetConsoleCursorInfo at the start of your application:

const CONSOLE_CURSOR_INFO info{ .dwSize = 1, .bVisible = true }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); 

As the other answers and comments already mentioned, to remove flickering and as bonus even speed up the draw time you should use a buffer to which you write your characters first, and then output the whole buffer at once. This could be achieved by something like the following:

void board() { std::array<char, HEIGHT* (WIDTH + 1)> buffer; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { 0, 0 }); for (int y = 0; y < HEIGHT; ++y) { for (int x = 0; x < WIDTH; ++x) { if (x == 0 || y == 0 || x == WIDTH - 1 || y == HEIGHT - 1) { buffer[y * (WIDTH + 1) + x] = '#'; } else { buffer[y * (WIDTH + 1) + x] = ' '; } } buffer[y * (WIDTH + 1) + WIDTH] = '\n'; } std::cout.write(buffer.data(), buffer.size()); } 

This sped up drawing by over 300 times on my machine.


If you would like to speed up your drawing even more, you could have two buffers and swap them back and forth with a second thread drawing them.

Source Link
Joel
  • 1.8k
  • 4
  • 24

If you just want to stop the flickering without modifying anything else, you could just hide the cursor using SetConsoleCursorInfo at the start of your application:

const CONSOLE_CURSOR_INFO info{ .dwSize = 1, .bVisible = true }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); 

As the other answers and comments already mentioned, to speed up the draw time you should use a buffer to which you write your characters first, and then output the whole buffer at once. This could be achieved by something like the following:

void board() { std::array<char, HEIGHT* (WIDTH + 1)> buffer; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { 0, 0 }); for (int y = 0; y < HEIGHT; ++y) { for (int x = 0; x < WIDTH; ++x) { if (x == 0 || y == 0 || x == WIDTH - 1 || y == HEIGHT - 1) { buffer[y * (WIDTH + 1) + x] = '#'; } else { buffer[y * (WIDTH + 1) + x] = ' '; } } buffer[y * (WIDTH + 1) + WIDTH] = '\n'; } std::cout.write(buffer.data(), buffer.size()); } 

This sped up drawing by over 300 times on my machine.


If you would like to speed up your drawing even more, you could have two buffers and swap them back and forth with a second thread drawing them.