Bonjour,
Je programme un jeu de course poursuite avec GLUT en utilisant microsoft visual C++. Je tiens à préciser que c’est la première fois que j’utilise du C++ et GLUT, donc je suis une novice… En gros, il y a un missile dirigé par l’utilisateur qui doit détruire un territoire ennemi et un anti-missile qui doit l’intercepter et qui est dirigé par l’ordinateur. Avant le début du jeu, l’utilisateur doit pouvoir choisir la caractéristique de vitesse de son missile et de l’anti-missile (c’est-à-dire dessiner un polygone autour du missile), ainsi que dessiner la forme du territoire à détruire.
Pour l’instant, j’ai programmé une fenêtre où l’on peut dessiner un polygone point par point, il y a un menu (j’aurais préféré des boutons, mais apparemment GLUT ne fait pas ça…) attaché au click droit de la souris et je voudrais que lorsque l’on choisit OK, la fenêtre se ferme, et en ouvre une autre (pour dessiner un autre polygone). Mais j’ai gros bug. Est-ce que j’ai fait une erreur de programmation ??
Est-ce que l’on peut intégrer du GLUT avec du visual C++. Je m’explique : je voudrais faire des interfaces utilisateurs claires (et non pas utiliser un terminal) avec des boites de dialogues,… Mais GLUT n’est pas vraiment fait pour ça. Par exemple je voudrais créer une boite de dialogue en visual C++ avec un bouton OK qui lance une fenêtre GLUT. Est-ce que je rêve ou est-ce possible ?
Encore un autre problème : pour le déplacement du missile, j’ai écrit un petit programme en GLUT qui permet de le diriger via les flèches du clavier (flèche du haut pour aller vers le haut de la fenêtre,…). Seulement ce n’est pas très réaliste car le missile peut faire une rotation de 180° d’un coup et il ne suit d’ailleurs pas le profil de vitesse qui lui est imposé au début du jeu…. Est-ce que vous avez une idée pour programmer un déplacement plus « intelligent » ?
Merci
Olga
[code]#include <iostream.h>
#include <gl/glut.h>
#include<gl/gl.h>
unsigned int n=0;
int vertices[100];
int windowWidth=200, windowHeight=200;
int a=windowWidth/2,b=windowHeight/2;
int window=0,w=1;
unsigned int i;
enum {OK,CANCEL,CLEAR} string;
void changeSize(int w, int h)
{
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
windowWidth=w;
windowHeight=h;
gluOrtho2D(0,windowWidth,windowHeight,0);
// Set the viewport to be the entire window
// Set the correct perspective.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Display the first window for drawing the velocity indicatrix of the missile
void displayWindow1(void)
{
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(0,0,0);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
// Display gradually a polygon (the speed profil)
if (n>4)
{
glBegin(GL_POLYGON);
for (i=0;i<n;i=i+2)
glVertex2f(vertices[i],vertices[i+1]);
}
// Display every chosen point of the polygon
for (i=0;i<n;i=i+2)
{
glColor3f(0,0,0);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glBegin(GL_POLYGON);
glVertex2f(-2+vertices[i],-2+vertices[i+1]);
glVertex2f(2+vertices[i],-2+vertices[i+1]);
glVertex2f(2+vertices[i],2+vertices[i+1]);
glVertex2f(-2+vertices[i],2+vertices[i+1]);
glEnd();
}
glPopMatrix();
glColor3f(1,0,0);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
// Display a square representing the missile
glBegin(GL_POLYGON);
glVertex2f(-4+windowWidth/2,-4+windowHeight/2);
glVertex2f(4+windowWidth/2,-4+windowHeight/2);
glVertex2f(4+windowWidth/2,4+windowHeight/2);
glVertex2f(-4+windowWidth/2,4+windowHeight/2);
glEnd();
glFlush();
glutSwapBuffers();
}
// Create the function which allow to add points to the polygon
// when the user click in the window
void processMouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
{
a=x;
b=y;
vertices[n]=a;
vertices[n+1]=b;
n=n+2;
glutIdleFunc(displayWindow1);
break;
}
if (state == GLUT_UP)
glutIdleFunc(NULL);
default:
glutIdleFunc(NULL);
break;
}
}
// Assign a function to each item of the menu
void menu(int choice)
{
switch(choice)
{
case OK :
{
glutDestroyWindow(w);
w++;
break;
}
case CLEAR:
{
n=0;
break;
}
case CANCEL:
{
if (n>0)
n=n-2;
break;
}
}
glutPostRedisplay();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(500,300);
glutInitWindowSize(windowWidth,windowHeight);
if (w==1) // The user draws the first polygon
{
glutCreateWindow("Fenêtre 1");
glutSetWindow(w);
int m =glutCreateMenu(menu);
glutSetMenu(m);
glutAddMenuEntry("OK",OK);
glutAddMenuEntry("Cancel",CANCEL);
glutAddMenuEntry("Clear",CLEAR);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMouseFunc(processMouse);
glutDisplayFunc(displayWindow1);
glutIdleFunc(displayWindow1);
}
if (w==2) // The user draws the second polygon
{
glutCreateWindow("Fenêtre 2");
glutSetWindow(w);
int m =glutCreateMenu(menu);
glutSetMenu(m);
glutAddMenuEntry("OK",OK);
glutAddMenuEntry("Cancel",CANCEL);
glutAddMenuEntry("Clear",CLEAR);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMouseFunc(processMouse);
glutDisplayFunc(displayWindow1);
glutIdleFunc(displayWindow1);
}
glutReshapeFunc(changeSize);
glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}[/code]