Subversion Repositories Kolibri OS

Rev

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

  1. <HTML
  2. ><HEAD
  3. ><TITLE
  4. >Graphics and Video</TITLE
  5. ><META
  6. NAME="GENERATOR"
  7. CONTENT="Modular DocBook HTML Stylesheet Version 1.64
  8. "><LINK
  9. REL="HOME"
  10. TITLE="SDL Library Documentation"
  11. HREF="index.html"><LINK
  12. REL="UP"
  13. TITLE="SDL Guide"
  14. HREF="guide.html"><LINK
  15. REL="PREVIOUS"
  16. TITLE="Initializing SDL"
  17. HREF="guidebasicsinit.html"><LINK
  18. REL="NEXT"
  19. TITLE="Using OpenGL With SDL"
  20. HREF="guidevideoopengl.html"></HEAD
  21. ><BODY
  22. CLASS="CHAPTER"
  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="guidebasicsinit.html"
  48. >Prev</A
  49. ></TD
  50. ><TD
  51. WIDTH="80%"
  52. ALIGN="center"
  53. VALIGN="bottom"
  54. ></TD
  55. ><TD
  56. WIDTH="10%"
  57. ALIGN="right"
  58. VALIGN="bottom"
  59. ><A
  60. HREF="guidevideoopengl.html"
  61. >Next</A
  62. ></TD
  63. ></TR
  64. ></TABLE
  65. ><HR
  66. ALIGN="LEFT"
  67. WIDTH="100%"></DIV
  68. ><DIV
  69. CLASS="CHAPTER"
  70. ><H1
  71. ><A
  72. NAME="GUIDEVIDEO"
  73. >Chapter 2. Graphics and Video</A
  74. ></H1
  75. ><DIV
  76. CLASS="TOC"
  77. ><DL
  78. ><DT
  79. ><B
  80. >Table of Contents</B
  81. ></DT
  82. ><DT
  83. ><A
  84. HREF="guidevideo.html#GUIDEVIDEOINTRO"
  85. >Introduction to SDL Video</A
  86. ></DT
  87. ><DT
  88. ><A
  89. HREF="guidevideoopengl.html"
  90. >Using OpenGL With SDL</A
  91. ></DT
  92. ></DL
  93. ></DIV
  94. ><DIV
  95. CLASS="SECT1"
  96. ><H1
  97. CLASS="SECT1"
  98. ><A
  99. NAME="GUIDEVIDEOINTRO"
  100. >Introduction to SDL Video</A
  101. ></H1
  102. ><P
  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.</P
  106. ><DIV
  107. CLASS="SECT2"
  108. ><H2
  109. CLASS="SECT2"
  110. ><A
  111. NAME="AEN68"
  112. >Initializing the Video Display</A
  113. ></H2
  114. ><P
  115. >This is what almost all SDL programs have to do in one way or
  116. another.</P
  117. ><DIV
  118. CLASS="EXAMPLE"
  119. ><A
  120. NAME="AEN71"
  121. ></A
  122. ><P
  123. ><B
  124. >Example 2-1. Initializing the Video Display</B
  125. ></P
  126. ><PRE
  127. CLASS="PROGRAMLISTING"
  128. >    SDL_Surface *screen;
  129.  
  130.     /* Initialize the SDL library */
  131.     if( SDL_Init(SDL_INIT_VIDEO) &#60; 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.     }</PRE
  150. ></DIV
  151. ></DIV
  152. ><DIV
  153. CLASS="SECT2"
  154. ><H2
  155. CLASS="SECT2"
  156. ><A
  157. NAME="AEN74"
  158. >Initializing the Best Video Mode</A
  159. ></H2
  160. ><P
  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.</P
  165. ><DIV
  166. CLASS="EXAMPLE"
  167. ><A
  168. NAME="AEN77"
  169. ></A
  170. ><P
  171. ><B
  172. >Example 2-2. Initializing the Best Video Mode</B
  173. ></P
  174. ><PRE
  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-&#62;format-&#62;BitsPerPixel);</PRE
  185. ></DIV
  186. ></DIV
  187. ><DIV
  188. CLASS="SECT2"
  189. ><H2
  190. CLASS="SECT2"
  191. ><A
  192. NAME="AEN80"
  193. >Loading and Displaying a BMP File</A
  194. ></H2
  195. ><P
  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.</P
  198. ><DIV
  199. CLASS="EXAMPLE"
  200. ><A
  201. NAME="AEN83"
  202. ></A
  203. ><P
  204. ><B
  205. >Example 2-3. Loading and Displaying a BMP File</B
  206. ></P
  207. ><PRE
  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-&#62;format-&#62;palette &#38;&#38; screen-&#62;format-&#62;palette) {
  226.     SDL_SetColors(screen, image-&#62;format-&#62;palette-&#62;colors, 0,
  227.                   image-&#62;format-&#62;palette-&#62;ncolors);
  228.     }
  229.  
  230.     /* Blit onto the screen surface */
  231.     if(SDL_BlitSurface(image, NULL, screen, NULL) &#60; 0)
  232.         fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());
  233.  
  234.     SDL_UpdateRect(screen, 0, 0, image-&#62;w, image-&#62;h);
  235.  
  236.     /* Free the allocated BMP surface */
  237.     SDL_FreeSurface(image);
  238. }</PRE
  239. ></DIV
  240. ></DIV
  241. ><DIV
  242. CLASS="SECT2"
  243. ><H2
  244. CLASS="SECT2"
  245. ><A
  246. NAME="AEN86"
  247. >Drawing Directly to the Display</A
  248. ></H2
  249. ><P
  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.</P
  255. ><P
  256. >To convert between pixel values and their red, green, blue
  257. components, use SDL_GetRGB() and SDL_MapRGB().</P
  258. ><DIV
  259. CLASS="EXAMPLE"
  260. ><A
  261. NAME="AEN90"
  262. ></A
  263. ><P
  264. ><B
  265. >Example 2-4. getpixel()</B
  266. ></P
  267. ><PRE
  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-&#62;format-&#62;BytesPerPixel;
  276.     /* Here p is the address to the pixel we want to retrieve */
  277.     Uint8 *p = (Uint8 *)surface-&#62;pixels + y * surface-&#62;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] &#60;&#60; 16 | p[1] &#60;&#60; 8 | p[2];
  289.         else
  290.             return p[0] | p[1] &#60;&#60; 8 | p[2] &#60;&#60; 16;
  291.  
  292.     case 4:
  293.         return *(Uint32 *)p;
  294.  
  295.     default:
  296.         return 0;       /* shouldn't happen, but avoids warnings */
  297.    }
  298. }</PRE
  299. ></DIV
  300. ><DIV
  301. CLASS="EXAMPLE"
  302. ><A
  303. NAME="AEN93"
  304. ></A
  305. ><P
  306. ><B
  307. >Example 2-5. putpixel()</B
  308. ></P
  309. ><PRE
  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-&#62;format-&#62;BytesPerPixel;
  318.    /* Here p is the address to the pixel we want to set */
  319.    Uint8 *p = (Uint8 *)surface-&#62;pixels + y * surface-&#62;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 &#62;&#62; 16) &#38; 0xff;
  333.            p[1] = (pixel &#62;&#62; 8) &#38; 0xff;
  334.            p[2] = pixel &#38; 0xff;
  335.        } else {
  336.            p[0] = pixel &#38; 0xff;
  337.            p[1] = (pixel &#62;&#62; 8) &#38; 0xff;
  338.            p[2] = (pixel &#62;&#62; 16) &#38; 0xff;
  339.        }
  340.        break;
  341.  
  342.    case 4:
  343.        *(Uint32 *)p = pixel;
  344.        break;
  345.    }
  346. }</PRE
  347. ></DIV
  348. ><P
  349. >The following code uses the putpixel() function above to set a
  350. yellow pixel in the middle of the screen.</P
  351. ><DIV
  352. CLASS="EXAMPLE"
  353. ><A
  354. NAME="AEN97"
  355. ></A
  356. ><P
  357. ><B
  358. >Example 2-6. Using putpixel()</B
  359. ></P
  360. ><PRE
  361. CLASS="PROGRAMLISTING"
  362. >&#13;    /* 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-&#62;format, 0xff, 0xff, 0x00);
  371.  
  372.    x = screen-&#62;w / 2;
  373.    y = screen-&#62;h / 2;
  374.  
  375.    /* Lock the screen for direct access to the pixels */
  376.    if ( SDL_MUSTLOCK(screen) ) {
  377.        if ( SDL_LockSurface(screen) &#60; 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;&#13;</PRE
  392. ></DIV
  393. ></DIV
  394. ></DIV
  395. ></DIV
  396. ><DIV
  397. CLASS="NAVFOOTER"
  398. ><HR
  399. ALIGN="LEFT"
  400. WIDTH="100%"><TABLE
  401. WIDTH="100%"
  402. BORDER="0"
  403. CELLPADDING="0"
  404. CELLSPACING="0"
  405. ><TR
  406. ><TD
  407. WIDTH="33%"
  408. ALIGN="left"
  409. VALIGN="top"
  410. ><A
  411. HREF="guidebasicsinit.html"
  412. >Prev</A
  413. ></TD
  414. ><TD
  415. WIDTH="34%"
  416. ALIGN="center"
  417. VALIGN="top"
  418. ><A
  419. HREF="index.html"
  420. >Home</A
  421. ></TD
  422. ><TD
  423. WIDTH="33%"
  424. ALIGN="right"
  425. VALIGN="top"
  426. ><A
  427. HREF="guidevideoopengl.html"
  428. >Next</A
  429. ></TD
  430. ></TR
  431. ><TR
  432. ><TD
  433. WIDTH="33%"
  434. ALIGN="left"
  435. VALIGN="top"
  436. >Initializing SDL</TD
  437. ><TD
  438. WIDTH="34%"
  439. ALIGN="center"
  440. VALIGN="top"
  441. ><A
  442. HREF="guide.html"
  443. >Up</A
  444. ></TD
  445. ><TD
  446. WIDTH="33%"
  447. ALIGN="right"
  448. VALIGN="top"
  449. >Using OpenGL With SDL</TD
  450. ></TR
  451. ></TABLE
  452. ></DIV
  453. ></BODY
  454. ></HTML
  455. >