Allow sheets to wrap an ITexture directly.
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Drawing.Imaging;
|
using System.Drawing.Imaging;
|
||||||
using OpenRA.FileFormats;
|
using OpenRA.FileFormats;
|
||||||
@@ -19,13 +20,21 @@ namespace OpenRA.Graphics
|
|||||||
{
|
{
|
||||||
ITexture texture;
|
ITexture texture;
|
||||||
bool dirty;
|
bool dirty;
|
||||||
public byte[] Data { get; private set; }
|
byte[] data;
|
||||||
|
|
||||||
public readonly Size Size;
|
public readonly Size Size;
|
||||||
|
public byte[] Data { get { return data ?? texture.GetData(); } }
|
||||||
|
|
||||||
public Sheet(Size size)
|
public Sheet(Size size)
|
||||||
{
|
{
|
||||||
Size = size;
|
Size = size;
|
||||||
Data = new byte[4*Size.Width*Size.Height];
|
data = new byte[4*Size.Width*Size.Height];
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sheet(ITexture texture)
|
||||||
|
{
|
||||||
|
this.texture = texture;
|
||||||
|
Size = texture.Size;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Sheet(string filename)
|
public Sheet(string filename)
|
||||||
@@ -33,7 +42,7 @@ namespace OpenRA.Graphics
|
|||||||
var bitmap = (Bitmap)Image.FromStream(FileSystem.Open(filename));
|
var bitmap = (Bitmap)Image.FromStream(FileSystem.Open(filename));
|
||||||
Size = bitmap.Size;
|
Size = bitmap.Size;
|
||||||
|
|
||||||
Data = new byte[4*Size.Width*Size.Height];
|
data = new byte[4*Size.Width*Size.Height];
|
||||||
var b = bitmap.LockBits(bitmap.Bounds(),
|
var b = bitmap.LockBits(bitmap.Bounds(),
|
||||||
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
||||||
|
|
||||||
@@ -48,10 +57,10 @@ namespace OpenRA.Graphics
|
|||||||
|
|
||||||
// Convert argb to bgra
|
// Convert argb to bgra
|
||||||
var argb = *(c + (y * b.Stride >> 2) + x);
|
var argb = *(c + (y * b.Stride >> 2) + x);
|
||||||
Data[i++] = (byte)(argb >> 0);
|
data[i++] = (byte)(argb >> 0);
|
||||||
Data[i++] = (byte)(argb >> 8);
|
data[i++] = (byte)(argb >> 8);
|
||||||
Data[i++] = (byte)(argb >> 16);
|
data[i++] = (byte)(argb >> 16);
|
||||||
Data[i++] = (byte)(argb >> 24);
|
data[i++] = (byte)(argb >> 24);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bitmap.UnlockBits(b);
|
bitmap.UnlockBits(b);
|
||||||
@@ -69,7 +78,7 @@ namespace OpenRA.Graphics
|
|||||||
|
|
||||||
if (dirty)
|
if (dirty)
|
||||||
{
|
{
|
||||||
texture.SetData(Data, Size.Width, Size.Height);
|
texture.SetData(data, Size.Width, Size.Height);
|
||||||
dirty = false;
|
dirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,6 +88,7 @@ namespace OpenRA.Graphics
|
|||||||
|
|
||||||
public Bitmap AsBitmap()
|
public Bitmap AsBitmap()
|
||||||
{
|
{
|
||||||
|
var d = Data;
|
||||||
var b = new Bitmap(Size.Width, Size.Height);
|
var b = new Bitmap(Size.Width, Size.Height);
|
||||||
var output = b.LockBits(new Rectangle(0, 0, Size.Width, Size.Height),
|
var output = b.LockBits(new Rectangle(0, 0, Size.Width, Size.Height),
|
||||||
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
|
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
|
||||||
@@ -93,7 +103,7 @@ namespace OpenRA.Graphics
|
|||||||
var i = 4*Size.Width*y + 4*x;
|
var i = 4*Size.Width*y + 4*x;
|
||||||
|
|
||||||
// Convert bgra to argb
|
// Convert bgra to argb
|
||||||
var argb = (Data[i+3] << 24) | (Data[i+2] << 16) | (Data[i+1] << 8) | Data[i];
|
var argb = (d[i+3] << 24) | (d[i+2] << 16) | (d[i+1] << 8) | d[i];
|
||||||
*(c + (y * output.Stride >> 2) + x) = argb;
|
*(c + (y * output.Stride >> 2) + x) = argb;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,6 +114,7 @@ namespace OpenRA.Graphics
|
|||||||
|
|
||||||
public Bitmap AsBitmap(TextureChannel channel, Palette pal)
|
public Bitmap AsBitmap(TextureChannel channel, Palette pal)
|
||||||
{
|
{
|
||||||
|
var d = Data;
|
||||||
var b = new Bitmap(Size.Width, Size.Height);
|
var b = new Bitmap(Size.Width, Size.Height);
|
||||||
var output = b.LockBits(new Rectangle(0, 0, Size.Width, Size.Height),
|
var output = b.LockBits(new Rectangle(0, 0, Size.Width, Size.Height),
|
||||||
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
|
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
|
||||||
@@ -115,7 +126,7 @@ namespace OpenRA.Graphics
|
|||||||
for (var x = 0; x < Size.Width; x++)
|
for (var x = 0; x < Size.Width; x++)
|
||||||
for (var y = 0; y < Size.Height; y++)
|
for (var y = 0; y < Size.Height; y++)
|
||||||
{
|
{
|
||||||
var index = Data[4*Size.Width*y + 4*x + (int)channel];
|
var index = d[4*Size.Width*y + 4*x + (int)channel];
|
||||||
*(c + (y * output.Stride >> 2) + x) = pal.GetColor(index).ToArgb();
|
*(c + (y * output.Stride >> 2) + x) = pal.GetColor(index).ToArgb();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,6 +135,12 @@ namespace OpenRA.Graphics
|
|||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MakeDirty() { dirty = true; }
|
public void CommitData()
|
||||||
|
{
|
||||||
|
if (data == null)
|
||||||
|
throw new InvalidOperationException("Texture-wrappers are read-only");
|
||||||
|
|
||||||
|
dirty = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ namespace OpenRA.Graphics
|
|||||||
{
|
{
|
||||||
var rect = Allocate(size);
|
var rect = Allocate(size);
|
||||||
Util.FastCopyIntoChannel(rect, src);
|
Util.FastCopyIntoChannel(rect, src);
|
||||||
|
current.CommitData();
|
||||||
return rect;
|
return rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,7 +104,6 @@ namespace OpenRA.Graphics
|
|||||||
}
|
}
|
||||||
|
|
||||||
var rect = new Sprite(current, new Rectangle(p, imageSize), channel);
|
var rect = new Sprite(current, new Rectangle(p, imageSize), channel);
|
||||||
current.MakeDirty();
|
|
||||||
p.X += imageSize.Width;
|
p.X += imageSize.Width;
|
||||||
|
|
||||||
return rect;
|
return rect;
|
||||||
|
|||||||
@@ -129,6 +129,8 @@ namespace OpenRA.Graphics
|
|||||||
p += face.Glyph.Bitmap.Pitch;
|
p += face.Glyph.Bitmap.Pitch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
s.sheet.CommitData();
|
||||||
|
|
||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ namespace OpenRA.Graphics
|
|||||||
Sprite s = sheetBuilder.Allocate(new Size(su, sv));
|
Sprite s = sheetBuilder.Allocate(new Size(su, sv));
|
||||||
Util.FastCopyIntoChannel(s, 0, colors);
|
Util.FastCopyIntoChannel(s, 0, colors);
|
||||||
Util.FastCopyIntoChannel(s, 1, normals);
|
Util.FastCopyIntoChannel(s, 1, normals);
|
||||||
s.sheet.MakeDirty();
|
s.sheet.CommitData();
|
||||||
|
|
||||||
var channels = new float2(channelSelect[(int)s.channel], channelSelect[(int)s.channel + 1]);
|
var channels = new float2(channelSelect[(int)s.channel], channelSelect[(int)s.channel + 1]);
|
||||||
return new Vertex[4]
|
return new Vertex[4]
|
||||||
|
|||||||
Reference in New Issue
Block a user