Moving multiple objets simultaneously.

Moving objets simultaneously.

See version :

Dépendances (dans l'archive) :
balle.bmp

Download :

#include <sdl/sdl.h>

#pragma comment(lib,"sdl.lib")
#pragma comment(lib,"sdlmain.lib")

#define XRESOL 1000
#define YRESOL 800
#define FULLSCREEN 0
#define NBSPRITE 2000

struct Sprite
{
    int x,y;
    int vx,vy;
    SDL_Surface* srf;
};

SDL_Surface* Charger(const char* fic)
{
    SDL_Surface *res;
    SDL_Surface* tmp = SDL_LoadBMP(fic);
    if (tmp==NULL)
    {
        printf("Erreur chargement %s\n",fic);
        exit(-1);
    }
    res = SDL_DisplayFormat(tmp);
    SDL_FreeSurface(tmp);
    return res;
}

int KeyHit()
{
    SDL_Event e;
    if (SDL_PollEvent(&e))
        if (e.type == SDL_KEYDOWN)
            return 1;
    return 0;
}

void Blit(SDL_Surface* source,SDL_Surface* dest,int x,int y)
{
    SDL_Rect R;
    R.x = x;
    R.y = y;
    SDL_BlitSurface(source,NULL,dest,&R);
}

void Initialiser(struct Sprite* sp,SDL_Surface* srf)
{
    sp->x = rand()%(XRESOL-50);
    sp->y = rand()%(YRESOL-50);
    sp->vx = rand()%7-3;
    sp->vy = rand()%7-3;
    if (sp->vx==0 && sp->vy==0)
        sp->vx = 1;
    sp->srf = srf;
}

void Evoluer(struct Sprite* sp)
{
    sp->x+=sp->vx;
    sp->y+=sp->vy;
    if (sp->vx<0 && sp->x<0)
        sp->vx = -sp->vx;
    if (sp->vy<0 && sp->y<0)
        sp->vy = -sp->vy;
    if (sp->vx>0 && sp->x>(XRESOL-50))
        sp->vx = -sp->vx;
    if (sp->vy>0 && sp->y>(YRESOL-50))
        sp->vy = -sp->vy;
}

int main(int argc,char** argv)
{
    struct Sprite TabSprite[NBSPRITE];
    SDL_Surface* balle,*screen;
    int i;
    SDL_Init(SDL_INIT_VIDEO);
    if (FULLSCREEN)
        screen=SDL_SetVideoMode(XRESOL,YRESOL,32,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);  
    else
        screen=SDL_SetVideoMode(XRESOL,YRESOL,32,SDL_SWSURFACE|SDL_DOUBLEBUF);  
    balle=Charger("balle.bmp");
    SDL_SetColorKey(balle,SDL_SRCCOLORKEY ,SDL_MapRGBA(balle->format,0,0,0,0));
    for(i=0;i<NBSPRITE;i++)
    {
        Initialiser(&TabSprite[i],balle);
    }
    while(!KeyHit())
    {
        SDL_FillRect(screen,NULL,0);
        for(i=0;i<NBSPRITE;i++)
        {
            Evoluer(&TabSprite[i]);
            Blit(TabSprite[i].srf,screen,TabSprite[i].x,TabSprite[i].y);
        }
        SDL_Flip(screen);
    }
    SDL_FreeSurface(balle);
    SDL_Quit();
    return 0;
}




Explanations

	No explanations yet.