1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include <stdio.h>
#include <GL/glut.h>
float pong_x = 0.0;
float pong_y = -0.8;
float go_x = 0.0;
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(pong_x, pong_y);
glVertex2f(pong_x, pong_y+0.05);
glVertex2f(pong_x + 0.2, pong_y + 0.05);
glVertex2f(pong_x + 0.2, pong_y);
glEnd();
glutSwapBuffers();
}
void PongDisplay()
{
pong_x += go_x;
glutPostRedisplay();
}
void keyb(unsigned char key, int x, int y)
{
switch(key)
{
case 'a':
go_x = -0.001;
glutIdleFunc(PongDisplay);
break;
case 'd':
go_x = 0.001;
glutIdleFunc(PongDisplay);
break;
default:
glutIdleFunc(NULL);
break;
}
printf("%d\n", key);
}
void keybup(unsigned char key, int x, int y)
{
glutIdleFunc(NULL);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(300, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyb);
glutKeyboardUpFunc(keybup);
glutMainLoop();
return 0;
} |
Partager