diff --git a/OpenRa.Game/HardwarePalette.cs b/OpenRa.Game/HardwarePalette.cs new file mode 100644 index 0000000000..79f89a986c --- /dev/null +++ b/OpenRa.Game/HardwarePalette.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Text; +using BluntDirectX.Direct3D; +using System.Drawing; +using System.IO; +using OpenRa.FileFormats; + +namespace OpenRa.Game +{ + class HardwarePalette + { + const int maxEntries = 16; // dont need anything like this many, + // but the hardware likes square textures better + + Bitmap bitmap = new Bitmap(256, maxEntries); + GraphicsDevice device; + int allocated = 0; + + Texture paletteTexture; + + public HardwarePalette(GraphicsDevice device) + { + this.device = device; + } + + public void Resolve() + { + const string filename = "../../../palette-cache.png"; + bitmap.Save(filename); + + using (Stream s = File.OpenRead(filename)) + paletteTexture = Texture.Create(s, device); + } + + public Texture PaletteTexture + { + get + { + if (paletteTexture == null) + Resolve(); + + return paletteTexture; + } + } + + public int AddPalette(Palette p) + { + for (int i = 0; i < 256; i++) + bitmap.SetPixel(i, allocated, p.GetColor(i)); + + return allocated++; + } + } +} diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index d01aa61933..9f6542433f 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -72,7 +72,13 @@ namespace OpenRa.Game foreach (TreeReference treeReference in map.Trees) world.Add(new Tree(treeReference, treeCache, map)); - UnitSheetBuilder.AddUnit( "mcv", renderer.Device, playerPal ); + UnitSheetBuilder.AddUnit( "mcv", playerPal ); + UnitSheetBuilder.AddUnit("1tnk", playerPal); + UnitSheetBuilder.AddUnit("2tnk", playerPal); + UnitSheetBuilder.AddUnit("3tnk", playerPal); + + UnitSheetBuilder.Resolve(renderer.Device); + world.Add( new Mcv( new PointF( 24 * 5, 24 * 5 ) ) ); } @@ -217,6 +223,15 @@ namespace OpenRa.Game 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")))); + + hardwarePalette.Resolve(); + return new TileSet(TileMix, TileSuffix, pal); } } diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index 7cb7e69f7b..5ad4281bfe 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -42,6 +42,7 @@ + Form diff --git a/OpenRa.Game/UnitSheetBuilder.cs b/OpenRa.Game/UnitSheetBuilder.cs index 4a5b7defa8..fa8a56a2eb 100644 --- a/OpenRa.Game/UnitSheetBuilder.cs +++ b/OpenRa.Game/UnitSheetBuilder.cs @@ -12,20 +12,30 @@ namespace OpenRa.Game static readonly Package unitsPackage = new Package( "../../../conquer.mix" ); public static readonly List> McvSheet = new List>(); - public static void AddUnit( string name, GraphicsDevice device, Palette pal ) - { - List sheets = new List(); - Size pageSize = new Size( 1024, 512 ); + static TileSheetBuilder builder; + static List sheets = new List(); + static Size pageSize = new Size(1024, 512); + static UnitSheetBuilder() + { Provider sheetProvider = delegate { - Sheet sheet = new Sheet( new Bitmap( pageSize.Width, pageSize.Height ) ); - sheets.Add( sheet ); + Sheet sheet = new Sheet(new Bitmap(pageSize.Width, pageSize.Height)); + sheets.Add(sheet); return sheet; }; - TileSheetBuilder builder = new TileSheetBuilder( pageSize, sheetProvider ); + builder = new TileSheetBuilder(pageSize, sheetProvider); + } + public static void Resolve( GraphicsDevice device ) + { + foreach (Sheet sheet in sheets) + sheet.LoadTexture(device); + } + + public static void AddUnit( string name, Palette pal ) + { ShpReader reader = new ShpReader( unitsPackage.GetContent( name + ".shp" ) ); foreach( ImageHeader h in reader ) { @@ -37,9 +47,6 @@ namespace OpenRa.Game McvSheet.Add( rect ); } - - foreach( Sheet sheet in sheets ) - sheet.LoadTexture( device ); } } }