/// <summary> /// Creates a new instance of the <see cref="Surface2D"/> class. /// </summary> /// <param name="source">The <see cref="SurfaceSource"/> from which to create the surface.</param> /// <returns>The instance of <see cref="Surface2D"/> that was created.</returns> public static Surface2D Create(SurfaceSource source) { Contract.Require(source, nameof(source)); var uv = UltravioletContext.DemandCurrent(); return(uv.GetFactoryMethod <Surface2DFromSourceFactory>()(uv, source)); }
/// <summary> /// Creates a new instance of SDL_Surface from the image data contained in the specified bitmap. /// </summary> /// <param name="source">The surface source that contains the image data from which to create the surface.</param> /// <returns>The instance of SDL_Surface that was created.</returns> public static SDL_Surface CreateFromSurfaceSource(SurfaceSource source) { Contract.Require(source, "source"); var width = source.Width; var height = source.Height; var bmpSurface = new SDL_Surface(width, height); for (int y = 0; y < height; y++) { var pDst = (uint*)((byte*)bmpSurface.Native->pixels + (bmpSurface.Native->pitch * y)); for (int x = 0; x < width; x++) { *pDst++ = source[x, y].ToArgb(); } } return bmpSurface; }
/// <summary> /// Creates a new instance of the <see cref="Surface2D"/> class. /// </summary> /// <param name="source">The <see cref="SurfaceSource"/> from which to create the surface.</param> /// <returns>The instance of <see cref="Surface2D"/> that was created.</returns> public static Surface2D Create(SurfaceSource source) { Contract.Require(source, nameof(source)); var uv = UltravioletContext.DemandCurrent(); return uv.GetFactoryMethod<Surface2DFromSourceFactory>()(uv, source); }
/// <summary> /// Creates a new instance of SDL_Surface from the image data contained in the specified bitmap. /// </summary> /// <param name="source">The surface source that contains the image data from which to create the surface.</param> /// <returns>The instance of SDL_Surface that was created.</returns> public static SDL_Surface CreateFromSurfaceSource(SurfaceSource source) { Contract.Require(source, nameof(source)); var width = source.Width; var height = source.Height; var bmpSurface = new SDL_Surface(width, height); var pDstData = (byte*)bmpSurface.Native->pixels; var pSrcData = (byte*)source.Data; var dstExtraBytes = bmpSurface.Native->pitch - (bmpSurface.Native->w * 4); var srcExtraBytes = source.Stride - (source.Width * 4); byte srcR, srcG, srcB, srcA; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { srcR = *pSrcData++; srcG = *pSrcData++; srcB = *pSrcData++; srcA = *pSrcData++; if (source.DataFormat == SurfaceSourceDataFormat.BGRA) { var temp = srcR; srcR = srcB; srcB = temp; } *pDstData++ = srcB; *pDstData++ = srcG; *pDstData++ = srcR; *pDstData++ = srcA; } pDstData += dstExtraBytes; pSrcData += srcExtraBytes; } return bmpSurface; }
/// <summary> /// Initializes a new instance of the OpenGLSurface2D class. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="source">The surface source from which to create the surface.</param> public OpenGLSurface2D(UltravioletContext uv, SurfaceSource source) : this(uv, SDL_Surface.CreateFromSurfaceSource(source)) { }