From 04e21ec2da03d5182d8cc3239413ff39ccf11316 Mon Sep 17 00:00:00 2001 From: chrisf Date: Wed, 11 Jul 2007 11:09:26 +0000 Subject: [PATCH] git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1169 993157c7-ee19-0410-b2c4-bb4e9862e678 --- OpenRa.Game/MainWindow.cs | 37 +++------------------- OpenRa.Game/OpenRa.Game.csproj | 1 + OpenRa.Game/Util.cs | 58 ++++++++++++++++++++++++++++++++++ OpenRa.Game/World.cs | 16 ++-------- 4 files changed, 66 insertions(+), 46 deletions(-) create mode 100644 OpenRa.Game/Util.cs diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 3fc23888bb..7f1ee3fa96 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -31,7 +31,7 @@ namespace OpenRa.Game Dictionary drawBatches = new Dictionary(); 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 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 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 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 indexList; if (!indexMap.TryGetValue(tile.sheet, out indexList)) indexMap.Add(tile.sheet, indexList = new List()); - 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(renderer.Device, vertices.Count, Vertex.Format); diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index 322e51d7e2..4d4bba0548 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -47,6 +47,7 @@ + diff --git a/OpenRa.Game/Util.cs b/OpenRa.Game/Util.cs new file mode 100644 index 0000000000..655814dc20 --- /dev/null +++ b/OpenRa.Game/Util.cs @@ -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 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 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 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 vertices, List indices, PointF o, SheetRectangle 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)); + } + } +} diff --git a/OpenRa.Game/World.cs b/OpenRa.Game/World.cs index 36e66cf911..091a66768d 100644 --- a/OpenRa.Game/World.cs +++ b/OpenRa.Game/World.cs @@ -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 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) {