Mercurial > sdl-ios-xcode
annotate docs/html/guidevideoexamples.html @ 78:e9582f471c02
Added the environment variable SDL_MOUSE_NORELATIVE to completely disable
relative mouse motion if the application desires it. UNDOCUMENTED!!
author | Sam Lantinga <slouken@lokigames.com> |
---|---|
date | Sat, 23 Jun 2001 18:51:29 +0000 |
parents | 55f1f1b3e27d |
children |
rev | line source |
---|---|
0 | 1 <HTML |
2 ><HEAD | |
3 ><TITLE | |
4 >Video Examples</TITLE | |
5 ><META | |
6 NAME="GENERATOR" | |
55
55f1f1b3e27d
Added new docs for SDL 1.2.1
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
7 CONTENT="Modular DocBook HTML Stylesheet Version 1.64 |
0 | 8 "><LINK |
9 REL="HOME" | |
10 TITLE="SDL Library Documentation" | |
11 HREF="index.html"><LINK | |
12 REL="UP" | |
13 TITLE="Examples" | |
14 HREF="guideexamples.html"><LINK | |
15 REL="PREVIOUS" | |
16 TITLE="Examples" | |
17 HREF="guideexamples.html"><LINK | |
18 REL="NEXT" | |
19 TITLE="Event Examples" | |
20 HREF="guideeventexamples.html"></HEAD | |
21 ><BODY | |
22 CLASS="SECT1" | |
23 BGCOLOR="#FFF8DC" | |
24 TEXT="#000000" | |
25 LINK="#0000ee" | |
26 VLINK="#551a8b" | |
27 ALINK="#ff0000" | |
28 ><DIV | |
29 CLASS="NAVHEADER" | |
30 ><TABLE | |
31 WIDTH="100%" | |
32 BORDER="0" | |
33 CELLPADDING="0" | |
34 CELLSPACING="0" | |
35 ><TR | |
36 ><TH | |
37 COLSPAN="3" | |
38 ALIGN="center" | |
39 >SDL Library Documentation</TH | |
40 ></TR | |
41 ><TR | |
42 ><TD | |
43 WIDTH="10%" | |
44 ALIGN="left" | |
45 VALIGN="bottom" | |
46 ><A | |
47 HREF="guideexamples.html" | |
48 >Prev</A | |
49 ></TD | |
50 ><TD | |
51 WIDTH="80%" | |
52 ALIGN="center" | |
53 VALIGN="bottom" | |
54 >Chapter 4. Examples</TD | |
55 ><TD | |
56 WIDTH="10%" | |
57 ALIGN="right" | |
58 VALIGN="bottom" | |
59 ><A | |
60 HREF="guideeventexamples.html" | |
61 >Next</A | |
62 ></TD | |
63 ></TR | |
64 ></TABLE | |
65 ><HR | |
66 ALIGN="LEFT" | |
67 WIDTH="100%"></DIV | |
68 ><DIV | |
69 CLASS="SECT1" | |
70 ><H1 | |
71 CLASS="SECT1" | |
72 ><A | |
73 NAME="GUIDEVIDEOEXAMPLES" | |
74 >Video Examples</A | |
75 ></H1 | |
76 ><P | |
77 ></P | |
78 ><DIV | |
79 CLASS="SECT2" | |
80 ><H2 | |
81 CLASS="SECT2" | |
82 ><A | |
55
55f1f1b3e27d
Added new docs for SDL 1.2.1
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
83 NAME="AEN375" |
0 | 84 >Initializing the video display</A |
85 ></H2 | |
86 ><P | |
87 ><PRE | |
88 CLASS="PROGRAMLISTING" | |
89 > SDL_Surface *screen; | |
90 | |
91 /* Initialize the SDL library */ | |
92 if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | |
93 fprintf(stderr, | |
94 "Couldn't initialize SDL: %s\n", SDL_GetError()); | |
95 exit(1); | |
96 } | |
97 | |
98 /* Clean up on exit */ | |
99 atexit(SDL_Quit); | |
100 | |
101 /* Initialize the display in a 640x480 8-bit palettized mode */ | |
102 screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE); | |
103 if ( screen == NULL ) { | |
104 fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n", | |
105 SDL_GetError()); | |
106 exit(1); | |
107 }</PRE | |
108 ></P | |
109 ></DIV | |
110 ><DIV | |
111 CLASS="SECT2" | |
112 ><H2 | |
113 CLASS="SECT2" | |
114 ><A | |
55
55f1f1b3e27d
Added new docs for SDL 1.2.1
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
115 NAME="AEN379" |
0 | 116 >Initializing the best video mode</A |
117 ></H2 | |
118 ><P | |
119 ><PRE | |
120 CLASS="PROGRAMLISTING" | |
121 > /* Have a preference for 8-bit, but accept any depth */ | |
122 screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_ANYFORMAT); | |
123 if ( screen == NULL ) { | |
124 fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n", | |
125 SDL_GetError()); | |
126 exit(1); | |
127 } | |
128 printf("Set 640x480 at %d bits-per-pixel mode\n", | |
129 screen->format->BitsPerPixel);</PRE | |
130 ></P | |
131 ></DIV | |
132 ><DIV | |
133 CLASS="SECT2" | |
134 ><H2 | |
135 CLASS="SECT2" | |
136 ><A | |
55
55f1f1b3e27d
Added new docs for SDL 1.2.1
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
137 NAME="AEN383" |
0 | 138 >Loading and displaying a BMP file</A |
139 ></H2 | |
140 ><P | |
141 ><PRE | |
142 CLASS="PROGRAMLISTING" | |
143 > SDL_Surface *image; | |
144 SDL_Rect dest; | |
145 int ncolors, i; | |
146 SDL_Color *colors; | |
147 | |
148 /* Load the BMP file into a surface */ | |
149 image = SDL_LoadBMP("sample.bmp"); | |
150 if ( image == NULL ) { | |
151 fprintf(stderr, "Couldn't load sample.bmp: %s\n", | |
152 SDL_GetError()); | |
153 return; | |
154 } | |
155 | |
156 /* Set the display colors -- SDL_SetColors() only does something on | |
157 palettized displays, but it doesn't hurt anything on HiColor or | |
158 TrueColor displays. | |
159 If the display colors have already been set, this step can be | |
160 skipped, and the library will automatically map the image to | |
161 the current display colors. | |
162 */ | |
163 if ( image->format->palette ) { | |
164 ncolors = image->format->palette->ncolors; | |
165 colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color)); | |
166 memcpy(colors, image->format->palette->colors, ncolors); | |
167 } | |
168 else { | |
169 int r, g, b; | |
170 | |
171 /* Allocate 256 color palette */ | |
172 ncolors = 256; | |
173 colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color)); | |
174 | |
175 /* Set a 3,3,2 color cube */ | |
176 for ( r=0; r<8; ++r ) { | |
177 for ( g=0; g<8; ++g ) { | |
178 for ( b=0; b<4; ++b ) { | |
179 i = ((r<<5)|(g<<2)|b); | |
180 colors[i].r = r<<5; | |
181 colors[i].g = g<<5; | |
182 colors[i].b = b<<6; | |
183 } | |
184 } | |
185 } | |
186 /* Note: A better way of allocating the palette might be | |
187 to calculate the frequency of colors in the image | |
188 and create a palette based on that information. | |
189 */ | |
190 } | |
191 /* Set colormap, try for all the colors, but don't worry about it */ | |
192 SDL_SetColors(screen, colors, 0, ncolors); | |
193 free(colors); | |
194 | |
195 /* Blit onto the screen surface */ | |
196 dest.x = 0; | |
197 dest.y = 0; | |
198 dest.w = image->w; | |
199 dest.h = image->h; | |
200 SDL_BlitSurface(image, NULL, screen, &dest); | |
201 | |
202 SDL_UpdateRects(screen, 1, &dest); | |
203 | |
204 /* Free the allocated BMP surface */ | |
205 SDL_FreeSurface(image); | |
206 return;</PRE | |
207 ></P | |
208 ></DIV | |
209 ><DIV | |
210 CLASS="SECT2" | |
211 ><H2 | |
212 CLASS="SECT2" | |
213 ><A | |
55
55f1f1b3e27d
Added new docs for SDL 1.2.1
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
214 NAME="AEN387" |
0 | 215 >Drawing directly to the display</A |
216 ></H2 | |
217 ><P | |
218 ><PRE | |
219 CLASS="PROGRAMLISTING" | |
220 > /* Code to set a yellow pixel at the center of the screen */ | |
221 | |
222 Sint32 X, Y; | |
223 Uint32 pixel; | |
224 Uint8 *bits, bpp; | |
225 | |
226 /* Map the color yellow to this display (R=0xFF, G=0xFF, B=0x00) | |
227 Note: If the display is palettized, you must set the palette first. | |
228 */ | |
229 pixel = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0x00); | |
230 | |
231 /* Calculate the framebuffer offset of the center of the screen */ | |
232 if ( SDL_MUSTLOCK(screen) ) { | |
233 if ( SDL_LockSurface(screen) < 0 ) | |
234 return; | |
235 } | |
236 bpp = screen->format->BytesPerPixel; | |
237 X = screen->w/2; | |
238 Y = screen->h/2; | |
239 bits = ((Uint8 *)screen->pixels)+Y*screen->pitch+X*bpp; | |
240 | |
241 /* Set the pixel */ | |
242 switch(bpp) { | |
243 case 1: | |
244 *((Uint8 *)(bits)) = (Uint8)pixel; | |
245 break; | |
246 case 2: | |
247 *((Uint16 *)(bits)) = (Uint16)pixel; | |
248 break; | |
249 case 3: { /* Format/endian independent */ | |
250 Uint8 r, g, b; | |
251 | |
252 r = (pixel>>screen->format->Rshift)&0xFF; | |
253 g = (pixel>>screen->format->Gshift)&0xFF; | |
254 b = (pixel>>screen->format->Bshift)&0xFF; | |
255 *((bits)+screen->format->Rshift/8) = r; | |
256 *((bits)+screen->format->Gshift/8) = g; | |
257 *((bits)+screen->format->Bshift/8) = b; | |
258 } | |
259 break; | |
260 case 4: | |
261 *((Uint32 *)(bits)) = (Uint32)pixel; | |
262 break; | |
263 } | |
264 | |
265 /* Update the display */ | |
266 if ( SDL_MUSTLOCK(screen) ) { | |
267 SDL_UnlockSurface(screen); | |
268 } | |
269 SDL_UpdateRect(screen, X, Y, 1, 1); | |
270 | |
271 return;</PRE | |
272 ></P | |
273 ></DIV | |
274 ><DIV | |
275 CLASS="SECT2" | |
276 ><H2 | |
277 CLASS="SECT2" | |
278 ><A | |
55
55f1f1b3e27d
Added new docs for SDL 1.2.1
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
279 NAME="AEN391" |
0 | 280 >Fastest possible surface blit</A |
281 ></H2 | |
282 ><P | |
283 >There are three different ways you can draw an image to the screen: | |
284 <P | |
285 ></P | |
286 ><TABLE | |
287 BORDER="0" | |
288 ><TBODY | |
289 ><TR | |
290 ><TD | |
291 >1.Create a surface and use <A | |
292 HREF="sdlblitsurface.html" | |
293 ><TT | |
294 CLASS="FUNCTION" | |
295 >SDL_BlitSurface</TT | |
296 ></A | |
297 > to blit it to the screen</TD | |
298 ></TR | |
299 ><TR | |
300 ><TD | |
301 >2.Create the video surface in system memory and call <A | |
302 HREF="sdlupdaterect.html" | |
303 ><TT | |
304 CLASS="FUNCTION" | |
305 >SDL_UpdateRect</TT | |
306 ></A | |
307 ></TD | |
308 ></TR | |
309 ><TR | |
310 ><TD | |
311 >3.Create the video surface in video memory and call <A | |
312 HREF="sdllocksurface.html" | |
313 ><TT | |
314 CLASS="FUNCTION" | |
315 >SDL_LockSurface</TT | |
316 ></A | |
317 ></TD | |
318 ></TR | |
319 ></TBODY | |
320 ></TABLE | |
321 ><P | |
322 ></P | |
323 > | |
324 The best way to do this is to combine methods: | |
325 <PRE | |
326 CLASS="PROGRAMLISTING" | |
327 >#include <stdio.h> | |
328 #include <stdlib.h> | |
329 #include "SDL.h" | |
330 #include "SDL_timer.h" | |
331 | |
332 void ComplainAndExit(void) | |
333 { | |
334 fprintf(stderr, "Problem: %s\n", SDL_GetError()); | |
335 exit(1); | |
336 } | |
337 | |
338 int main(int argc, char *argv[]) | |
339 { | |
340 SDL_PixelFormat fmt; | |
341 SDL_Surface *screen, *locked; | |
342 SDL_Surface *imagebmp, *image; | |
343 SDL_Rect dstrect; | |
344 int i; | |
345 Uint8 *buffer; | |
346 | |
347 /* Initialize SDL */ | |
348 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | |
349 ComplainAndExit(); | |
350 } | |
351 atexit(SDL_Quit); | |
352 | |
353 /* Load a BMP image into a surface */ | |
354 imagebmp = SDL_LoadBMP("image.bmp"); | |
355 if ( imagebmp == NULL ) { | |
356 ComplainAndExit(); | |
357 } | |
358 | |
359 /* Set the video mode (640x480 at native depth) */ | |
360 screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE|SDL_FULLSCREEN); | |
361 if ( screen == NULL ) { | |
362 ComplainAndExit(); | |
363 } | |
364 | |
365 /* Set the video colormap */ | |
366 if ( imagebmp->format->palette != NULL ) { | |
367 SDL_SetColors(screen, | |
368 imagebmp->format->palette->colors, 0, | |
369 imagebmp->format->palette->ncolors); | |
370 } | |
371 | |
372 /* Convert the image to the video format (maps colors) */ | |
373 image = SDL_DisplayFormat(imagebmp); | |
374 SDL_FreeSurface(imagebmp); | |
375 if ( image == NULL ) { | |
376 ComplainAndExit(); | |
377 } | |
378 | |
379 /* Draw bands of color on the raw surface */ | |
380 if ( SDL_MUSTLOCK(screen) ) { | |
381 if ( SDL_LockSurface(screen) < 0 ) | |
382 ComplainAndExit(); | |
383 } | |
384 buffer=(Uint8 *)screen->pixels; | |
385 for ( i=0; i<screen->h; ++i ) { | |
386 memset(buffer,(i*255)/screen->h, | |
387 screen->w*screen->format->BytesPerPixel); | |
388 buffer += screen->pitch; | |
389 } | |
390 if ( SDL_MUSTLOCK(screen) ) { | |
391 SDL_UnlockSurface(screen); | |
392 } | |
393 | |
394 /* Blit the image to the center of the screen */ | |
395 dstrect.x = (screen->w-image->w)/2; | |
396 dstrect.y = (screen->h-image->h)/2; | |
397 dstrect.w = image->w; | |
398 dstrect.h = image->h; | |
399 if ( SDL_BlitSurface(image, NULL, screen, &dstrect) < 0 ) { | |
400 SDL_FreeSurface(image); | |
401 ComplainAndExit(); | |
402 } | |
403 SDL_FreeSurface(image); | |
404 | |
405 /* Update the screen */ | |
406 SDL_UpdateRects(screen, 1, &dstrect); | |
407 | |
408 SDL_Delay(5000); /* Wait 5 seconds */ | |
409 exit(0); | |
410 }</PRE | |
411 ></P | |
412 ></DIV | |
413 ></DIV | |
414 ><DIV | |
415 CLASS="NAVFOOTER" | |
416 ><HR | |
417 ALIGN="LEFT" | |
418 WIDTH="100%"><TABLE | |
419 WIDTH="100%" | |
420 BORDER="0" | |
421 CELLPADDING="0" | |
422 CELLSPACING="0" | |
423 ><TR | |
424 ><TD | |
425 WIDTH="33%" | |
426 ALIGN="left" | |
427 VALIGN="top" | |
428 ><A | |
429 HREF="guideexamples.html" | |
430 >Prev</A | |
431 ></TD | |
432 ><TD | |
433 WIDTH="34%" | |
434 ALIGN="center" | |
435 VALIGN="top" | |
436 ><A | |
437 HREF="index.html" | |
438 >Home</A | |
439 ></TD | |
440 ><TD | |
441 WIDTH="33%" | |
442 ALIGN="right" | |
443 VALIGN="top" | |
444 ><A | |
445 HREF="guideeventexamples.html" | |
446 >Next</A | |
447 ></TD | |
448 ></TR | |
449 ><TR | |
450 ><TD | |
451 WIDTH="33%" | |
452 ALIGN="left" | |
453 VALIGN="top" | |
454 >Examples</TD | |
455 ><TD | |
456 WIDTH="34%" | |
457 ALIGN="center" | |
458 VALIGN="top" | |
459 ><A | |
460 HREF="guideexamples.html" | |
461 >Up</A | |
462 ></TD | |
463 ><TD | |
464 WIDTH="33%" | |
465 ALIGN="right" | |
466 VALIGN="top" | |
467 >Event Examples</TD | |
468 ></TR | |
469 ></TABLE | |
470 ></DIV | |
471 ></BODY | |
472 ></HTML | |
473 > |