Da się, ale jest to słaby pomysł.
#include <iostream>
#include <windows.h>
#include <cstdint>
#include <chrono>
#include <vector>
#include <thread>
class FlashingChar
{
public:
FlashingChar(const char argCharacter,
const std::int16_t argX, const std::int16_t argY,
const double argInterval = 0.7)
: character(argCharacter), x(argX), y(argY),
interval(argInterval), visible(false), currentTime(0)
{
}
void Update(const double deltaTime)
{
currentTime += deltaTime;
while (currentTime >= interval)
{
currentTime -= interval;
visible = !visible;
this->modify();
}
}
void modify()
{
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = { this->x, this->y };
COORD currentPos;
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(output, &csbi);
currentPos = csbi.dwCursorPosition;
}
{
SetConsoleCursorPosition(output, pos);
DWORD dwBytesWritten = 0;
char space = ' ';
if (this->visible)
WriteConsoleOutputCharacterA(output, &(this->character), 1, pos, &dwBytesWritten);
else
WriteConsoleOutputCharacterA(output, &space, 1, pos, &dwBytesWritten);
}
SetConsoleCursorPosition(output, currentPos);
}
char character;
std::int16_t x, y;
double interval;
double currentTime;
bool visible;
};
int main() {
std::vector<FlashingChar> characters = {
{ 'Z', 10, 1, 0.4 },
{ 'A', 15, 2, 0.9 },
{ ']', 4, 3, 0.7 },
{ 'x', 12, 7, 0.2 },
};
auto getCurrentTime = []() {
return std::chrono::high_resolution_clock::now();
};
auto lastTime = getCurrentTime();
double deltaTime = 1.0 / 60.0;
while (true)
{
deltaTime = std::chrono::duration<double>(getCurrentTime() - lastTime).count();
lastTime = getCurrentTime();
for (auto &character : characters)
{
character.Update(deltaTime);
}
auto frameTime = std::chrono::duration<double>(getCurrentTime() - lastTime).count();
if (frameTime < 1.0 / 60.0)
std::this_thread::sleep_for(std::chrono::duration<double>(1.0 / 60.0 - frameTime));
}
return 0;
}