Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- #include <windows.h>
- #include <stdio.h>
- #include <conio.h>
- inline WORD ifw(WORD w)
- {
- return (rand() % 2 == 1) ? w : 0;
- }
- int main()
- {
- srand(time(NULL)); // Initialization, should only be called once.
- //int r = rand(); // Returns a pseudo-random integer between 0 and RAND_MAX.
- CHAR_INFO* chiBuffer;
- HANDLE hStdout, hNewScreenBuffer;
- SMALL_RECT srctWriteRect;
- COORD coordBufSize;
- COORD coordBufCoord;
- BOOL fSuccess;
- hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_SCREEN_BUFFER_INFO scrBuffInfo;
- if (!GetConsoleScreenBufferInfo(hStdout, &scrBuffInfo))
- {
- printf("GetConsoleScreenBufferInfo failed - (%d)\n", GetLastError());
- return 1;
- }
- size_t scrBuffLen;
- COORD scrBuffDim; // window size, not actual buffer, no point drawing off screen
- scrBuffDim.X = scrBuffInfo.srWindow.Right - scrBuffInfo.srWindow.Left;
- scrBuffDim.Y = scrBuffInfo.srWindow.Bottom - scrBuffInfo.srWindow.Top;
- scrBuffLen = scrBuffDim.X * scrBuffDim.Y;
- chiBuffer = new CHAR_INFO[scrBuffLen];
- for (size_t i = 0; i < scrBuffLen; i++)
- {
- chiBuffer[i].Char.AsciiChar = rand() % 91 + 32;
- chiBuffer[i].Attributes = ifw(FOREGROUND_BLUE) | ifw(FOREGROUND_GREEN) | ifw(FOREGROUND_RED) |
- ifw(BACKGROUND_RED) | ifw(BACKGROUND_BLUE) | ifw(BACKGROUND_GREEN) |
- ifw(BACKGROUND_INTENSITY) | ifw(FOREGROUND_INTENSITY);
- }
- hNewScreenBuffer = CreateConsoleScreenBuffer(
- GENERIC_READ | // read/write access
- GENERIC_WRITE,
- FILE_SHARE_READ |
- FILE_SHARE_WRITE, // shared
- NULL, // default security attributes
- CONSOLE_TEXTMODE_BUFFER, // must be TEXTMODE
- NULL); // reserved; must be NULL
- if (hStdout == INVALID_HANDLE_VALUE ||
- hNewScreenBuffer == INVALID_HANDLE_VALUE)
- {
- printf("CreateConsoleScreenBuffer failed - (%d)\n", GetLastError());
- return 1;
- }
- if (!SetConsoleActiveScreenBuffer(hNewScreenBuffer))
- {
- printf("SetConsoleActiveScreenBuffer failed - (%d)\n", GetLastError());
- return 1;
- }
- coordBufSize = scrBuffDim;
- coordBufCoord.X = 0;
- coordBufCoord.Y = 0;
- srctWriteRect.Left = 1; // 1 char wide border all around
- srctWriteRect.Top = 1;
- srctWriteRect.Right = scrBuffDim.X - 2;
- srctWriteRect.Bottom = scrBuffDim.Y - 2;
- // better timer resolution
- if (timeBeginPeriod(1) != TIMERR_NOERROR)
- {
- printf("timeBeginPeriod failed - (%d)\n", GetLastError());
- return 1;
- }
- DWORD t1 = timeGetTime();
- fSuccess = WriteConsoleOutput(
- hNewScreenBuffer, // screen buffer to write to
- chiBuffer, // buffer to copy from
- coordBufSize, // col-row size of chiBuffer
- coordBufCoord, // top left src cell in chiBuffer
- &srctWriteRect); // dest. screen buffer rectangle
- if (!fSuccess)
- {
- printf("WriteConsoleOutput failed - (%d)\n", GetLastError());
- return 1;
- }
- DWORD t2 = timeGetTime();
- timeEndPeriod(1);
- delete chiBuffer;
- _getch();
- std::cout << "\nTook " << t2 - t1 << " ms\n";
- }
- // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
- // Debug program: F5 or Debug > Start Debugging menu
- // Tips for Getting Started:
- // 1. Use the Solution Explorer window to add/manage files
- // 2. Use the Team Explorer window to connect to source control
- // 3. Use the Output window to see build output and other messages
- // 4. Use the Error List window to view errors
- // 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
- // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement