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;
}
'c/c++' 카테고리의 다른 글
키보드 전역 후킹 (0) | 2011.08.29 |
---|---|
Console API 모음 (MSDN) (0) | 2011.07.14 |
쓰레드를 이용한 행렬 곱연산 (0) | 2010.09.16 |
c/c++ 2차원(이차원) 배열 동적할당 방법2 (0) | 2010.08.06 |
c언어 2차원(이차원) 배열 동적 할당 방법 (0) | 2010.08.03 |