I've been learning SDL this weekend and moving around blocks of images might be good enough for some people, but I wanted to move strangly shaped images. I made a tiny little library with a function (drawImage) to draw images fast, and with some adjustments I made the user able to send the red, green, and blue values that would make the colour that is not drawn. Inside drawImage(), the R G B values (if valid) are passed to another function:
{{{SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(image->format, r, g, b)); }}}
This works fine, if the image has a white background and you draw it with drawImage("image.bmp", 0, 0, screen, 255, 255, 255) then the white will not be there, what was behind it will be there. This is only half of the battle though. If you make the image move (i.e. move it and redraw) then it will leave a trail of itself. Obviously, you could redraw background and then redraw the image in its place but that would use up many more resources then neccessary, especialy if the image you are moving covers only a tiny area of the background. So, to get around this I specify only a rectangle the size of the image taken from the same place in the background as the image to be redrawn. For example:
{{{BackChunk.x = i; BackChunk.y = i; BackChunk.w = 136; BackChunk.h = 144;}}}
This takes a chunk from the background with the width and height of the image I want to go ontop of it, and then I draw it all with:
{{{SDL_BlitSurface(background, &BackChunk, screen, &BackChunk); }}}
That blits the chunk of the background to the screen, and this:
{{{ drawImage("fred.bmp", i, i, screen, 255, 255, 255); }}}
draws fred.bmp ontop of that chunk and then this:
{{{ SDL_UpdateRects(screen, 1, &BackRect); }}}
updates the screen. Note that the updaterects only updates BackRect, if you had more then one thing being drawn then you would update the whole thing. Inside drawimage.h it automatically updates screen and frees where the bitmap image was put into memory, but in order to increase you may comment out those two lines. WARNING: this gets more speed by causing a memory leak. The only solution I can think of for this is to not use drawImage at all, which should increase speed even more, and getting rid of drawimage will be the next implementation (sadly).
The sdl.c and drawimage.h as well the the bmps here: http://b3gh405ay14mf.bc.hsia.telus.net/~jeffrey/prj/ This has all of the code to do what was explained above. Those are the files that I work on though, so they will be changing quite frequently. Normally sdl.c can be compiled with gcc -l SDL -o sdltest sdl.c, and drawimage.h will hopefully/sadly be illiminated some time soon.
