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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/*
* Fichier de base pour ouvrir une fenetre SDL, si l'utilisateur appuie sur 'q', on quitte le programme
*/
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define WIDTH 300
#define HEIGHT 300
inline int getPixel(int i, int j, int w, int bytesperPix)
{
return (i*w + j)*bytesperPix;
}
SDL_Surface *MonFlipV(SDL_Surface *im)
{
SDL_Surface *res;
int i,j,src,dest;
int bytesperpix;
unsigned char *srcpixels, *destpixels;
if(im==NULL)
return NULL;
bytesperpix = im->format->BytesPerPixel;
// Creation d'une surface de meme taille
res = SDL_CreateRGBSurface(SDL_HWSURFACE, im->w, im->h, im->format->BitsPerPixel,
im->format->Rmask, im->format->Gmask, im->format->Bmask, im->format->Amask);
if(res == NULL)
return NULL;
// Maintenant on inverse les pixels
srcpixels = (unsigned char*) im->pixels;
destpixels = (unsigned char*) res->pixels;
for(i=0;i<im->h;i++)
for(j=0;j<im->w;j++)
{
src = getPixel(i,j,im->w,bytesperpix);
dest = getPixel(im->h-1-i,j,im->w,bytesperpix);
destpixels[dest] = srcpixels[src];
dest++;src++;
destpixels[dest] = srcpixels[src];
dest++;src++;
destpixels[dest] = srcpixels[src];
dest++;src++;
if(bytesperpix==4)
{
destpixels[dest] = srcpixels[src];
dest++;src++;
}
}
return res;
}
SDL_Surface *MonFlipH(SDL_Surface *im)
{
SDL_Surface *res;
int i,j,src,dest;
int bytesperpix;
unsigned char *srcpixels, *destpixels;
if(im==NULL)
return NULL;
bytesperpix = im->format->BytesPerPixel;
// Creation d'une surface de meme taille
res = SDL_CreateRGBSurface(SDL_HWSURFACE, im->w, im->h, im->format->BitsPerPixel,
im->format->Rmask, im->format->Gmask, im->format->Bmask, im->format->Amask);
if(res == NULL)
return NULL;
// Maintenant on inverse les pixels
srcpixels = (unsigned char*) im->pixels;
destpixels = (unsigned char*) res->pixels;
for(i=0;i<im->h;i++)
for(j=0;j<im->w;j++)
{
src = getPixel(i,j,im->w,bytesperpix);
dest = getPixel(i,im->w-1-j,im->w,bytesperpix);
destpixels[dest] = srcpixels[src];
dest++;src++;
destpixels[dest] = srcpixels[src];
dest++;src++;
destpixels[dest] = srcpixels[src];
dest++;src++;
if(bytesperpix==4)
{
destpixels[dest] = srcpixels[src];
dest++;src++;
}
}
return res;
}
int main()
{
SDL_Event event;
SDL_Surface *screen,*image[4];
int done = 0;
int lequel;
SDL_Rect rect;
//Initialisation
if(SDL_Init(SDL_INIT_VIDEO)!=0) {
printf("Probleme pour initialiser SDL: %s",SDL_GetError());
return EXIT_FAILURE;
}
//Mettre un titre �la fen�re
SDL_WM_SetCaption("Images Version 1.0", NULL);
//Ouvrir une fenetre
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
if(screen==NULL)
done = 1;
// Chargement de l'image et creation des 3 autres
image[0] = SDL_LoadBMP("tata.bmp");
image[1] = MonFlipV(image[0]);
image[2] = MonFlipH(image[0]);
image[3] = MonFlipH(image[1]);
if((image[0] == NULL) || (image[1] == NULL) || (image[2] == NULL) || (image[3] == NULL))
{
printf("Erreur dans le chargement et le flip des images\n");
return EXIT_FAILURE;
}
//Boucle generale
while(!done)
{
//Traiter les evenements
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
done=1;
break;
case SDL_KEYUP:
if(event.key.keysym.sym==SDLK_q)
done=1;
break;
default:
break;
}
}
// Affichage
// On remet �z�o
SDL_FillRect(screen,NULL,0);
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
{
lequel = 2*i+j ; //lequel prendra les valeurs : 0,1,2,3
rect.x = j*image[lequel]->w;
rect.y = i*image[lequel]->h;
SDL_BlitSurface(image[lequel],NULL,screen,&rect);
}
SDL_Flip(screen);
}
for(int k=0;k<4;k++)
SDL_FreeSurface(image[k]);
SDL_FreeSurface(screen);
SDL_Quit();
return EXIT_SUCCESS;
} |
Partager