/// <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(); } }
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(); } }
/// <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(); } }
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)); } }
public override void Load(IResourceCache cache, string diskPath) { if (!File.Exists(diskPath)) { throw new FileNotFoundException(diskPath); } godotTexture = new Godot.ImageTexture(); godotTexture.Load(diskPath); // If it fails to load it won't change the texture dimensions, so they'll still be at zero. if (godotTexture.GetWidth() == 0) { throw new InvalidDataException(); } // Disable filter by default because pixel art. godotTexture.SetFlags(godotTexture.GetFlags() & ~(int)Godot.Texture.FlagsEnum.Filter); Texture = new GodotTextureSource(godotTexture); // Primarily for tracking down iCCP sRGB errors in the image files. Logger.Debug($"Loaded texture {Path.GetFullPath(diskPath)}."); }
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); }
public override void Load(IResourceCache cache, ResourcePath path) { if (!cache.ContentFileExists(path)) { throw new FileNotFoundException("Content file does not exist for texture"); } if (!cache.TryGetDiskFilePath(path, out string diskPath)) { throw new InvalidOperationException("Textures can only be loaded from disk."); } godotTexture = new Godot.ImageTexture(); godotTexture.Load(diskPath); // If it fails to load it won't change the texture dimensions, so they'll still be at zero. if (godotTexture.GetWidth() == 0) { throw new InvalidDataException(); } // Disable filter by default because pixel art. godotTexture.SetFlags(godotTexture.GetFlags() & ~(int)Godot.Texture.FlagsEnum.Filter); Texture = new GodotTextureSource(godotTexture); // Primarily for tracking down iCCP sRGB errors in the image files. Logger.Debug($"Loaded texture {Path.GetFullPath(diskPath)}."); }
public override void Dispose() { godotTexture.Dispose(); godotTexture = null; }
private Texture2D(Godot.ImageTexture texture) { _texture = texture; }
internal Texture2D(byte[] data) { _texture = new Godot.ImageTexture(); LoadRawTextureData(data); }