Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
463 lines (439 loc) · 8.97 KB

guidevideo.html

File metadata and controls

463 lines (439 loc) · 8.97 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
3
4
5
6
<HTML
><HEAD
><TITLE
>Graphics and Video</TITLE
><META
NAME="GENERATOR"
Feb 10, 2004
Feb 10, 2004
7
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
Apr 26, 2001
Apr 26, 2001
8
9
10
11
12
13
14
15
16
17
18
"><LINK
REL="HOME"
TITLE="SDL Library Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SDL Guide"
HREF="guide.html"><LINK
REL="PREVIOUS"
TITLE="Initializing SDL"
HREF="guidebasicsinit.html"><LINK
REL="NEXT"
Jun 10, 2001
Jun 10, 2001
19
20
TITLE="Using OpenGL With SDL"
HREF="guidevideoopengl.html"></HEAD
Apr 26, 2001
Apr 26, 2001
21
22
23
24
25
26
27
28
29
30
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFF8DC"
TEXT="#000000"
LINK="#0000ee"
VLINK="#551a8b"
ALINK="#ff0000"
><DIV
CLASS="NAVHEADER"
><TABLE
Feb 10, 2004
Feb 10, 2004
31
SUMMARY="Header navigation table"
Apr 26, 2001
Apr 26, 2001
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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="guidebasicsinit.html"
Feb 10, 2004
Feb 10, 2004
49
ACCESSKEY="P"
Apr 26, 2001
Apr 26, 2001
50
51
52
53
54
55
56
57
58
59
60
61
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
Jun 10, 2001
Jun 10, 2001
62
HREF="guidevideoopengl.html"
Feb 10, 2004
Feb 10, 2004
63
ACCESSKEY="N"
Apr 26, 2001
Apr 26, 2001
64
65
66
67
68
69
70
71
72
73
74
75
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="GUIDEVIDEO"
Feb 10, 2004
Feb 10, 2004
76
77
></A
>Chapter 2. Graphics and Video</H1
Apr 26, 2001
Apr 26, 2001
78
><DIV
Jun 10, 2001
Jun 10, 2001
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="guidevideo.html#GUIDEVIDEOINTRO"
>Introduction to SDL Video</A
></DT
><DT
><A
HREF="guidevideoopengl.html"
>Using OpenGL With SDL</A
></DT
></DL
></DIV
><DIV
Apr 26, 2001
Apr 26, 2001
98
99
100
101
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
Jun 10, 2001
Jun 10, 2001
102
NAME="GUIDEVIDEOINTRO"
Feb 10, 2004
Feb 10, 2004
103
104
></A
>Introduction to SDL Video</H1
Apr 26, 2001
Apr 26, 2001
105
><P
Jun 10, 2001
Jun 10, 2001
106
107
108
>Video is probably the most common thing that SDL is used for, and
so it has the most complete subsystem. Here are a few
examples to demonstrate the basics.</P
Apr 26, 2001
Apr 26, 2001
109
110
111
112
113
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
Jun 10, 2001
Jun 10, 2001
114
NAME="AEN68"
Feb 10, 2004
Feb 10, 2004
115
116
></A
>Initializing the Video Display</H2
Apr 26, 2001
Apr 26, 2001
117
><P
Jun 10, 2001
Jun 10, 2001
118
119
120
121
122
123
>This is what almost all SDL programs have to do in one way or
another.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN71"
Apr 26, 2001
Apr 26, 2001
124
></A
Jun 10, 2001
Jun 10, 2001
125
126
127
128
129
><P
><B
>Example 2-1. Initializing the Video Display</B
></P
><PRE
Apr 26, 2001
Apr 26, 2001
130
CLASS="PROGRAMLISTING"
Jun 10, 2001
Jun 10, 2001
131
> SDL_Surface *screen;
Apr 26, 2001
Apr 26, 2001
132
Jun 10, 2001
Jun 10, 2001
133
134
135
136
137
/* Initialize the SDL library */
if( SDL_Init(SDL_INIT_VIDEO) &#60; 0 ) {
fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
Apr 26, 2001
Apr 26, 2001
138
139
}
Jun 10, 2001
Jun 10, 2001
140
141
142
/* Clean up on exit */
atexit(SDL_Quit);
Apr 26, 2001
Apr 26, 2001
143
/*
Jun 10, 2001
Jun 10, 2001
144
145
* Initialize the display in a 640x480 8-bit palettized mode,
* requesting a software surface
Apr 26, 2001
Apr 26, 2001
146
*/
Jun 10, 2001
Jun 10, 2001
147
148
149
150
151
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);
Apr 26, 2001
Apr 26, 2001
152
}</PRE
Jun 10, 2001
Jun 10, 2001
153
></DIV
Apr 26, 2001
Apr 26, 2001
154
155
156
157
158
159
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
Jun 10, 2001
Jun 10, 2001
160
NAME="AEN74"
Feb 10, 2004
Feb 10, 2004
161
162
></A
>Initializing the Best Video Mode</H2
Apr 26, 2001
Apr 26, 2001
163
><P
Jun 10, 2001
Jun 10, 2001
164
165
166
167
168
169
170
171
>If you have a preference for a certain pixel depth but will accept any
other, use SDL_SetVideoMode with SDL_ANYFORMAT as below. You can also
use SDL_VideoModeOK() to find the native video mode that is closest to
the mode you request.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN77"
Apr 26, 2001
Apr 26, 2001
172
173
></A
><P
Jun 10, 2001
Jun 10, 2001
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
><B
>Example 2-2. Initializing the Best Video Mode</B
></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
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN80"
Feb 10, 2004
Feb 10, 2004
196
197
></A
>Loading and Displaying a BMP File</H2
Jun 10, 2001
Jun 10, 2001
198
199
200
><P
>The following function loads and displays a BMP file given as
argument, once SDL is initialised and a video mode has been set.</P
Apr 26, 2001
Apr 26, 2001
201
202
203
><DIV
CLASS="EXAMPLE"
><A
Jun 10, 2001
Jun 10, 2001
204
NAME="AEN83"
Apr 26, 2001
Apr 26, 2001
205
206
207
></A
><P
><B
Jun 10, 2001
Jun 10, 2001
208
>Example 2-3. Loading and Displaying a BMP File</B
Apr 26, 2001
Apr 26, 2001
209
210
211
></P
><PRE
CLASS="PROGRAMLISTING"
Jun 10, 2001
Jun 10, 2001
212
>void display_bmp(char *file_name)
Apr 26, 2001
Apr 26, 2001
213
{
Jun 10, 2001
Jun 10, 2001
214
SDL_Surface *image;
Apr 26, 2001
Apr 26, 2001
215
Jun 10, 2001
Jun 10, 2001
216
217
218
219
220
/* Load the BMP file into a surface */
image = SDL_LoadBMP(file_name);
if (image == NULL) {
fprintf(stderr, "Couldn't load %s: %s\n", file_name, SDL_GetError());
return;
Apr 26, 2001
Apr 26, 2001
221
222
223
}
/*
Jun 10, 2001
Jun 10, 2001
224
225
226
* Palettized screen modes will have a default palette (a standard
* 8*8*4 colour cube), but if the image is palettized as well we can
* use that palette for a nicer colour matching
Apr 26, 2001
Apr 26, 2001
227
*/
Jun 10, 2001
Jun 10, 2001
228
229
230
if (image-&#62;format-&#62;palette &#38;&#38; screen-&#62;format-&#62;palette) {
SDL_SetColors(screen, image-&#62;format-&#62;palette-&#62;colors, 0,
image-&#62;format-&#62;palette-&#62;ncolors);
Apr 26, 2001
Apr 26, 2001
231
232
}
Jun 10, 2001
Jun 10, 2001
233
234
235
/* Blit onto the screen surface */
if(SDL_BlitSurface(image, NULL, screen, NULL) &#60; 0)
fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());
Apr 26, 2001
Apr 26, 2001
236
Jun 10, 2001
Jun 10, 2001
237
SDL_UpdateRect(screen, 0, 0, image-&#62;w, image-&#62;h);
Apr 26, 2001
Apr 26, 2001
238
Jun 10, 2001
Jun 10, 2001
239
240
241
242
243
244
245
246
247
248
249
/* Free the allocated BMP surface */
SDL_FreeSurface(image);
}</PRE
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN86"
Feb 10, 2004
Feb 10, 2004
250
251
></A
>Drawing Directly to the Display</H2
Jun 10, 2001
Jun 10, 2001
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
><P
>The following two functions can be used to get and set single
pixels of a surface. They are carefully written to work with any depth
currently supported by SDL. Remember to lock the surface before
calling them, and to unlock it before calling any other SDL
functions.</P
><P
>To convert between pixel values and their red, green, blue
components, use SDL_GetRGB() and SDL_MapRGB().</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN90"
></A
><P
><B
>Example 2-4. getpixel()</B
></P
><PRE
CLASS="PROGRAMLISTING"
>/*
* Return the pixel value at (x, y)
* NOTE: The surface must be locked before calling this!
*/
Uint32 getpixel(SDL_Surface *surface, int x, int y)
Apr 26, 2001
Apr 26, 2001
277
{
Jun 10, 2001
Jun 10, 2001
278
279
280
int bpp = surface-&#62;format-&#62;BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface-&#62;pixels + y * surface-&#62;pitch + x * bpp;
Apr 26, 2001
Apr 26, 2001
281
Jun 10, 2001
Jun 10, 2001
282
283
284
switch(bpp) {
case 1:
return *p;
Apr 26, 2001
Apr 26, 2001
285
Jun 10, 2001
Jun 10, 2001
286
287
case 2:
return *(Uint16 *)p;
Apr 26, 2001
Apr 26, 2001
288
Jun 10, 2001
Jun 10, 2001
289
290
291
292
293
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] &#60;&#60; 16 | p[1] &#60;&#60; 8 | p[2];
else
return p[0] | p[1] &#60;&#60; 8 | p[2] &#60;&#60; 16;
Apr 26, 2001
Apr 26, 2001
294
Jun 10, 2001
Jun 10, 2001
295
296
case 4:
return *(Uint32 *)p;
Apr 26, 2001
Apr 26, 2001
297
Jun 10, 2001
Jun 10, 2001
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}</PRE
></DIV
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN93"
></A
><P
><B
>Example 2-5. putpixel()</B
></P
><PRE
CLASS="PROGRAMLISTING"
>/*
* Set the pixel at (x, y) to the given value
* NOTE: The surface must be locked before calling this!
*/
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
Apr 26, 2001
Apr 26, 2001
319
{
Jun 10, 2001
Jun 10, 2001
320
321
322
int bpp = surface-&#62;format-&#62;BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface-&#62;pixels + y * surface-&#62;pitch + x * bpp;
Apr 26, 2001
Apr 26, 2001
323
Jun 10, 2001
Jun 10, 2001
324
325
326
327
switch(bpp) {
case 1:
*p = pixel;
break;
Apr 26, 2001
Apr 26, 2001
328
Jun 10, 2001
Jun 10, 2001
329
330
331
case 2:
*(Uint16 *)p = pixel;
break;
Apr 26, 2001
Apr 26, 2001
332
Jun 10, 2001
Jun 10, 2001
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel &#62;&#62; 16) &#38; 0xff;
p[1] = (pixel &#62;&#62; 8) &#38; 0xff;
p[2] = pixel &#38; 0xff;
} else {
p[0] = pixel &#38; 0xff;
p[1] = (pixel &#62;&#62; 8) &#38; 0xff;
p[2] = (pixel &#62;&#62; 16) &#38; 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
Apr 26, 2001
Apr 26, 2001
348
}
Jun 10, 2001
Jun 10, 2001
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
}</PRE
></DIV
><P
>The following code uses the putpixel() function above to set a
yellow pixel in the middle of the screen.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN97"
></A
><P
><B
>Example 2-6. Using putpixel()</B
></P
><PRE
CLASS="PROGRAMLISTING"
>&#13; /* Code to set a yellow pixel at the center of the screen */
Apr 26, 2001
Apr 26, 2001
366
Jun 10, 2001
Jun 10, 2001
367
368
int x, y;
Uint32 yellow;
Apr 26, 2001
Apr 26, 2001
369
Jun 10, 2001
Jun 10, 2001
370
371
372
373
/* 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.
*/
yellow = SDL_MapRGB(screen-&#62;format, 0xff, 0xff, 0x00);
Apr 26, 2001
Apr 26, 2001
374
Jun 10, 2001
Jun 10, 2001
375
376
x = screen-&#62;w / 2;
y = screen-&#62;h / 2;
Apr 26, 2001
Apr 26, 2001
377
Jun 10, 2001
Jun 10, 2001
378
379
380
381
382
383
/* Lock the screen for direct access to the pixels */
if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) &#60; 0 ) {
fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
return;
}
Apr 26, 2001
Apr 26, 2001
384
385
}
Jun 10, 2001
Jun 10, 2001
386
putpixel(screen, x, y, yellow);
Apr 26, 2001
Apr 26, 2001
387
Jun 10, 2001
Jun 10, 2001
388
389
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
Apr 26, 2001
Apr 26, 2001
390
}
Jun 10, 2001
Jun 10, 2001
391
392
/* Update just the part of the display that we've changed */
SDL_UpdateRect(screen, x, y, 1, 1);
Apr 26, 2001
Apr 26, 2001
393
Jun 10, 2001
Jun 10, 2001
394
return;&#13;</PRE
Apr 26, 2001
Apr 26, 2001
395
396
397
398
399
400
401
402
403
></DIV
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
Feb 10, 2004
Feb 10, 2004
404
SUMMARY="Footer navigation table"
Apr 26, 2001
Apr 26, 2001
405
406
407
408
409
410
411
412
413
414
415
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="guidebasicsinit.html"
Feb 10, 2004
Feb 10, 2004
416
ACCESSKEY="P"
Apr 26, 2001
Apr 26, 2001
417
418
419
420
421
422
423
424
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
Feb 10, 2004
Feb 10, 2004
425
ACCESSKEY="H"
Apr 26, 2001
Apr 26, 2001
426
427
428
429
430
431
432
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
Jun 10, 2001
Jun 10, 2001
433
HREF="guidevideoopengl.html"
Feb 10, 2004
Feb 10, 2004
434
ACCESSKEY="N"
Apr 26, 2001
Apr 26, 2001
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Initializing SDL</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="guide.html"
Feb 10, 2004
Feb 10, 2004
450
ACCESSKEY="U"
Apr 26, 2001
Apr 26, 2001
451
452
453
454
455
456
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
Jun 10, 2001
Jun 10, 2001
457
>Using OpenGL With SDL</TD
Apr 26, 2001
Apr 26, 2001
458
459
460
461
462
463
></TR
></TABLE
></DIV
></BODY
></HTML
>