summaryrefslogtreecommitdiff
path: root/sdl-test
diff options
context:
space:
mode:
authorDamien Appert <dappert>2010-11-02 11:22:03 +0000
committerDamien Appert <dappert>2010-11-02 11:22:03 +0000
commitd288aec4d6a37f76f53262ebda8fd4ae2737a0e7 (patch)
tree225f94e4b4f4702cbc77f2bf2fa48776f98ae3f6 /sdl-test
parent4f086dc29744f3ab893746bdb2aefdadae3dcf43 (diff)
download2010-netlemmings-d288aec4d6a37f76f53262ebda8fd4ae2737a0e7.tar.gz
2010-netlemmings-d288aec4d6a37f76f53262ebda8fd4ae2737a0e7.tar.bz2
2010-netlemmings-d288aec4d6a37f76f53262ebda8fd4ae2737a0e7.zip
2 Gif dans fire
git-svn-id: file:///var/svn/2010-netlemmings/trunk@131 077b3477-7977-48bd-8428-443f22f7bfda
Diffstat (limited to 'sdl-test')
-rw-r--r--sdl-test/SDL_tuto/chap5_transparence/test_transparence/Makefile16
-rw-r--r--sdl-test/SDL_tuto/chap5_transparence/test_transparence/main.c365
-rw-r--r--sdl-test/SDL_tuto/chap5_transparence/test_transparence/paysage_neige.pngbin0 -> 590730 bytes
-rw-r--r--sdl-test/SDL_tuto/chap5_transparence/test_transparence/tux-sit.pngbin0 -> 3368 bytes
4 files changed, 381 insertions, 0 deletions
diff --git a/sdl-test/SDL_tuto/chap5_transparence/test_transparence/Makefile b/sdl-test/SDL_tuto/chap5_transparence/test_transparence/Makefile
new file mode 100644
index 0000000..495ec59
--- /dev/null
+++ b/sdl-test/SDL_tuto/chap5_transparence/test_transparence/Makefile
@@ -0,0 +1,16 @@
+# Makefile
+#PATH = /usr/include/SDL
+TARGET = ch5transparence
+OBJECTS = main.o
+
+CFLAGS = -O3 -Wall -g -I/usr/include/SDL -L/usr/lib
+LIBS = -lSDL -lSDL_image -lSDL_ttf
+CC = gcc
+
+all: $(TARGET)
+
+$(TARGET): $(OBJECTS)
+ $(CC) $(CFLAGS) -o $@ $^ $(LIBS)
+
+clean:
+ -rm *.o ./ch5transparence
diff --git a/sdl-test/SDL_tuto/chap5_transparence/test_transparence/main.c b/sdl-test/SDL_tuto/chap5_transparence/test_transparence/main.c
new file mode 100644
index 0000000..546f74e
--- /dev/null
+++ b/sdl-test/SDL_tuto/chap5_transparence/test_transparence/main.c
@@ -0,0 +1,365 @@
+/*******************************************************/
+/** Programme de PERRUCHON Romain pour developpez.com **/
+/** Chapitre IV : Transparence - SDL_Color_Key **/
+/*******************************************************/
+
+//Les fichiers d'entête
+//SDL_image pour utiliser IMG_load (chargement image autre que Bitmap : ici *.png
+#include "SDL/SDL.h"
+#include "SDL/SDL_image.h"
+
+//Les attributs de l'ecran (640 * 480)
+const int SCREEN_WIDTH = 640;
+const int SCREEN_HEIGHT = 480;
+const int SCREEN_BPP = 32;
+
+//Les surfaces
+SDL_Surface *background = NULL;
+SDL_Surface *tux = NULL;
+SDL_Surface *screen = NULL;
+SDL_Surface *pa = NULL;
+SDL_Surface *pb = NULL;
+void *temp_pixels = NULL;
+
+#define ccc_Empty 0xff000000
+#define ccc_Brick 0xff00ff00
+#define ccc_fleche 0xff0000ff
+
+//La structure d'evenement
+SDL_Event event;
+
+SDL_Surface *load_image( char* filename, Uint32 c )
+{
+ //L'image qui est chargée
+ SDL_Surface* loadedImage = NULL;
+
+ //L'image optimisée que nous utiliserons par la suite
+ SDL_Surface* optimizedImage = NULL;
+
+ //Chargement de l'image
+ loadedImage = IMG_Load( filename );
+
+ //Si l'image est chargée
+ if( loadedImage != NULL )
+ {
+ //creation de l'image optimisée
+ optimizedImage = SDL_DisplayFormat( loadedImage );
+
+ //liberation de l'ancienne image
+ SDL_FreeSurface( loadedImage );
+
+ //si l'image optimisée créé est bonne
+ if( optimizedImage != NULL )
+ {
+ //on met tout les pixel de couleur R 0, G 0xFF, B 0xFF transparent
+ SDL_SetColorKey( optimizedImage,
+ SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA,
+ SDL_MapRGBA( optimizedImage->format,
+ (c >> 16) & 0xff,
+ (c >> 8) & 0xff,
+ (c & 0xff),
+ (c >> 24) & 0xff
+ )
+ );
+ }
+ }
+
+ //on retourne l'image optimisé
+ return optimizedImage;
+}
+
+void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
+{
+ //rectangle temporaire
+ SDL_Rect offset;
+
+ offset.x = x;
+ offset.y = y;
+
+ //on blit la surface
+ SDL_BlitSurface( source, NULL, destination, &offset );
+}
+
+int init()
+{
+ //initialisation de tout les sous-systemes de sdl
+ if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
+ {
+ return 1;
+ }
+
+ //on met en place l'ecran
+ screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF );//| SDL_SRCALPHA );
+
+ //Si il y a une erreur lors de la mise en place de l'ecran
+ if( screen == NULL )
+ {
+ return 1;
+ }
+ SDL_SetColorKey(screen, SDL_SRCCOLORKEY|SDL_RLEACCEL, ccc_Empty);
+
+
+ pa = SDL_CreateRGBSurface(SDL_HWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT,
+ SCREEN_BPP,0,0,0,0xff);
+ if( pa == NULL ) {
+ return 3;
+ }
+
+ pb = SDL_CreateRGBSurface(SDL_HWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT,
+ SCREEN_BPP,0,0,0,0xff);
+ if( pb == NULL ) {
+ return 3;
+ }
+
+ //on met en place la barre caption de la fenetre
+ SDL_WM_SetCaption( "\"Hello!\"", NULL );
+
+ //si tout s'est bien passé
+ return 1;
+}
+
+int load_files()
+{
+ //chargement du fond
+ background = load_image( "paysage_neige.png" , 0xff000000);
+
+ //si le fond ne se charge pas
+ if( background == NULL )
+ {
+ return 0;
+ }
+
+ //on charge l'image qu'on va appliquer sur le fond
+ tux = load_image( "tux-sit.png" , 0x0000ffff);
+
+ //si l'image se charge mal
+ if( tux == NULL )
+ {
+ return 0;
+ }
+
+ return 1;
+}
+
+void clean_up()
+{
+ //Liberation des surfaces surfaces
+ SDL_FreeSurface( background );
+ SDL_FreeSurface( tux );
+
+ //On quitte SDL
+ SDL_Quit();
+}
+
+int putPixel(SDL_Surface *surface,Uint16 x,Uint16 y,Uint32 colori)
+{
+ /* bpp de la surface ecran */
+ Uint8 bpp = surface->format->BytesPerPixel;
+ Uint8 *p = ((Uint8 *)surface->pixels) + y * surface->pitch + x * bpp;
+
+ switch(bpp)
+ {
+ case 1:
+ *p = (Uint8) colori;
+ break;
+ case 2:
+ *(Uint16 *)p = (Uint16) colori;
+ break;
+ case 3:
+ if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
+ {
+ *(Uint16 *)p = ((colori >> 8) & 0xff00) | ((colori >> 8) & 0xff);
+ *(p + 2) = colori & 0xffff;
+ }
+ else
+ {
+ *(Uint16 *)p = colori & 0xffff;
+ *(p + 2) = ((colori >> 16) & 0xffff) ;
+ }
+ break;
+ case 4:{
+ *(Uint32 *)p = colori;
+ }
+ break;
+ }
+ return 0;
+}
+
+inline Uint32 get_pixel32( int x, int y, SDL_Surface *surface )
+{
+ //Convertie les pixels en 32 bit
+ Uint32 *pixels = (Uint32 *)surface->pixels;
+ return pixels[ ( y * surface->w ) + x ];
+
+}
+
+void affichePx(SDL_Surface *s, int x0, int y0, int xn, int yn, int step ){
+ int i,j;
+ Uint32 *pixels = (Uint32 *)s->pixels;
+ printf("-----------------\n");
+ for(j=y0;j<yn;j+=step){
+ for(i=x0;i<xn;i+=step){
+ printf("%10p-",pixels[( j * s->w ) + i]);
+ }
+ printf("\n");
+ }
+ printf("#################\n");
+
+}
+
+int main( int argc, char* args[] )
+{
+ //ce qui va nous permettre de quitter
+ int quit = 0,i,j,k, size;
+ SDL_Rect to,rect;
+
+ Uint32 ccc_temp;
+
+
+ //Initialisation
+ if( init() == 0 )
+ {
+ return 1;
+ }
+
+ //chargement des fichiers
+ if( load_files() == 0 )
+ {
+ return 1;
+ }
+
+ //Application des surfaces sur l'ecran
+
+ SDL_FillRect(screen,NULL,ccc_Empty);
+ SDL_FillRect(pa,NULL,ccc_Empty);
+ SDL_FillRect(pb,NULL,ccc_Empty);
+ /*if(SDL_SetAlpha(screen, SDL_SRCALPHA, 180)!=0){
+ printf("error\n");
+ }*/
+
+// SDL_SetAlpha(screen, SDL_SRCALPHA, 0);
+
+
+ //FULL
+ to.x = 50;
+ to.y = 50;
+
+ rect.x=80;
+ rect.y=25;
+ rect.w=10;
+ rect.h=100;
+
+ SDL_FillRect(screen,&rect,ccc_Brick);
+ SDL_BlitSurface(tux,NULL, screen,&to);
+
+ //VIS ON TERRAIN
+ to.x = 150;
+ to.y = 50;
+
+ rect.x=180;
+ rect.y=25;
+ rect.w=10;
+ rect.h=100;
+
+ SDL_FillRect(screen,&rect,ccc_Brick);
+
+ size=tux->h*tux->pitch;
+ temp_pixels=malloc(size);
+
+ SDL_LockSurface(tux);
+ SDL_LockSurface(screen);
+ memcpy(temp_pixels,tux->pixels,size);
+ for(j=0;j<tux->h;j++) {
+ for(i=0;i<tux->w;i++) {
+ ccc_temp=((Uint32 *)screen->pixels)[(to.y+j)*screen->w+(to.x+i)];
+ if (ccc_temp==screen->format->colorkey) {
+ ((Uint32 *)tux->pixels)[j*tux->w+i]=tux->format->colorkey;
+ }
+ }
+ }
+ SDL_UnlockSurface(screen);
+ SDL_UnlockSurface(tux);
+ SDL_BlitSurface(tux,NULL, screen,&to);
+
+ SDL_LockSurface(tux);
+ memcpy(tux->pixels,temp_pixels,size);
+ SDL_UnlockSurface(tux);
+ //printf("x %d,y %d, w %d, h %d \n",to.x,to.y,tux->w,tux->h);
+
+ //affichePx(screen,to.x,to.y,tux->w,tux->h,4);
+
+ //NO OVERWRITE
+ to.x = 250;
+ to.y = 50;
+
+ rect.x=280;
+ rect.y=25;
+ rect.w=10;
+ rect.h=100;
+
+
+ SDL_FillRect(screen,&rect,ccc_Brick);
+
+ size=tux->h*tux->pitch;
+ temp_pixels=malloc(size);
+
+ SDL_LockSurface(tux);
+ SDL_LockSurface(screen);
+ memcpy(temp_pixels,tux->pixels,size);
+ for(j=0;j<tux->h;j++) {
+ for(i=0;i<tux->w;i++) {
+ ccc_temp=((Uint32 *)screen->pixels)[(to.y+j)*screen->w+(to.x+i)];
+ if (ccc_temp!=screen->format->colorkey) {
+ ((Uint32 *)tux->pixels)[j*tux->w+i]=tux->format->colorkey;
+ }
+ }
+ }
+ SDL_UnlockSurface(screen);
+ SDL_UnlockSurface(tux);
+ SDL_BlitSurface(tux,NULL, screen,&to);
+
+ SDL_LockSurface(tux);
+ memcpy(tux->pixels,temp_pixels,size);
+ SDL_UnlockSurface(tux);
+
+
+
+
+ //screen=SDL_DisplayFormatAlpha(screen);
+
+ //mise à jour de l'ecran
+ if( SDL_Flip( screen ) == -1 )
+ {
+ return 1;
+ }
+
+ //Tant que l'utilisateur n'a pas quitter
+ while( quit == 0 )
+ {
+ //tant qu'il y a un evenement
+ while( SDL_PollEvent( &event ) )
+ {
+ switch(event.key.keysym.sym){
+ case SDLK_HOME : break;
+ case SDLK_END : break;
+ case SDLK_PAGEUP : break;
+ case SDLK_PAGEDOWN : break;
+ case SDLK_w : break;
+ case SDLK_ESCAPE : quit = 1; break;
+ default:break;
+ }
+ //Si l'utilisateur ferme la fenetre avec le X
+ if( event.type == SDL_QUIT )
+ {
+ //On quitte le programme
+ quit = 1;
+ }
+ }
+ SDL_Delay(3);
+ }
+
+ //liberation des surface et on quitte sdl
+ clean_up();
+
+ return 0;
+}
diff --git a/sdl-test/SDL_tuto/chap5_transparence/test_transparence/paysage_neige.png b/sdl-test/SDL_tuto/chap5_transparence/test_transparence/paysage_neige.png
new file mode 100644
index 0000000..41c0f1b
--- /dev/null
+++ b/sdl-test/SDL_tuto/chap5_transparence/test_transparence/paysage_neige.png
Binary files differ
diff --git a/sdl-test/SDL_tuto/chap5_transparence/test_transparence/tux-sit.png b/sdl-test/SDL_tuto/chap5_transparence/test_transparence/tux-sit.png
new file mode 100644
index 0000000..a8c8a75
--- /dev/null
+++ b/sdl-test/SDL_tuto/chap5_transparence/test_transparence/tux-sit.png
Binary files differ