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
|
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL_image.h>
int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL, *image = NULL;
SDL_Event event;
int continuer = 1;
//tableau pour gérer l'état des touches (clavier, souris, etc)
int touches[324] ={0};
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetIcon(SDL_LoadBMP("sdl_icone.bmp"),NULL); //On choisi l'icone du programme
ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
SDL_FillRect(ecran,NULL,SDL_MapRGB(ecran->format,255,255,255));
SDL_WM_SetCaption("Gestion des evenements",NULL); //Titre de la fenetre entre""
image = SDL_LoadBMP("image.bmp");
//on fait disparaitre le fond de l'image (couleur unie)
SDL_SetColorKey(image,SDL_SRCCOLORKEY, SDL_MapRGB(image->format,0,0,255));
SDL_Rect positionImage;
positionImage.x = ecran->w /2 - image->w /2;
positionImage.y = ecran->h /2 - image->h /2;
SDL_BlitSurface(image,NULL,ecran,&positionImage);
SDL_Flip(ecran);
//SDL_EnableKeyRepeat(10,10);
while(continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
//on redimensionne la surface ecran si l'utilisateur redimensionne la fenêtre
case SDL_RESIZABLE:
ecran = SDL_SetVideoMode(event.resize.w, event.resize.h, 32, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
SDL_FillRect(ecran,NULL,SDL_MapRGB(ecran->format,255,255,255));
break;
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
touches[event.key.keysym.sym] = 1; //on repère quelle touche est appuyée
break;
case SDL_KEYUP:
touches[event.key.keysym.sym] = 0; //on repère quelle touche est relachée
case SDL_MOUSEBUTTONDOWN: //on repère quel bouton est appuyée
touches[event.button.button] = 1;
break;
case SDL_MOUSEBUTTONUP: //on repère quel bouton est relachée
touches[event.button.button] = 0;
break;
case SDL_MOUSEMOTION:
if ( (event.motion.x > ecran->w - 10) ||
(event.motion.y > ecran->h - 15) )
{
SDL_WarpMouse (event.motion.x <= ecran->w - 10 ? event.motion.x : ecran->w-10,
event.motion.y <= ecran->h - 15 ? event.motion.y : ecran->h-15);
}
if (touches[SDL_BUTTON_LEFT])
{
positionImage.x = event.motion.x;
positionImage.y = event.motion.y;
}
break;
default : break;
}
if (touches[SDLK_ESCAPE])
{
continuer = 0;
}
/* on efface l'ecran*/
SDL_FillRect(ecran,NULL,SDL_MapRGB(ecran->format,255,255,255));
/*On fait les SDL_BlitSurface nécessaires*/
SDL_BlitSurface(image,NULL,ecran,&positionImage);
/*On met l'affichage à jour*/
SDL_Flip(ecran);
}
SDL_FreeSurface(image);
SDL_Quit(); //On arrete la SDL
return EXIT_SUCCESS;
} |