UltimateZoneBuilder/Source/Data/TextureImage.cs

161 lines
4.2 KiB
C#
Raw Normal View History

2007-10-01 20:57:41 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
2007-10-08 14:29:31 +00:00
using System.Drawing;
using System.Drawing.Imaging;
using CodeImp.DoomBuilder.Rendering;
using CodeImp.DoomBuilder.IO;
using System.IO;
2007-10-01 20:57:41 +00:00
#endregion
namespace CodeImp.DoomBuilder.Data
{
2008-01-02 21:49:43 +00:00
internal sealed unsafe class TextureImage : ImageData
2007-10-01 20:57:41 +00:00
{
#region ================== Variables
private List<TexturePatch> patches;
2007-10-07 22:21:47 +00:00
private float scalex;
private float scaley;
2007-10-01 20:57:41 +00:00
#endregion
#region ================== Constructor / Disposer
// Constructor
2007-10-07 22:21:47 +00:00
public TextureImage(string name, int width, int height, float scalex, float scaley)
2007-10-01 20:57:41 +00:00
{
// Initialize
2007-10-07 22:21:47 +00:00
this.width = width;
this.height = height;
this.scalex = scalex;
this.scaley = scaley;
2007-10-08 14:29:31 +00:00
this.scaledwidth = (float)width * scalex;
this.scaledheight = (float)height * scaley;
this.patches = new List<TexturePatch>();
SetName(name);
2007-10-01 20:57:41 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
#endregion
#region ================== Methods
// This adds a patch to the texture
public void AddPatch(TexturePatch patch)
{
// Add it
patches.Add(patch);
}
2007-10-01 20:57:41 +00:00
// This loads the image
public override void LoadImage()
{
2007-10-08 14:29:31 +00:00
uint datalength = (uint)(width * height * sizeof(PixelColor));
IImageReader reader;
2007-10-08 14:29:31 +00:00
BitmapData bitmapdata;
MemoryStream mem;
2007-10-08 14:29:31 +00:00
PixelColor* pixels;
Stream patchdata;
byte[] membytes;
bool failed = false;
2007-10-08 14:29:31 +00:00
// Checks
2007-10-01 20:57:41 +00:00
if(this.IsLoaded) return;
if((width == 0) || (height == 0)) return;
lock(this)
2007-10-08 14:29:31 +00:00
{
// Create texture bitmap
try
{
bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
bitmapdata = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
pixels = (PixelColor*)bitmapdata.Scan0.ToPointer();
General.ZeroMemory(new IntPtr(pixels), width * height * sizeof(PixelColor));
}
catch(Exception e)
{
// Unable to make bitmap
General.WriteLogLine("ERROR: Unable to load texture image '" + this.Name + "'. " + e.GetType().Name + ": " + e.Message);
return;
}
// Go for all patches
foreach(TexturePatch p in patches)
2007-10-08 14:29:31 +00:00
{
// Get the patch data stream
patchdata = General.Map.Data.GetPatchData(p.lumpname);
if(patchdata != null)
{
// Copy patch data to memory
patchdata.Seek(0, SeekOrigin.Begin);
membytes = new byte[(int)patchdata.Length];
patchdata.Read(membytes, 0, (int)patchdata.Length);
mem = new MemoryStream(membytes);
mem.Seek(0, SeekOrigin.Begin);
2007-10-26 18:04:54 +00:00
// 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: Patch lump '" + p.lumpname + "' data format could not be read, while loading texture '" + this.Name + "'!");
failed = true;
}
2007-10-27 13:59:24 +00:00
else
{
// Draw the patch
mem.Seek(0, SeekOrigin.Begin);
reader.DrawToPixelData(mem, pixels, width, height, p.x, p.y);
}
}
else
{
// Missing a patch lump!
General.WriteLogLine("WARNING: Missing patch lump '" + p.lumpname + "' while loading texture '" + this.Name + "'!");
}
}
// Done
bitmap.UnlockBits(bitmapdata);
// When failed, use the error picture
2007-10-27 13:59:24 +00:00
if(failed)
{
bitmap.Dispose();
bitmap = UnknownImageReader.ReadAsBitmap();
}
// Pass on to base
base.LoadImage();
2007-10-08 14:29:31 +00:00
}
2007-10-01 20:57:41 +00:00
}
#endregion
}
}