一个简单的基于双缓冲区的控制台绘制类 C++实现

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <vector>


// 头文件
class Drawer {
private:
    char** frame_;
    HANDLE output, buffer;
    COORD coord = { 0,0 };
    DWORD bytes = 0;

    int control_ = 0; // 用于切换输出区和缓冲区
    int lenth_; // 绘制区域长度
    int width_;// 绘制区域宽度
    double fps_; // 帧数
public:
    Drawer(int len = 10,int wid = 10, double fps = 10) :lenth_(len), width_(wid), fps_(1000.0 / fps) {
        if(lenth_ < 0)
            lenth_ = 15;
        if(width_ < 0)
            width_ = 15;
        if(fps_ < 0)
            fps_ = 20;
        Init();
    }
    void Init();
    void GetFrame(char** frame) {
        frame_ = frame;
    }
    void Draw();
};

// 实现
void Drawer::Draw() {
    for (int i = 0; i < width_; i++)
    {
        coord.Y = i;
        if (!control_)
            WriteConsoleOutputCharacterA(buffer, frame_[i], lenth_, coord, &bytes);
        else
            WriteConsoleOutputCharacterA(output, frame_[i], lenth_, coord, &bytes);
    }
    if (!control_)
        SetConsoleActiveScreenBuffer(buffer);
    else

        SetConsoleActiveScreenBuffer(output);
    Sleep(fps_);
    control_ = !control_;
}

void::Drawer::Init() {
    buffer = CreateConsoleScreenBuffer(
        GENERIC_WRITE,
        FILE_SHARE_WRITE,
        NULL,
        CONSOLE_TEXTMODE_BUFFER,
        NULL
    );
    output = CreateConsoleScreenBuffer(
        GEN
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值