/// <summary>
        /// Blits the surface onto the specified destination surface.
        /// </summary>
        /// <param name="src">The source surface.</param>
        /// <param name="srcRect">The area of this surface that will be copied to the destination surface.</param>
        /// <param name="dst">The destination surface.</param>
        /// <param name="dstRect">The area on the destination surface to which this surface will be copied.</param>
        private static void BlitInternal(OpenGLSurface2D src, Rectangle srcRect, OpenGLSurface2D dst, Rectangle dstRect)
        {
            var sdlSrcRect = new SDL_Rect()
            {
                x = srcRect.X, y = srcRect.Y, w = srcRect.Width, h = srcRect.Height
            };
            var sdlDstRect = new SDL_Rect()
            {
                x = dstRect.X, y = dstRect.Y, w = dstRect.Width, h = dstRect.Height
            };

            if (SDL.SetSurfaceBlendMode(src.nativesurf.Native, SDL_BlendMode.NONE) < 0)
            {
                throw new SDL2Exception();
            }

            if (srcRect.Width != dstRect.Width || srcRect.Height != dstRect.Height)
            {
                if (SDL.BlitScaled(src.nativesurf.Native, &sdlSrcRect, dst.nativesurf.Native, &sdlDstRect) < 0)
                {
                    throw new SDL2Exception();
                }
            }
            else
            {
                if (SDL.BlitSurface(src.nativesurf.Native, &sdlSrcRect, dst.nativesurf.Native, &sdlDstRect) < 0)
                {
                    throw new SDL2Exception();
                }
            }
        }
        /// <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));
        }
        /// <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));
            }
        }
Exemple #4
0
        private unsafe void Create(IntPtr sdlsurface)
        {
            Sdl.Surface *surface = (Sdl.Surface *)sdlsurface;
            uint         width   = NextPowerOfTwo((uint)surface->w);
            uint         height  = NextPowerOfTwo((uint)surface->h);

            IntPtr pixelbufferp;

            if (BitConverter.IsLittleEndian)
            {
                pixelbufferp = SDL.CreateRGBSurface(SDL.SWSURFACE,
                                                    (int)width, (int)height, 32,
                                                    0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
            }
            else
            {
                pixelbufferp = SDL.CreateRGBSurface(SDL.SWSURFACE,
                                                    (int)width, (int)height, 32,
                                                    0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
            }
            if (pixelbufferp == IntPtr.Zero)
            {
                throw new Exception("Couldn't create surface texture (out of memory?)");
            }

            try {
                SDL.SetAlpha(sdlsurface, 0, 0);
                SDL.BlitSurface(sdlsurface, IntPtr.Zero, pixelbufferp, IntPtr.Zero);

                CreateFromSurface(pixelbufferp, gl.RGBA);
            } finally {
                SDL.FreeSurface(pixelbufferp);
            }

            ImageWidth  = (float)surface->w;
            ImageHeight = (float)surface->h;
        }