Un carré au milieu d'une fenêtre noire.
Un clic du bouton gauche de la souris permet de changer sa couleur
et d'afficher la position de la souris.
Un clic du bouton droit permet de quitter l'application.
#include <stdio.h> #include <windows.h> #include <GL/gl.h> #include <GL/glut.h> int c = 0 ; void mouse(int bouton,int etat,int x,int y) { if ( etat == GLUT_DOWN ) switch ( bouton ) { case GLUT_LEFT_BUTTON : c = (c+1)%7; printf("%4d %4d\n",x,y); glutPostRedisplay(); break ; case GLUT_RIGHT_BUTTON : exit(0); break; } } void display(void) { glClearColor(0.0F,0.0F,0.0F,0.0F) ; glClear(GL_COLOR_BUFFER_BIT) ; glOrtho(-1.0,1.0,-1.0,1.0, -1.0,1.0); switch(c) { case 0 : glColor3f(1.0F,1.0F,1.0F) ; break ; case 1 : glColor3f(1.0F,1.0F,0.0F) ; break ; case 2 : glColor3f(1.0F,0.0F,1.0F) ; break ; case 3 : glColor3f(0.0F,1.0F,1.0F) ; break ; case 4 : glColor3f(0.0F,1.0F,0.0F) ; break ; case 5 : glColor3f(0.0F,0.0F,1.0F) ; break ; case 6 : glColor3f(1.0F,0.0F,0.0F) ; break ; } glBegin(GL_POLYGON) ; glVertex2f(-0.5F,-0.5F) ; glVertex2f(-0.5F,0.5F) ; glVertex2f(0.5F,0.5F) ; glVertex2f(0.5F,-0.5F) ; glEnd() ; glFlush() ; } void main(int argc,char **argv) { glutInit(&argc,argv); glutInitWindowSize(200,200); glutInitWindowPosition(100,100); glutInitDisplayMode(GLUT_RGBA|GLUT_SINGLE); glutCreateWindow("Carre de couleur") ; glutDisplayFunc(display) ; glutMouseFunc(mouse) ; glutMainLoop() ; }
RETOUR