Advertisement
xth

?

xth
Jun 7th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1.  
  2. #define NOMINMAX
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <Windows.h>
  5. #include <iostream>
  6.  
  7.  
  8. void cls()
  9. {
  10.     // Get the Win32 handle representing standard output.
  11.     // This generally only has to be done once, so we make it static.
  12.     static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  13.  
  14.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  15.     COORD topLeft = { 0, 0 };
  16.  
  17.     // std::cout uses a buffer to batch writes to the underlying console.
  18.     // We need to flush that to the console because we're circumventing
  19.     // std::cout entirely; after we clear the console, we don't want
  20.     // stale buffered text to randomly be written out.
  21.     std::cout.flush();
  22.  
  23.     // Figure out the current width and height of the console window
  24.     if (!GetConsoleScreenBufferInfo(hOut, &csbi)) {
  25.         // TODO: Handle failure!
  26.         abort();
  27.     }
  28.     DWORD length = csbi.dwSize.X * csbi.dwSize.Y;
  29.  
  30.     DWORD written;
  31.  
  32.     // Flood-fill the console with spaces to clear it
  33.     FillConsoleOutputCharacter(hOut, TEXT(' '), length, topLeft, &written);
  34.  
  35.     // Reset the attributes of every character to the default.
  36.     // This clears all background colour formatting, if any.
  37.     FillConsoleOutputAttribute(hOut, csbi.wAttributes, length, topLeft, &written);
  38.  
  39.     // Move the cursor back to the top left for the next sequence of writes
  40.     SetConsoleCursorPosition(hOut, topLeft);
  41. }
  42.  
  43. // x is the column, y is the row. The origin (0,0) is top-left.
  44. void setCursorPosition(int x, int y)
  45. {
  46.     static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  47.     std::cout.flush();
  48.     COORD coord = { (SHORT)x, (SHORT)y };
  49.     SetConsoleCursorPosition(hOut, coord);
  50. }
  51.  
  52.  
  53. /* Trying to design a ship for ASCII art im just grabbing from text art
  54.  this one is called sjw for some dumb reason T_T
  55.  but its the samllest one they had
  56.       /\
  57.      (  )
  58.      (  )
  59.     /|/\|\
  60.    /_||||_\
  61.  
  62. */
  63.  
  64. void setConsoleColour(unsigned short colour)
  65. {
  66.     static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  67.     std::cout.flush();
  68.     SetConsoleTextAttribute(hOut, colour);
  69. }
  70.  
  71. int main(int argc, int argv[])
  72. {
  73.     // Step through with a debugger, or insert sleeps, to see the effect.
  74.     setCursorPosition(10, 5);
  75.     std::cout << "CHEESE";
  76.     setCursorPosition(10, 5);
  77.     std::cout << 'W';
  78.     setCursorPosition(10, 9);
  79.     std::cout << 'Z';
  80.     setCursorPosition(10, 5);
  81.     std::cout << "     ";  // Overwrite characters with spaces to "erase" them
  82.     std::cout.flush();
  83.     // Voilà, 'CHEESE' converted to 'WHEEZE', then all but the last 'E' erased
  84.  
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement