Scrolling horizontal et vertical.

Scrolling horizontal et vertical contrôlé au clavier.

Voir version :

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

Télécharger :

#include <sdl/sdl.h>

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

#define SPEED 1
#define IMX 368
#define IMY 388

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 negatmodulo(int x,int mod)
{
    if (x>=0)
        return x%mod;
    return (x%mod)+mod;
}

int main(int argc,char** argv)
{
    SDL_Surface* fond,*screen;
    Uint8* key;
    int x,y;
    x=y=0;
    SDL_Init(SDL_INIT_VIDEO);
    screen=SDL_SetVideoMode(IMX,IMY,32,SDL_SWSURFACE|SDL_DOUBLEBUF);  
    fond=Charger("fond.bmp");
    do
    {
        SDL_Rect R;
        key = SDL_GetKeyState(NULL);
        SDL_PumpEvents();
        if (key[SDLK_LEFT])
            x-=SPEED;
        if (key[SDLK_RIGHT])
            x+=SPEED;
        if (key[SDLK_UP])
            y-=SPEED;
        if (key[SDLK_DOWN])
            y+=SPEED;
        R.x = negatmodulo(x,IMX);
        R.y = negatmodulo(y,IMY);
        SDL_BlitSurface(fond,NULL,screen,&R);
        R.x = negatmodulo(x,IMX) - IMX;
        R.y = negatmodulo(y,IMY);
        SDL_BlitSurface(fond,NULL,screen,&R);
        R.x = negatmodulo(x,IMX) - IMX;
        R.y = negatmodulo(y,IMY) - IMY;
        SDL_BlitSurface(fond,NULL,screen,&R);
        R.x = negatmodulo(x,IMX);
        R.y = negatmodulo(y,IMY) - IMY;
        SDL_BlitSurface(fond,NULL,screen,&R);
        SDL_Flip(screen);
        SDL_Delay(3);
    } 
    while(!key[SDLK_ESCAPE]);
    SDL_FreeSurface(fond);
    SDL_Quit();
    return 0;
}



Commentaires

	Un deuxième exemple de scrolling.
	Cette fois, vous le contrôlez avec les flèches, et dans quatre directions.

	Par rapport à l'exemple précédent, regardons juste le main.
	Je me sers ici de SDL_GetKeyState, qui regarde à chaque début de boucle quelle touche est en haut, quelle touche est en bas.
	Habituellement, j'utilise une fonction à moi que j'appelle UpdateEvents, mais je vais tenter celle ci, qui marche très bien pour ce qu'on en fait ici.
	Il est important d'appeler SDL_PumpEvents après pour mettre à jour. La doc le spécifie.

	Je fais évoluer simplement x et y en fonction des flèches : les if se comprennent.

	Et puis, tout comme je collais deux images dans l'exemple d'avant. Ici j'en colle quatre, de la même manière. La fonction de Blit s'occupe du découpage.