Skip to content

Latest commit

 

History

History
473 lines (445 loc) · 9.94 KB

guidevideoexamples.html

File metadata and controls

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