4 >Graphics and Video
</TITLE
7 CONTENT=
"Modular DocBook HTML Stylesheet Version 1.76b+
10 TITLE=
"SDL Library Documentation"
11 HREF=
"index.html"><LINK
14 HREF=
"guide.html"><LINK
16 TITLE=
"Initializing SDL"
17 HREF=
"guidebasicsinit.html"><LINK
19 TITLE=
"Using OpenGL With SDL"
20 HREF=
"guidevideoopengl.html"></HEAD
31 SUMMARY=
"Header navigation table"
40 >SDL Library Documentation
</TH
48 HREF=
"guidebasicsinit.html"
62 HREF=
"guidevideoopengl.html"
77 >Chapter
2. Graphics and Video
</H1
87 HREF=
"guidevideo.html#GUIDEVIDEOINTRO"
88 >Introduction to SDL Video
</A
92 HREF=
"guidevideoopengl.html"
93 >Using OpenGL With SDL
</A
102 NAME=
"GUIDEVIDEOINTRO"
104 >Introduction to SDL Video
</H1
106 >Video is probably the most common thing that SDL is used for, and
107 so it has the most complete subsystem. Here are a few
108 examples to demonstrate the basics.
</P
116 >Initializing the Video Display
</H2
118 >This is what almost all SDL programs have to do in one way or
127 >Example
2-
1. Initializing the Video Display
</B
130 CLASS=
"PROGRAMLISTING"
131 > SDL_Surface *screen;
133 /* Initialize the SDL library */
134 if( SDL_Init(SDL_INIT_VIDEO)
< 0 ) {
136 "Couldn't initialize SDL: %s\n", SDL_GetError());
140 /* Clean up on exit */
144 * Initialize the display in a
640x480
8-bit palettized mode,
145 * requesting a software surface
147 screen = SDL_SetVideoMode(
640,
480,
8, SDL_SWSURFACE);
148 if ( screen == NULL ) {
149 fprintf(stderr,
"Couldn't set 640x480x8 video mode: %s\n",
162 >Initializing the Best Video Mode
</H2
164 >If you have a preference for a certain pixel depth but will accept any
165 other, use SDL_SetVideoMode with SDL_ANYFORMAT as below. You can also
166 use SDL_VideoModeOK() to find the native video mode that is closest to
167 the mode you request.
</P
175 >Example
2-
2. Initializing the Best Video Mode
</B
178 CLASS=
"PROGRAMLISTING"
179 > /* Have a preference for
8-bit, but accept any depth */
180 screen = SDL_SetVideoMode(
640,
480,
8, SDL_SWSURFACE|SDL_ANYFORMAT);
181 if ( screen == NULL ) {
182 fprintf(stderr,
"Couldn't set 640x480x8 video mode: %s\n",
186 printf(
"Set 640x480 at %d bits-per-pixel mode\n",
187 screen-
>format-
>BitsPerPixel);
</PRE
197 >Loading and Displaying a BMP File
</H2
199 >The following function loads and displays a BMP file given as
200 argument, once SDL is initialised and a video mode has been set.
</P
208 >Example
2-
3. Loading and Displaying a BMP File
</B
211 CLASS=
"PROGRAMLISTING"
212 >void display_bmp(char *file_name)
216 /* Load the BMP file into a surface */
217 image = SDL_LoadBMP(file_name);
219 fprintf(stderr,
"Couldn't load %s: %s\n", file_name, SDL_GetError());
224 * Palettized screen modes will have a default palette (a standard
225 *
8*
8*
4 colour cube), but if the image is palettized as well we can
226 * use that palette for a nicer colour matching
228 if (image-
>format-
>palette
&& screen-
>format-
>palette) {
229 SDL_SetColors(screen, image-
>format-
>palette-
>colors,
0,
230 image-
>format-
>palette-
>ncolors);
233 /* Blit onto the screen surface */
234 if(SDL_BlitSurface(image, NULL, screen, NULL)
< 0)
235 fprintf(stderr,
"BlitSurface error: %s\n", SDL_GetError());
237 SDL_UpdateRect(screen,
0,
0, image-
>w, image-
>h);
239 /* Free the allocated BMP surface */
240 SDL_FreeSurface(image);
251 >Drawing Directly to the Display
</H2
253 >The following two functions can be used to get and set single
254 pixels of a surface. They are carefully written to work with any depth
255 currently supported by SDL. Remember to lock the surface before
256 calling them, and to unlock it before calling any other SDL
259 >To convert between pixel values and their red, green, blue
260 components, use SDL_GetRGB() and SDL_MapRGB().
</P
268 >Example
2-
4. getpixel()
</B
271 CLASS=
"PROGRAMLISTING"
273 * Return the pixel value at (x, y)
274 * NOTE: The surface must be locked before calling this!
276 Uint32 getpixel(SDL_Surface *surface, int x, int y)
278 int bpp = surface-
>format-
>BytesPerPixel;
279 /* Here p is the address to the pixel we want to retrieve */
280 Uint8 *p = (Uint8 *)surface-
>pixels + y * surface-
>pitch + x * bpp;
290 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
291 return p[
0]
<< 16 | p[
1]
<< 8 | p[
2];
293 return p[
0] | p[
1]
<< 8 | p[
2]
<< 16;
299 return
0; /* shouldn't happen, but avoids warnings */
310 >Example
2-
5. putpixel()
</B
313 CLASS=
"PROGRAMLISTING"
315 * Set the pixel at (x, y) to the given value
316 * NOTE: The surface must be locked before calling this!
318 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
320 int bpp = surface-
>format-
>BytesPerPixel;
321 /* Here p is the address to the pixel we want to set */
322 Uint8 *p = (Uint8 *)surface-
>pixels + y * surface-
>pitch + x * bpp;
330 *(Uint16 *)p = pixel;
334 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
335 p[
0] = (pixel
>> 16)
& 0xff;
336 p[
1] = (pixel
>> 8)
& 0xff;
337 p[
2] = pixel
& 0xff;
339 p[
0] = pixel
& 0xff;
340 p[
1] = (pixel
>> 8)
& 0xff;
341 p[
2] = (pixel
>> 16)
& 0xff;
346 *(Uint32 *)p = pixel;
352 >The following code uses the putpixel() function above to set a
353 yellow pixel in the middle of the screen.
</P
361 >Example
2-
6. Using putpixel()
</B
364 CLASS=
"PROGRAMLISTING"
365 > /* Code to set a yellow pixel at the center of the screen */
370 /* Map the color yellow to this display (R=
0xff, G=
0xFF, B=
0x00)
371 Note: If the display is palettized, you must set the palette first.
373 yellow = SDL_MapRGB(screen-
>format,
0xff,
0xff,
0x00);
375 x = screen-
>w /
2;
376 y = screen-
>h /
2;
378 /* Lock the screen for direct access to the pixels */
379 if ( SDL_MUSTLOCK(screen) ) {
380 if ( SDL_LockSurface(screen)
< 0 ) {
381 fprintf(stderr,
"Can't lock screen: %s\n", SDL_GetError());
386 putpixel(screen, x, y, yellow);
388 if ( SDL_MUSTLOCK(screen) ) {
389 SDL_UnlockSurface(screen);
391 /* Update just the part of the display that we've changed */
392 SDL_UpdateRect(screen, x, y,
1,
1);
404 SUMMARY=
"Footer navigation table"
415 HREF=
"guidebasicsinit.html"
433 HREF=
"guidevideoopengl.html"
443 >Initializing SDL
</TD
457 >Using OpenGL With SDL
</TD