Pages : 1
#1 Le 01/06/2007, à 06:55
- Strategygame
transparence alpha avec la SDL
Voilà je recherche à faire de la transparence au pixel par pixel.
J'ai établit un code pas spécialement complexe grace à un tuto que j'ai trouvé ici, mais rien a faire la transparence veut pas faire son effet ...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
Uint32 getPixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Ici p est l'adresse du pixel que l'on veut connaitre */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* Ne devrait pas arriver, mais évite les erreurs */
}
}
void setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Ici p est l'adresse du pixel que l'on veut modifier */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL, *surface=NULL;
SDL_Event event;
int continuer = 1;
SDL_Init(SDL_INIT_VIDEO);
ecran = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
surface = SDL_CreateRGBSurface(SDL_HWSURFACE, 100, 100, 32, 0, 0, 0, 0);
SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 255, 0, 0));
Uint32 pixel;
Uint8 r,g,b,a;
int x,y;
x=3;
y=5;
SDL_LockSurface(surface); //On bloque la surface
for (y=0;y<surface->h;y++)
{
for (x=0;x<surface->w;x++)
{
pixel=getPixel(surface,x,y);
SDL_GetRGBA(pixel, surface->format, &r, &g, &b, &a);
//Ici, on mettra du code pour modifier les pixels.
a=0;
//Et une fois qu'on les a modifiés :
pixel=SDL_MapRGBA(surface->format, r, g, b, a);
//Et pour changer la valeur d'un pixel :
setPixel(surface,x,y,pixel);
}
}
SDL_UnlockSurface(surface); /*On libère la surface, elle peut être utilisée pour une autre tâche*/
SDL_Rect position;
position.x = 350;
position.y = 250;
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
}
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
SDL_BlitSurface(surface, NULL, ecran, &position);
SDL_Flip(ecran);
}
SDL_Quit();
return EXIT_SUCCESS;
}
Hors ligne
#2 Le 01/06/2007, à 09:25
- toto4455
Re : transparence alpha avec la SDL
on appelant SDL_CreateRGBSurface avec les bon masques ca devrait te faire un beau white screen
http://www.die.net/doc/linux/man/man3/sdl_creatergbsurface.3.html
Uint32 rmask, gmask, bmask, amask;
/* SDL interprets each pixel as a 32-bit number, so our masks must depend
on the endianness (byte order) of the machine */
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
surface = SDL_CreateRGBSurface(SDL_HWSURFACE, 100, 100, 32, rmask, gmask, bmask, amask);
#3 Le 01/06/2007, à 21:10
- Strategygame
Re : transparence alpha avec la SDL
Merci milles fois !!!!! J'aurais pas pu trouver ça, encore merci, ma vie est sauve ^^
Hors ligne