Saturday, July 9, 2011

Moving the loaded image with SDL

Filled Rectangles (SDL_FillRect)

SDL_FillRect is used to render color-filled rectangles.

int SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);

This function returns an int. If successful, the value returned will be 0. If it fails, the return value will be -1. There are three parameters. The first (dst) is a pointer to an SDL_Surface on which you are drawing the filled rectangle. The second (dstrect) is a pointer to an SDL_Rect that describes the rectangular area that you want to fill. The third (color) is a Uint32 that represents the color with which you want to fill the rectangle.



clip_rect

The clip_rect member is an SDL_Rect. The values stored here limit the area on the SDL_Surface on which you can blit. SDL supports only rectangular clipping areas and only one rectangle at a time.


Now you are ready to load and animate or move the loaded image on the SDL screen you have created.



#include <sdl/sdl.h>
SDL_Surface *screen=0;
SDL_Surface *bitmap=0;

void draw_surface(SDL_Surface *sur, int x, int y)
{
    SDL_Rect pos={x,y};
    SDL_BlitSurface(sur, 0, screen, &pos);

}
int main(int argc, char* argv[])
{
    char done;
    SDL_Init(SDL_INIT_VIDEO);
    screen=SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    bitmap=SDL_LoadBMP("bitmap.bmp");

    done = 0;
    SDL_Event event;
    int x=0;

    while(done!=1)
    {

        while(SDL_PollEvent(&event))
        {

            if(event.type ==SDL_QUIT)
            {

                done=1;

            }
        }

        SDL_FillRect(screen, &screen->clip_rect, 0xff00000);
        draw_surface(bitmap, x, 0);

        SDL_Flip(screen);
        x++;
    }
    SDL_Quit();
    return(0);
}

Now you can animate the loaded bitmap as per your requirement just manipulating the value of x and y while passing them to the draw_surface(bitmap,x,0) where the value given to y in the above code is 0.

Source code available at:

No comments:

Post a Comment