From ff21ec16051eac51a0d5dfa5542a3066ade2269e Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 26 Oct 2013 17:47:14 +1300 Subject: [PATCH] Add PaletteFromR8 for loading embedded palettes. The frames that define a custom palette are: Frame range -> offset (mem offset) 38 -> 12007 (22063136) 178 -> 159947 (21859680) 3494 - 3501 -> 2437846 (23366680) 3502 - 3509 -> 2443858 (23422744) 3510 - 3511 -> 2459105 (23363608) 3621 - 3625 -> 2572352 (23550488) 3739 - 3742 -> 2676956 (23534796) --- OpenRA.Mods.D2k/OpenRA.Mods.D2k.csproj | 1 + OpenRA.Mods.D2k/PaletteFromR8.cs | 53 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 OpenRA.Mods.D2k/PaletteFromR8.cs diff --git a/OpenRA.Mods.D2k/OpenRA.Mods.D2k.csproj b/OpenRA.Mods.D2k/OpenRA.Mods.D2k.csproj index 01d307f517..f0c67781f0 100644 --- a/OpenRA.Mods.D2k/OpenRA.Mods.D2k.csproj +++ b/OpenRA.Mods.D2k/OpenRA.Mods.D2k.csproj @@ -79,6 +79,7 @@ + diff --git a/OpenRA.Mods.D2k/PaletteFromR8.cs b/OpenRA.Mods.D2k/PaletteFromR8.cs new file mode 100644 index 0000000000..697b31e67c --- /dev/null +++ b/OpenRA.Mods.D2k/PaletteFromR8.cs @@ -0,0 +1,53 @@ +#region Copyright & License Information +/* + * Copyright 2007-2012 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System.IO; +using OpenRA.FileFormats; +using OpenRA.Traits; +using OpenRA.Graphics; + +namespace OpenRA.Mods.RA +{ + class PaletteFromR8Info : ITraitInfo + { + [Desc("Internal palette name")] + public readonly string Name = null; + [Desc("Filename to load")] + public readonly string Filename = null; + [Desc("Palette byte offset")] + public readonly long Offset = 0; + public readonly bool AllowModifiers = true; + + public object Create(ActorInitializer init) { return new PaletteFromR8(this); } + } + + class PaletteFromR8 : IPalette + { + readonly PaletteFromR8Info info; + public PaletteFromR8(PaletteFromR8Info info) { this.info = info; } + + public void InitPalette(WorldRenderer wr) + { + var colors = new uint[256]; + using (var s = FileSystem.Open(info.Filename)) + { + s.Seek(info.Offset, SeekOrigin.Begin); + + for (var i = 0; i < 256; i++) + { + var packed = s.ReadUInt16(); + colors[i] = (uint)((255 << 24) | ((packed & 0xF800) << 8) | ((packed & 0x7E0) << 5) | ((packed & 0x1f) << 3)); + } + } + + wr.AddPalette(info.Name, new Palette(colors), info.AllowModifiers); + } + } +}