src/video/SDL_video.c
changeset 3166 3a63a5824557
parent 3162 dc1eb82ffdaa
child 3169 f294338ca6eb
equal deleted inserted replaced
3165:7168651d5706 3166:3a63a5824557
    46 #undef CreateWindow
    46 #undef CreateWindow
    47 #endif
    47 #endif
    48 #endif /* SDL_VIDEO_OPENGL */
    48 #endif /* SDL_VIDEO_OPENGL */
    49 
    49 
    50 /* Available video drivers */
    50 /* Available video drivers */
    51 static const VideoBootStrap *const bootstrap[] = {
    51 static VideoBootStrap *bootstrap[] = {
    52 #if SDL_VIDEO_DRIVER_COCOA
    52 #if SDL_VIDEO_DRIVER_COCOA
    53     &COCOA_bootstrap,
    53     &COCOA_bootstrap,
    54 #endif
    54 #endif
    55 #if SDL_VIDEO_DRIVER_X11
    55 #if SDL_VIDEO_DRIVER_X11
    56     &X11_bootstrap,
    56     &X11_bootstrap,
  1596 SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
  1596 SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
  1597 {
  1597 {
  1598     SDL_TextureID textureID;
  1598     SDL_TextureID textureID;
  1599     Uint32 requested_format = format;
  1599     Uint32 requested_format = format;
  1600     SDL_PixelFormat *fmt;
  1600     SDL_PixelFormat *fmt;
       
  1601     SDL_Renderer *renderer;
  1601     int bpp;
  1602     int bpp;
  1602     Uint32 Rmask, Gmask, Bmask, Amask;
  1603     Uint32 Rmask, Gmask, Bmask, Amask;
  1603 
  1604 
  1604     if (!surface) {
  1605     if (!surface) {
  1605         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1606         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1606         return 0;
  1607         return 0;
  1607     }
  1608     }
  1608     fmt = surface->format;
  1609     fmt = surface->format;
       
  1610 
       
  1611     renderer = SDL_CurrentDisplay.current_renderer;
       
  1612     if (!renderer) {
       
  1613         return 0;
       
  1614     }
  1609 
  1615 
  1610     if (format) {
  1616     if (format) {
  1611         if (!SDL_PixelFormatEnumToMasks
  1617         if (!SDL_PixelFormatEnumToMasks
  1612             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1618             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1613             SDL_SetError("Unknown pixel format");
  1619             SDL_SetError("Unknown pixel format");
  1623             Gmask = fmt->Gmask;
  1629             Gmask = fmt->Gmask;
  1624             Bmask = fmt->Bmask;
  1630             Bmask = fmt->Bmask;
  1625             Amask = fmt->Amask;
  1631             Amask = fmt->Amask;
  1626         } else {
  1632         } else {
  1627             /* Need a format with alpha */
  1633             /* Need a format with alpha */
       
  1634             int it;
       
  1635             int apfmt;
       
  1636 
       
  1637             /* Pixel formats with alpha, sorted by best first */
       
  1638             static const Uint32 sdl_alpha_pformats[]={
       
  1639                SDL_PIXELFORMAT_ARGB8888,
       
  1640                SDL_PIXELFORMAT_RGBA8888,
       
  1641                SDL_PIXELFORMAT_ABGR8888,
       
  1642                SDL_PIXELFORMAT_BGRA8888,
       
  1643                SDL_PIXELFORMAT_ARGB1555,
       
  1644                SDL_PIXELFORMAT_ABGR1555,
       
  1645                SDL_PIXELFORMAT_ARGB4444,
       
  1646                SDL_PIXELFORMAT_ABGR4444,
       
  1647                SDL_PIXELFORMAT_ARGB2101010,
       
  1648                SDL_PIXELFORMAT_UNKNOWN};
       
  1649 
  1628             bpp = 32;
  1650             bpp = 32;
  1629             Rmask = 0x00FF0000;
  1651             Rmask = 0x00FF0000;
  1630             Gmask = 0x0000FF00;
  1652             Gmask = 0x0000FF00;
  1631             Bmask = 0x000000FF;
  1653             Bmask = 0x000000FF;
  1632             Amask = 0xFF000000;
  1654             Amask = 0xFF000000;
  1633         }
  1655 
       
  1656             format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
       
  1657             if (!format) {
       
  1658                 SDL_SetError("Unknown pixel format");
       
  1659                 return 0;
       
  1660             }
       
  1661 
       
  1662             /* Search this format in the supported texture formats */
       
  1663             /* by current renderer                                 */
       
  1664             for (it=0; it<renderer->info.num_texture_formats; it++)
       
  1665             {
       
  1666                 if (renderer->info.texture_formats[it]==format)
       
  1667                 {
       
  1668                    break;
       
  1669                 }
       
  1670             }
       
  1671 
       
  1672             /* If this format can't be found, search any best       */
       
  1673             /* compatible format with alpha which renderer provides */
       
  1674             if (it==renderer->info.num_texture_formats)
       
  1675             {
       
  1676                 apfmt=0;
       
  1677                 for (;;)
       
  1678                 {
       
  1679                     if (sdl_alpha_pformats[apfmt]==SDL_PIXELFORMAT_UNKNOWN)
       
  1680                     {
       
  1681                         break;
       
  1682                     }
       
  1683 
       
  1684                     for (it=0; it<renderer->info.num_texture_formats; it++)
       
  1685                     {
       
  1686                        if (renderer->info.texture_formats[it]==sdl_alpha_pformats[apfmt])
       
  1687                        {
       
  1688                           break;
       
  1689                        }
       
  1690                     }
       
  1691 
       
  1692                     if (it!=renderer->info.num_texture_formats)
       
  1693                     {
       
  1694                        /* Compatible format has been found */
       
  1695                        break;
       
  1696                     }
       
  1697                     apfmt++;
       
  1698                 }
       
  1699 
       
  1700                 /* If compatible format can't be found, then return an error */
       
  1701                 if (it==renderer->info.num_texture_formats)
       
  1702                 {
       
  1703                     SDL_SetError("Compatible pixel format can't be found");
       
  1704                     return 0;
       
  1705                 }
       
  1706 
       
  1707                 /* Convert found pixel format back to color masks */
       
  1708                 if (SDL_PixelFormatEnumToMasks(renderer->info.texture_formats[it],
       
  1709                        &bpp, &Rmask, &Gmask, &Bmask, &Amask)!=SDL_TRUE)
       
  1710                 {
       
  1711                     SDL_SetError("Unknown pixel format");
       
  1712                     return 0;
       
  1713                 }
       
  1714             }
       
  1715         }
       
  1716 
  1634         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1717         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1635         if (!format) {
  1718         if (!format) {
  1636             SDL_SetError("Unknown pixel format");
  1719             SDL_SetError("Unknown pixel format");
  1637             return 0;
  1720             return 0;
  1638         }
  1721         }