0

I'm writing a terminal game for Windows, and the refresh was quite slow so I asked here for help.

std::cout << "\33[2J\33[H"; // They told me to write this...
system("cls") // ...instead of this

But sending that ASCII escape sequence just makes the terminal "scroll", without deleting anything.


Lets say I really want to delete the old frames sometimes: how should I do it?


Just to make an example, they told me that...

std::cout << '\n'; // This...
std::cout << std::endl; // ...was faster than this

Then I asked what was missing to \n to become equivalent to std::endl, and they told me std::flush.

So my question is: what is missing to \33[2J\33[H to be equivalent to system("cls")?

FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
  • 1
    Since you're targeting Windows, I suggest you use the Windows-specific [console functions](https://learn.microsoft.com/en-us/windows/console/console-functions), rather than trying to keep it portable using common escape sequences and `std::cout`. – Some programmer dude Jul 08 '22 at 07:11
  • I liked the idea to make it portable, but Windows has a completely different way to get characters, so conio.h is only compatible with Windows, and what's compatible with Linux/MacOS isn't compatible with Windows. So, since it can't be cross-platform unless I write an hell of #ifdef, I'll probably use system("cls"). I was just curious to know if under the hood cls was using that escape sequence. – FLAK-ZOSO Jul 08 '22 at 07:16
  • 1
    Even when targeting only Windows, *please* don't use `system("cls")`. It adds a *lot* of overhead. Use the console functions I linked to earlier. See e.g. the second code snippet in [this old answer](https://stackoverflow.com/a/42500322/440558). Or [this other old answer](https://stackoverflow.com/a/6487534/440558). – Some programmer dude Jul 08 '22 at 07:20
  • Thank you, I tried the first one and it works properly. I've never messed up with the Windows API so this was very useful to me. – FLAK-ZOSO Jul 08 '22 at 08:00
  • 1
    Under the hood, `cls` is using the WinAPI directly and not generating ANSI escape sequences. – Eljay Jul 08 '22 at 12:48

0 Answers0