Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. // ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <conio.h>
  8.  
  9. inline WORD ifw(WORD w)
  10. {
  11.     return (rand() % 2 == 1) ? w : 0;
  12. }
  13.  
  14. int main()
  15. {
  16.     srand(time(NULL));   // Initialization, should only be called once.
  17.     //int r = rand();      // Returns a pseudo-random integer between 0 and RAND_MAX.
  18.  
  19.     CHAR_INFO* chiBuffer;
  20.     HANDLE hStdout, hNewScreenBuffer;
  21.     SMALL_RECT srctWriteRect;
  22.     COORD coordBufSize;
  23.     COORD coordBufCoord;
  24.     BOOL fSuccess;
  25.  
  26.     hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  27.  
  28.     CONSOLE_SCREEN_BUFFER_INFO scrBuffInfo;
  29.     if (!GetConsoleScreenBufferInfo(hStdout, &scrBuffInfo))
  30.     {
  31.         printf("GetConsoleScreenBufferInfo failed - (%d)\n", GetLastError());
  32.         return 1;
  33.     }
  34.  
  35.     size_t scrBuffLen;
  36.     COORD scrBuffDim; // window size, not actual buffer, no point drawing off screen
  37.     scrBuffDim.X = scrBuffInfo.srWindow.Right - scrBuffInfo.srWindow.Left;
  38.     scrBuffDim.Y = scrBuffInfo.srWindow.Bottom - scrBuffInfo.srWindow.Top;
  39.     scrBuffLen = scrBuffDim.X * scrBuffDim.Y;
  40.  
  41.     chiBuffer = new CHAR_INFO[scrBuffLen];
  42.     for (size_t i = 0; i < scrBuffLen; i++)
  43.     {
  44.         chiBuffer[i].Char.AsciiChar = rand() % 91 + 32;
  45.         chiBuffer[i].Attributes = ifw(FOREGROUND_BLUE) | ifw(FOREGROUND_GREEN) | ifw(FOREGROUND_RED) |
  46.             ifw(BACKGROUND_RED) | ifw(BACKGROUND_BLUE) | ifw(BACKGROUND_GREEN) |
  47.             ifw(BACKGROUND_INTENSITY) | ifw(FOREGROUND_INTENSITY);
  48.     }
  49.  
  50.     hNewScreenBuffer = CreateConsoleScreenBuffer(
  51.         GENERIC_READ |           // read/write access
  52.         GENERIC_WRITE,
  53.         FILE_SHARE_READ |
  54.         FILE_SHARE_WRITE,        // shared
  55.         NULL,                    // default security attributes
  56.         CONSOLE_TEXTMODE_BUFFER, // must be TEXTMODE
  57.         NULL);                   // reserved; must be NULL
  58.     if (hStdout == INVALID_HANDLE_VALUE ||
  59.         hNewScreenBuffer == INVALID_HANDLE_VALUE)
  60.     {
  61.         printf("CreateConsoleScreenBuffer failed - (%d)\n", GetLastError());
  62.         return 1;
  63.     }
  64.  
  65.     if (!SetConsoleActiveScreenBuffer(hNewScreenBuffer))
  66.     {
  67.         printf("SetConsoleActiveScreenBuffer failed - (%d)\n", GetLastError());
  68.         return 1;
  69.     }
  70.  
  71.     coordBufSize = scrBuffDim;
  72.  
  73.     coordBufCoord.X = 0;
  74.     coordBufCoord.Y = 0;
  75.  
  76.     srctWriteRect.Left = 1; // 1 char wide border all around
  77.     srctWriteRect.Top = 1;
  78.     srctWriteRect.Right = scrBuffDim.X - 2;
  79.     srctWriteRect.Bottom = scrBuffDim.Y - 2;
  80.  
  81.     // better timer resolution
  82.     if (timeBeginPeriod(1) != TIMERR_NOERROR)
  83.     {
  84.         printf("timeBeginPeriod failed - (%d)\n", GetLastError());
  85.         return 1;
  86.     }
  87.     DWORD t1 = timeGetTime();
  88.  
  89.     fSuccess = WriteConsoleOutput(
  90.         hNewScreenBuffer, // screen buffer to write to
  91.         chiBuffer,        // buffer to copy from
  92.         coordBufSize,     // col-row size of chiBuffer
  93.         coordBufCoord,    // top left src cell in chiBuffer
  94.         &srctWriteRect);  // dest. screen buffer rectangle
  95.     if (!fSuccess)
  96.     {
  97.         printf("WriteConsoleOutput failed - (%d)\n", GetLastError());
  98.         return 1;
  99.     }
  100.  
  101.     DWORD t2 = timeGetTime();
  102.     timeEndPeriod(1);
  103.     delete chiBuffer;
  104.  
  105.     _getch();
  106.     std::cout << "\nTook " << t2 - t1 << " ms\n";
  107.  
  108. }
  109.  
  110. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  111. // Debug program: F5 or Debug > Start Debugging menu
  112.  
  113. // Tips for Getting Started:
  114. //   1. Use the Solution Explorer window to add/manage files
  115. //   2. Use the Team Explorer window to connect to source control
  116. //   3. Use the Output window to see build output and other messages
  117. //   4. Use the Error List window to view errors
  118. //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  119. //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement