git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1221 993157c7-ee19-0410-b2c4-bb4e9862e678

This commit is contained in:
chrisf
2007-07-14 06:27:30 +00:00
parent 36491edc12
commit 6e0bf41569
5 changed files with 59 additions and 46 deletions

View File

@@ -87,7 +87,7 @@ namespace OpenRa.Game
p = new Point(0,0); p = new Point(0,0);
} }
Sprite rect = new Sprite(current, p, imageSize, channel.Value); Sprite rect = new Sprite(current, new Rectangle(p, imageSize), channel.Value);
p.X += imageSize.Width; p.X += imageSize.Width;
return rect; return rect;

View File

@@ -7,18 +7,37 @@ namespace OpenRa.Game
{ {
class Sprite class Sprite
{ {
public readonly Point origin; public readonly Rectangle bounds;
public readonly Size size;
public readonly Sheet sheet; public readonly Sheet sheet;
public readonly TextureChannel channel; public readonly TextureChannel channel;
internal Sprite(Sheet sheet, Point origin, Size size, TextureChannel channel) internal Sprite(Sheet sheet, Rectangle bounds, TextureChannel channel)
{ {
this.origin = origin; this.bounds = bounds;
this.size = size;
this.sheet = sheet; this.sheet = sheet;
this.channel = channel; this.channel = channel;
} }
RectangleF TextureCoords
{
get
{
return new RectangleF(
(float)(bounds.Left + 0.5f) / sheet.Size.Width,
(float)(bounds.Top + 0.5f) / sheet.Size.Height,
(float)(bounds.Width) / sheet.Size.Width,
(float)(bounds.Height) / sheet.Size.Height);
}
}
public PointF MapTextureCoords(PointF p)
{
RectangleF uv = TextureCoords;
return new PointF(
p.X > 0 ? uv.Right : uv.Left,
p.Y > 0 ? uv.Bottom : uv.Top);
}
} }
public enum TextureChannel public enum TextureChannel

View File

@@ -9,22 +9,6 @@ namespace OpenRa.Game
{ {
static class Util static class Util
{ {
public static float U(Sprite s, float u)
{
float u0 = (float)(s.origin.X + 0.5f) / (float)s.sheet.bitmap.Width;
float u1 = (float)(s.origin.X + s.size.Width) / (float)s.sheet.bitmap.Width;
return (u > 0) ? u1 : u0;
}
public static float V(Sprite s, float v)
{
float v0 = (float)(s.origin.Y + 0.5f) / (float)s.sheet.bitmap.Height;
float v1 = (float)(s.origin.Y + s.size.Height) / (float)s.sheet.bitmap.Height;
return (v > 0) ? v1 : v0;
}
public static float Constrain(float x, Range<float> range) public static float Constrain(float x, Range<float> range)
{ {
return x < range.Start ? range.Start : x > range.End ? range.End : x; return x < range.Start ? range.Start : x > range.End ? range.End : x;
@@ -48,15 +32,14 @@ namespace OpenRa.Game
return new PointF(paletteLine / 16.0f, channelEncoder(channel)); return new PointF(paletteLine / 16.0f, channelEncoder(channel));
} }
public static Vertex MakeVertex(PointF o, float u, float v, Sprite r, int palette) public static Vertex MakeVertex(PointF o, PointF uv, Sprite r, int palette)
{ {
float x2 = o.X + r.size.Width; PointF farCorner = new PointF(o.X + r.bounds.Width, o.Y + r.bounds.Height);
float y2 = o.Y + r.size.Height;
PointF p = EncodeVertexAttributes(r.channel, palette); return new Vertex(
Lerp( o, farCorner, uv ),
return new Vertex(Lerp(o.X, x2, u), Lerp(o.Y, y2, v), 0, U(r, u), V(r, v), r.MapTextureCoords(uv),
p.X, p.Y); EncodeVertexAttributes(r.channel, palette));
} }
static float Lerp(float a, float b, float t) static float Lerp(float a, float b, float t)
@@ -64,14 +47,27 @@ namespace OpenRa.Game
return (1 - t) * a + t * b; return (1 - t) * a + t * b;
} }
static PointF Lerp(PointF a, PointF b, PointF t)
{
return new PointF(
Lerp(a.X, b.X, t.X),
Lerp(a.Y, b.Y, t.Y));
}
static PointF[] uv =
{
new PointF( 0,0 ),
new PointF( 1,0 ),
new PointF( 0,1 ),
new PointF( 1,1 ),
};
public static void CreateQuad(List<Vertex> vertices, List<ushort> indices, PointF o, Sprite r, int palette) public static void CreateQuad(List<Vertex> vertices, List<ushort> indices, PointF o, Sprite r, int palette)
{ {
ushort offset = (ushort)vertices.Count; ushort offset = (ushort)vertices.Count;
vertices.Add(Util.MakeVertex(o, 0, 0, r, palette)); foreach( PointF p in uv )
vertices.Add(Util.MakeVertex(o, 1, 0, r, palette)); vertices.Add(Util.MakeVertex(o, p, r, palette));
vertices.Add(Util.MakeVertex(o, 0, 1, r, palette));
vertices.Add(Util.MakeVertex(o, 1, 1, r, palette));
indices.Add(offset); indices.Add(offset);
indices.Add((ushort)(offset + 1)); indices.Add((ushort)(offset + 1));
@@ -84,11 +80,11 @@ namespace OpenRa.Game
public static void CopyIntoChannel(Sprite dest, byte[] src) public static void CopyIntoChannel(Sprite dest, byte[] src)
{ {
for (int i = 0; i < dest.size.Width; i++) for (int i = 0; i < dest.bounds.Width; i++)
for (int j = 0; j < dest.size.Height; j++) for (int j = 0; j < dest.bounds.Height; j++)
{ {
Point p = new Point(dest.origin.X + i, dest.origin.Y + j); Point p = new Point(dest.bounds.Left + i, dest.bounds.Top + j);
byte b = src[i + dest.size.Width * j]; byte b = src[i + dest.bounds.Width * j];
Color original = dest.sheet.bitmap.GetPixel(p.X, p.Y); Color original = dest.sheet.bitmap.GetPixel(p.X, p.Y);
dest.sheet.bitmap.SetPixel(p.X, p.Y, ReplaceChannel(original, dest.channel, b)); dest.sheet.bitmap.SetPixel(p.X, p.Y, ReplaceChannel(original, dest.channel, b));
} }

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using BluntDirectX.Direct3D; using BluntDirectX.Direct3D;
using System.Drawing;
namespace OpenRa.Game namespace OpenRa.Game
{ {
@@ -12,14 +13,11 @@ namespace OpenRa.Game
public float x, y, z, u, v; public float x, y, z, u, v;
public float p, c; public float p, c;
public Vertex(float x, float y, float z, float u, float v, float p, float c) public Vertex(PointF xy, PointF uv, PointF pc)
{ {
this.x = x; this.y = y; this.z = z; this.x = xy.X; this.y = xy.Y; this.z = 0;
this.u = u; this.u = uv.X; this.v = uv.Y;
this.v = v; this.p = pc.X; this.c = pc.Y;
this.p = p;
this.c = c;
} }
public const VertexFormat Format = VertexFormat.Position | VertexFormat.Texture2; public const VertexFormat Format = VertexFormat.Position | VertexFormat.Texture2;

View File

@@ -30,10 +30,10 @@ namespace OpenRa.Game
if (images == null) if (images == null)
continue; continue;
if (a.location.X > xr.End || a.location.X < xr.Start - images[0].size.Width) if (a.location.X > xr.End || a.location.X < xr.Start - images[0].bounds.Width)
continue; continue;
if (a.location.Y > yr.End || a.location.Y < yr.Start - images[0].size.Height) if (a.location.Y > yr.End || a.location.Y < yr.Start - images[0].bounds.Height)
continue; continue;
foreach (Sprite image in images) foreach (Sprite image in images)