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
|
SDL_Surface *screen;
SDL_Surface *srcbuf;
SDL_Surface *bg;
void initSDL();
int main(int argc, char *argv[])
{
int done = 0;
SDL_Event event;
/*rec : rectangle temporaire chargé sur scrbuf
dst : rectangle qui va recevoir rec sur screen
recPcache : rectangle temporaire chargé sur srcbuf
dstPcache : rectangle qui va recevoir recPcache sur screen et qui va cacher l'ancienne position de dst
*/
SDL_Rect rec, dst, recPcache, dstPcache, recbg;
Uint8 *keys;
/* INITIALISATION DES SURFACES */
initSDL();//initialisation de screen
srcbuf = SDL_CreateRGBSurface
(
SDL_SWSURFACE,
640,
480,
screen->format->BitsPerPixel,
screen->format->Rmask,
screen->format->Gmask,
screen->format->Bmask,
screen->format->Amask
);
bg = SDL_LoadBMP("bg.bmp");//chargement d'une image de fond dans la surface bg
/*********************************/
rec.x=0;
rec.y=0;
rec.h=200;
rec.w=200;
recbg.x=0;
recbg.y=0;
recbg.h=200;
recbg.w=200;
dst.x = 100;
dst.y = 100;
dst.h = 200;
dst.w = 200;
recPcache.x = 200;
recPcache.y = 200;
recPcache.h = 200;
recPcache.w = 200;
dstPcache.x = 100;
dstPcache.y = 100;
dstPcache.h = 200;
dstPcache.w = 200;
//Enregistrement de rec sur srcbuf ainsi que recPcache
SDL_FillRect(srcbuf, &rec, SDL_MapRGB(srcbuf->format, 0, 0, 255));
SDL_FillRect(srcbuf, &recPcache, SDL_MapRGB(srcbuf->format, 0, 0, 0));
while(done == 0)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
done = 1;
break;
}
}
keys = SDL_GetKeyState(NULL);
if(keys[SDLK_UP])
{
dst.y-- ;
dstPcache.y = dst.y + 1;//on cache la position d'avant
}
if(keys[SDLK_DOWN])
{
dst.y++;
dstPcache.y = dst.y - 1;//on cache la position d'avant
}
if(keys[SDLK_RIGHT])
{
dst.x++;
dstPcache.x = dst.x - 1;//on cache la position d'avant
}
if(keys[SDLK_LEFT])
{
dst.x--;
dstPcache.x = dst.x + 1;//on cache la position d'avant
}
SDL_BlitSurface(bg, NULL, screen, &recbg);//on charge l'arriere plan
SDL_BlitSurface(srcbuf, &recPcache, screen, &dstPcache);//on charge le rectangle pour cacher
SDL_BlitSurface(srcbuf, &rec, screen, &dst);//on charge le rectangle visible
dstPcache.x = dst.x;//mise a jour des cordonnées du rectangle pour caché
dstPcache.y = dst.y;
SDL_Flip(screen);
}
SDL_FreeSurface(srcbuf);
SDL_FreeSurface(bg);
return 0;
} |
Partager