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

This commit is contained in:
chrisf
2007-07-11 11:09:26 +00:00
parent 492c76262b
commit 04e21ec2da
4 changed files with 66 additions and 46 deletions

View File

@@ -31,7 +31,7 @@ namespace OpenRa.Game
Dictionary<Sheet, IndexBuffer> drawBatches = new Dictionary<Sheet, IndexBuffer>();
World world;
TreeCache treeRenderer;
TreeCache treeCache;
void LoadTextures()
{
@@ -68,26 +68,10 @@ namespace OpenRa.Game
s.LoadTexture(renderer.Device);
world = new World(renderer.Device);
treeRenderer = new TreeCache(renderer.Device, map, TileMix, pal);
treeCache = new TreeCache(renderer.Device, map, TileMix, pal);
foreach (TreeReference treeReference in map.Trees)
world.Add(new Tree(treeReference, treeRenderer, map));
}
float U(SheetRectangle<Sheet> 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;// (1 - u) * u0 + u * u1;
}
float V(SheetRectangle<Sheet> 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;// return (1 - v) * v0 + v * v1;
world.Add(new Tree(treeReference, treeCache, map));
}
void LoadVertexBuffer()
@@ -100,24 +84,13 @@ namespace OpenRa.Game
{
SheetRectangle<Sheet> tile = tileMapping[map.MapTiles[i + map.XOffset, j + map.YOffset]];
ushort offset = (ushort)vertices.Count;
vertices.Add(new Vertex(24 * i, 24 * j, 0, U(tile, 0), V(tile, 0)));
vertices.Add(new Vertex(24 + 24 * i, 24 * j, 0, U(tile, 1), V(tile, 0)));
vertices.Add(new Vertex(24 * i, 24 + 24 * j, 0, U(tile, 0), V(tile, 1)));
vertices.Add(new Vertex(24 + 24 * i, 24 + 24 * j, 0, U(tile, 1), V(tile, 1)));
List<ushort> indexList;
if (!indexMap.TryGetValue(tile.sheet, out indexList))
indexMap.Add(tile.sheet, indexList = new List<ushort>());
indexList.Add(offset);
indexList.Add((ushort)(offset + 1));
indexList.Add((ushort)(offset + 2));
ushort offset = (ushort)vertices.Count;
indexList.Add((ushort)(offset + 1));
indexList.Add((ushort)(offset + 3));
indexList.Add((ushort)(offset + 2));
Util.CreateQuad(vertices, indexList, new PointF(24 * i, 24 * j), tile);
}
vertexBuffer = new FvfVertexBuffer<Vertex>(renderer.Device, vertices.Count, Vertex.Format);

View File

@@ -47,6 +47,7 @@
<Compile Include="Sheet.cs" />
<Compile Include="Tree.cs" />
<Compile Include="TreeCache.cs" />
<Compile Include="Util.cs" />
<Compile Include="Vertex.cs" />
<Compile Include="World.cs" />
</ItemGroup>

58
OpenRa.Game/Util.cs Normal file
View File

@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenRa.FileFormats;
using System.Drawing;
namespace OpenRa.Game
{
static class Util
{
public static float U(SheetRectangle<Sheet> 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;// (1 - u) * u0 + u * u1;
}
public static float V(SheetRectangle<Sheet> 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;// return (1 - v) * v0 + v * v1;
}
public static Vertex MakeVertex(PointF o, float u, float v, SheetRectangle<Sheet> r)
{
float x2 = o.X + r.size.Width;
float y2 = o.Y + r.size.Height;
return new Vertex(Lerp(o.X, x2, u), Lerp(o.Y, y2, v), 0, U(r, u), V(r, v));
}
static float Lerp(float a, float b, float t)
{
return (1 - t) * a + t * b;
}
public static void CreateQuad(List<Vertex> vertices, List<ushort> indices, PointF o, SheetRectangle<Sheet> r)
{
ushort offset = (ushort)vertices.Count;
vertices.Add(Util.MakeVertex(o, 0, 0, r));
vertices.Add(Util.MakeVertex(o, 1, 0, r));
vertices.Add(Util.MakeVertex(o, 0, 1, r));
vertices.Add(Util.MakeVertex(o, 1, 1, r));
indices.Add(offset);
indices.Add((ushort)(offset + 1));
indices.Add((ushort)(offset + 2));
indices.Add((ushort)(offset + 1));
indices.Add((ushort)(offset + 3));
indices.Add((ushort)(offset + 2));
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text;
using BluntDirectX.Direct3D;
using OpenRa.FileFormats;
using System.Drawing;
namespace OpenRa.Game
{
@@ -47,21 +48,8 @@ namespace OpenRa.Game
foreach (SheetRectangle<Sheet> image in a.currentImages)
{
int offset = vertices.Count;
vertices.Add(new Vertex(a.location.X, a.location.Y, 0, U(image, 0), V(image, 0)));
vertices.Add(new Vertex(a.location.X + image.size.Width, a.location.Y, 0, U(image, 1), V(image, 0)));
vertices.Add(new Vertex(a.location.X, a.location.Y + image.size.Height, 0, U(image, 0), V(image, 1)));
vertices.Add(new Vertex(a.location.X + image.size.Width, a.location.Y + image.size.Height, 0, U(image, 1), V(image, 1)));
indices.Add((ushort)offset);
indices.Add((ushort)(offset + 1));
indices.Add((ushort)(offset + 2));
indices.Add((ushort)(offset + 1));
indices.Add((ushort)(offset + 3));
indices.Add((ushort)(offset + 2));
sheet = image.sheet;
Util.CreateQuad(vertices, indices, a.location, image);
if (++sprites >= spritesPerBatch)
{