예제 #1
0
        public static Texture LoadFromImage <T>(Image <T> image) where T : struct, IPixel <T>
        {
            var stream = new MemoryStream();

            try
            {
                image.SaveAsPng(stream, new PngEncoder {
                    CompressionLevel = 1
                });

                var gdImage = new Godot.Image();
                var ret     = gdImage.LoadPngFromBuffer(stream.ToArray());
                if (ret != Godot.Error.Ok)
                {
                    throw new InvalidDataException(ret.ToString());
                }

                // Godot does not provide a way to load from memory directly so we turn it into a PNG I guess.
                var texture = new Godot.ImageTexture();
                texture.CreateFromImage(gdImage);
                return(new GodotTextureSource(texture));
            }
            finally
            {
                stream.Dispose();
            }
        }
예제 #2
0
        /// <summary>
        ///     Loads an image from a stream containing PNG data.
        /// </summary>
        /// <param name="stream">The stream to load the image from.</param>
        /// <param name="name">The "name" of this texture. This can be referred to later to aid debugging.</param>
        public static Texture LoadFromPNGStream(Stream stream, string name = null)
        {
            switch (GameController.Mode)
            {
            case GameController.DisplayMode.Headless:
                return(new BlankTexture());

            case GameController.DisplayMode.Godot:
                using (var memoryStream = new MemoryStream())
                {
                    stream.CopyTo(memoryStream);

                    var data    = memoryStream.ToArray();
                    var gdImage = new Godot.Image();
                    var ret     = gdImage.LoadPngFromBuffer(data);
                    if (ret != Godot.Error.Ok)
                    {
                        throw new InvalidDataException(ret.ToString());
                    }

                    var texture = new Godot.ImageTexture();
                    texture.CreateFromImage(gdImage);
                    return(new GodotTextureSource(texture));
                }

            case GameController.DisplayMode.OpenGL:
            {
                var manager = IoCManager.Resolve <IDisplayManagerOpenGL>();
                return(manager.LoadTextureFromPNGStream(stream, name));
            }

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
예제 #3
0
        /// <summary>
        ///     Loads a new texture an existing image.
        /// </summary>
        /// <param name="image">The image to load.</param>
        /// <param name="name">The "name" of this texture. This can be referred to later to aid debugging.</param>
        /// <param name="loadParameters">
        ///     Parameters that influence the loading of textures.
        ///     Defaults to <see cref="TextureLoadParameters.Default"/> if <c>null</c>.
        /// </param>
        /// <typeparam name="T">The type of pixels of the image. At the moment, images must be <see cref="Rgba32"/>.</typeparam>
        public static Texture LoadFromImage <T>(Image <T> image, string name         = null,
                                                TextureLoadParameters?loadParameters = null) where T : unmanaged, IPixel <T>
        {
            switch (GameController.Mode)
            {
            case GameController.DisplayMode.Headless:
                return(new DummyTexture());

            case GameController.DisplayMode.Godot:
            {
                var stream = new MemoryStream();

                try
                {
                    image.SaveAsPng(stream, new PngEncoder {
                            CompressionLevel = 1
                        });

                    var gdImage = new Godot.Image();
                    var ret     = gdImage.LoadPngFromBuffer(stream.ToArray());
                    if (ret != Godot.Error.Ok)
                    {
                        throw new InvalidDataException(ret.ToString());
                    }

                    // Godot does not provide a way to load from memory directly so we turn it into a PNG I guess.
                    var texture = new Godot.ImageTexture();
                    texture.CreateFromImage(gdImage);
                    (loadParameters ?? TextureLoadParameters.Default).SampleParameters.ApplyToGodotTexture(texture);
                    return(new GodotTextureSource(texture));
                }
                finally
                {
                    stream.Dispose();
                }
            }

            case GameController.DisplayMode.Clyde:
            {
                var manager = IoCManager.Resolve <IClyde>();
                return(manager.LoadTextureFromImage(image, name));
            }

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
예제 #4
0
        public static Texture LoadFromPNGStream(Stream stream)
        {
            using (var memoryStream = new MemoryStream())
            {
                stream.CopyTo(memoryStream);

                var data    = memoryStream.ToArray();
                var gdImage = new Godot.Image();
                var ret     = gdImage.LoadPngFromBuffer(data);
                if (ret != Godot.Error.Ok)
                {
                    throw new InvalidDataException(ret.ToString());
                }

                var texture = new Godot.ImageTexture();
                texture.CreateFromImage(gdImage);
                return(new GodotTextureSource(texture));
            }
        }
예제 #5
0
        private void _loadGodot(IResourceCache cache, ResourcePath path, TextureLoadParameters?parameters)
        {
            DebugTools.Assert(GameController.Mode == GameController.DisplayMode.Godot);

            using (var stream = cache.ContentFileRead(path))
            {
                var buffer = stream.ToArray();
                var image  = new Godot.Image();
                var error  = image.LoadPngFromBuffer(buffer);
                if (error != Godot.Error.Ok)
                {
                    throw new InvalidDataException($"Unable to load texture from buffer, reason: {error}");
                }
                godotTexture = new Godot.ImageTexture();
                godotTexture.CreateFromImage(image);
            }

            // Disable filter by default because pixel art.
            (parameters ?? TextureLoadParameters.Default).SampleParameters.ApplyToGodotTexture(godotTexture);
            Texture = new GodotTextureSource(godotTexture);
        }
예제 #6
0
        public void LoadRawTextureData(byte[] data)
        {
            Godot.Image image = new Godot.Image();
            Bitmap      bm;

            try
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
                {
                    bm = new Bitmap(ms);
                }

                byte[] bytes   = new byte[bm.Width * bm.Height * 4];
                int    bytePos = 0;

                for (int y = 0; y < bm.Height; y++)
                {
                    for (int x = 0; x < bm.Width; x++)
                    {
                        var color = bm.GetPixel(x, y);
                        bytes[bytePos]     = color.R;
                        bytes[bytePos + 1] = color.G;
                        bytes[bytePos + 2] = color.B;
                        bytes[bytePos + 3] = color.A;
                        bytePos           += 4;
                    }
                }

                image.CreateFromData(bm.Width, bm.Height, false, Godot.Image.Format.Rgba8, bytes);
                _texture.CreateFromImage(image);
            }
            catch (System.Exception e)
            {
                Debug.Log(e.Message + "\n\n" + e.StackTrace);
            }
        }