5131 |
clevermous |
1 |
|
|
|
2 |
> |
|
|
3 |
> |
|
|
4 |
>Video Examples |
|
|
5 |
> |
|
|
6 |
NAME="GENERATOR" |
|
|
7 |
CONTENT="Modular DocBook HTML Stylesheet Version 1.64 |
|
|
8 |
"> |
|
|
9 |
REL="HOME" |
|
|
10 |
TITLE="SDL Library Documentation" |
|
|
11 |
HREF="index.html"> |
|
|
12 |
REL="UP" |
|
|
13 |
TITLE="Examples" |
|
|
14 |
HREF="guideexamples.html"> |
|
|
15 |
REL="PREVIOUS" |
|
|
16 |
TITLE="Examples" |
|
|
17 |
HREF="guideexamples.html"> |
|
|
18 |
REL="NEXT" |
|
|
19 |
TITLE="Event Examples" |
|
|
20 |
HREF="guideeventexamples.html"> |
|
|
21 |
> |
|
|
22 |
CLASS="SECT1" |
|
|
23 |
BGCOLOR="#FFF8DC" |
|
|
24 |
TEXT="#000000" |
|
|
25 |
LINK="#0000ee" |
|
|
26 |
VLINK="#551a8b" |
|
|
27 |
ALINK="#ff0000" |
|
|
28 |
> |
|
|
29 |
CLASS="NAVHEADER" |
|
|
30 |
>
|
|
31 |
WIDTH="100%" |
|
|
32 |
BORDER="0" |
|
|
33 |
CELLPADDING="0" |
|
|
34 |
CELLSPACING="0" |
|
|
35 |
>
|
|
36 |
> | |
|
|
37 |
COLSPAN="3" |
|
|
38 |
ALIGN="center" |
|
|
39 |
>SDL Library Documentation |
|
|
40 |
> | |
|
|
41 |
>
|
|
42 |
> |
|
|
|
43 |
WIDTH="10%" |
|
|
44 |
ALIGN="left" |
|
|
45 |
VALIGN="bottom" |
|
|
46 |
> |
|
|
47 |
HREF="guideexamples.html" |
|
|
48 |
>Prev |
|
|
49 |
> |
|
|
50 |
> |
|
|
|
51 |
WIDTH="80%" |
|
|
52 |
ALIGN="center" |
|
|
53 |
VALIGN="bottom" |
|
|
54 |
>Chapter 4. Examples |
|
|
55 |
> |
|
|
|
56 |
WIDTH="10%" |
|
|
57 |
ALIGN="right" |
|
|
58 |
VALIGN="bottom" |
|
|
59 |
> |
|
|
60 |
HREF="guideeventexamples.html" |
|
|
61 |
>Next |
|
|
62 |
> |
|
|
63 |
> | |
|
|
64 |
> | |
|
|
65 |
>
|
|
|
66 |
ALIGN="LEFT" |
|
|
67 |
WIDTH="100%"> |
|
|
68 |
> |
|
|
69 |
CLASS="SECT1" |
|
|
70 |
> |
|
|
71 |
CLASS="SECT1" |
|
|
72 |
> |
|
|
73 |
NAME="GUIDEVIDEOEXAMPLES" |
|
|
74 |
>Video Examples |
|
|
75 |
> |
|
|
76 |
> |
|
|
77 |
> |
|
|
78 |
> |
|
|
79 |
CLASS="SECT2" |
|
|
80 |
> |
|
|
81 |
CLASS="SECT2" |
|
|
82 |
> |
|
|
83 |
NAME="AEN375" |
|
|
84 |
>Initializing the video display |
|
|
85 |
> |
|
|
86 |
> |
|
|
87 |
> |
|
|
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 |
} |
|
|
108 |
> |
|
|
109 |
> |
|
|
110 |
> |
|
|
111 |
CLASS="SECT2" |
|
|
112 |
> |
|
|
113 |
CLASS="SECT2" |
|
|
114 |
> |
|
|
115 |
NAME="AEN379" |
|
|
116 |
>Initializing the best video mode |
|
|
117 |
> |
|
|
118 |
> |
|
|
119 |
> |
|
|
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); |
|
|
130 |
> |
|
|
131 |
> |
|
|
132 |
> |
|
|
133 |
CLASS="SECT2" |
|
|
134 |
> |
|
|
135 |
CLASS="SECT2" |
|
|
136 |
> |
|
|
137 |
NAME="AEN383" |
|
|
138 |
>Loading and displaying a BMP file |
|
|
139 |
> |
|
|
140 |
> |
|
|
141 |
> |
|
|
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; |
|
|
207 |
> |
|
|
208 |
> |
|
|
209 |
> |
|
|
210 |
CLASS="SECT2" |
|
|
211 |
> |
|
|
212 |
CLASS="SECT2" |
|
|
213 |
> |
|
|
214 |
NAME="AEN387" |
|
|
215 |
>Drawing directly to the display |
|
|
216 |
> |
|
|
217 |
> |
|
|
218 |
> |
|
|
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; |
|
|
272 |
> |
|
|
273 |
> |
|
|
274 |
> |
|
|
275 |
CLASS="SECT2" |
|
|
276 |
> |
|
|
277 |
CLASS="SECT2" |
|
|
278 |
> |
|
|
279 |
NAME="AEN391" |
|
|
280 |
>Fastest possible surface blit |
|
|
281 |
> |
|
|
282 |
> |
|
|
283 |
>There are three different ways you can draw an image to the screen: |
|
|
284 |
|
|
|
285 |
> |
|
|
286 |
>
|
|
287 |
BORDER="0" |
|
|
288 |
>
|
|
289 |
>
|
|
290 |
> |
|
|
|
291 |
>1.Create a surface and use |
|
|
292 |
HREF="sdlblitsurface.html" |
|
|
293 |
> |
|
|
294 |
CLASS="FUNCTION" |
|
|
295 |
>SDL_BlitSurface |
|
|
296 |
> |
|
|
297 |
> to blit it to the screen |
|
|
298 |
> | |
|
|
299 |
>
|
|
300 |
> |
|
|
|
301 |
>2.Create the video surface in system memory and call |
|
|
302 |
HREF="sdlupdaterect.html" |
|
|
303 |
> |
|
|
304 |
CLASS="FUNCTION" |
|
|
305 |
>SDL_UpdateRect |
|
|
306 |
> |
|
|
307 |
> |
|
|
308 |
> | |
|
|
309 |
>
|
|
310 |
> |
|
|
|
311 |
>3.Create the video surface in video memory and call |
|
|
312 |
HREF="sdllocksurface.html" |
|
|
313 |
> |
|
|
314 |
CLASS="FUNCTION" |
|
|
315 |
>SDL_LockSurface |
|
|
316 |
> |
|
|
317 |
> |
|
|
318 |
> | |
|
|
319 |
> | |
|
|
320 |
> | |
|
|
321 |
> |
|
|
322 |
> |
|
|
323 |
> |
|
|
324 |
The best way to do this is to combine methods: |
|
|
325 |
|
|
|
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 |
} |
|
|
411 |
> |
|
|
412 |
> |
|
|
413 |
> |
|
|
414 |
> |
|
|
415 |
CLASS="NAVFOOTER" |
|
|
416 |
>
|
|
|
417 |
ALIGN="LEFT" |
|
|
418 |
WIDTH="100%"> |
|
|
470 |
> |
|
|
471 |
> |
|
|
472 |
> |
|
|
473 |
> |