/// <summary>
        ///     Loads the specified resource
        /// </summary>
        /// <param name="name">Resource name</param>
        /// <returns>Stream for the resource</returns>
        public Stream getSubresourceByName(string name)
        {
            if (Path.GetExtension(name).ToUpper().Equals(".TGA"))
            {
                if (string.IsNullOrEmpty(this.BaseDirectory))
                {
                    return(TargaSolver.LoadTargaImage(name));
                }
                return(TargaSolver.LoadTargaImage(Path.Combine(this.BaseDirectory, name)));
            }
            else if (string.IsNullOrEmpty(this.BaseDirectory))
            {
                return(File.OpenRead(name));
            }
            else
            {
                string path = Path.Combine(this.BaseDirectory, name);
                if (File.Exists(path))
                {
                    return(File.OpenRead(path));
                }
                else
                {
                    return(null);

                    Debug.WriteLine(string.Format("\"{0}\"は見つかりませんでした。", path));
                }
            }
        }
Beispiel #2
0
        static public ShaderResourceView FromFile(Device device, string path, out Texture2D texture)
        {
            var srv = (ShaderResourceView)null;

            texture = null;

            try
            {
                using (var image = new System.Drawing.Bitmap(path))
                {
                    var imageRect = new System.Drawing.Rectangle(0, 0, image.Width, image.Height);
                    using (var bitmap = image.Clone(imageRect, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                    {
                        var locks       = bitmap.LockBits(imageRect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
                        var dataBox     = new[] { new SharpDX.DataBox(locks.Scan0, bitmap.Width * 4, bitmap.Height) };
                        var textureDesc = new Texture2DDescription()
                        {
                            ArraySize         = 1,
                            BindFlags         = BindFlags.ShaderResource,
                            CpuAccessFlags    = CpuAccessFlags.None,
                            Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                            Height            = bitmap.Height,
                            Width             = bitmap.Width,
                            MipLevels         = 1,
                            OptionFlags       = ResourceOptionFlags.None,
                            SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                            Usage             = ResourceUsage.Default
                        };

                        texture = new Texture2D(device, textureDesc, dataBox);
                        bitmap.UnlockBits(locks);
                        srv = new ShaderResourceView(device, texture);
                    }
                }
            }
            catch
            {
                // 失敗したら Targa かもしれないのでそちらでリトライ。
                srv = FromStream(device, TargaSolver.LoadTargaImage(path), out texture);
            }

            return(srv);
        }