Drawing colors to console causes color flicker

瘦欲@ 提交于 2020-01-17 15:51:08

问题


This is my code. It experimentally generates various shades of colors. It is then set onto a loop that displays it in a framerate of 60fps, as for example a video game would be expected to.

#include <windows.h>
#include <stdint.h>

int main() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD start = {0, 0};
    DWORD b = 0;
    char shades[2000];
    for (int i=0; i<2000; i+=80){
        for (int k=0; k<16; k++){
            shades[i+k]=32;
        }
        for (int k=16; k<32; k++){
            shades[i+k]=176;
        }
        for (int k=32; k<48; k++){
            shades[i+k]=177;
        }
        for (int k=48; k<64; k++){
            shades[i+k]=178;
        }
        for (int k=64; k<80; k++){
            shades[i+k]=219;
        }
    }
    uint16_t colors[2000];
    for (int i=0; i<2000; i++){
        colors[i] = (uint8_t)((i%16)+((i/80)*16));
    }
    uint8_t flag = 0;
    while (!flag){
        SetConsoleCursorPosition(hConsole, start);
        WriteConsole(hConsole, shades, 1280, NULL, NULL);
        WriteConsoleOutputAttribute(hConsole, colors, 1280, start, &b);
        Sleep(16);
    }
    return 0;
}

This should be run at 80 characters per line.

The thing is, when this is run, it causes a flicker effect that only affects the foreground and background color. The screen will flicker between the default colors and the assigned colors.

That is, it flickers between those two images:

rather than displaying the latter one. But this flicker effect is unexpected, as actual video games don't flicker like that despite having graphics display at 60fps.


回答1:


This is not a proper solution

An alternative method will actually not flicker:

#include <windows.h>
#include <stdint.h>

int main() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD start = {0, 0};
    DWORD b = 0;
    char shades[2000];
    for (int i=0; i<2000; i+=80){
        for (int k=0; k<16; k++){
            shades[i+k]=32;
        }
        for (int k=16; k<32; k++){
            shades[i+k]=176;
        }
        for (int k=32; k<48; k++){
            shades[i+k]=177;
        }
        for (int k=48; k<64; k++){
            shades[i+k]=178;
        }
        for (int k=64; k<80; k++){
            shades[i+k]=219;
        }
    }
    uint16_t colors[2000];
    for (int i=0; i<2000; i++){
        colors[i] = (uint8_t)((i%16)+((i/80)*16));
    }
    CHAR_INFO screen[2000];
    for (int i=0; i<2000; i++){
        screen[i].Char.AsciiChar = shades[i];
        screen[i].Attributes = colors[i];
    }
    SMALL_RECT screenrect;
    screenrect.Left = 0;
    screenrect.Top = 0;
    screenrect.Right = 79;
    screenrect.Bottom = 24;
    COORD screensize = {80, 25};
    uint8_t flag = 0;
    int o = 600;
    SMALL_RECT testscreenrect;
    testscreenrect.Left = 0;
    testscreenrect.Top = 0;
    testscreenrect.Right = 79;
    testscreenrect.Bottom = 15;
    COORD testscreensize = {80, 16};
    while (o){
        WriteConsoleOutput(hConsole, screen, testscreensize, start, &testscreenrect);
        o--;
    }
    return 0;
}

However, this is not a proper solution. It doesn't properly sync to 60fps, but rather runs at CPU speed.

Damn it! I thought I had it, but it doesn't really work properly!



来源:https://stackoverflow.com/questions/59438588/drawing-colors-to-console-causes-color-flicker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!