void gravity (double* affectedGravity,double gravityWanted);
int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL;
SDL_Surface *carre = NULL;
SDL_Surface *mur = NULL;
int continuer = 1;
SDL_Event event;
SDL_Rect positioncarre;
SDL_Rect positionmur;
SDL_Init(SDL_INIT_VIDEO);
putenv("SDL_VIDEO_WINDOW_POS=center"); // Centre la fenêtre
ecran = SDL_SetVideoMode(1000, 800, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE );
carre = SDL_CreateRGBSurface(SDL_HWSURFACE, 100, 100, 32, 0, 0, 0, 0);
mur = SDL_CreateRGBSurface(SDL_HWSURFACE, 200, 300, 32, 0, 0, 0, 0);
SDL_WM_SetCaption("Test jeu 2D", NULL);
positioncarre.x = 500;
positioncarre.y = 200;
positionmur.x = 475;
positionmur.y = 425;
double v_x = 0;
double v_y = 0;
double v_saut = -4;
double v_grav = 0.08;
while(continuer)
{
while (SDL_PollEvent(&event))
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_RIGHT: v_x = 1;break;
case SDLK_LEFT: v_x = -1; break;
case SDLK_UP: v_y = -1;break;
case SDLK_DOWN: v_y = 1;break;
case SDLK_SPACE: v_y = v_saut;gravity(&v_grav, 0.08); break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym)
{
case SDLK_RIGHT: if( v_x == 1) v_x = 0; break;
case SDLK_LEFT: if(v_x == -1) v_x = 0; break;
case SDLK_UP: if(v_y == -1) v_y = 0; break;
case SDLK_DOWN: if(v_y == 1) v_y = 0; break;
case SDLK_SPACE: if(v_y == v_saut) v_y = 0; break;
}
break;
}
if(!((positioncarre.x+v_x) + positioncarre.w <= positionmur.x
|| (positioncarre.x+v_x) >= positionmur.x + positionmur.w
|| (positioncarre.y+v_y) + positioncarre.h <= positionmur.y
|| (positioncarre.y+v_y) >= positionmur.y + positionmur.h))
{
v_x = 0;
v_y = 0;
gravity(&v_grav, 0);
positioncarre.y -=1;
}
else if((positioncarre.x+v_x) + positioncarre.w < positionmur.x || (positioncarre.x+v_x) > positionmur.x + positionmur.w)
{
gravity(&v_grav, 0.08);
}
positioncarre.x += v_x;
positioncarre.y += v_y;
v_y += v_grav;
SDL_Delay(5);
SDL_FillRect(mur, NULL, SDL_MapRGB(mur->format, 0, 0, 255));
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
SDL_FillRect(carre, NULL, SDL_MapRGB(carre->format, 255, 255, 255));
SDL_BlitSurface(mur, NULL, ecran, &positionmur);
SDL_BlitSurface(carre, NULL, ecran, &positioncarre);
SDL_Flip(ecran);
}
SDL_FreeSurface(mur);
SDL_FreeSurface(carre);
SDL_Quit();
return EXIT_SUCCESS;
}
void gravity (double* affectedGravity,double gravityWanted)
{
*affectedGravity = gravityWanted;
}