CreateConsoleScreenBuffer() 와 SetConsoleActiveScreenBuffer()를 이용하여, 콘솔에서 더블버퍼링을 구현해보았다.

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

void PrintConsoleScreen(const int nCurView, HANDLE hConsoleBuf[2], LPCTSTR lpCharacter, COORD dwWriteCoord);
void UpdateScreen(int *nCurView, HANDLE hConsoleBuf[2]);

void DrawRectangle(){
 char Mark[21][3] = { "☆","★","○","●","◎","◇","◆","□","■","△","▲","▽","▼","◈","▣","♤","♠","♡","♥","♧","♣"};
 int x,y;
 system("cls");
 for(y=0; y<20; y++){
  for(x=0; x<30; x++){
   printf("%s", Mark[rand()%21]);
  }
  printf("\n");
 }
}

void DrawRectangleBuffering(int *nCurView, HANDLE hConsoleBuf[2]){
 COORD pos = {0, 0};
 int x,y;
 
 LPCTSTR Mark[21] = { _T("☆"),_T("★"),_T("○"),_T("●"),_T("◎"),_T("◇"),_T("◆"),_T("□"),_T("■"),_T("△"),_T("▲"),_T("▽"),_T("▼"),_T("◈"),_T("▣"),_T("♤"),_T("♠"),_T("♡"),_T("♥"),_T("♧"),_T("♣")};
 
 for(y=0; y<20; y++){
  pos.Y = y;
  for(x=0; x<30; x++){
   pos.X = 2*x;
   PrintConsoleScreen(*nCurView, hConsoleBuf, Mark[rand()%21],  pos);
  }
 }
 UpdateScreen(nCurView, hConsoleBuf);
}

int main(int argc, const char *argv[]){
 HANDLE hConsoleBuf[2];
 int nCurView=0;
 bool bFlag;
 int i=0;
 hConsoleBuf[0] = CreateConsoleScreenBuffer(GENERIC_READ|GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
 hConsoleBuf[1] = CreateConsoleScreenBuffer(GENERIC_READ|GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
 while(i++<100){
  //DrawRectangleBuffering(&nCurView, hConsoleBuf);
  DrawRectangleA();
  Sleep(1000);
 }
 return 0;
}
void PrintConsoleScreen(const int nCurView, HANDLE hConsoleBuf[2], LPCTSTR lpCharacter, COORD dwWriteCoord)
{
 WriteConsoleOutputCharacter(hConsoleBuf[nCurView], lpCharacter, _tcslen(lpCharacter), dwWriteCoord, NULL);
}
void UpdateScreen(int *nCurView, HANDLE hConsoleBuf[2])
{
 SetConsoleActiveScreenBuffer(hConsoleBuf[*nCurView]);
 *nCurView = (*nCurView+1)%2;
}




Posted by Нуеоп
,