sprite images implementation

This commit is contained in:
codeimp 2007-10-26 13:31:02 +00:00
parent eb92d78761
commit 9fe4910e3a
6 changed files with 88 additions and 36 deletions

View file

@ -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
}

View file

@ -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
}
}

View file

@ -95,7 +95,7 @@ namespace CodeImp.DoomBuilder.Data
#endregion
#region ================== Textures
#endregion
}
}

View file

@ -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();

View file

@ -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
}
}

View file

@ -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();