J´ai un énorme problème. Je suis en train de m´entrainer a la lib sdl, alors j´ai créer plusieurs balles qui rebondissaient contre l´écran. Puis j´ai voulu y rajouter un objet qui bougerait en fonction de la souris, et maintenant, le programme se lance, mais les images ne s´affichent qu´au bout de 1 min. Puis les boules ont déja avancées, mais elles restent immobiles, plus rien ne bouge. Mais je peux quand meme faire return et quitter le programme...
Voici mon code :
- include <SDL/SDL.h>
- include <windows.h>
SDL_Surface* screen;
SDL_Event event;
char key[400];
int xsouris=0,ysouris=0;
void init_SDL()
{
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(800, 600, 32,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
SDL_ShowCursor(0);
}
SDL_Rect Rect(int x,int y,int w=0,int h=0)
{
SDL_Rect r;
r.x=x;
r.y=y;
r.w=w;
r.h=h;
return r;
}
void UpdateEvents()
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
key[event.key.keysym.sym]=1;
break;
case SDL_KEYUP:
key[event.key.keysym.sym]=0;
break;
case SDL_MOUSEMOTION:
xsouris=event.motion.x;
ysouris=event.motion.y;
break;
}
}
}
SDL_Surface* LoadBMP(char* fichier,int vram=1)
{
SDL_Surface* f = SDL_LoadBMP(fichier);
SDL_Surface* r=NULL;
if (vram)
r=SDL_CreateRGBSurface(SDL_HWSURFACE, f->w, f->h, 32, 0, 0, 0, 0);
if (r==NULL) vram=0;
if (!vram)
r=SDL_CreateRGBSurface(SDL_SWSURFACE, f->w, f->h, 32, 0, 0, 0, 0);
SDL_Rect R=Rect(0,0,f->w,f->h);
SDL_BlitSurface(f,NULL,r,&R); M
SDL_FreeSurface(f);
return r;
}
int main(int argc ,char **argv)
{
int s=1;
int sx1=1,sy1=1, sx2=1,sy2=1, sx3=1,sy3=1, sx4=1,sy4=1;
SDL_Rect rect1;
SDL_Rect rect2;
SDL_Rect rect3;
SDL_Rect rect4;
SDL_Rect rect5;
SDL_Surface* firstscreen;
SDL_Surface* balle1;
SDL_Surface* balle2;
SDL_Surface* balle3;
SDL_Surface* balle4;
SDL_Surface* plaque;
int fini=0;
init_SDL();
// on dessine
rect1.x = 100;
rect1.y = 100;
rect2.x = 90;
rect2.y = 90;
rect3.x = 75;
rect3.y = 75;
rect4.x = 55;
rect4.y = 55;
rect5.x = 300;
rect5.y = 570;
firstscreen=LoadBMP("font.bmp");
balle1=LoadBMP("boule_de_feu.bmp");
balle2=LoadBMP("boule_de_feu2.bmp");
balle3=LoadBMP("boule_de_feu3.bmp");
balle4=LoadBMP("boule_de_feu4.bmp");
plaque=LoadBMP("plaque.bmp");
SDL_SetColorKey(balle1,SDL_SRCCOLORKEY ,SDL_MapRGBA(balle1->format,255,0,255,0));
SDL_SetColorKey(balle2,SDL_SRCCOLORKEY ,SDL_MapRGBA(balle2->format,255,0,255,0));
SDL_SetColorKey(balle3,SDL_SRCCOLORKEY ,SDL_MapRGBA(balle3->format,255,0,255,0));
SDL_SetColorKey(balle4,SDL_SRCCOLORKEY ,SDL_MapRGBA(balle4->format,255,0,255,0));
SDL_SetColorKey(plaque,SDL_SRCCOLORKEY ,SDL_MapRGBA(plaque->format,255,0,255,0));
while(1)
{
UpdateEvents();
if (key[SDLK_RETURN])
break;
rect1.x+=8*sx1;
rect1.y+=8*sy1;
if (rect1.x<0) sx1=1;
if (rect1.y<0) sy1=1;
if (rect1.x>700) sx1=-1;
if (rect1.y>500) sy1=-1;
rect2.x+=6*sx2;
rect2.y+=6*sy2;
if (rect2.x<0) sx2=1;
if (rect2.y<0) sy2=1;
if (rect2.x>720)sx2=-1;
if (rect2.y>520)sy2=-1;
rect3.x+=4*sx3;
rect3.y+=4*sy3;
if (rect3.x<0) sx3=1;
if (rect3.y<0) sy3=1;
if (rect3.x>740)sx3=-1;
if (rect3.y>540) sy3=-1;
rect4.x+=6*sx4;
rect4.y+=6*sy4;
if (rect4.x<0) sx4=1;
if (rect4.y<0) sy4=1;
if (rect4.x>760) sx4=-1;
if (rect4.y>560) sy4=-1;
rect5.x = 100;
SDL_BlitSurface(firstscreen,NULL,screen,NULL);
SDL_BlitSurface(balle1,NULL,screen,&rect1);
SDL_BlitSurface(balle2,NULL,screen,&rect2);
SDL_BlitSurface(balle3,NULL,screen,&rect3);
SDL_BlitSurface(balle4,NULL,screen,&rect4);
SDL_BlitSurface(plaque,NULL,screen,&rect5);
SDL_Flip(screen);
}
SDL_Quit();
SDL_ShowCursor(1);
return 0;
}
Si jamais vous trouvez quelquonque problème dans mon code, n´hésitez pas !