SDL_image and example

Does anybody have a simple example of the use of SDL_image
to load a .jpeg/.png file, set a colorkey to (0,0,0) and
blit it in the screen to (100,100), by example?

thx a lot for any help!–
ANTENA3 TV = MANIPULACION DE INFORMACION (=Telefonica).

Windows 98: a 32 bit graphical front end to a 16 bit patch
on an 8 bit operating system written for a 4 bit processor by
a 2 bit company without 1 bit of decency…
-----------------------------------------------------

NoP / Compiler – sromero at unix-shells.com
POWERED BY - Linux RedHat 6.0 - Reg. User #74.821
http://web.jet.es/s.romero

~-----------------------------------------------------~

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Santiago Romero escribi?:

Does anybody have a simple example of the use of SDL_image
to load a .jpeg/.png file, set a colorkey to (0,0,0) and
blit it in the screen to (100,100), by example?

thx a lot for any help!

Something like this ?


#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>

int main(int argc, char **argv)
{
int exited=0;
SDL_Surface *screen, *image;
SDL_Rect rect;

	  if(SDL_Init(SDL_INIT_VIDEO)<0){
				 fprintf(stderr,"Couldn't initialise SDL: %i\n",

SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
screen=SDL_SetVideoMode(640,480,16,SDL_SWSURFACE);
if(screen==NULL){
fprintf(stderr,“Couldn’t set video mode: %s\n”,
SDL_GetError());
exit(1);
}
image=IMG_Load("/windoc/face.jpg");
if(image==NULL){
fprintf(stderr,“Couldn’t load image\n”);
exit(1);
}

if(SDL_SetColorKey(image,SDL_RLEACCEL,SDL_MapRGB(image->format,0,0,0))
0){
fprintf(stderr,“Couldn’t ser color key: %s\n”,SDL_GetError());
exit(1);
}
image=SDL_DisplayFormat(image);
rect.x=100;rect.y=100;rect.w=image->w;rect.h=image->h;
SDL_BlitSurface(image,0,screen,&rect);
SDL_UpdateRects(screen,1,&rect);
while(exited==0){
SDL_Event event;
if(SDL_PollEvent(&event)==1){
if((event.type==SDL_KEYDOWN &&
event.key.keysym.sym==SDLK_ESCAPE) ||
(event.type==SDL_QUIT))exited=1;
}
}
return 0;
}


Un saludo.


Jes?s Carrete Monta?a ----> KUANTIKO___________________________________________
| .~. |
| /V\ jrcarmon(arroba)teleline.es |
| // \ Linux Registered |
| /( )\ User #158442 |
| ^`~’^ |
|___________________________________________|

Clave p?blica PGP disponible por e-mail.

-----BEGIN PGP SIGNATURE-----
Version: PGP 6.5.1i for non-commercial use http://www.pgpi.com/

iQA/AwUBOV3fwVXBzV6UX+HYEQJvaACfcBW5DsSitmDpaGOEXC1EERkKMekAoN/Q
QIt2x28m00dNAx4LbbwUplFC
=nBvD
-----END PGP SIGNATURE-----

Does anybody have a simple example of the use of SDL_image
to load a .jpeg/.png file, set a colorkey to (0,0,0) and
blit it in the screen to (100,100), by example?

Using jpeg and colour keys is unwise, since jpeg uses lossy encoding.
Pixel values are not guaranteed to be restored exactly, and you may
get ugly fringes and loss of transparency. Colour keys depend on exact
pixel values.

Something like this ?
image=SDL_DisplayFormat(image);

this will leak memory. SDL_DisplayFormat returns a copy of the original
surface.

    while(exited==0){
  			SDL_Event event;
  			if(SDL_PollEvent(&event)==1){
  					  if((event.type==SDL_KEYDOWN &&

event.key.keysym.sym==SDLK_ESCAPE) ||
(event.type==SDL_QUIT))exited=1;
}
}

And this is a busy loop, using 100% cpu doing nothing

Santiago Romero wrote:

Does anybody have a simple example of the use of SDL_image
to load a .jpeg/.png file, set a colorkey to (0,0,0) and
blit it in the screen to (100,100), by example?

SDL_Surface *tmp, *image;

tmp = IMG_Load(“testimage.png”);
if (tmp == NULL)
ComplainLoudly();
SDL_SetColorKey(tmp,SDL_SRCCOLORKEY,0);
image = SDL_DisplayFormat(tmp);
free(tmp);
if (image == NULL)
ComplainLoudly();

Hope this helps.

-John

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mattias Engdegard escribi?:

Something like this ?
image=SDL_DisplayFormat(image);

this will leak memory. SDL_DisplayFormat returns a copy of the
original surface.

Then, must I do this?

SDL_surface *image,*temporal

temporal=SDL_DisplayFormat(image);
SDL_FreeSurface(image);
image=temporal;
temporal=NULL;
            while(exited==0){
                                  SDL_Event event;

if(SDL_PollEvent(&event)==1){

if((event.type==SDL_KEYDOWN &&
event.key.keysym.sym==SDLK_ESCAPE) ||
(event.type==SDL_QUIT))exited=1;
}
}

And this is a busy loop, using 100% cpu doing nothing

I know, but this is a simple example, not an actual program. I'd

never do that.

Un saludo.


Jes?s Carrete Monta?a ----> KUANTIKO___________________________________________
| .~. |
| /V\ jrcarmon(arroba)teleline.es |
| // \ Linux Registered |
| /( )\ User #158442 |
| ^`~’^ |
|___________________________________________|

Clave p?blica PGP disponible por e-mail.

-----BEGIN PGP SIGNATURE-----
Version: PGP 6.5.1i for non-commercial use http://www.pgpi.com/

iQA/AwUBOV4IqVXBzV6UX+HYEQLfGACg2oUPx9Ko7o7sprhpZVXQDJjaFJ0AoP3l
So4SCseGcbPulrO6oG6WBC63
=l45Q
-----END PGP SIGNATURE-----