Example #1
0
        /// <inheritdoc/>
        public override Texture3D ImportPreprocessed(ContentManager manager, IContentProcessorMetadata metadata, BinaryReader reader)
        {
            var caps = manager.Ultraviolet.GetGraphics().Capabilities;

            var version = 0u;
            var depth   = reader.ReadInt32();

            if (depth == Int32.MaxValue)
            {
                version = reader.ReadUInt32();
            }

            if (version > 0u)
            {
                depth = reader.ReadInt32();
            }

            var srgbEncoded = false;

            if (version > 0u)
            {
                srgbEncoded = reader.ReadBoolean() && caps.SrgbEncodingEnabled;
            }

            var layerSurfaces = new List <SurfaceSource>();
            var layerPointers = new List <IntPtr>();

            try
            {
                for (int i = 0; i < depth; i++)
                {
                    var length = reader.ReadInt32();
                    var bytes  = reader.ReadBytes(length);

                    using (var stream = new MemoryStream(bytes))
                    {
                        var surfaceSource = SurfaceSource.Create(stream);
                        layerSurfaces.Add(surfaceSource);
                        layerPointers.Add(surfaceSource.Data);
                    }
                }

                var layerWidth  = layerSurfaces[0].Width;
                var layerHeight = layerSurfaces[0].Height;

                var internalformat = OpenGLTextureUtil.GetInternalFormatFromBytesPerPixel(4, srgbEncoded);
                var format         = (layerSurfaces[0].DataFormat == SurfaceSourceDataFormat.RGBA) ? gl.GL_RGBA : gl.GL_BGRA;

                return(new OpenGLTexture3D(manager.Ultraviolet, internalformat, layerWidth, layerHeight, format,
                                           gl.GL_UNSIGNED_BYTE, layerPointers, true));
            }
            finally
            {
                foreach (var layerSurface in layerSurfaces)
                {
                    layerSurface.Dispose();
                }
            }
        }
        /// <summary>
        /// Imports the data from the specified file.
        /// </summary>
        /// <param name="metadata">The asset metadata for the asset to import.</param>
        /// <param name="stream">The stream that contains the data to import.</param>
        /// <returns>The data structure that was imported from the file.</returns>
        public override PlatformNativeSurface Import(IContentImporterMetadata metadata, Stream stream)
        {
            var data = new Byte[stream.Length];

            stream.Read(data, 0, data.Length);

            using (var mstream = new MemoryStream(data))
                using (var source = SurfaceSource.Create(mstream))
                {
                    return(new SDL2PlatformNativeSurface(source));
                }
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SDL2PlatformNativeSurface"/> class.
        /// </summary>
        /// <param name="source">The <see cref="SurfaceSource"/> from which to create the surface.</param>
        public SDL2PlatformNativeSurface(SurfaceSource source)
        {
            Contract.Require(source, nameof(source));

            var width  = source.Width;
            var height = source.Height;

            if ((this.ptr = SDL_CreateRGBSurface(0, width, height, 32, rmask, gmask, bmask, amask)) == null)
            {
                throw new SDL2Exception();
            }

            if (SDL_SetSurfaceBlendMode(this.ptr, SDL_BLENDMODE_NONE) < 0)
            {
                throw new SDL2Exception();
            }

            var pDstData = (byte *)ptr->pixels;
            var pSrcData = (byte *)source.Data;

            var dstExtraBytes = ptr->pitch - (ptr->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++)
                {
                    srcB = *pSrcData++;
                    srcG = *pSrcData++;
                    srcR = *pSrcData++;
                    srcA = *pSrcData++;

                    if (source.DataFormat == SurfaceSourceDataFormat.RGBA)
                    {
                        var temp = srcR;
                        srcR = srcB;
                        srcB = temp;
                    }

                    *pDstData++ = srcR;
                    *pDstData++ = srcG;
                    *pDstData++ = srcB;
                    *pDstData++ = srcA;
                }

                pDstData += dstExtraBytes;
                pSrcData += srcExtraBytes;
            }
        }
Example #4
0
        /// <summary>
        /// Creates a new instance of SDL_Surface from the image data contained in the specified stream.
        /// </summary>
        /// <param name="stream">The stream 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 CreateFromStream(Stream stream)
        {
            Contract.Require(stream, nameof(stream));

            var data = new Byte[stream.Length];

            stream.Read(data, 0, data.Length);

            using (var mstream = new MemoryStream(data))
            {
                using (var src = SurfaceSource.Create(mstream))
                {
                    return(CreateFromSurfaceSource(src));
                }
            }
        }
Example #5
0
        /// <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);
        }
Example #6
0
        /// <inheritdoc/>
        public override Texture2D ImportPreprocessed(ContentManager manager, IContentProcessorMetadata metadata, BinaryReader reader)
        {
            var length = reader.ReadInt32();
            var bytes  = reader.ReadBytes(length);

            using (var stream = new MemoryStream(bytes))
            {
                using (var source = SurfaceSource.Create(stream))
                {
                    using (var imgSurface = new SDL_Surface(source.Width, source.Height))
                    {
                        var imgTexture = new OpenGLTexture2D(manager.Ultraviolet, gl.IsGLES2 ? gl.GL_RGBA : gl.GL_RGBA8,
                                                             source.Width, source.Height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, source.Data, true);

                        return(imgTexture);
                    }
                }
            }
        }
        /// <inheritdoc/>
        public override Texture2D ImportPreprocessed(ContentManager manager, IContentProcessorMetadata metadata, BinaryReader reader)
        {
            var caps = manager.Ultraviolet.GetGraphics().Capabilities;

            var version = 0u;
            var length  = reader.ReadInt32();

            if (length == Int32.MaxValue)
            {
                version = reader.ReadUInt32();
            }

            if (version > 0u)
            {
                length = reader.ReadInt32();
            }

            var srgbEncoded = false;

            if (version > 0u)
            {
                srgbEncoded = reader.ReadBoolean() && caps.SrgbEncodingEnabled;
            }

            var bytes = reader.ReadBytes(length);

            using (var stream = new MemoryStream(bytes))
            {
                using (var source = SurfaceSource.Create(stream))
                {
                    var format         = (source.DataFormat == SurfaceSourceDataFormat.RGBA) ? gl.GL_RGBA : gl.GL_BGRA;
                    var internalformat = OpenGLTextureUtil.GetInternalFormatFromBytesPerPixel(4, srgbEncoded);

                    return(new OpenGLTexture2D(manager.Ultraviolet, internalformat,
                                               source.Width, source.Height, format, gl.GL_UNSIGNED_BYTE, source.Data, true));
                }
            }
        }
        /// <inheritdoc/>
        public override Surface2D LoadIcon()
        {
            var asmEntry  = Assembly.GetEntryAssembly();
            var asmLoader = typeof(NETCore3IconLoader).Assembly;

            var asmResourceNames  = asmEntry.GetManifestResourceNames();
            var asmResourcePrefix = GetLongestCommonResourcePrefix(asmResourceNames);
            var asmResourceIcon   = String.IsNullOrEmpty(asmResourcePrefix) && asmResourceNames.Length == 1 && asmResourceNames[0].EndsWith(".icon.ico") ?
                                    asmResourceNames[0] : $"{asmResourcePrefix}.icon.ico";

            var iconStream =
                asmEntry.GetManifestResourceStream(asmResourceIcon) ??
                asmLoader.GetManifestResourceStream($"Ultraviolet.Shims.NETCore3.icon.ico");

            if (iconStream != null)
            {
                using (var source = SurfaceSource.Create(iconStream))
                {
                    return(Surface2D.Create(source, SurfaceOptions.SrgbColor));
                }
            }

            return(null);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SDL2Surface2D"/> class.
 /// </summary>
 /// <param name="uv">The Ultraviolet context.</param>
 /// <param name="source">The surface source from which to create the surface.</param>
 /// <param name="options">The surface's configuration options.</param>
 public SDL2Surface2D(UltravioletContext uv, SurfaceSource source, SurfaceOptions options)
     : this(uv, new SDL2PlatformNativeSurface(source), options)
 {
 }
Example #10
0
 /// <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))
 {
 }