2019-08-09 04:18:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Rendering
|
|
|
|
|
{
|
|
|
|
|
public class BaseTexture : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public BaseTexture()
|
|
|
|
|
{
|
|
|
|
|
Handle = Texture_New();
|
|
|
|
|
if (Handle == IntPtr.Zero)
|
|
|
|
|
throw new Exception("Texture_New failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BaseTexture()
|
|
|
|
|
{
|
|
|
|
|
Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Disposed { get { return Handle == IntPtr.Zero; } }
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (!Disposed)
|
|
|
|
|
{
|
|
|
|
|
Texture_Delete(Handle);
|
|
|
|
|
Handle = IntPtr.Zero;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-09 22:46:51 +00:00
|
|
|
|
internal IntPtr Handle;
|
2019-08-09 04:18:08 +00:00
|
|
|
|
|
|
|
|
|
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
|
2019-08-09 21:15:48 +00:00
|
|
|
|
protected static extern IntPtr Texture_New();
|
2019-08-09 04:18:08 +00:00
|
|
|
|
|
|
|
|
|
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
|
2019-08-09 21:15:48 +00:00
|
|
|
|
protected static extern void Texture_Delete(IntPtr handle);
|
|
|
|
|
|
|
|
|
|
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
|
protected static extern void Texture_Set2DImage(IntPtr handle, int width, int height);
|
|
|
|
|
|
|
|
|
|
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
|
protected static extern void Texture_SetPixels(IntPtr handle, IntPtr data);
|
|
|
|
|
|
|
|
|
|
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
|
protected static extern IntPtr Texture_Lock(IntPtr handle);
|
|
|
|
|
|
|
|
|
|
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
|
protected static extern void Texture_Unlock(IntPtr handle);
|
|
|
|
|
|
|
|
|
|
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
|
protected static extern void Texture_SetCubeImage(IntPtr handle, int size);
|
|
|
|
|
|
|
|
|
|
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
|
protected static extern void Texture_SetCubePixels(IntPtr handle, CubeMapFace face, IntPtr data);
|
2019-08-09 04:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Texture : BaseTexture
|
|
|
|
|
{
|
2019-08-09 21:15:48 +00:00
|
|
|
|
public Texture(int width, int height)
|
2019-08-09 04:18:08 +00:00
|
|
|
|
{
|
2019-08-09 21:15:48 +00:00
|
|
|
|
Width = width;
|
|
|
|
|
Height = height;
|
|
|
|
|
Texture_Set2DImage(Handle, Width, Height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Texture(System.Drawing.Bitmap bitmap)
|
|
|
|
|
{
|
|
|
|
|
Width = bitmap.Width;
|
|
|
|
|
Height = bitmap.Height;
|
|
|
|
|
Texture_Set2DImage(Handle, Width, Height);
|
|
|
|
|
SetPixels(bitmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Texture(System.Drawing.Image image)
|
|
|
|
|
{
|
|
|
|
|
using (var bitmap = new System.Drawing.Bitmap(image))
|
|
|
|
|
{
|
|
|
|
|
Width = bitmap.Width;
|
|
|
|
|
Height = bitmap.Height;
|
|
|
|
|
Texture_Set2DImage(Handle, Width, Height);
|
|
|
|
|
SetPixels(bitmap);
|
|
|
|
|
}
|
2019-08-09 04:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Width { get; private set; }
|
|
|
|
|
public int Height { get; private set; }
|
|
|
|
|
|
|
|
|
|
public object Tag { get; set; }
|
|
|
|
|
|
|
|
|
|
public void SetPixels(System.Drawing.Bitmap bitmap)
|
|
|
|
|
{
|
2019-08-09 21:15:48 +00:00
|
|
|
|
System.Drawing.Imaging.BitmapData bmpdata = bitmap.LockBits(
|
|
|
|
|
new System.Drawing.Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height),
|
|
|
|
|
System.Drawing.Imaging.ImageLockMode.ReadOnly,
|
|
|
|
|
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
|
2019-08-09 04:18:08 +00:00
|
|
|
|
|
2019-08-09 21:15:48 +00:00
|
|
|
|
Texture_SetPixels(Handle, bmpdata.Scan0);
|
2019-08-09 04:18:08 +00:00
|
|
|
|
|
|
|
|
|
bitmap.UnlockBits(bmpdata);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal Plotter LockPlotter(int visibleWidth, int visibleHeight)
|
|
|
|
|
{
|
2019-08-09 21:15:48 +00:00
|
|
|
|
unsafe
|
|
|
|
|
{
|
|
|
|
|
IntPtr data = Texture_Lock(Handle);
|
|
|
|
|
return new Plotter((PixelColor*)data.ToPointer(), Width, Height, Math.Min(Width, visibleWidth), Math.Min(Height, visibleHeight));
|
|
|
|
|
}
|
2019-08-09 04:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnlockPlotter()
|
|
|
|
|
{
|
2019-08-09 21:15:48 +00:00
|
|
|
|
Texture_Unlock(Handle);
|
2019-08-09 04:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CubeTexture : BaseTexture
|
|
|
|
|
{
|
2019-08-09 21:15:48 +00:00
|
|
|
|
public CubeTexture(int size)
|
2019-08-09 04:18:08 +00:00
|
|
|
|
{
|
2019-08-09 21:15:48 +00:00
|
|
|
|
Texture_SetCubeImage(Handle, size);
|
2019-08-09 04:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetPixels(CubeMapFace face, System.Drawing.Bitmap bitmap)
|
|
|
|
|
{
|
2019-08-09 21:15:48 +00:00
|
|
|
|
System.Drawing.Imaging.BitmapData bmpdata = bitmap.LockBits(
|
|
|
|
|
new System.Drawing.Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height),
|
|
|
|
|
System.Drawing.Imaging.ImageLockMode.ReadOnly,
|
|
|
|
|
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
|
|
|
|
|
|
|
|
|
|
Texture_SetCubePixels(Handle, face, bmpdata.Scan0);
|
|
|
|
|
|
|
|
|
|
bitmap.UnlockBits(bmpdata);
|
2019-08-09 04:18:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-09 21:15:48 +00:00
|
|
|
|
public enum CubeMapFace : int { PositiveX, PositiveY, PositiveZ, NegativeX, NegativeY, NegativeZ }
|
2019-08-09 04:18:08 +00:00
|
|
|
|
}
|