diff --git a/Source/Data/DataManager.cs b/Source/Data/DataManager.cs index 47f6dceb..2505486a 100644 --- a/Source/Data/DataManager.cs +++ b/Source/Data/DataManager.cs @@ -449,27 +449,46 @@ namespace CodeImp.DoomBuilder.Data #region ================== Sprites - // This returns an image by string + // This returns an image by long public ImageData GetSpriteImage(string name) { - // Get the long name + Stream spritedata = null; long longname = Lump.MakeLongName(name); - return GetSpriteImage(longname); - } + SpriteImage image; - // This returns an image by long - public ImageData GetSpriteImage(long longname) - { - // Does this sprite exist? + // Sprite already loaded? if(sprites.ContainsKey(longname)) { - // Return sprite + // Return exiting sprite return sprites[longname]; } else { - // Return null image - return new NullImage(); + // Go for all opened containers + for(int i = containers.Count - 1; i >= 0; i--) + { + // This contain provides this sprite? + spritedata = containers[i].GetSpriteData(name); + if(spritedata != null) break; + } + + // Found anything? + if(spritedata != null) + { + // Make new sprite image + image = new SpriteImage(name); + + // Add to collection + sprites.Add(longname, image); + + // Return result + return image; + } + else + { + // Return null image + return new NullImage(); + } } } @@ -481,14 +500,6 @@ namespace CodeImp.DoomBuilder.Data return img.Bitmap; } - // This returns a bitmap by string - public Bitmap GetSpriteBitmap(long longname) - { - ImageData img = GetSpriteImage(longname); - img.LoadImage(); - return img.Bitmap; - } - // This returns a texture by string public Texture GetSpriteTexture(string name) { @@ -497,15 +508,6 @@ namespace CodeImp.DoomBuilder.Data img.CreateTexture(); return img.Texture; } - - // This returns a texture by string - public Texture GetSpriteTexture(long longname) - { - ImageData img = GetSpriteImage(longname); - img.LoadImage(); - img.CreateTexture(); - return img.Texture; - } #endregion } diff --git a/Source/Data/DataReader.cs b/Source/Data/DataReader.cs index 5c5c135b..2db3ac71 100644 --- a/Source/Data/DataReader.cs +++ b/Source/Data/DataReader.cs @@ -116,5 +116,12 @@ namespace CodeImp.DoomBuilder.Data public virtual Stream GetFlatData(string pname) { return null; } #endregion + + #region ================== Sprites + + // When implemented, this returns the sprite lump + public virtual Stream GetSpriteData(string pname) { return null; } + + #endregion } } diff --git a/Source/Data/DirectoryReader.cs b/Source/Data/DirectoryReader.cs index 992a75f5..2d0ef61d 100644 --- a/Source/Data/DirectoryReader.cs +++ b/Source/Data/DirectoryReader.cs @@ -95,7 +95,7 @@ namespace CodeImp.DoomBuilder.Data #endregion #region ================== Textures - + #endregion } } diff --git a/Source/Data/SpriteImage.cs b/Source/Data/SpriteImage.cs index 3ed27af3..de6187f6 100644 --- a/Source/Data/SpriteImage.cs +++ b/Source/Data/SpriteImage.cs @@ -3,6 +3,8 @@ using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Text; +using System.IO; +using CodeImp.DoomBuilder.IO; namespace CodeImp.DoomBuilder.Data { @@ -14,9 +16,6 @@ namespace CodeImp.DoomBuilder.Data #region ================== Variables - private DataReader source; - private string lumpname; - #endregion #region ================== Properties @@ -26,11 +25,9 @@ namespace CodeImp.DoomBuilder.Data #region ================== Constructor / Disposer // Constructor - public SpriteImage(string name, DataReader source, string lumpname) + public SpriteImage(string name) { // Initialize - this.source = source; - this.lumpname = lumpname; SetName(name); // We have no destructor @@ -57,10 +54,42 @@ namespace CodeImp.DoomBuilder.Data // This loads the image public override void LoadImage() { + Stream lumpdata; + MemoryStream mem; + IImageReader reader; + byte[] membytes; + // Leave when already loaded if(this.IsLoaded) return; + // Get the lump data stream + lumpdata = General.Map.Data.GetPatchData(Name); + if(lumpdata != null) + { + // Copy lump data to memory + lumpdata.Seek(0, SeekOrigin.Begin); + membytes = new byte[(int)lumpdata.Length]; + lumpdata.Read(membytes, 0, (int)lumpdata.Length); + mem = new MemoryStream(membytes); + mem.Seek(0, SeekOrigin.Begin); + // Get a reader for the data + reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMPICTURE, General.Map.Data.Palette); + if(reader is UnknownImageReader) + { + // Data is in an unknown format! + General.WriteLogLine("WARNING: Sprite lump '" + Name + "' data format could not be read!"); + } + + // Read data as bitmap + mem.Seek(0, SeekOrigin.Begin); + bitmap = reader.ReadAsBitmap(mem); + } + else + { + // Missing a patch lump! + General.WriteLogLine("WARNING: Missing sprite lump '" + Name + "'!"); + } // Pass on to base base.LoadImage(); diff --git a/Source/Data/WADReader.cs b/Source/Data/WADReader.cs index a3ef7f0f..f587a8fa 100644 --- a/Source/Data/WADReader.cs +++ b/Source/Data/WADReader.cs @@ -383,5 +383,19 @@ namespace CodeImp.DoomBuilder.Data } #endregion + + #region ================== Sprite + + // This finds and returns a sprite stream + public override Stream GetSpriteData(string pname) + { + Lump lump; + + // Find the lump + lump = file.FindLump(pname); + if(lump != null) return lump.Stream; else return null; + } + + #endregion } } diff --git a/Source/Interface/ThingInfoPanel.cs b/Source/Interface/ThingInfoPanel.cs index 672b2a23..bae2a729 100644 --- a/Source/Interface/ThingInfoPanel.cs +++ b/Source/Interface/ThingInfoPanel.cs @@ -82,7 +82,7 @@ namespace CodeImp.DoomBuilder.Interface tag.Text = ""; // TODO angle.Text = t.AngleDeg.ToString() + "\u00B0"; spritename.Text = ti.Sprite; - spritetex.BackgroundImage = General.Map.Data.GetSpriteBitmap(ti.SpriteLongName); + spritetex.BackgroundImage = General.Map.Data.GetSpriteBitmap(ti.Sprite); // Show the whole thing this.Show();