From 866b45d3a7566ec449254f97bdd6c8e47b997a59 Mon Sep 17 00:00:00 2001 From: bob Date: Thu, 5 Jul 2007 11:38:48 +0000 Subject: [PATCH] git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1102 993157c7-ee19-0410-b2c4-bb4e9862e678 --- OpenRa.FileFormats/IniFile.cs | 12 ++++++- OpenRa.FileFormats/Map.cs | 27 +++++++++++++++ OpenRa.FileFormats/TileSet.cs | 2 ++ ShpViewer/MapViewControl.cs | 63 +++++++++++++++++++++++++++++++++-- ShpViewer/ShpViewForm.cs | 24 +------------ 5 files changed, 102 insertions(+), 26 deletions(-) diff --git a/OpenRa.FileFormats/IniFile.cs b/OpenRa.FileFormats/IniFile.cs index 144fa018d2..589c3a161d 100644 --- a/OpenRa.FileFormats/IniFile.cs +++ b/OpenRa.FileFormats/IniFile.cs @@ -63,7 +63,7 @@ namespace OpenRa.FileFormats } } - public class IniSection + public class IniSection : IEnumerable> { string name; Dictionary values = new Dictionary(); @@ -83,5 +83,15 @@ namespace OpenRa.FileFormats string s; return values.TryGetValue( key, out s ) ? s : defaultValue; } + + public IEnumerator> GetEnumerator() + { + return values.GetEnumerator(); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } } } diff --git a/OpenRa.FileFormats/Map.cs b/OpenRa.FileFormats/Map.cs index ef1e935773..6b1b1d24bb 100644 --- a/OpenRa.FileFormats/Map.cs +++ b/OpenRa.FileFormats/Map.cs @@ -17,6 +17,7 @@ namespace OpenRa.FileFormats public readonly int Height; public readonly TileReference[ , ] MapTiles = new TileReference[ 128, 128 ]; + public readonly List Trees = new List(); public Map( IniFile file ) { @@ -35,6 +36,8 @@ namespace OpenRa.FileFormats MemoryStream ms = ReadMapPack( file ); UnpackTileData( ms ); + + ReadTrees( file ); } static MemoryStream ReadMapPack( IniFile file ) @@ -104,6 +107,16 @@ namespace OpenRa.FileFormats for( int j = 0 ; j < 128 ; j++ ) MapTiles[ j, i ].image = ReadByte( ms ); } + + void ReadTrees( IniFile file ) + { + IniSection terrain = file.GetSection( "TERRAIN" ); + foreach( KeyValuePair kv in terrain ) + { + int xy = int.Parse( kv.Key ); + Trees.Add( new TreeReference( xy % 128, xy / 128, kv.Value ) ); + } + } } public struct TileReference @@ -111,4 +124,18 @@ namespace OpenRa.FileFormats public ushort tile; public byte image; } + + public struct TreeReference + { + public readonly int X; + public readonly int Y; + public readonly string Image; + + public TreeReference( int x, int y, string image ) + { + X = x; + Y = y; + Image = image; + } + } } diff --git a/OpenRa.FileFormats/TileSet.cs b/OpenRa.FileFormats/TileSet.cs index b14432c583..1d534c70cb 100644 --- a/OpenRa.FileFormats/TileSet.cs +++ b/OpenRa.FileFormats/TileSet.cs @@ -9,9 +9,11 @@ namespace OpenRa.FileFormats public class TileSet { public readonly Dictionary tiles = new Dictionary(); + public readonly Package MixFile; public TileSet( Package mixFile, string suffix, Palette pal ) { + MixFile = mixFile; StreamReader tileIdFile = File.OpenText( "../../../tileSet.til" ); while( true ) diff --git a/ShpViewer/MapViewControl.cs b/ShpViewer/MapViewControl.cs index c644b63512..4d5150795d 100644 --- a/ShpViewer/MapViewControl.cs +++ b/ShpViewer/MapViewControl.cs @@ -4,6 +4,7 @@ using System.Text; using System.Windows.Forms; using OpenRa.FileFormats; using System.Drawing; +using System.IO; namespace ShpViewer { @@ -11,8 +12,22 @@ namespace ShpViewer { public int XScroll, YScroll; - public Map Map; - public TileSet TileSet; + Map map; + public Map Map + { + get { return map; } + set + { + map = value; + TileSet = LoadTileSet( Map ); + } + } + + Palette pal; + TileSet TileSet; + Package TileMix; + string TileSuffix; + Dictionary TreeCache = new Dictionary(); public MapViewControl() { @@ -62,6 +77,50 @@ namespace ShpViewer } } } + + foreach( TreeReference tr in Map.Trees ) + { + int tX = tr.X - Map.XOffset - XScroll; + int tY = tr.Y - Map.YOffset - YScroll; + g.DrawImage( GetTree( tr.Image, TileMix ), tX * 24, tY * 24 ); + } + } + + Bitmap GetTree( string name, Package mix ) + { + Bitmap ret; + if( !TreeCache.TryGetValue( name, out ret ) ) + { + ShpReader shp = new ShpReader( TileSet.MixFile.GetContent( name + TileSuffix ) ); + ret = BitmapBuilder.FromBytes( shp[ 0 ].Image, shp.Width, shp.Height, pal ); ; + TreeCache.Add( name, ret ); + } + return ret; + } + + TileSet LoadTileSet( Map currentMap ) + { + switch( currentMap.Theater.ToLowerInvariant() ) + { + case "temperate": + pal = new Palette( File.OpenRead( "../../../temperat.pal" ) ); + TileMix = new Package( "../../../temperat.mix" ); + TileSuffix = ".tem"; + break; + case "snow": + pal = new Palette( File.OpenRead( "../../../snow.pal" ) ); + TileMix = new Package( "../../../snow.mix" ); + TileSuffix = ".sno"; + break; + case "interior": + pal = new Palette( File.OpenRead( "../../../interior.pal" ) ); + TileMix = new Package( "../../../interior.mix" ); + TileSuffix = ".int"; + break; + default: + throw new NotImplementedException(); + } + return new TileSet( TileMix, TileSuffix, pal ); } } } diff --git a/ShpViewer/ShpViewForm.cs b/ShpViewer/ShpViewForm.cs index 25200eeb49..07706b9432 100644 --- a/ShpViewer/ShpViewForm.cs +++ b/ShpViewer/ShpViewForm.cs @@ -57,13 +57,11 @@ namespace ShpViewer { IniFile iniFile = new IniFile( File.OpenRead( filename ) ); Map map = new Map( iniFile ); - TileSet tileSet = LoadTileSet( map ); flowLayoutPanel1.Visible = false; flowLayoutPanel1.BackColor = Color.Blue; mapViewControl1.Visible = true; mapViewControl1.Map = map; - mapViewControl1.TileSet = tileSet; mapViewControl1.Invalidate(); int ux = 0, uy = 0; @@ -104,9 +102,8 @@ namespace ShpViewer if( e.Button == MouseButtons.Left ) { mapViewControl1.Map = new Map( iniFile ); - mapViewControl1.TileSet = LoadTileSet( map ); + mapViewControl1.Invalidate(); } - mapViewControl1.Invalidate(); }; } @@ -121,24 +118,5 @@ namespace ShpViewer Focus(); BringToFront(); } - - TileSet LoadTileSet( Map currentMap ) - { - Palette pal; - switch( currentMap.Theater.ToLowerInvariant() ) - { - case "temperate": - pal = new Palette( File.OpenRead( "../../../temperat.pal" ) ); - return new TileSet( new Package( "../../../temperat.mix" ), ".tem", pal ); - case "snow": - pal = new Palette( File.OpenRead( "../../../snow.pal" ) ); - return new TileSet( new Package( "../../../snow.mix" ), ".sno", pal ); - case "interior": - pal = new Palette( File.OpenRead( "../../../interior.pal" ) ); - return new TileSet( new Package( "../../../interior.mix" ), ".int", pal ); - } - - throw new NotImplementedException(); - } } }