Update console without flickering - c++

前端 未结 3 1386
庸人自扰
庸人自扰 2020-12-04 13:33

I\'m attempting to make a console side scrolling shooter, I know this isn\'t the ideal medium for it but I set myself a bit of a challenge.

The problem is that whene

3条回答
  •  臣服心动
    2020-12-04 14:21

    One method is to write the formatted data to a string (or buffer) then block write the buffer to the console.

    Every call to a function has an overhead. Try go get more done in a function. In your Output, this could mean a lot of text per output request.

    For example:

    static char buffer[2048];
    char * p_next_write = &buffer[0];
    for (int y = 0; y < MAX_Y; y++)
    {
        for (int x = 0; x < MAX_X; x++)
        {
            *p_next_write++ = battleField[x][y];
        }
        *p_next_write++ = '\n';
    }
    *p_next_write = '\0'; // "Insurance" for C-Style strings.
    cout.write(&buffer[0], std::distance(p_buffer - &buffer[0]));
    

    I/O operations are expensive (execution-wise), so the best use is to maximize the data per output request.

提交回复
热议问题