src/SDL12_compat.c
changeset 59 39067b54d1b0
parent 58 75a93435ce44
child 60 76cf0dc4bce4
equal deleted inserted replaced
58:75a93435ce44 59:39067b54d1b0
  1585 {
  1585 {
  1586     return CurrentCursor12;
  1586     return CurrentCursor12;
  1587 }
  1587 }
  1588 
  1588 
  1589 static void
  1589 static void
  1590 GetEnvironmentWindowPosition(int w, int h, int *x, int *y)
  1590 GetEnvironmentWindowPosition(int *x, int *y)
  1591 {
  1591 {
  1592     int display = VideoDisplayIndex;
  1592     int display = VideoDisplayIndex;
  1593     const char *window = SDL20_getenv("SDL_VIDEO_WINDOW_POS");
  1593     const char *window = SDL20_getenv("SDL_VIDEO_WINDOW_POS");
  1594     const char *center = SDL20_getenv("SDL_VIDEO_CENTERED");
  1594     const char *center = SDL20_getenv("SDL_VIDEO_CENTERED");
  1595     if (window) {
  1595     if (window) {
  1627         SDL20_DisableScreenSaver();
  1627         SDL20_DisableScreenSaver();
  1628     }
  1628     }
  1629 }
  1629 }
  1630 
  1630 
  1631 
  1631 
       
  1632 static SDL12_Surface *
       
  1633 EndVidModeCreate(void)
       
  1634 {
       
  1635     if (VideoTexture20) {
       
  1636         SDL20_DestroyTexture(VideoTexture20);
       
  1637         VideoTexture20 = NULL;
       
  1638     }
       
  1639     if (VideoRenderer20) {
       
  1640         SDL20_DestroyRenderer(VideoRenderer20);
       
  1641         VideoRenderer20 = NULL;
       
  1642     }
       
  1643     if (VideoGLContext20) {
       
  1644         SDL20_GL_MakeCurrent(NULL, NULL);
       
  1645         SDL20_GL_DeleteContext(VideoGLContext20);
       
  1646         VideoGLContext20 = NULL;
       
  1647     }
       
  1648     if (VideoWindow20) {
       
  1649         SDL20_DestroyWindow(VideoWindow20);
       
  1650         VideoWindow20 = NULL;
       
  1651     }
       
  1652     if (VideoSurface12) {
       
  1653         SDL20_free(VideoSurface12->pixels);
       
  1654         VideoSurface12->pixels = NULL;
       
  1655         SDL_FreeSurface(VideoSurface12);
       
  1656         VideoSurface12 = NULL;
       
  1657     }
       
  1658     if (VideoConvertSurface20) {
       
  1659         SDL20_FreeSurface(VideoConvertSurface20);
       
  1660         VideoConvertSurface20 = NULL;
       
  1661     }
       
  1662     return NULL;
       
  1663 }
       
  1664 
       
  1665 
       
  1666 static SDL12_Surface *
       
  1667 CreateSurface12WithFormat(const int w, const int h, const Uint32 fmt)
       
  1668 {
       
  1669     Uint32 rmask, gmask, bmask, amask;
       
  1670     int bpp;
       
  1671     if (!SDL20_PixelFormatEnumToMasks(fmt, &bpp, &rmask, &gmask, &bmask, &amask)) {
       
  1672         return NULL;
       
  1673     }
       
  1674     return SDL_CreateRGBSurface(0, w, h, bpp, rmask, gmask, bmask, amask);
       
  1675 }
       
  1676 
       
  1677 static SDL_Surface *
       
  1678 CreateNullPixelSurface20(const int width, const int height, const Uint32 fmt)
       
  1679 {
       
  1680     SDL_Surface *surface20 = SDL20_CreateRGBSurfaceWithFormat(0, 0, 0, SDL_BITSPERPIXEL(fmt), fmt);
       
  1681     if (surface20) {
       
  1682         surface20->flags |= SDL_PREALLOC;
       
  1683         surface20->pixels = NULL;
       
  1684         surface20->w = width;
       
  1685         surface20->h = height;
       
  1686         surface20->pitch = 0;
       
  1687         SDL20_SetClipRect(surface20, NULL);
       
  1688     }
       
  1689     return surface20;
       
  1690 }
       
  1691 
       
  1692 
  1632 DECLSPEC SDL12_Surface * SDLCALL
  1693 DECLSPEC SDL12_Surface * SDLCALL
  1633 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
  1694 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
  1634 {
  1695 {
  1635 #error write me
  1696     FIXME("currently ignores SDL_WINDOWID, which we could use with SDL_CreateWindowFrom ...?");
       
  1697     SDL_DisplayMode dmode;
       
  1698     Uint32 fullscreen_flags20 = 0;
       
  1699     Uint32 appfmt;
       
  1700 
       
  1701     /* SDL_SetVideoMode() implicitly inits if necessary. */
       
  1702     if (SDL20_WasInit(SDL_INIT_VIDEO) == 0) {
       
  1703         if (SDL20_Init(SDL_INIT_VIDEO) < 0) {
       
  1704             return NULL;
       
  1705         }
       
  1706     }
       
  1707 
       
  1708     if ((flags12 & SDL12_OPENGLBLIT) == SDL12_OPENGLBLIT) {
       
  1709         FIXME("No OPENGLBLIT support at the moment");
       
  1710         SDL20_SetError("SDL_OPENGLBLIT is (currently) unsupported");
       
  1711         return NULL;
       
  1712     }
       
  1713 
       
  1714     FIXME("handle SDL_ANYFORMAT");
       
  1715 
       
  1716     if ((width < 0) || (height < 0)) {
       
  1717         SDL20_SetError("Invalid width or height");
       
  1718         return NULL;
       
  1719     }
       
  1720 
       
  1721     FIXME("There's an environment variable to choose a display");
       
  1722     if (SDL20_GetCurrentDisplayMode(0, &dmode) < 0) {
       
  1723         return NULL;
       
  1724     }
       
  1725 
       
  1726     if (width == 0) {
       
  1727         width = dmode.w;
       
  1728     }
       
  1729 
       
  1730     if (height == 0) {
       
  1731         height = dmode.h;
       
  1732     }
       
  1733 
       
  1734     if (bpp == 0) {
       
  1735         bpp = SDL_BITSPERPIXEL(dmode.format);
       
  1736     }
       
  1737 
       
  1738     switch (bpp) {
       
  1739         case  8: appfmt = SDL_PIXELFORMAT_INDEX8; break;
       
  1740         case 16: appfmt = SDL_PIXELFORMAT_RGB565; FIXME("bgr instead of rgb?"); break;
       
  1741         case 24: appfmt = SDL_PIXELFORMAT_RGB24; FIXME("bgr instead of rgb?"); break;
       
  1742         case 32: appfmt = SDL_PIXELFORMAT_ARGB8888; FIXME("bgr instead of rgb?"); break;
       
  1743         default: SDL20_SetError("Unsupported bits-per-pixel"); return NULL;
       
  1744     }
       
  1745 
       
  1746     SDL_assert((VideoSurface12 != NULL) == (VideoWindow20 != NULL));
       
  1747 
       
  1748     FIXME("don't do anything if the window's dimensions, etc haven't changed.");
       
  1749     FIXME("we need to preserve VideoSurface12 (but not its pixels), I think...");
       
  1750 
       
  1751     if ( VideoSurface12 && ((VideoSurface12->flags & SDL12_OPENGL) != (flags12 & SDL12_OPENGL)) ) {
       
  1752         EndVidModeCreate();  /* rebuild the window if moving to/from a GL context */
       
  1753     } else if ( VideoSurface12 && (VideoSurface12->surface20->format->format != appfmt)) {
       
  1754         EndVidModeCreate();  /* rebuild the window if changing pixel format */
       
  1755     } else if (VideoGLContext20) {
       
  1756         /* SDL 1.2 (infuriatingly!) destroys the GL context on each resize, so we will too */
       
  1757         SDL20_GL_MakeCurrent(NULL, NULL);
       
  1758         SDL20_GL_DeleteContext(VideoGLContext20);
       
  1759         VideoGLContext20 = NULL;
       
  1760     }
       
  1761 
       
  1762     if (flags12 & SDL12_FULLSCREEN) {
       
  1763         // OpenGL tries to force the real resolution requested, but for
       
  1764         //  software rendering, we're just going to push it off onto the
       
  1765         //  GPU, so use FULLSCREEN_DESKTOP and logical scaling there.
       
  1766         FIXME("OpenGL will still expect letterboxing and centering if it didn't get an exact resolution match.");
       
  1767         if (flags12 & SDL12_OPENGL) {
       
  1768             fullscreen_flags20 |= SDL_WINDOW_FULLSCREEN;
       
  1769         } else {
       
  1770             fullscreen_flags20 |= SDL_WINDOW_FULLSCREEN_DESKTOP;
       
  1771         }
       
  1772     }
       
  1773 
       
  1774     if (!VideoWindow20) {  /* create it */
       
  1775         int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED;
       
  1776         Uint32 flags20 = fullscreen_flags20;
       
  1777         if (flags12 & SDL12_OPENGL) { flags20 |= SDL_WINDOW_OPENGL; }
       
  1778         if (flags12 & SDL12_RESIZABLE) { flags20 |= SDL_WINDOW_RESIZABLE; }
       
  1779         if (flags12 & SDL12_NOFRAME) { flags20 |= SDL_WINDOW_BORDERLESS; }
       
  1780 
       
  1781         /* most platforms didn't check these environment variables, but the major
       
  1782            ones did (x11, windib, quartz), so we'll just offer it everywhere. */
       
  1783         GetEnvironmentWindowPosition(&x, &y);
       
  1784 
       
  1785         VideoWindow20 = SDL20_CreateWindow(WindowTitle, x, y, width, height, flags20);
       
  1786         if (!VideoWindow20) {
       
  1787             return EndVidModeCreate();
       
  1788         }
       
  1789     } else {  /* resize it */
       
  1790         SDL20_SetWindowSize(VideoWindow20, width, height);
       
  1791         SDL20_SetWindowFullscreen(VideoWindow20, fullscreen_flags20);
       
  1792         SDL20_SetWindowBordered(VideoWindow20, (flags12 & SDL12_NOFRAME) ? SDL_FALSE : SDL_TRUE);
       
  1793         SDL20_SetWindowResizable(VideoWindow20, (flags12 & SDL12_RESIZABLE) ? SDL_TRUE : SDL_FALSE);
       
  1794     }
       
  1795 
       
  1796     if (VideoSurface12) {
       
  1797         SDL20_free(VideoSurface12->pixels);
       
  1798     } else {
       
  1799         VideoSurface12 = CreateSurface12WithFormat(0, 0, appfmt);
       
  1800         if (!VideoSurface12) {
       
  1801             return EndVidModeCreate();
       
  1802         }
       
  1803     }
       
  1804 
       
  1805     VideoSurface12->surface20->flags |= SDL_PREALLOC;
       
  1806     VideoSurface12->flags |= SDL12_PREALLOC;
       
  1807     VideoSurface12->pixels = VideoSurface12->surface20->pixels = NULL;
       
  1808     VideoSurface12->w = VideoSurface12->surface20->w = width;
       
  1809     VideoSurface12->h = VideoSurface12->surface20->h = height;
       
  1810     VideoSurface12->pitch = VideoSurface12->surface20->pitch = width * SDL_BYTESPERPIXEL(appfmt);
       
  1811     SDL_SetClipRect(VideoSurface12, NULL);
       
  1812 
       
  1813     if (flags12 & SDL12_OPENGL) {
       
  1814         SDL_assert(!VideoTexture20);  /* either a new window or we destroyed all this */
       
  1815         SDL_assert(!VideoRenderer20);
       
  1816         VideoGLContext20 = SDL20_GL_CreateContext(VideoWindow20);
       
  1817         if (!VideoGLContext20) {
       
  1818             return EndVidModeCreate();
       
  1819         }
       
  1820 
       
  1821         VideoSurface12->flags |= SDL12_OPENGL;
       
  1822     } else {
       
  1823         /* always use a renderer for non-OpenGL windows. */
       
  1824         SDL_RendererInfo rinfo;
       
  1825         SDL_assert(!VideoGLContext20);  /* either a new window or we destroyed all this */
       
  1826         if (!VideoRenderer20) {
       
  1827             VideoRenderer20 = SDL20_CreateRenderer(VideoWindow20, -1, 0);
       
  1828             if (!VideoRenderer20) {
       
  1829                 return EndVidModeCreate();
       
  1830             }
       
  1831         }
       
  1832 
       
  1833         SDL20_RenderSetLogicalSize(VideoRenderer20, width, height);
       
  1834         SDL20_SetRenderDrawColor(VideoRenderer20, 0, 0, 0, 255);
       
  1835         SDL20_RenderClear(VideoRenderer20);
       
  1836         SDL20_RenderPresent(VideoRenderer20);
       
  1837         SDL20_SetRenderDrawColor(VideoRenderer20, 255, 255, 255, 255);
       
  1838 
       
  1839         if (SDL20_GetRendererInfo(VideoRenderer20, &rinfo) < 0) {
       
  1840             return EndVidModeCreate();
       
  1841         }
       
  1842 
       
  1843         if (VideoTexture20) {
       
  1844             SDL20_DestroyTexture(VideoTexture20);
       
  1845         }
       
  1846 
       
  1847         if (VideoConvertSurface20) {
       
  1848             SDL20_FreeSurface(VideoConvertSurface20);
       
  1849             VideoConvertSurface20 = NULL;
       
  1850         }
       
  1851 
       
  1852         VideoTexture20 = SDL20_CreateTexture(VideoRenderer20, rinfo.texture_formats[0], SDL_TEXTUREACCESS_STREAMING, width, height);
       
  1853         if (!VideoTexture20) {
       
  1854             return EndVidModeCreate();
       
  1855         }
       
  1856 
       
  1857         if (rinfo.texture_formats[0] != appfmt) {
       
  1858             /* need to convert between app's format and texture format */
       
  1859             VideoConvertSurface20 = CreateNullPixelSurface20(width, height, rinfo.texture_formats[0]);
       
  1860             if (!VideoConvertSurface20) {
       
  1861                 return EndVidModeCreate();
       
  1862             }
       
  1863         }
       
  1864 
       
  1865         VideoSurface12->flags &= ~SDL12_OPENGL;
       
  1866         VideoSurface12->surface20->pixels = SDL20_malloc(height * VideoSurface12->pitch);
       
  1867         VideoSurface12->pixels = VideoSurface12->surface20->pixels;
       
  1868         if (!VideoSurface12->pixels) {
       
  1869             SDL20_OutOfMemory();
       
  1870             return EndVidModeCreate();
       
  1871         }
       
  1872     }
       
  1873 
       
  1874     FIXME("setup screen saver");
       
  1875 
       
  1876     return VideoSurface12;
  1636 }
  1877 }
  1637 
  1878 
  1638 DECLSPEC SDL12_Surface * SDLCALL
  1879 DECLSPEC SDL12_Surface * SDLCALL
  1639 SDL_GetVideoSurface(void)
  1880 SDL_GetVideoSurface(void)
  1640 {
  1881 {
  1700     if (WindowIconTitle) {
  1941     if (WindowIconTitle) {
  1701         SDL20_free(WindowIconTitle);
  1942         SDL20_free(WindowIconTitle);
  1702     }
  1943     }
  1703     WindowTitle = title ? SDL_strdup(title) : NULL;
  1944     WindowTitle = title ? SDL_strdup(title) : NULL;
  1704     WindowIconTitle = icon ? SDL_strdup(icon) : NULL;
  1945     WindowIconTitle = icon ? SDL_strdup(icon) : NULL;
  1705     SDL20_SetWindowTitle(VideoWindow20, WindowTitle);
  1946     if (VideoWindow20) {
       
  1947         SDL20_SetWindowTitle(VideoWindow20, WindowTitle);
       
  1948     }
  1706 }
  1949 }
  1707 
  1950 
  1708 DECLSPEC void SDLCALL
  1951 DECLSPEC void SDLCALL
  1709 SDL_WM_GetCaption(const char **title, const char **icon)
  1952 SDL_WM_GetCaption(const char **title, const char **icon)
  1710 {
  1953 {