From d4a7e5fb9ea4410dae18ff06fff76a457787b72d Mon Sep 17 00:00:00 2001 From: chrisf Date: Fri, 13 Jul 2007 11:12:16 +0000 Subject: [PATCH] git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1195 993157c7-ee19-0410-b2c4-bb4e9862e678 --- OpenRa.FileFormats/TileSet.cs | 28 ++++++++++---------- OpenRa.FileFormats/TileSheetBuilder.cs | 36 +++++++++++++++++++++----- OpenRa.Game/CoreSheetBuilder.cs | 28 ++++++++++++++++++++ OpenRa.Game/MainWindow.cs | 28 +++++--------------- OpenRa.Game/OpenRa.Game.csproj | 1 + OpenRa.Game/Sheet.cs | 4 +-- OpenRa.Game/TreeCache.cs | 25 ++---------------- OpenRa.Game/UnitSheetBuilder.cs | 24 ++--------------- OpenRa.Game/Util.cs | 14 +++++----- OpenRa.Game/World.cs | 14 +++++++--- 10 files changed, 102 insertions(+), 100 deletions(-) create mode 100644 OpenRa.Game/CoreSheetBuilder.cs diff --git a/OpenRa.FileFormats/TileSet.cs b/OpenRa.FileFormats/TileSet.cs index 1d534c70cb..2ab9889fb9 100644 --- a/OpenRa.FileFormats/TileSet.cs +++ b/OpenRa.FileFormats/TileSet.cs @@ -24,28 +24,26 @@ namespace OpenRa.FileFormats if( countStr == null || startStr == null || pattern == null ) break; - //try + int count = int.Parse( countStr ); + int start = int.Parse( startStr, NumberStyles.HexNumber ); + for( int i = 0 ; i < count ; i++ ) { - int count = int.Parse( countStr ); - int start = int.Parse( startStr, NumberStyles.HexNumber ); - for( int i = 0 ; i < count ; i++ ) + Stream s; + try { - Stream s; - try - { - s = mixFile.GetContent( string.Format( pattern, i + 1 ) ); - } - catch { continue; } - Terrain t = new Terrain( s, pal ); - if( tiles.ContainsKey( (ushort)( start + i ) ) ) - continue; - tiles.Add( (ushort)( start + i ), t ); + s = mixFile.GetContent( string.Format( pattern, i + 1 ) ); } + catch { continue; } + Terrain t = new Terrain( s, pal ); + if( tiles.ContainsKey( (ushort)( start + i ) ) ) + continue; + tiles.Add( (ushort)( start + i ), t ); } - //catch { } } tileIdFile.Close(); } + + public byte[] GetBytes(TileReference r) { return tiles[r.tile].TileBitmapBytes[r.image]; } } } diff --git a/OpenRa.FileFormats/TileSheetBuilder.cs b/OpenRa.FileFormats/TileSheetBuilder.cs index 450c49f259..55ef8f3917 100644 --- a/OpenRa.FileFormats/TileSheetBuilder.cs +++ b/OpenRa.FileFormats/TileSheetBuilder.cs @@ -21,6 +21,24 @@ namespace OpenRa.FileFormats T current = null; int x = 0, y = 0, rowHeight = 0; + TextureChannel? channel; + + TextureChannel? NextChannel(TextureChannel? t) + { + if (t == null) + return TextureChannel.Red; + + if (t == TextureChannel.Red) + return TextureChannel.Green; + + if (t == TextureChannel.Green) + return TextureChannel.Blue; + + if (t == TextureChannel.Blue) + return TextureChannel.Alpha; + + return null; + } public SheetRectangle AddImage(Size imageSize) { @@ -28,7 +46,10 @@ namespace OpenRa.FileFormats return null; if (current == null) + { current = pageProvider(); + channel = NextChannel(null); + } if (rowHeight == 0 || imageSize.Width + x > pageSize.Width) { @@ -42,11 +63,17 @@ namespace OpenRa.FileFormats if (y + imageSize.Height > pageSize.Height) { - current = pageProvider(); + + if (null == (channel = NextChannel(channel))) + { + current = pageProvider(); + channel = NextChannel(channel); + } + x = y = rowHeight = 0; } - SheetRectangle rect = new SheetRectangle(current, new Point(x, y), imageSize); + SheetRectangle rect = new SheetRectangle(current, new Point(x, y), imageSize, channel.Value); x += imageSize.Width; return rect; @@ -68,11 +95,6 @@ namespace OpenRa.FileFormats this.sheet = sheet; this.channel = channel; } - - internal SheetRectangle(T sheet, Point origin, Size size) - : this(sheet, origin, size, TextureChannel.Red) - { - } } public enum TextureChannel diff --git a/OpenRa.Game/CoreSheetBuilder.cs b/OpenRa.Game/CoreSheetBuilder.cs new file mode 100644 index 0000000000..13ba01ade7 --- /dev/null +++ b/OpenRa.Game/CoreSheetBuilder.cs @@ -0,0 +1,28 @@ +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; + } + } +} diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 546aad6e6d..24a19668f1 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -33,18 +33,7 @@ namespace OpenRa.Game void LoadTextures() { - List sheets = new List(); - - Size pageSize = new Size(1024, 512); - - Provider sheetProvider = delegate - { - Sheet t = new Sheet(pageSize, renderer.Device); - sheets.Add(t); - return t; - }; - - TileSheetBuilder builder = new TileSheetBuilder(pageSize, sheetProvider); + Size tileSize = new Size(24, 24); for (int i = 0; i < map.Width; i++) for (int j = 0; j < map.Height; j++) @@ -52,13 +41,7 @@ namespace OpenRa.Game TileReference tileRef = map.MapTiles[i + map.XOffset, j + map.YOffset]; if (!tileMapping.ContainsKey(tileRef)) - { - SheetRectangle rect = builder.AddImage(new Size(24, 24)); - Util.CopyIntoChannel(rect.sheet.bitmap, TextureChannel.Red, - tileSet.tiles[tileRef.tile].TileBitmapBytes[tileRef.image], rect); - - tileMapping.Add(tileRef, rect); - } + tileMapping.Add(tileRef, CoreSheetBuilder.Add(tileSet.GetBytes(tileRef), tileSize)); } world = new World(renderer.Device); @@ -67,7 +50,6 @@ namespace OpenRa.Game foreach (TreeReference treeReference in map.Trees) world.Add(new Tree(treeReference, treeCache, map)); - UnitSheetBuilder.Initialize(renderer.Device); UnitSheetBuilder.AddUnit("mcv"); UnitSheetBuilder.AddUnit("1tnk"); UnitSheetBuilder.AddUnit("2tnk"); @@ -120,6 +102,8 @@ namespace OpenRa.Game renderer = new Renderer(this, GetResolution(settings), false); Visible = true; + CoreSheetBuilder.Initialize(renderer.Device); + string mapName = settings.GetValue("map", "scm12ea.ini"); IniFile mapFile = new IniFile(File.OpenRead("../../../" + mapName)); @@ -177,7 +161,9 @@ namespace OpenRa.Game foreach (KeyValuePair batch in drawBatches) DrawTerrainBatch(batch); - world.Draw(renderer); + world.Draw(renderer, + new Range(scrollPos.X, scrollPos.X + ClientSize.Width), + new Range(scrollPos.Y, scrollPos.Y + ClientSize.Height)); renderer.EndFrame(); } diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index 05ca24df24..932d801e5a 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -42,6 +42,7 @@ + Form diff --git a/OpenRa.Game/Sheet.cs b/OpenRa.Game/Sheet.cs index b45e743419..53ec38346d 100644 --- a/OpenRa.Game/Sheet.cs +++ b/OpenRa.Game/Sheet.cs @@ -20,8 +20,8 @@ namespace OpenRa.Game 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); + //using (Graphics g = Graphics.FromImage(bitmap)) + // g.FillRectangle(Brushes.Fuchsia, 0, 0, size.Width, size.Height); } public Texture Texture diff --git a/OpenRa.Game/TreeCache.cs b/OpenRa.Game/TreeCache.cs index d2355d818f..c89892285c 100644 --- a/OpenRa.Game/TreeCache.cs +++ b/OpenRa.Game/TreeCache.cs @@ -11,22 +11,8 @@ namespace OpenRa.Game { Dictionary> trees = new Dictionary>(); - public readonly Sheet sh; - public TreeCache(GraphicsDevice device, Map map, Package package) { - Size pageSize = new Size(1024, 512); - List sheets = new List(); - - Provider sheetProvider = delegate - { - Sheet sheet = new Sheet(pageSize, device); - sheets.Add(sheet); - return sheet; - }; - - TileSheetBuilder builder = new TileSheetBuilder(pageSize, sheetProvider); - foreach (TreeReference r in map.Trees) { if (trees.ContainsKey(r.Image)) @@ -35,17 +21,10 @@ namespace OpenRa.Game string filename = r.Image + "." + map.Theater.Substring(0, 3); ShpReader reader = new ShpReader(package.GetContent(filename)); - SheetRectangle rect = builder.AddImage(reader.Size); - Util.CopyIntoChannel(rect.sheet.bitmap, TextureChannel.Red, reader[0].Image, rect); - trees.Add(r.Image, rect); + trees.Add(r.Image, CoreSheetBuilder.Add(reader[0].Image, reader.Size)); } - - sh = sheets[0]; } - public SheetRectangle GetImage(string tree) - { - return trees[tree]; - } + public SheetRectangle GetImage(string tree) { return trees[tree]; } } } diff --git a/OpenRa.Game/UnitSheetBuilder.cs b/OpenRa.Game/UnitSheetBuilder.cs index 82f0123f39..1d5507788a 100644 --- a/OpenRa.Game/UnitSheetBuilder.cs +++ b/OpenRa.Game/UnitSheetBuilder.cs @@ -12,31 +12,11 @@ namespace OpenRa.Game static readonly Package unitsPackage = new Package( "../../../conquer.mix" ); public static readonly List> McvSheet = new List>(); - static TileSheetBuilder builder; - static List sheets = new List(); - static Size pageSize = new Size(1024, 512); - - public static void Initialize( GraphicsDevice device ) - { - Provider sheetProvider = delegate - { - Sheet sheet = new Sheet(pageSize, device); - sheets.Add(sheet); - return sheet; - }; - - builder = new TileSheetBuilder(pageSize, sheetProvider); - } - public static void AddUnit( string name ) { ShpReader reader = new ShpReader( unitsPackage.GetContent( name + ".shp" ) ); - foreach( ImageHeader h in reader ) - { - SheetRectangle rect = builder.AddImage(reader.Size); - Util.CopyIntoChannel(rect.sheet.bitmap, TextureChannel.Red, h.Image, rect); - McvSheet.Add( rect ); - } + foreach (ImageHeader h in reader) + McvSheet.Add(CoreSheetBuilder.Add(h.Image, reader.Size)); } } } diff --git a/OpenRa.Game/Util.cs b/OpenRa.Game/Util.cs index 7136e908d7..1bf5a6292c 100644 --- a/OpenRa.Game/Util.cs +++ b/OpenRa.Game/Util.cs @@ -76,15 +76,15 @@ namespace OpenRa.Game indices.Add((ushort)(offset + 2)); } - public static void CopyIntoChannel(Bitmap bitmap, TextureChannel channel, byte[] src, SheetRectangle s) + public static void CopyIntoChannel(SheetRectangle dest, byte[] src) { - for( int i = 0; i < s.size.Width; i++ ) - for (int j = 0; j < s.size.Height; j++) + for (int i = 0; i < dest.size.Width; i++) + for (int j = 0; j < dest.size.Height; j++) { - Point p = new Point(s.origin.X + i, s.origin.Y + j); - byte b = src[i + s.size.Width * j]; - Color original = bitmap.GetPixel(p.X, p.Y); - bitmap.SetPixel(p.X, p.Y, ReplaceChannel(original, channel, b)); + Point p = new Point(dest.origin.X + i, dest.origin.Y + j); + byte b = src[i + dest.size.Width * j]; + Color original = dest.sheet.bitmap.GetPixel(p.X, p.Y); + dest.sheet.bitmap.SetPixel(p.X, p.Y, ReplaceChannel(original, dest.channel, b)); } } diff --git a/OpenRa.Game/World.cs b/OpenRa.Game/World.cs index 8806d98a0b..e2f70ef73f 100644 --- a/OpenRa.Game/World.cs +++ b/OpenRa.Game/World.cs @@ -37,7 +37,7 @@ namespace OpenRa.Game // assumption: when people fix these items, they might update the warning comment? - public void Draw(Renderer renderer) + public void Draw(Renderer renderer, Range xr, Range yr) { int sprites = 0; List vertices = new List(); @@ -46,10 +46,18 @@ namespace OpenRa.Game foreach (Actor a in actors) { - if (a.CurrentImages == null) + SheetRectangle[] images = a.CurrentImages; + + if (images == null) continue; - foreach (SheetRectangle image in a.CurrentImages) + if (a.location.X > xr.End || a.location.X < xr.Start - images[0].size.Width) + continue; + + if (a.location.Y > yr.End || a.location.Y < yr.Start - images[0].size.Height) + continue; + + foreach (SheetRectangle image in images) { if( image.sheet != sheet && sprites > 0 && sheet != null ) {