0
# include "DISPLAY.h"


DoubleBuffer::DoubleBuffer()
{
    COORD size = { WINDOW_X_SIZE , WINDOW_Y_SIZE };
    SMALL_RECT rect;
    rect.Left = 0;
    rect.Right = WINDOW_X_SIZE - 1;
    rect.Top = 0;
    rect.Bottom = WINDOW_Y_SIZE - 1;

    hBuffer[0] = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    SetConsoleScreenBufferSize(hBuffer[0], size);
    SetConsoleWindowInfo(hBuffer[0], TRUE, &rect);

    hBuffer[1] = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    SetConsoleScreenBufferSize(hBuffer[1], size);
    SetConsoleWindowInfo(hBuffer[1], TRUE, &rect);

    CONSOLE_CURSOR_INFO cursorinfo;
    cursorinfo.dwSize = 1;
    cursorinfo.bVisible = FALSE;

    SetConsoleCursorInfo(hBuffer[0], &cursorinfo);
    SetConsoleCursorInfo(hBuffer[1], &cursorinfo);

    nBufferIndex = 0;
}

void DoubleBuffer::WriteBuffer(int x, int y, char *string)
{
    DWORD dw;
    COORD startposition = { x,y };

    SetConsoleCursorPosition(hBuffer[nBufferIndex], startposition);
    WriteFile(hBuffer[nBufferIndex], string, strlen(string), &dw, NULL);
}

void DoubleBuffer::FlippBuffer()
{
    Sleep(1000);
    SetConsoleActiveScreenBuffer(hBuffer[nBufferIndex]);
    nBufferIndex = !nBufferIndex;
}

void DoubleBuffer::ClearBuffer()
{
    COORD coord = { 0,0 };
    DWORD dw;
    FillConsoleOutputCharacter(hBuffer[nBufferIndex], ' ', WINDOW_X_SIZE*WINDOW_Y_SIZE, coord, &dw);
}

void DoubleBuffer::ReleaseBuffer()
{
    CloseHandle(hBuffer[0]);
    CloseHandle(hBuffer[1]);
}

I used this code to construct a double buffer.

The entire game map which would be printed at the function 'WriteBuffer' was designed as a 1D char array.

But a big problem is that if the length of string goes over 80, then the console just shows 80 character in one line regardless of the console window size.

I mean, for example the input string is

    char 1D_map[90] = {'D'};

and the console window size is 5*20.

Then no matter what the window size is, it shows like behind.

DDDDD (.. the left 70 characters are hidden..)

DDDDD (.. the left 5 character are hidden..)

As I pull the side bar of window to its maximum, it shows like behind.

DDDDDDDDDDDDDDDD ... 80 characters ... DDDDDDDDDDDDDDDDD

DDDDDDDDDD

So when I use these codes, then I cannot adjust the game map size. (concretely horizontal size.)

Because whatever I do, it just shows 80 characters in a line!!

Can I fix the problem? or Should I apply other double buffering method?

  • Have you considered adding a line break after every 20 characters? – Joshua Waring Aug 20 '17 at 09:42
  • @JoshuaWaring Well... You mean adding a new line character such as '\n' ? I consider this one. But if do so, there are two problems. First, I designed a map in 2D array ,so it becomes al little bit complicated to program. Second, when I enlarge the size of window more than 80 on the horizontal axis, I cannot show them in one line. For example, I want to show 100 characters in one line, but the maximum size of horizontal axis is still 80 characters. – 이채운 Aug 20 '17 at 11:45

0 Answers0