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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| int main(int argc, char *argv[])
{
bool continuer = 1;
double tempsA = 0, tempsP = 0, tempsA2 = 0, tempsP2 = 0;
int i, largeur, hauteur;
int coordonne[2];
bool collision = 0;
Forme carre1(500,500,1,2);
Forme carre2(500,350,1,3);
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Un joli \"carr\u00E9\"",NULL);
SDL_SetVideoMode(LARGEUR_ECRAN, HAUTEUR_ECRAN, 32, SDL_OPENGL);
SDL_Event event;
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
gluOrtho2D(0,LARGEUR_ECRAN,0,HAUTEUR_ECRAN);
while(continuer)
{
collision = carre2.collision(coordonne[0], coordonne[1], largeur, hauteur);//on test si il y a collision
SDL_PollEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_UP:
tempsA = SDL_GetTicks();
if (tempsA - tempsP > 20) /* Si 30 ms se sont écoulées depuis le dernier tour de boucle*/
{
if(collision == 0)
{
carre1.changerPos(0,1);
}
else{carre1.changerPos(0,-3);}
tempsP = tempsA ;
}
break;
case SDLK_LEFT:
tempsA2 = SDL_GetTicks();
if (tempsA2 - tempsP2 > 20) /* Si 30 ms se sont écoulées depuis le dernier tour de boucle*/
{
if(collision == 0)
{
carre1.changerPos(-1,0);
}
else{carre1.changerPos(3,0);}
tempsP2 = tempsA2 ;
}
break;
case SDLK_RIGHT:
tempsA2 = SDL_GetTicks();
if (tempsA2 - tempsP2 > 20) /* Si 30 ms se sont écoulées depuis le dernier tour de boucle*/
{
if(collision == 0)
{
carre2.rotation(1);
}
else{carre1.changerPos(-3,0);}
tempsP2 = tempsA2 ;
}
break;
case SDLK_DOWN:
tempsA2 = SDL_GetTicks();
if (tempsA2 - tempsP2 > 20) /* Si 30 ms se sont écoulées depuis le dernier tour de boucle*/
{
if(collision == 0)
{
carre1.changerPos(0,-1);
}
else{carre1.changerPos(0,3);}
tempsP2 = tempsA2 ;
}
break;
case SDLK_SPACE:
tempsA2 = SDL_GetTicks();
if (tempsA2 - tempsP2 > 20) /* Si 30 ms se sont écoulées depuis le dernier tour de boucle*/
{
if(collision == 0)
{
carre2.changerPos(0,1);
}
else{carre2.changerPos(0,-3);}
tempsP2 = tempsA2 ;
}
break;
}
break;
}
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
carre1.refreshForme();
carre2.refreshForme();
coordonne[0] = carre1.getCoorX();
coordonne[1] = carre1.getCoorY();
largeur = carre1.getLargeur();
hauteur = carre1.getHauteur();
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
} |
Partager