SDL Learning Note II (Loading and displaying of pictures)

Source: Internet
Author: User

Image loading is a common step in the game, and SDL provides an easy way to load and display pictures

Before this, however, you have to create a window to show that SDL provides the following simple function

sdl_surface* sdl_setvideomode (int width, int height, int bitsperpixel, Uint32 flags);

width, and height two parameters represent the width and height of the window, the BitsPerPixel parameter refers to the display bit depth (a pixel with a few bits to save information), the flags parameter is more complex, to control where the window is saved, whether the full screen and so on


With this function, it is easy to create a window with the following function to specify the window's caption and icon

void sdl_wm_setcaption (const char *title, const char *icon);

These two functions are basically enough for a window to be created.



Let's look at how to load a picture, SDL only provides the BMP format of the load, the function is as follows:

Sdl_surface *sdl_loadbmp (const char *file);

The file parameter represents the name of the picture, but usually the image format we need is not BMP, but the SDL extension Library (sdl_image) provides the corresponding function as follows:

Sdl_surface *img_load (const char *file);

This function supports most common formats, so you don't have to remember

Preferably formatted as a window after loading

Sdl_surface *sdl_displayformat (Sdl_surface *surface);


The picture is loaded, but how to display it on the screen, this involves the block move Blit operation, that is, one part of the image is displayed to another part of the image, the specific function is as follows:

int Sdl_blitsurface (Sdl_surface *src, Sdl_rect *srcrect, Sdl_surface *dst, Sdl_rect *dstrect);

This function looks very long, but in fact it is very simple, src points to the picture to move, DST points to the target image, and two rect is the corresponding rectangle, the successful move returns 0, otherwise returns-1


Finally, the image is drawn to the screen with the following functions:

int Sdl_flip (sdl_surface* screen);

This function, in a system that supports double buffering, is analogous to the content of a conversion buffer, which can be imagined to draw the contents of the screen structure pointed to screen


Finally, a simple delay function is introduced:

void Sdl_delay (Uint32 ms);

We just have to specify the number of milliseconds to delay

In this way, we can keep the image for a while without flashing


The above function is probably the usual function of working with pictures.


It is important to note that the image loaded into the bit depth is not necessarily the same as the bit depth we give the window, so it is better to format the image immediately after loading, so as to avoid the automatic format of each drawing to reduce program efficiency


Note that the structure of the image that was loaded before formatting is released, and the system is not really formatted but is copied to the appropriate format. Sometimes, we do not want to display a picture of the background, this is called cutout ...

We can do this with the following two functions, first creating a background color to be keyed off:

Uint32sdl_maprgb (constsdl_pixelformat* format,uint8r,uint8g,uint8b);

This function returns the color format to be stripped, and the format parameter refers to a member of the surface structure that is the member of the picture to be formatted.

R,g,b is the corresponding color parameter


With the color format, we want to make this color effective

int Sdl_setcolorkey (sdl_surface* surface,int flag,uint32 key);

Surface is the picture to be set, if the value of flag is Sdl_srccolorkey, set key to Transparent color, if flag or up sdl_rleaccel, then use RLE acceleration when calling Sdl_blitsurface, if it is 0, Clears all Key;key, which is the color returned by Sdl_maprgb

Successful call returns 0, otherwise returns-1

Note that for the original is transparent picture, it is best not to try to set its colorkey, and the format of the function corresponding to change to Sdl_displayformatalpha

We can customize the following function to load the image:

Sdl_surface *loadimage (char *fname)
 {
     sdl_surface *pimage=null, *pformat=null;
     Pimage=img_load (fname);
     if (pimage!=null)
     {
         pformat=sdl_displayformat (pimage);
         Sdl_freesurface (pimage);
         if (pformat!=null)
         {
             Uint32 Colorkey=sdl_maprgb (Pformat->format, 0xFF, 0xFF, 0xFF);
             Sdl_setcolorkey (Pformat, sdl_srccolorkey| Sdl_rleaccel, Colorkey);
         }
     }
    return pformat;
}



Now, we can easily deal with the image, haha

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.