#include"sdl_image.h"/** Return the pixel value at (x, y)
* NOTE: The surface must be locked before calling this!*/Uint32 getpixel(SDL_Surface*surface,intx,inty)
{intbpp=surface->format->BytesPerPixel;/*Here p is the address to the pixel we want to retrieve*/Uint8*p=(Uint8*)surface->pixels+y*surface->pitch+x*bpp;switch(bpp) {case1:return*p;case2:return*(Uint16*)p;case3:if(SDL_BYTEORDER==SDL_BIG_ENDIAN)returnp[0]<<16|p[1]<<8|p[2];elsereturnp[0]|p[1]<<8|p[2]<<16;case4:return*(Uint32*)p;default:return0;/*shouldn't happen, but avoids warnings*/}
}/** Set the pixel at (x, y) to the given value
* NOTE: The surface must be locked before calling this!*/voidputpixel(SDL_Surface*surface,intx,inty, Uint32 pixel)
{intbpp=surface->format->BytesPerPixel;/*Here p is the address to the pixel we want to set*/Uint8*p=(Uint8*)surface->pixels+y*surface->pitch+x*bpp;switch(bpp) {case1:*p=pixel;break;case2:*(Uint16*)p=pixel;break;case3: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;case4:*(Uint32*)p=pixel;break;
}
}boolshowpic(SDL_Surface*surface,constchar*filename)
{
SDL_Surface*image;/*Load the BMP file into a surface*/image=IMG_Load(filename);if(image==NULL) {
fprintf(stderr,"Couldn't load %s: %s\n",filename,SDL_GetError());returnfalse;
}/*Blit onto the screen surface*/if(SDL_BlitSurface(image, NULL, surface, NULL)<0)
fprintf(stderr,"BlitSurface error: %s\n", SDL_GetError());
SDL_UpdateRect(surface,0,0, image->w, image->h);/*Free the allocated BMP surface*/SDL_FreeSurface(image);returntrue;
}
SDL_Surface*createsurf(constchar*filename)
{
SDL_Surface*image;/*Load the BMP file into a surface*/image=IMG_Load(filename);if(image==NULL) {
fprintf(stderr,"Couldn't load %s: %s\n",filename,SDL_GetError());return0;
}returnimage;
}