Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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