Keybord

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <GL/glut.

h> // (or others, depending on the system in use)


#include <stdlib.h>
#include <iostream.h>
int x=100,y=100;
void init (void)
{
glClearColor (1.0, 0.0, 0.0, 0.0); // Set display-window color to white.
glPointSize(3.0);
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (0.0, 800.0, 0.0, 600.0);
}

void Draw()
{
glClear(GL_COLOR_BUFFER_BIT |GL_RGBA);
GLsizei r,g,b;
r=rand()%9;
g=rand()%7;
b=rand()%3;
glColor3f(r,g,b);
glBegin(GL_POINTS);
glVertex2i(x,y);

glEnd ( );

glFlush ( ); // Process all OpenGL routines as quickly as possible.

}
void keyboard(unsigned char key,int mousex,int mousey)
{
switch(key)
{
case 'w': //GLUT_KEY_LEFT:
case 'W':
y++;
break;
case 'x'://GLUT_KEY_RIGHT:
case'X':
y--;
break;
case 'a'://GLUT_KEY_UP:
case'A':
x--;
break;
case 'D'://GLUT_KEY_DOWN:
case'd':
x++;
break;
}
glutPostRedisplay();
glEnd();
glFlush();
}

void main (int argc, char** argv)


{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB); // Set display mode.
glutInitWindowPosition(50, 100); // Set top-left display-window position.
glutInitWindowSize(800,600); // Set display-window width and height.
glutCreateWindow ("An Example OpenGL Program"); // Create display window.

init ( ); // Execute initialization procedure.


glutDisplayFunc(Draw); // Send graphics to display window.
glutKeyboardFunc(keyboard);
glutMainLoop( ); // Display everything and wait.
}

You might also like