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 Нуеоп
,

#3. OpenGL 선 그려보기

OpenGL 2011. 7. 12. 18:56
무작위 선을 50~250개 그려보았다.

소스
 
 1 #include <stdio.h>
 2 #include <GL/glut.h>
 3
 4 void drawRandomLines(int num){
 5     static GLfloat width = 2.0;
 6     static GLfloat height = 2.0;
 7     int i=0;
 8     GLfloat x,y;
 9     inline void drawLine(GLfloat x, GLfloat y){
10         glVertex2f(x,y);
11     }
12    
13     glBegin(GL_LINES);
14     {   
15         for(i=0; i<num; i++){
16             glVertex2f(0.0,0.0);
17             glColor3f(rand()%10/10.0, rand()%10/10.0, rand()%10/10.0);
18             x = (rand()%100)/50.0 - width/2.0;
19             y = (rand()%100)/50.0 - height/2.0;
20             drawLine(x,y);
21         }
22     }
23     glEnd();
24 }
25
26 void DoDisplay()
27 {
28     glClear(GL_COLOR_BUFFER_BIT);
29     drawRandomLines(250);
30     glFlush();
31 }
32
33 int main(int argc, char *argv[])
34 {
35     glutInit( &argc, argv );
36     glutCreateWindow("OpenGL");
37     glutDisplayFunc(DoDisplay);
38     glutMainLoop();
39 }

결과


Posted by Нуеоп
,
소스
/home/hyeon/programming/opengl/sample4.c.html  
1 #include <GL/glut.h>
 2
 3 void drawRectangle(
GLfloat x, GLfloat y, GLfloat r, GLfloat g, GLfloat b)
 4 {
 5     static
GLfloat width = 1.0;
 6     static 
GLfloat height = 1.0;
 7
 8     glBegin(GL_POLYGON);
 9     {
10         glColor3f(r,g,b);
11         glVertex2f(x,y);
12         glColor3f(r,b,g);
13         glVertex2f(x+width,y);
14         glColor3f(g,r,b);
15         glVertex2f(x+width,y+height);
16         glColor3f(b,g,r);
17         glVertex2f(x,y+height);
18     }
19     glEnd();
20 }
21
22 void DoDisplay()
23 {
24     glClear(GL_COLOR_BUFFER_BIT);
25     drawRectangle(-0.5,-0.5, 0.8,0.2,0.1);
26     glFlush();
27 }
28
29 int main(int argc, char *argv[])
30 {
31     glutInit( &argc, argv );
32     glutCreateWindow("OpenGL");
33     glutDisplayFunc(DoDisplay);
34     glutMainLoop();
35 }

컴파일
$ gcc -o sample4 sample4.c -lglut


실행 화면


'OpenGL' 카테고리의 다른 글

#4. OpenGL koch snowflake (코흐 눈송이)  (0) 2011.08.03
#3. OpenGL 선 그려보기  (0) 2011.07.12
#1. OpenGL 설치 및 예제 파일 실행  (0) 2011.07.12
Ubuntu에 OpenGL 설치하기  (0) 2011.07.12
Posted by Нуеоп
,