So far, we have created only one window and nothing else. This time, we will display images in the window. This is the most common function of the video subsystem. The SDL video subsystem can only load bmp format bitmaps. The call function is SDL_Surface * SDL_LoadBMP (const char * file). The parameter of this function is a string of C language, and the return value is a drawing surface. There are two kinds of drawing surfaces in SDL. The first is the display surface or window generated using the SDL_SetVideoMode function. The display surface is unique and can only be generated using SDL_SetVideoMod, the display surface can be directly displayed on the screen. In addition to the display surface, the image-generated surface and the font-Generated Surface cannot be directly displayed on the screen, this is the difference between the two surfaces.
Before starting this example, we need to prepare a bmp image. Then we can write a function to load the bmp image by ourselves:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/201K96050-0.gif "alt =" Copy code "style =" margin: 0px; padding: 0px; border: none! Important; "/>
/*--------------------------------------------------------------------
Function Name: loadImage
Parameter: name of the char * filename Image File
Returned value: SDL_Surface * returns a pointer to the image surface. Otherwise, NULL is returned.
Power: loading Images
Secondary note:
----------------------------------------------------------------------*/
SDL_Surface * loadImage (char * aFilename)
{
SDL_Surface * loadedImage = NULL;
SDL_Surface * optimizedImage = NULL;
// Load the image
LoadedImage = SDL_LoadBMP (aFilename );
If (NULL! = LoadedImage) // if the load is successful, loadedImage is not empty.
{
// Create an optimized Image
OptimizedImage = SDL_DisplayFormat (loadedImage );
// Release loadedImage
SDL_FreeSurface (loadedImage );
}
Return optimizedImage;
}
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/201K96050-0.gif "alt =" Copy code "style =" margin: 0px; padding: 0px; border: none! Important; "/>
First, load the bmp image in the function, and then determine whether the loading is successful. If the loading is successful, optimize the generated surface. Why? Because the format of the Surface Generated by loading an image may be different from that of the display table, the image surface format will be converted to the display table format each time when the image is displayed, of course, this work is done by the system, but this will reduce the efficiency of the system. Therefore, we need to convert the image surface according to the display table format, and then return the converted surface pointer.
The function of SDL_DisplayFormat is to generate a copy of the surface indicated by loadedImage according to the display table format. Therefore, after the copy is generated, the original surface indicated by loadedImage will be released, then the new surface pointer optimizedImage is returned.
In this example, we will display a pair of clocks as the background image. The clock size is 580*580, and the image name is clock.bmp. First, create an empty console application in vs2008 and set the project properties. This is detailed in the first installation tutorial. Running Effect of the example:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/201K91214-2.jpg "width =" 400 "style =" margin: 0px auto; padding: 0px; border: 0px; display: block; "/>
Project source code:
/* Function: demonstrate bitmap loading by: csl Date: 2012-5-5 */# include <stdio. h> # include <stdlib. h> # include "sdl \ SDL. h "// defines the window WIDTH, height, and depth # define WIDTH 800 # define HEIGH 600 # define BPP 32 // defines the surface pointer SDL_Surface * gpScreen = NULL; // display window pointer SDL_Surface * gpClock = NULL; // bmp image surface pointer SDL_Surface * loadImage (char * aFilename); // load bmp image int main (int arc, char * agv []) {if (SDL_Init (SDL_INIT_EVERYTHING) =-1) // initialize the SDL subsystem {printf ("Unable to init SDL: % S \ n ", SDL_GetError (); exit (-1);} atexit (SDL_Quit); // register SDL_Quit and call it when exiting, make the program automatically clean up when exiting // create a window gpScreen = SDL_SetVideoMode (WIDTH, HEIGH, BPP, SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF); if (! GpScreen) {printf ("Unable to create window: % s \ n", SDL_GetError (); exit (1 );} // load the image gpClock = loadImage ("clock.bmp"); // bit Block Transmission SDL_BlitSurface (gpClock, NULL, gpScreen, NULL); // display the image SDL_Flip (gpScreen ); SDL_Delay (30000); // pause for 3 seconds // The surface pointer SDL_FreeSurface (gpScreen); SDL_FreeSurface (gpClock); system ("pause"); return 0;} must be released before exiting the program ;} /* -------------------------------------------------------------------- function name: load Image parameter: char * filename Image file name return value: SDL_Surface * returns pointer to the Image surface; function: loading the Image; Note: character */SDL_Surface * loadImage (char * aFilename) {SDL_Surface * loadedImage = NULL; SDL_Surface * optimizedImage = NULL; // load the image loadedImage = SDL_LoadBMP (aFilename); if (NULL! = LoadedImage) // If the image loaded {// create an optimized image optimizedImage = SDL_DisplayFormat (loadedImage); // release loadImage SDL_FreeSurface (loadedImage);} return optimizedImage ;}
In row 3 of the main function, we call our own loadImage function to load the bmp image of the clock. After loading successfully, we need to "Paste" the surface on the display surface, call the refresh function to refresh the image. Therefore, three steps are required to display an image: 1. Load the image; 2. paste the image to the display surface; 3. Refresh the screen.
We have completed Step 1. How can we add the image to the display surface? First, you need to know how to display things on the screen. Everything displayed on the screen is stored in a frame cache. The frame cache is similar to a two-dimensional array and is a memory area, the display data of each pixel on the screen is stored. When the image is displayed, the cached data of frames is displayed on the screen, and the display surface is the frame cache, to display other surfaces, you must transmit your display data to the display surface to overwrite the data in the corresponding display table, this is similar to sticking a picture surface to the whiteboard display surface. In SDL, this is called bits (bit block transfer). It can transmit data on the source surface to the target surface and overwrite the corresponding data. This transmission is very fast, the source table format is automatically converted to the target table format. Functions that complete this function in SDL:
int SDL_BlitSurface
(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);
Parameter: src Source Surface
The place where the srcrect source surface needs to be displayed, represented by a rectangle, SDL_Rect is a struct describing a rectangle)
Dst target surface
The position where dstrect is displayed on the target surface. It is also represented by a rectangle.
Usage: when calling this function, you need to pass the source surface pointer to the first parameter. In SDL, any image is represented by a rectangle SDL_Rect, you need to show the source surface, which is pointed out by a rectangle. You need to pass the pointer of this rectangle to the srcrect. If it is NULL, all content on the source surface is displayed; pass the pointer to the target surface to the 3rd dst parameter. If you want to specify the display position on the target surface, you also need to use a rectangle to represent this position. If it is NULL, the source surface is displayed from the upper left corner of the target surface. In this way, we understand the meaning of the 43rd rows, display all the clocks on the display surface, starting from the upper left corner of the target surface.
After the bit block is transferred, it cannot be seen on the screen. Because you want to tell the video card to refresh the screen, you need to call the SDL_Flip function. The parameter of the function is to display the table pointer. After refreshing the screen, you can see the image.
To display any image, perform these three steps. Finally, remember to release the corresponding surface when exiting the program. Therefore, row 50 and line 51 release the display surface and image surface.
In this example, there is a new data type SDL_Rect, which is the struct in SDL.
Typedef struct { Sint16 x, y; // coordinates of the upper left corner of the rectangle Uint16 w, h; // width and height, in pixels }SDL_Rect; |
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/201KaW4-3.jpg "width =" 200 "style =" margin: 0px; padding: 0px; border: 0px; "/> |
If you want to display the clock in the middle of the window, you can write
SDL_Rect dstRect;
DstRect. x = (WIDTH-gpClock-> w)/2;
DstRect. y = (HEIGH-gpClock-> h)/2;
DstRect. w = gpClock-> w;
DstRect. h = gpClock-> h;
SDL_BlitSurface (gpClock, NULL, gpScreen,&DstRect); // remember the address of dstRect.
Use this code to replace line 1 and display the clock in the middle of the window. Pay attention to SDL_Rect dstRect. This sentence should be placed at the beginning of the function, because the C language cannot define variables in the middle. The source code of this example can be downloaded here. Note that this example is successfully debugged under vs2008.
How can I load images of other formats?
SDL has an extension library SDL_image, which supports BMP, PPM, XPM, PCX, GIF, JPEG, PNG, and TGA images, this extension library has already been introduced in the installation tutorial. If this extension library is installed, introduce its header file in your source file: # include "SDL_image.h ", then add the reference SDL_image.lib of the static library in the Project Settings. Then we can modify the loadImage function.
In the loadImage function, we can use the IMG_Load function instead of the SDLLoadBmp function to load images of other formats.
/* -------------------------------------------------------------------- Function name: loadImage parameter: char * filename image file name return value: SDL_Surface * returns pointer to the image surface function: Load image note: optional */SDL_Surface * loadImage (char * aFilename) {SDL_Surface * loadedImage = NULL; SDL_Surface * optimizedImage = NULL; // load the image loadedImage = IMG_Load (aFilename); if (NULL! = LoadedImage) // If the image loaded {// create an optimized image optimizedImage = SDL_DisplayFormat (loadedImage); // release loadImage SDL_FreeSurface (loadedImage);} return optimizedImage ;}
Upload, you can load IMG_Load ("clock.bmp") in this way. If you put all the image files under the subdirectory image in the source file directory, you can use IMG_Load ("image \ clock.bmp "); remember that there must be two escape characters. Two \ s must be used to represent one \. If it is an absolute directory, for example, loading the clock.bmp file under d: \ bmp, you can call IMG_Load ("d: \ bmp \ clock.bmp") like this ");.
This article is from the "Learning Joy" blog, please be sure to keep this source http://chengshaolei.blog.51cto.com/1994169/1291967