diff --git a/OpenRa.FileFormats/BitmapBuilder.cs b/OpenRa.FileFormats/BitmapBuilder.cs deleted file mode 100644 index f80370d956..0000000000 --- a/OpenRa.FileFormats/BitmapBuilder.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Drawing; - -namespace OpenRa.FileFormats -{ - public static class BitmapBuilder - { - public static Bitmap FromBytes(byte[] imageBytes, Size size, Palette pal) - { - Bitmap bitmap = new Bitmap(size.Width, size.Height); - for (int x = 0; x < size.Width; x++) - for (int y = 0; y < size.Height; y++) - bitmap.SetPixel(x, y, pal.GetColor(imageBytes[x + size.Width * y])); - - return bitmap; - } - } -} diff --git a/OpenRa.FileFormats/Map.cs b/OpenRa.FileFormats/Map.cs index b5704ac475..83d1c811b6 100644 --- a/OpenRa.FileFormats/Map.cs +++ b/OpenRa.FileFormats/Map.cs @@ -19,65 +19,69 @@ namespace OpenRa.FileFormats public readonly TileReference[ , ] MapTiles = new TileReference[ 128, 128 ]; public readonly List Trees = new List(); - public Map( IniFile file ) + static string Truncate( string s, int maxLength ) { - IniSection basic = file.GetSection( "Basic" ); - Title = basic.GetValue( "Name", "(null)" ); - - IniSection map = file.GetSection( "Map" ); - Theater = map.GetValue( "Theater", "TEMPERATE" ); - - XOffset = int.Parse( map.GetValue( "X", "0" ) ); - YOffset = int.Parse( map.GetValue( "Y", "0" ) ); - - Width = int.Parse( map.GetValue( "Width", "0" ) ); - Height = int.Parse( map.GetValue( "Height", "0" ) ); - - MemoryStream ms = ReadMapPack( file ); - - UnpackTileData( ms ); - - ReadTrees( file ); + return s.Length <= maxLength ? s : s.Substring(0,maxLength ); } - static MemoryStream ReadMapPack( IniFile file ) + public string TileSuffix { get { return "." + Truncate(Theater, 3); } } + + public Map(IniFile file) { - IniSection mapPackSection = file.GetSection( "MapPack" ); + IniSection basic = file.GetSection("Basic"); + Title = basic.GetValue("Name", "(null)"); + + IniSection map = file.GetSection("Map"); + Theater = Truncate(map.GetValue("Theater", "TEMPERATE"), 8); + + XOffset = int.Parse(map.GetValue("X", "0")); + YOffset = int.Parse(map.GetValue("Y", "0")); + + Width = int.Parse(map.GetValue("Width", "0")); + Height = int.Parse(map.GetValue("Height", "0")); + + UnpackTileData(ReadMapPack(file)); + ReadTrees(file); + } + + static MemoryStream ReadMapPack(IniFile file) + { + IniSection mapPackSection = file.GetSection("MapPack"); StringBuilder sb = new StringBuilder(); - for( int i = 1 ; ; i++ ) + for (int i = 1; ; i++) { - string line = mapPackSection.GetValue( i.ToString(), null ); - if( line == null ) + string line = mapPackSection.GetValue(i.ToString(), null); + if (line == null) break; - sb.Append( line.Trim() ); + sb.Append(line.Trim()); } - byte[] data = Convert.FromBase64String( sb.ToString() ); + byte[] data = Convert.FromBase64String(sb.ToString()); List chunks = new List(); - BinaryReader reader = new BinaryReader( new MemoryStream( data ) ); + BinaryReader reader = new BinaryReader(new MemoryStream(data)); try { - while( true ) + while (true) { uint length = reader.ReadUInt32() & 0xdfffffff; - byte[] dest = new byte[ 8192 ]; - byte[] src = reader.ReadBytes( (int)length ); + byte[] dest = new byte[8192]; + byte[] src = reader.ReadBytes((int)length); - int actualLength = Format80.DecodeInto( new MemoryStream( src ), dest ); + int actualLength = Format80.DecodeInto(new MemoryStream(src), dest); - chunks.Add( dest ); + chunks.Add(dest); } } - catch( EndOfStreamException ) { } + catch (EndOfStreamException) { } MemoryStream ms = new MemoryStream(); - foreach( byte[] chunk in chunks ) - ms.Write( chunk, 0, chunk.Length ); + foreach (byte[] chunk in chunks) + ms.Write(chunk, 0, chunk.Length); ms.Position = 0; @@ -95,33 +99,26 @@ namespace OpenRa.FileFormats void UnpackTileData( MemoryStream ms ) { for( int i = 0 ; i < 128 ; i++ ) - { for( int j = 0 ; j < 128 ; j++ ) { MapTiles[ j, i ].tile = ReadByte( ms ); MapTiles[ j, i ].tile |= (ushort)( ReadByte( ms ) << 8 ); } - } for( int i = 0 ; i < 128 ; i++ ) - { for( int j = 0 ; j < 128 ; j++ ) { MapTiles[ j, i ].image = ReadByte( ms ); if( MapTiles[ j, i ].tile == 0xff || MapTiles[ j, i ].tile == 0xffff ) MapTiles[ j, i ].image = (byte)( i % 4 + ( j % 4 ) * 4 ); } - } } 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 ) ); - } + Trees.Add( new TreeReference( int.Parse( kv.Key ), kv.Value ) ); } } @@ -130,10 +127,7 @@ namespace OpenRa.FileFormats public ushort tile; public byte image; - public override int GetHashCode() - { - return tile.GetHashCode() ^ image.GetHashCode(); - } + public override int GetHashCode() { return tile.GetHashCode() ^ image.GetHashCode(); } public override bool Equals(object obj) { @@ -144,15 +138,8 @@ namespace OpenRa.FileFormats return (r.image == image && r.tile == tile); } - public static bool operator ==(TileReference a, TileReference b) - { - return a.Equals(b); - } - - public static bool operator !=(TileReference a, TileReference b) - { - return !a.Equals(b); - } + public static bool operator ==(TileReference a, TileReference b) { return a.Equals(b); } + public static bool operator !=(TileReference a, TileReference b) { return !a.Equals(b); } } public struct TreeReference @@ -161,10 +148,10 @@ namespace OpenRa.FileFormats public readonly int Y; public readonly string Image; - public TreeReference( int x, int y, string image ) + public TreeReference(int xy, string image) { - X = x; - Y = y; + X = xy % 128; + Y = xy / 128; Image = image; } } diff --git a/OpenRa.FileFormats/OpenRa.FileFormats.csproj b/OpenRa.FileFormats/OpenRa.FileFormats.csproj index dfaa075a4f..f328bd7d6b 100644 --- a/OpenRa.FileFormats/OpenRa.FileFormats.csproj +++ b/OpenRa.FileFormats/OpenRa.FileFormats.csproj @@ -35,7 +35,6 @@ - diff --git a/OpenRa.FileFormats/Terrain.cs b/OpenRa.FileFormats/Terrain.cs index 106cca99bf..dba709d3c6 100644 --- a/OpenRa.FileFormats/Terrain.cs +++ b/OpenRa.FileFormats/Terrain.cs @@ -10,7 +10,7 @@ namespace OpenRa.FileFormats { public readonly List TileBitmapBytes = new List(); - public Terrain( Stream stream, Palette pal ) + public Terrain( Stream stream ) { int Width, Height, XDim, YDim, NumTiles; diff --git a/OpenRa.FileFormats/TileSet.cs b/OpenRa.FileFormats/TileSet.cs index db5948be7b..aa6dcc513f 100644 --- a/OpenRa.FileFormats/TileSet.cs +++ b/OpenRa.FileFormats/TileSet.cs @@ -11,7 +11,7 @@ namespace OpenRa.FileFormats public readonly Dictionary tiles = new Dictionary(); public readonly Package MixFile; - public TileSet( Package mixFile, string suffix, Palette pal ) + public TileSet( Package mixFile, string suffix ) { MixFile = mixFile; StreamReader tileIdFile = File.OpenText( "../../../tileSet.til" ); @@ -32,7 +32,7 @@ namespace OpenRa.FileFormats { Stream s = mixFile.GetContent(string.Format(pattern, i + 1)); if (!tiles.ContainsKey((ushort)(start + i))) - tiles.Add((ushort)(start + i), new Terrain(s, pal)); + tiles.Add((ushort)(start + i), new Terrain(s)); } catch { } } diff --git a/OpenRa.Game/HardwarePalette.cs b/OpenRa.Game/HardwarePalette.cs index 852a31c77b..bb4e91f8df 100644 --- a/OpenRa.Game/HardwarePalette.cs +++ b/OpenRa.Game/HardwarePalette.cs @@ -19,9 +19,16 @@ namespace OpenRa.Game Texture paletteTexture; - public HardwarePalette(GraphicsDevice device) + public HardwarePalette(GraphicsDevice device, Map map) { this.device = device; + + Palette pal = new Palette(File.OpenRead("../../../" + map.Theater + ".pal")); + AddPalette(pal); + + foreach (string remap in new string[] { "blue", "red", "orange", "teal", "salmon", "green", "gray" }) + AddPalette(new Palette(pal, new PaletteRemap( + File.OpenRead("../../../" + remap + ".rem")))); } void Resolve() @@ -44,7 +51,7 @@ namespace OpenRa.Game } } - public int AddPalette(Palette p) + int AddPalette(Palette p) { for (int i = 0; i < 256; i++) bitmap.SetPixel(i, allocated, p.GetColor(i)); diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 24a19668f1..32e0019307 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -13,36 +13,37 @@ namespace OpenRa.Game class MainWindow : Form { readonly Renderer renderer; - readonly Map map; - readonly TileSet tileSet; - Palette pal, playerPal; Package TileMix; - string TileSuffix; - - Dictionary> tileMapping = - new Dictionary>(); - - FvfVertexBuffer vertexBuffer; - - Dictionary drawBatches = new Dictionary(); World world; TreeCache treeCache; + TerrainRenderer terrain; - void LoadTextures() + static Size GetResolution(Settings settings) { - Size tileSize = new Size(24, 24); + Size desktopResolution = Screen.PrimaryScreen.Bounds.Size; - for (int i = 0; i < map.Width; i++) - for (int j = 0; j < map.Height; j++) - { - TileReference tileRef = map.MapTiles[i + map.XOffset, j + map.YOffset]; + return new Size(settings.GetValue("width", desktopResolution.Width), + settings.GetValue("height", desktopResolution.Height)); + } - if (!tileMapping.ContainsKey(tileRef)) - tileMapping.Add(tileRef, CoreSheetBuilder.Add(tileSet.GetBytes(tileRef), tileSize)); - } + public MainWindow( Settings settings ) + { + FormBorderStyle = FormBorderStyle.None; + BackColor = Color.Black; + renderer = new Renderer(this, GetResolution(settings), false); + Visible = true; + + CoreSheetBuilder.Initialize(renderer.Device); + + map = new Map(new IniFile(File.OpenRead("../../../" + settings.GetValue("map", "scm12ea.ini")))); + + TileMix = new Package("../../../" + map.Theater + ".mix"); + + renderer.SetPalette(new HardwarePalette(renderer.Device, map)); + terrain = new TerrainRenderer(renderer, map, TileMix); world = new World(renderer.Device); treeCache = new TreeCache(renderer.Device, map, TileMix); @@ -60,63 +61,6 @@ namespace OpenRa.Game world.Add(new Mcv(new PointF(24 * 9, 24 * 5), 1)); } - void LoadVertexBuffer() - { - Dictionary> indexMap = new Dictionary>(); - List vertices = new List(); - - for (int j = 0; j < map.Height; j++) - for (int i = 0; i < map.Width; i++) - { - SheetRectangle tile = tileMapping[map.MapTiles[i + map.XOffset, j + map.YOffset]]; - - List indexList; - if (!indexMap.TryGetValue(tile.sheet, out indexList)) - indexMap.Add(tile.sheet, indexList = new List()); - - Util.CreateQuad(vertices, indexList, new PointF(24 * i, 24 * j), tile, 0); - } - - vertexBuffer = new FvfVertexBuffer(renderer.Device, vertices.Count, Vertex.Format); - vertexBuffer.SetData(vertices.ToArray()); - - foreach (KeyValuePair> p in indexMap) - { - IndexBuffer indexBuffer = new IndexBuffer(renderer.Device, p.Value.Count); - indexBuffer.SetData(p.Value.ToArray()); - drawBatches.Add(p.Key, indexBuffer); - } - } - - static Size GetResolution(Settings settings) - { - Size desktopResolution = Screen.PrimaryScreen.Bounds.Size; - - return new Size(settings.GetValue("width", desktopResolution.Width), - settings.GetValue("height", desktopResolution.Height)); - } - - public MainWindow( Settings settings ) - { - FormBorderStyle = FormBorderStyle.None; - 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)); - map = new Map(mapFile); - - Text = string.Format("OpenRA - {0} - {1}", map.Title, mapName); - - tileSet = LoadTileSet(map); - - LoadTextures(); - LoadVertexBuffer(); - } - internal void Run() { while (Created && Visible) @@ -126,8 +70,7 @@ namespace OpenRa.Game } } - PointF scrollPos = new PointF(1, 5); - PointF oldPos; + PointF scrollPos, oldPos; int x1,y1; protected override void OnMouseDown(MouseEventArgs e) @@ -158,8 +101,7 @@ namespace OpenRa.Game renderer.BeginFrame(r1, r2, scrollPos); - foreach (KeyValuePair batch in drawBatches) - DrawTerrainBatch(batch); + terrain.Draw( ClientSize, scrollPos ); world.Draw(renderer, new Range(scrollPos.X, scrollPos.X + ClientSize.Width), @@ -167,54 +109,5 @@ namespace OpenRa.Game renderer.EndFrame(); } - - void DrawTerrainBatch(KeyValuePair batch) - { - int indicesPerRow = map.Width * 6; - int verticesPerRow = map.Width * 4; - - int visibleRows = (int)(ClientSize.Height / 24.0f + 2); - - int firstRow = (int)(scrollPos.Y / 24.0f); - int lastRow = firstRow + visibleRows; - - if (lastRow < 0 || firstRow > map.Height) - return; - - if (firstRow < 0) firstRow = 0; - if (lastRow > map.Height) lastRow = map.Height; - - renderer.DrawWithShader(ShaderQuality.Low, delegate - { - renderer.DrawBatch(vertexBuffer, batch.Value, - new Range(verticesPerRow * firstRow, verticesPerRow * lastRow), - new Range(indicesPerRow * firstRow, indicesPerRow * lastRow), - batch.Key.Texture); - }); - } - - TileSet LoadTileSet(Map currentMap) - { - string theaterName = currentMap.Theater; - if (theaterName.Length > 8) - theaterName = theaterName.Substring(0, 8); - - pal = new Palette(File.OpenRead("../../../" + theaterName + ".pal")); - TileMix = new Package("../../../" + theaterName + ".mix"); - TileSuffix = "." + theaterName.Substring(0, 3); - - playerPal = new Palette(pal, new PaletteRemap(File.OpenRead("../../../red.rem"))); - - HardwarePalette hardwarePalette = new HardwarePalette(renderer.Device); - hardwarePalette.AddPalette(pal); - - foreach (string remap in new string[] { "blue", "red", "orange", "teal", "salmon", "green", "gray" }) - hardwarePalette.AddPalette(new Palette(pal, new PaletteRemap( - File.OpenRead("../../../" + remap + ".rem")))); - - renderer.SetPalette(hardwarePalette); - - return new TileSet(TileMix, TileSuffix, pal); - } } } diff --git a/OpenRa.Game/Mcv.cs b/OpenRa.Game/Mcv.cs index 590b68eeac..7d2fe7e68d 100644 --- a/OpenRa.Game/Mcv.cs +++ b/OpenRa.Game/Mcv.cs @@ -23,7 +23,7 @@ namespace OpenRa.Game public override SheetRectangle[] CurrentImages { - get { return new SheetRectangle[] { UnitSheetBuilder.McvSheet[ GetFacing() ] }; } + get { return new SheetRectangle[] { UnitSheetBuilder.McvSheet[GetFacing()] }; } } } } diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index f14f7545cf..75a8d94b80 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -54,6 +54,7 @@ + diff --git a/OpenRa.Game/TerrainRenderer.cs b/OpenRa.Game/TerrainRenderer.cs new file mode 100644 index 0000000000..4322c1edb6 --- /dev/null +++ b/OpenRa.Game/TerrainRenderer.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Text; +using OpenRa.FileFormats; +using BluntDirectX.Direct3D; +using System.Drawing; +using System.IO; + +namespace OpenRa.Game +{ + class TerrainRenderer + { + FvfVertexBuffer vertexBuffer; + IndexBuffer indexBuffer; + Sheet terrainSheet; + TileSet tileSet; + + Renderer renderer; + Map map; + + public TerrainRenderer(Renderer renderer, Map map, Package tilePackage) + { + this.renderer = renderer; + this.map = map; + + tileSet = new TileSet(tilePackage, map.TileSuffix); + + Dictionary> tileMapping = + new Dictionary>(); + + Size tileSize = new Size(24, 24); + + List vertices = new List(); + List indices = new List(); + + for (int j = 0; j < map.Height; j++) + for (int i = 0; i < map.Width; i++) + { + TileReference tileRef = map.MapTiles[i + map.XOffset, j + map.YOffset]; + SheetRectangle tile; + + if (!tileMapping.TryGetValue(tileRef, out tile)) + tileMapping.Add(tileRef, tile = CoreSheetBuilder.Add(tileSet.GetBytes(tileRef), tileSize)); + + terrainSheet = tile.sheet; + + Util.CreateQuad(vertices, indices, new PointF(24 * i, 24 * j), tile, 0); + } + + vertexBuffer = new FvfVertexBuffer(renderer.Device, vertices.Count, Vertex.Format); + vertexBuffer.SetData(vertices.ToArray()); + + indexBuffer = new IndexBuffer(renderer.Device, indices.Count); + indexBuffer.SetData(indices.ToArray()); + } + + public void Draw( Size screenSize, PointF scrollPos ) + { + int indicesPerRow = map.Width * 6; + int verticesPerRow = map.Width * 4; + + int visibleRows = (int)(screenSize.Height / 24.0f + 2); + + int firstRow = (int)(scrollPos.Y / 24.0f); + int lastRow = firstRow + visibleRows; + + if (lastRow < 0 || firstRow > map.Height) + return; + + if (firstRow < 0) firstRow = 0; + if (lastRow > map.Height) lastRow = map.Height; + + renderer.DrawWithShader(ShaderQuality.Low, delegate + { + renderer.DrawBatch(vertexBuffer, indexBuffer, + new Range(verticesPerRow * firstRow, verticesPerRow * lastRow), + new Range(indicesPerRow * firstRow, indicesPerRow * lastRow), + terrainSheet.Texture); + }); + } + } +} diff --git a/OpenRa.TechTree/Item.cs b/OpenRa.TechTree/Item.cs index 97102d7bca..9a15354410 100644 --- a/OpenRa.TechTree/Item.cs +++ b/OpenRa.TechTree/Item.cs @@ -124,38 +124,6 @@ namespace OpenRa.TechTree bool canBuild; public bool CanBuild { get { return canBuild; } } - - Bitmap icon; - public Bitmap Icon - { - get { return icon ?? (icon = LoadIcon(tag)); } - } - - static Package package = new Package("../../../hires.mix"); - static Palette palette = new Palette(File.OpenRead("../../../temperat.pal")); - - static Bitmap LoadIcon(string tag) - { - string filename = tag + "icon.shp"; - - try - { - Stream s = package.GetContent(filename); - ShpReader reader = new ShpReader(s); - foreach (ImageHeader h in reader) - return BitmapBuilder.FromBytes(h.Image, reader.Size, palette); - - return null; - } - catch (FileNotFoundException) { return LoadIcon("dog"); } - } - - public string Tooltip - { - get - { - return string.Format("{0} ({1})\n{2}", friendlyName, tag, owner); - } - } + public string Tooltip { get { return string.Format("{0} ({1})\n{2}", friendlyName, tag, owner); } } } }