UltimateZoneBuilder/Source/Data/ImageData.cs

180 lines
4.5 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;
using System.Drawing;
using SlimDX.Direct3D9;
using System.Drawing.Imaging;
using CodeImp.DoomBuilder.Rendering;
2007-10-07 22:21:47 +00:00
using CodeImp.DoomBuilder.IO;
2007-10-21 04:07:36 +00:00
using System.IO;
2007-11-04 22:19:30 +00:00
using System.Windows.Forms;
2007-10-01 20:57:41 +00:00
#endregion
namespace CodeImp.DoomBuilder.Data
{
2007-10-24 17:25:03 +00:00
public abstract unsafe class ImageData : IDisposable
2007-10-01 20:57:41 +00:00
{
#region ================== Constants
#endregion
#region ================== Variables
2007-10-01 20:57:41 +00:00
// Properties
private string name;
private long longname;
2007-10-08 14:29:31 +00:00
protected int width;
protected int height;
protected float scaledwidth;
protected float scaledheight;
2007-10-07 22:21:47 +00:00
2007-10-01 20:57:41 +00:00
// GDI bitmap
protected Bitmap bitmap;
// 2D rendering data
2007-10-27 13:59:24 +00:00
private PixelColorBlock pixeldata;
2007-10-01 20:57:41 +00:00
// Direct3D texture
private Texture texture;
2007-10-01 20:57:41 +00:00
// Disposing
protected bool isdisposed = false;
#endregion
#region ================== Properties
public string Name { get { return name; } }
2007-10-07 22:21:47 +00:00
public long LongName { get { return longname; } }
2007-10-27 13:59:24 +00:00
public PixelColorBlock PixelData { get { lock(this) { return pixeldata; } } }
2007-11-04 22:19:30 +00:00
public Bitmap Bitmap { get { lock(this) { if(bitmap != null) return bitmap; else return CodeImp.DoomBuilder.Properties.Resources.Hourglass; } } }
2007-10-26 18:04:54 +00:00
public Texture Texture { get { lock(this) { return texture; } } }
2007-10-08 14:29:31 +00:00
public bool IsLoaded { get { return (bitmap != null); } }
2007-10-01 20:57:41 +00:00
public bool IsDisposed { get { return isdisposed; } }
2007-10-08 14:29:31 +00:00
public int Width { get { return width; } }
public int Height { get { return height; } }
public float ScaledWidth { get { return scaledwidth; } }
public float ScaledHeight { get { return scaledheight; } }
2007-10-01 20:57:41 +00:00
#endregion
#region ================== Constructor / Disposer
// Constructor
public ImageData()
{
// We have no destructor
GC.SuppressFinalize(this);
}
// Disposer
2007-10-01 20:57:41 +00:00
public virtual void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
lock(this)
2007-10-09 05:34:05 +00:00
{
// Clean up
if(bitmap != null) bitmap.Dispose();
if(texture != null) texture.Dispose();
2007-10-27 13:59:24 +00:00
pixeldata = null;
// Done
isdisposed = true;
2007-10-09 05:34:05 +00:00
}
2007-10-01 20:57:41 +00:00
}
}
#endregion
#region ================== Management
2007-11-10 19:24:52 +00:00
// This sets the name
protected void SetName(string name)
{
this.name = name;
2007-10-07 22:21:47 +00:00
this.longname = Lump.MakeLongName(name);
}
// This requests loading the image
2007-10-01 20:57:41 +00:00
public virtual void LoadImage()
2007-10-09 05:34:05 +00:00
{
}
// This creates the 2D pixel data
2007-10-24 17:25:03 +00:00
public virtual void CreatePixelData()
2007-10-01 20:57:41 +00:00
{
BitmapData bmpdata;
2007-10-09 05:34:05 +00:00
2007-10-26 18:04:54 +00:00
lock(this)
2007-10-01 20:57:41 +00:00
{
2007-10-26 18:04:54 +00:00
// Only do this when data is not created yet
if((pixeldata == null) && IsLoaded)
{
// Make a data copy of the bits for the 2D renderer
bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
2007-10-27 13:59:24 +00:00
pixeldata = new PixelColorBlock(bitmap.Size.Width, bitmap.Size.Height);
General.CopyMemory((void*)pixeldata.Pointer, bmpdata.Scan0.ToPointer(), new UIntPtr(pixeldata.Length));
bitmap.UnlockBits(bmpdata);
}
2007-10-01 20:57:41 +00:00
}
}
// This creates the Direct3D texture
2007-10-24 17:25:03 +00:00
public virtual void CreateTexture()
2007-10-01 20:57:41 +00:00
{
2007-10-21 04:07:36 +00:00
MemoryStream memstream;
2007-10-26 18:04:54 +00:00
lock(this)
2007-10-21 04:07:36 +00:00
{
2007-10-26 18:04:54 +00:00
// Only do this when texture is not created yet
if((texture == null) && IsLoaded)
{
// Write to memory stream and read from memory
memstream = new MemoryStream();
bitmap.Save(memstream, ImageFormat.Bmp);
memstream.Seek(0, SeekOrigin.Begin);
texture = Texture.FromStream(General.Map.Graphics.Device, memstream, (int)memstream.Length, bitmap.Size.Width, bitmap.Size.Height, 0,
Usage.None, Format.Unknown, Pool.Managed, Filter.Box, Filter.Box, 0);
memstream.Dispose();
}
2007-10-21 04:07:36 +00:00
}
2007-10-01 20:57:41 +00:00
}
// This destroys the Direct3D texture
public void ReleaseTexture()
{
lock(this)
{
// Trash it
if(texture != null) texture.Dispose();
texture = null;
}
2007-10-01 20:57:41 +00:00
}
#endregion
}
}