From c77a2588bdffcfbdc3e40d3bb5481a37bb82ae03 Mon Sep 17 00:00:00 2001 From: chrisf Date: Fri, 13 Jul 2007 17:35:07 +0000 Subject: [PATCH] git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1213 993157c7-ee19-0410-b2c4-bb4e9862e678 --- OpenRa.FileFormats/OpenRa.FileFormats.csproj | 1 - OpenRa.FileFormats/TileSheetBuilder.cs | 106 ------------------- OpenRa.Game/Actor.cs | 2 +- OpenRa.Game/CoreSheetBuilder.cs | 55 ---------- OpenRa.Game/MainWindow.cs | 2 +- OpenRa.Game/Mcv.cs | 11 +- OpenRa.Game/OpenRa.Game.csproj | 4 +- OpenRa.Game/Program.cs | 4 +- OpenRa.Game/Refinery.cs | 9 +- OpenRa.Game/Sheet.cs | 5 +- OpenRa.Game/SheetBuilder.cs | 92 ++++++++++++++++ OpenRa.Game/Sidebar.cs | 3 +- OpenRa.Game/Sprite.cs | 31 ++++++ OpenRa.Game/SpriteRenderer.cs | 2 +- OpenRa.Game/SpriteSheetBuilder.cs | 25 +++++ OpenRa.Game/TerrainRenderer.cs | 8 +- OpenRa.Game/Tree.cs | 6 +- OpenRa.Game/TreeCache.cs | 6 +- OpenRa.Game/UnitSheetBuilder.cs | 4 +- OpenRa.Game/Util.cs | 10 +- OpenRa.Game/World.cs | 4 +- 21 files changed, 184 insertions(+), 206 deletions(-) delete mode 100644 OpenRa.FileFormats/TileSheetBuilder.cs delete mode 100644 OpenRa.Game/CoreSheetBuilder.cs create mode 100644 OpenRa.Game/SheetBuilder.cs create mode 100644 OpenRa.Game/Sprite.cs create mode 100644 OpenRa.Game/SpriteSheetBuilder.cs diff --git a/OpenRa.FileFormats/OpenRa.FileFormats.csproj b/OpenRa.FileFormats/OpenRa.FileFormats.csproj index f328bd7d6b..88233e0e8d 100644 --- a/OpenRa.FileFormats/OpenRa.FileFormats.csproj +++ b/OpenRa.FileFormats/OpenRa.FileFormats.csproj @@ -48,7 +48,6 @@ - diff --git a/OpenRa.FileFormats/TileSheetBuilder.cs b/OpenRa.FileFormats/TileSheetBuilder.cs deleted file mode 100644 index f7f98a3d70..0000000000 --- a/OpenRa.FileFormats/TileSheetBuilder.cs +++ /dev/null @@ -1,106 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Drawing; - -namespace OpenRa.FileFormats -{ - public delegate T Provider(); - - public class TileSheetBuilder - where T : class - { - readonly Size pageSize; - readonly Provider pageProvider; - - public TileSheetBuilder(Size pageSize, Provider pageProvider) - { - this.pageSize = pageSize; - this.pageProvider = pageProvider; - } - - T current = null; - int x = 0, y = 0, rowHeight = 0; - TextureChannel? channel; - - TextureChannel? NextChannel(TextureChannel? t) - { - if (t == null) - return TextureChannel.Red; - - switch (t.Value) - { - case TextureChannel.Red: return TextureChannel.Green; - case TextureChannel.Green: return TextureChannel.Blue; - case TextureChannel.Blue: return TextureChannel.Alpha; - case TextureChannel.Alpha: return null; - - default: return null; - } - } - - public SheetRectangle AddImage(Size imageSize) - { - if (imageSize.Width > pageSize.Width || imageSize.Height > pageSize.Height) - return null; - - if (current == null) - { - current = pageProvider(); - channel = NextChannel(null); - } - - if (rowHeight == 0 || imageSize.Width + x > pageSize.Width) - { - y += rowHeight; - rowHeight = imageSize.Height; - x = 0; - } - - if (imageSize.Height > rowHeight) - rowHeight = imageSize.Height; - - if (y + imageSize.Height > pageSize.Height) - { - - if (null == (channel = NextChannel(channel))) - { - current = pageProvider(); - channel = NextChannel(channel); - } - - x = y = rowHeight = 0; - } - - SheetRectangle rect = new SheetRectangle(current, new Point(x, y), imageSize, channel.Value); - x += imageSize.Width; - - return rect; - } - } - - public class SheetRectangle - where T : class - { - public readonly Point origin; - public readonly Size size; - public readonly T sheet; - public readonly TextureChannel channel; - - internal SheetRectangle(T sheet, Point origin, Size size, TextureChannel channel) - { - this.origin = origin; - this.size = size; - this.sheet = sheet; - this.channel = channel; - } - } - - public enum TextureChannel - { - Red, - Green, - Blue, - Alpha, - } -} diff --git a/OpenRa.Game/Actor.cs b/OpenRa.Game/Actor.cs index d6eff22170..a558fa6d13 100644 --- a/OpenRa.Game/Actor.cs +++ b/OpenRa.Game/Actor.cs @@ -12,6 +12,6 @@ namespace OpenRa.Game { public PointF location; public int palette; - public abstract SheetRectangle[] CurrentImages { get; } + public abstract Sprite[] CurrentImages { get; } } } diff --git a/OpenRa.Game/CoreSheetBuilder.cs b/OpenRa.Game/CoreSheetBuilder.cs deleted file mode 100644 index ad0ec61b75..0000000000 --- a/OpenRa.Game/CoreSheetBuilder.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Drawing; -using BluntDirectX.Direct3D; -using OpenRa.FileFormats; - -namespace OpenRa.Game -{ - static class CoreSheetBuilder - { - static TileSheetBuilder builder; - static Size pageSize = new Size(512,512); - - public static void Initialize(GraphicsDevice device) - { - Provider sheetProvider = delegate { return new Sheet(pageSize, device); }; - builder = new TileSheetBuilder(pageSize, sheetProvider); - } - - public static SheetRectangle Add(byte[] src, Size size) - { - SheetRectangle rect = builder.AddImage(size); - Util.CopyIntoChannel(rect, src); - return rect; - } - - public static SheetRectangle Add(Size size, byte paletteIndex) - { - byte[] data = new byte[size.Width * size.Height]; - for (int i = 0; i < data.Length; i++) - data[i] = paletteIndex; - - return Add(data, size); - } - } - - static class SpriteSheetBuilder - { - static Dictionary> sprites = - new Dictionary>(); - - public static SheetRectangle LoadSprite(Package package, string filename) - { - SheetRectangle value; - if (!sprites.TryGetValue(filename, out value)) - { - ShpReader shp = new ShpReader(package.GetContent(filename)); - sprites.Add(filename, value = CoreSheetBuilder.Add(shp[0].Image, shp.Size)); - } - - return value; - } - } -} diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index a3f0cd7c54..0ccefb7f4d 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -38,7 +38,7 @@ namespace OpenRa.Game renderer = new Renderer(this, GetResolution(settings), false); Visible = true; - CoreSheetBuilder.Initialize(renderer.Device); + SheetBuilder.Initialize(renderer.Device); map = new Map(new IniFile(File.OpenRead("../../../" + settings.GetValue("map", "scm12ea.ini")))); diff --git a/OpenRa.Game/Mcv.cs b/OpenRa.Game/Mcv.cs index d6a5926c0b..c5b6efaf60 100644 --- a/OpenRa.Game/Mcv.cs +++ b/OpenRa.Game/Mcv.cs @@ -20,18 +20,13 @@ namespace OpenRa.Game mcvRange = UnitSheetBuilder.AddUnit("mcv"); } - int GetFacing() - { - int x = (Environment.TickCount >> 6) % 32; - return x; - //return x < 32 ? x : 63 - x; - } + int GetFacing() { return (Environment.TickCount >> 6) % 32; } - public override SheetRectangle[] CurrentImages + public override Sprite[] CurrentImages { get { - return new SheetRectangle[] + return new Sprite[] { UnitSheetBuilder.sprites[GetFacing() + mcvRange.Value.Start] }; diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index 2f227234a0..a699d11977 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -42,7 +42,7 @@ - + Form @@ -55,7 +55,9 @@ + + diff --git a/OpenRa.Game/Program.cs b/OpenRa.Game/Program.cs index 7d7e0d04b4..78180cde20 100644 --- a/OpenRa.Game/Program.cs +++ b/OpenRa.Game/Program.cs @@ -21,8 +21,8 @@ namespace OpenRa.Game } catch( Exception e ) { - File.WriteAllText( "error.log", e.ToString() ); - throw; + File.WriteAllText( "error.log", e.ToString() ); + throw; } } } diff --git a/OpenRa.Game/Refinery.cs b/OpenRa.Game/Refinery.cs index e043c7bd45..adc4d61009 100644 --- a/OpenRa.Game/Refinery.cs +++ b/OpenRa.Game/Refinery.cs @@ -20,16 +20,13 @@ namespace OpenRa.Game this.palette = palette; } - int GetFrame() - { - return 1;// - } + int GetFrame() { return 1; } - public override SheetRectangle[] CurrentImages + public override Sprite[] CurrentImages { get { - return new SheetRectangle[] { UnitSheetBuilder.sprites[refineryRange.Value.Start + GetFrame()] }; + return new Sprite[] { UnitSheetBuilder.sprites[refineryRange.Value.Start + GetFrame()] }; } } } diff --git a/OpenRa.Game/Sheet.cs b/OpenRa.Game/Sheet.cs index 53ec38346d..1200b6312e 100644 --- a/OpenRa.Game/Sheet.cs +++ b/OpenRa.Game/Sheet.cs @@ -15,13 +15,12 @@ namespace OpenRa.Game readonly GraphicsDevice device; Texture texture; + public Size Size { get { return bitmap.Size; } } + public Sheet(Size size, GraphicsDevice d) { bitmap = new Bitmap(size.Width, size.Height); device = d; - - //using (Graphics g = Graphics.FromImage(bitmap)) - // g.FillRectangle(Brushes.Fuchsia, 0, 0, size.Width, size.Height); } public Texture Texture diff --git a/OpenRa.Game/SheetBuilder.cs b/OpenRa.Game/SheetBuilder.cs new file mode 100644 index 0000000000..a772527b11 --- /dev/null +++ b/OpenRa.Game/SheetBuilder.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; +using BluntDirectX.Direct3D; +using OpenRa.FileFormats; + +namespace OpenRa.Game +{ + delegate T Provider(); + + static class SheetBuilder + { + public static void Initialize(GraphicsDevice device) + { + pageProvider = delegate { return new Sheet(new Size(512,512), device); }; + } + + public static Sprite Add(byte[] src, Size size) + { + Sprite rect = AddImage(size); + Util.CopyIntoChannel(rect, src); + return rect; + } + + public static Sprite Add(Size size, byte paletteIndex) + { + byte[] data = new byte[size.Width * size.Height]; + for (int i = 0; i < data.Length; i++) + data[i] = paletteIndex; + + return Add(data, size); + } + + static Provider pageProvider; + static Sheet current = null; + static int x = 0, y = 0, rowHeight = 0; + static TextureChannel? channel = null; + + static TextureChannel? NextChannel(TextureChannel? t) + { + if (t == null) + return TextureChannel.Red; + + switch (t.Value) + { + case TextureChannel.Red: return TextureChannel.Green; + case TextureChannel.Green: return TextureChannel.Blue; + case TextureChannel.Blue: return TextureChannel.Alpha; + case TextureChannel.Alpha: return null; + + default: return null; + } + } + + static Sprite AddImage(Size imageSize) + { + if (current == null) + { + current = pageProvider(); + channel = NextChannel(null); + } + + if (rowHeight == 0 || imageSize.Width + x > current.Size.Width) + { + y += rowHeight; + rowHeight = imageSize.Height; + x = 0; + } + + if (imageSize.Height > rowHeight) + rowHeight = imageSize.Height; + + if (y + imageSize.Height > current.Size.Height) + { + + if (null == (channel = NextChannel(channel))) + { + current = pageProvider(); + channel = NextChannel(channel); + } + + x = y = rowHeight = 0; + } + + Sprite rect = new Sprite(current, new Point(x, y), imageSize, channel.Value); + x += imageSize.Width; + + return rect; + } + } +} diff --git a/OpenRa.Game/Sidebar.cs b/OpenRa.Game/Sidebar.cs index 9624010536..7d587fb506 100644 --- a/OpenRa.Game/Sidebar.cs +++ b/OpenRa.Game/Sidebar.cs @@ -10,7 +10,6 @@ using System.IO; namespace OpenRa.Game { - using Sprite = SheetRectangle; class Sidebar { TechTree.TechTree techTree = new TechTree.TechTree(); @@ -37,7 +36,7 @@ namespace OpenRa.Game LoadSprites("../../../buildings.txt"); LoadSprites("../../../units.txt"); - sprites.Add("BLANK", CoreSheetBuilder.Add(new Size(64, 48), 16)); + sprites.Add("BLANK", SheetBuilder.Add(new Size(64, 48), 16)); techTree.CurrentRace = race; } diff --git a/OpenRa.Game/Sprite.cs b/OpenRa.Game/Sprite.cs new file mode 100644 index 0000000000..cb277d3aa7 --- /dev/null +++ b/OpenRa.Game/Sprite.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; + +namespace OpenRa.Game +{ + class Sprite + { + public readonly Point origin; + public readonly Size size; + public readonly Sheet sheet; + public readonly TextureChannel channel; + + internal Sprite(Sheet sheet, Point origin, Size size, TextureChannel channel) + { + this.origin = origin; + this.size = size; + this.sheet = sheet; + this.channel = channel; + } + } + + public enum TextureChannel + { + Red, + Green, + Blue, + Alpha, + } +} diff --git a/OpenRa.Game/SpriteRenderer.cs b/OpenRa.Game/SpriteRenderer.cs index d524462258..f3b7b36b02 100644 --- a/OpenRa.Game/SpriteRenderer.cs +++ b/OpenRa.Game/SpriteRenderer.cs @@ -49,7 +49,7 @@ namespace OpenRa.Game } } - public void DrawSprite(SheetRectangle s, PointF location) + public void DrawSprite(Sprite s, PointF location) { if (s.sheet != currentSheet) Flush(); diff --git a/OpenRa.Game/SpriteSheetBuilder.cs b/OpenRa.Game/SpriteSheetBuilder.cs new file mode 100644 index 0000000000..bffb3c1511 --- /dev/null +++ b/OpenRa.Game/SpriteSheetBuilder.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; +using OpenRa.FileFormats; + +namespace OpenRa.Game +{ + static class SpriteSheetBuilder + { + static Dictionary sprites = + new Dictionary(); + + public static Sprite LoadSprite(Package package, string filename) + { + Sprite value; + if (!sprites.TryGetValue(filename, out value)) + { + ShpReader shp = new ShpReader(package.GetContent(filename)); + sprites.Add(filename, value = SheetBuilder.Add(shp[0].Image, shp.Size)); + } + + return value; + } + } +} diff --git a/OpenRa.Game/TerrainRenderer.cs b/OpenRa.Game/TerrainRenderer.cs index 4322c1edb6..202fc25e34 100644 --- a/OpenRa.Game/TerrainRenderer.cs +++ b/OpenRa.Game/TerrainRenderer.cs @@ -25,8 +25,8 @@ namespace OpenRa.Game tileSet = new TileSet(tilePackage, map.TileSuffix); - Dictionary> tileMapping = - new Dictionary>(); + Dictionary tileMapping = + new Dictionary(); Size tileSize = new Size(24, 24); @@ -37,10 +37,10 @@ namespace OpenRa.Game for (int i = 0; i < map.Width; i++) { TileReference tileRef = map.MapTiles[i + map.XOffset, j + map.YOffset]; - SheetRectangle tile; + Sprite tile; if (!tileMapping.TryGetValue(tileRef, out tile)) - tileMapping.Add(tileRef, tile = CoreSheetBuilder.Add(tileSet.GetBytes(tileRef), tileSize)); + tileMapping.Add(tileRef, tile = SheetBuilder.Add(tileSet.GetBytes(tileRef), tileSize)); terrainSheet = tile.sheet; diff --git a/OpenRa.Game/Tree.cs b/OpenRa.Game/Tree.cs index d6e7213334..94043dc99a 100644 --- a/OpenRa.Game/Tree.cs +++ b/OpenRa.Game/Tree.cs @@ -11,11 +11,11 @@ namespace OpenRa.Game public Tree(TreeReference r, TreeCache renderer, Map map) { location = new PointF(24 * (r.X - map.XOffset), 24 * (r.Y - map.YOffset)); - currentImages = new SheetRectangle[] { renderer.GetImage(r.Image) }; + currentImages = new Sprite[] { renderer.GetImage(r.Image) }; } - SheetRectangle[] currentImages; - public override SheetRectangle[] CurrentImages + Sprite[] currentImages; + public override Sprite[] CurrentImages { get { return currentImages; } } diff --git a/OpenRa.Game/TreeCache.cs b/OpenRa.Game/TreeCache.cs index c89892285c..ca8f3e6d30 100644 --- a/OpenRa.Game/TreeCache.cs +++ b/OpenRa.Game/TreeCache.cs @@ -9,7 +9,7 @@ namespace OpenRa.Game { class TreeCache { - Dictionary> trees = new Dictionary>(); + Dictionary trees = new Dictionary(); public TreeCache(GraphicsDevice device, Map map, Package package) { @@ -21,10 +21,10 @@ namespace OpenRa.Game string filename = r.Image + "." + map.Theater.Substring(0, 3); ShpReader reader = new ShpReader(package.GetContent(filename)); - trees.Add(r.Image, CoreSheetBuilder.Add(reader[0].Image, reader.Size)); + trees.Add(r.Image, SheetBuilder.Add(reader[0].Image, reader.Size)); } } - public SheetRectangle GetImage(string tree) { return trees[tree]; } + public Sprite GetImage(string tree) { return trees[tree]; } } } diff --git a/OpenRa.Game/UnitSheetBuilder.cs b/OpenRa.Game/UnitSheetBuilder.cs index a1211c463c..e8c39d5319 100644 --- a/OpenRa.Game/UnitSheetBuilder.cs +++ b/OpenRa.Game/UnitSheetBuilder.cs @@ -12,7 +12,7 @@ namespace OpenRa.Game static readonly Package unitsPackage = new Package( "../../../conquer.mix" ); static readonly Package otherUnitsPackage = new Package("../../../hires.mix"); - public static readonly List> sprites = new List>(); + public static readonly List sprites = new List(); static ShpReader Load(string filename) { @@ -28,7 +28,7 @@ namespace OpenRa.Game int low = sprites.Count; ShpReader reader = Load(name + ".shp"); foreach (ImageHeader h in reader) - sprites.Add(CoreSheetBuilder.Add(h.Image, reader.Size)); + sprites.Add(SheetBuilder.Add(h.Image, reader.Size)); return new Range(low, sprites.Count - 1); } diff --git a/OpenRa.Game/Util.cs b/OpenRa.Game/Util.cs index 2696780c8f..405010e404 100644 --- a/OpenRa.Game/Util.cs +++ b/OpenRa.Game/Util.cs @@ -9,7 +9,7 @@ namespace OpenRa.Game { static class Util { - public static float U(SheetRectangle s, float u) + 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; @@ -17,7 +17,7 @@ namespace OpenRa.Game return (u > 0) ? u1 : u0; } - public static float V(SheetRectangle s, float v) + 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; @@ -48,7 +48,7 @@ namespace OpenRa.Game return new PointF(paletteLine / 16.0f, channelEncoder(channel)); } - public static Vertex MakeVertex(PointF o, float u, float v, SheetRectangle r, int palette) + public static Vertex MakeVertex(PointF o, float u, float v, Sprite r, int palette) { float x2 = o.X + r.size.Width; float y2 = o.Y + r.size.Height; @@ -64,7 +64,7 @@ namespace OpenRa.Game return (1 - t) * a + t * b; } - public static void CreateQuad(List vertices, List indices, PointF o, SheetRectangle r, int palette) + public static void CreateQuad(List vertices, List indices, PointF o, Sprite r, int palette) { ushort offset = (ushort)vertices.Count; @@ -82,7 +82,7 @@ namespace OpenRa.Game indices.Add((ushort)(offset + 2)); } - public static void CopyIntoChannel(SheetRectangle dest, byte[] src) + public static void CopyIntoChannel(Sprite dest, byte[] src) { for (int i = 0; i < dest.size.Width; i++) for (int j = 0; j < dest.size.Height; j++) diff --git a/OpenRa.Game/World.cs b/OpenRa.Game/World.cs index e2f70ef73f..361b969100 100644 --- a/OpenRa.Game/World.cs +++ b/OpenRa.Game/World.cs @@ -46,7 +46,7 @@ namespace OpenRa.Game foreach (Actor a in actors) { - SheetRectangle[] images = a.CurrentImages; + Sprite[] images = a.CurrentImages; if (images == null) continue; @@ -57,7 +57,7 @@ namespace OpenRa.Game if (a.location.Y > yr.End || a.location.Y < yr.Start - images[0].size.Height) continue; - foreach (SheetRectangle image in images) + foreach (Sprite image in images) { if( image.sheet != sheet && sprites > 0 && sheet != null ) {