Advertisement
NickG

Cross-Platform clear() - clear.cpp

Apr 4th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #ifdef __WIN32__
  2. #include <windows.h>
  3. #endif
  4.  
  5. #ifdef __linux__
  6. #ifndef _GLIBCXX_IOSTREAM
  7. #include <iostream>
  8. using namespace std;
  9. #endif
  10. #endif
  11.  
  12. void clear() {
  13. #ifdef __linux__
  14. cout << "\Ec";
  15. #endif
  16. #ifdef __WIN32__
  17.   HANDLE                     hStdOut;
  18.   CONSOLE_SCREEN_BUFFER_INFO csbi;
  19.   DWORD                      count;
  20.   DWORD                      cellCount;
  21.   COORD                      homeCoords = { 0, 0 };
  22.  
  23.   hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  24.   if (hStdOut == INVALID_HANDLE_VALUE) return;
  25.  
  26.   /* Get the number of cells in the current buffer */
  27.   if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  28.   cellCount = csbi.dwSize.X *csbi.dwSize.Y;
  29.  
  30.   /* Fill the entire buffer with spaces */
  31.   if (!FillConsoleOutputCharacter(
  32.     hStdOut,
  33.     (TCHAR) ' ',
  34.     cellCount,
  35.     homeCoords,
  36.     &count
  37.     )) return;
  38.  
  39.   /* Fill the entire buffer with the current colors and attributes */
  40.   if (!FillConsoleOutputAttribute(
  41.     hStdOut,
  42.     csbi.wAttributes,
  43.     cellCount,
  44.     homeCoords,
  45.     &count
  46.     )) return;
  47.  
  48.   /* Move the cursor home */
  49.   SetConsoleCursorPosition( hStdOut, homeCoords );
  50. #endif
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement