/// <summary> /// Creates a copy of the surface. /// </summary> /// <returns>A new surface that is a copy of this surface.</returns> public SDL_Surface CreateCopy() { Contract.EnsureNotDisposed(this, disposed); var copy = new SDL_Surface(Width, Height); if (SDL.BlitSurface(ptr, null, copy.ptr, null) < 0) throw new SDL2Exception(); }
/// <summary> /// Initializes a new instance of the OpenGLSurface2D class. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="nativesurf">The native SDL surface that this object represents.</param> public OpenGLSurface2D(UltravioletContext uv, SDL_Surface nativesurf) : base(uv) { if (nativesurf == null) throw new ArgumentNullException("nativesurf"); this.nativesurf = nativesurf; }
/// <summary> /// Initializes a new instance of the OpenGLSurface2D class. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="width">The width of the surface in pixels.</param> /// <param name="height">The height of the surface in pixels.</param> public OpenGLSurface2D(UltravioletContext uv, Int32 width, Int32 height) : base(uv) { Contract.EnsureRange(width > 0, nameof(width)); Contract.EnsureRange(height > 0, nameof(height)); this.nativesurf = new SDL_Surface(width, height); }
/// <summary> /// Identifies the positions of the glyphs on the specified surface. /// </summary> /// <param name="surface">The surface on which to identify glyphs.</param> /// <param name="source">The region on the surface in which to look for glyphs, or null to examine the entire surface.</param> /// <returns>A collection of rectangles describing the positions of the glyphs on the specified surface.</returns> public static IEnumerable<Rectangle> IdentifyGlyphs(SDL_Surface surface, Rectangle? source = null) { if (source.HasValue && (source.Value.Width > surface.Width | source.Value.Height > surface.Height)) throw new ArgumentOutOfRangeException("source"); var regionValue = source ?? new Rectangle(0, 0, surface.Width, surface.Height); var data = new Color[regionValue.Width * regionValue.Height]; surface.GetData(data, regionValue); // Find the glyphs on the texture. var positions = new List<Rectangle>(); var nextLine = 0; var nextGlyph = 0; for (int y = 0; y < regionValue.Height; y = nextLine) { nextLine = y + 1; for (int x = 0; x <regionValue.Width; x = nextGlyph) { nextGlyph = x + 1; // Skip buffer pixels. var pixel = data[y * regionValue.Width + x]; if (pixel.Equals(Color.Magenta)) continue; // Find the width of the glyph. var x2 = x + 1; while (x2 < regionValue.Width) { if (data[y * regionValue.Width + x2++].Equals(Color.Magenta)) break; } var glyphWidth = (x2 - x) - 1; // Find the height of the glyph. var y2 = y + 1; while (y2 < regionValue.Height) { if (data[y2++ * regionValue.Width + x].Equals(Color.Magenta)) break; } var glyphHeight = (y2 - y) - 1; // Calculate the position of the next glyph and the next line of glyphs. nextGlyph = x + glyphWidth + 1; if (y + glyphHeight > nextLine) { nextLine = y + glyphHeight + 1; } // Store the glyph's position. positions.Add(new Rectangle(regionValue.X + x, regionValue.Y + y, glyphWidth, glyphHeight)); } } return positions; }
/// <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> /// 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 copy of the surface. /// </summary> /// <returns>A new surface that is a copy of this surface.</returns> public SDL_Surface CreateCopy() { Contract.EnsureNotDisposed(this, disposed); var copy = new SDL_Surface(Width, Height); if (SDL.BlitSurface(ptr, null, copy.ptr, null) < 0) throw new SDL2Exception(); return copy; }
/// <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> /// Creates a texture from the surface. /// </summary> /// <param name="premultiplyAlpha">A value indicating whether to premultiply the surface's alpha when creating the texture.</param> /// <returns>The texture that was created from the surface.</returns> public override Texture2D CreateTexture(Boolean premultiplyAlpha) { Contract.EnsureNotDisposed(this, Disposed); using (var copysurf = new SDL_Surface(Width, Height)) { if (SDL.SetSurfaceBlendMode(nativesurf.Native, SDL_BlendMode.NONE) < 0) throw new SDL2Exception(); if (SDL.BlitSurface(nativesurf.Native, null, copysurf.Native, null) < 0) throw new SDL2Exception(); copysurf.PrepareForTextureExport(premultiplyAlpha); return new OpenGLTexture2D(Ultraviolet, copysurf); } }
/// <summary> /// Creates a copy of a region of this surface. /// </summary> /// <param name="region">The region of this surface to copy.</param> /// <returns>A new surface which is a copy of the specified region of this surface.</returns> public override Surface2D CreateSurface(Rectangle region) { Contract.EnsureNotDisposed(this, Disposed); if (region.Left < 0 || region.Top < 0 || region.Right > Width || region.Bottom > Height || region.Width <= 0 || region.Height <= 0) throw new ArgumentOutOfRangeException("region"); var copysurf = new SDL_Surface(region.Width, region.Height); var srcrect = new SDL_Rect() { x = region.X, y = region.Y, w = region.Width, h = region.Height }; var dstrect = new SDL_Rect() { x = 0, y = 0, w = region.Width, h = region.Height }; if (SDL.BlitSurface(nativesurf.Native, &srcrect, copysurf.Native, &dstrect) < 0) throw new SDL2Exception(); return new OpenGLSurface2D(Ultraviolet, copysurf); }