Remove unnecessary indirection from palette creation.

This commit is contained in:
Paul Chote
2013-02-22 12:12:45 +13:00
parent db7887687b
commit 2d10f6b739
8 changed files with 73 additions and 61 deletions

View File

@@ -76,6 +76,13 @@ namespace OpenRA.FileFormats
colors = (uint[])p.colors.Clone();
}
public Palette(uint[] data)
{
if (data.Length != 256)
throw new InvalidDataException("Attempting to create palette with incorrect array size");
colors = (uint[])data.Clone();
}
public ColorPalette AsSystemPalette()
{
ColorPalette pal;

View File

@@ -0,0 +1,44 @@
#region Copyright & License Information
/*
* Copyright 2007-2013 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.Drawing;
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class FogPaletteInfo : ITraitInfo
{
public readonly string Name = "fog";
public object Create(ActorInitializer init) { return new FogPalette(this); }
}
class FogPalette : IPalette
{
readonly FogPaletteInfo info;
public FogPalette(FogPaletteInfo info) { this.info = info; }
public void InitPalette(WorldRenderer wr)
{
var c = new[] {
Color.Transparent, Color.Green,
Color.Blue, Color.Yellow,
Color.FromArgb(128,0,0,0),
Color.FromArgb(128,0,0,0),
Color.FromArgb(128,0,0,0),
Color.FromArgb(64,0,0,0)
};
wr.AddPalette(info.Name, new Palette(Exts.MakeArray(256, i => (uint)c[i % 8].ToArgb())), false);
}
}
}

View File

@@ -407,6 +407,7 @@
<Compile Include="Widgets\Logic\CheatsLogic.cs" />
<Compile Include="CloakPaletteEffect.cs" />
<Compile Include="Widgets\ColorPreviewManagerWidget.cs" />
<Compile Include="FogPalette.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -38,28 +38,14 @@ namespace OpenRA.Mods.RA
this.info = info;
}
public void InitPalette( WorldRenderer wr )
public void InitPalette(WorldRenderer wr)
{
if (info.Tileset == null || info.Tileset.ToLowerInvariant() == world.Map.Tileset.ToLowerInvariant())
{
// TODO: This shouldn't rely on a base palette
var pal = wr.GetPalette("terrain");
wr.AddPalette(info.Name, new Palette(pal, new SingleColorRemap(Color.FromArgb(info.A, info.R, info.G, info.B))), info.AllowModifiers);
}
}
}
// Enable palette only for a specific tileset
if (info.Tileset != null && info.Tileset.ToLowerInvariant() != world.Map.Tileset.ToLowerInvariant())
return;
class SingleColorRemap : IPaletteRemap
{
Color c;
public SingleColorRemap(Color c)
{
this.c = c;
}
public Color GetRemappedColor(Color original, int index)
{
return original.A > 0 ? c : original;
var c = (uint)((info.A << 24) | (info.R << 16) | (info.G << 8) | info.B);
wr.AddPalette(info.Name, new Palette(Exts.MakeArray(256, i => (i == 0) ? 0 : c)), info.AllowModifiers);
}
}
}

View File

@@ -18,8 +18,6 @@ namespace OpenRA.Mods.RA
class ShroudPaletteInfo : ITraitInfo
{
public readonly string Name = "shroud";
public readonly bool IsFog = false;
public object Create(ActorInitializer init) { return new ShroudPalette(this); }
}
@@ -31,34 +29,16 @@ namespace OpenRA.Mods.RA
public void InitPalette(WorldRenderer wr)
{
var pal = wr.GetPalette("terrain");
wr.AddPalette(info.Name, new Palette(pal, new ShroudPaletteRemap(info.IsFog)), false);
}
}
class ShroudPaletteRemap : IPaletteRemap
{
bool isFog;
public ShroudPaletteRemap(bool isFog) { this.isFog = isFog; }
public Color GetRemappedColor(Color original, int index)
{
if (isFog)
return new[] {
Color.Transparent, Color.Green,
Color.Blue, Color.Yellow,
Color.FromArgb(128,0,0,0),
Color.FromArgb(128,0,0,0),
Color.FromArgb(128,0,0,0),
Color.FromArgb(64,0,0,0)}[index % 8];
else
return new[] {
var c = new[] {
Color.Transparent, Color.Green,
Color.Blue, Color.Yellow,
Color.Black,
Color.FromArgb(128,0,0,0),
Color.Transparent,
Color.Transparent}[index % 8];
Color.Transparent
};
wr.AddPalette(info.Name, new Palette(Exts.MakeArray(256, i => (uint)c[i % 8].ToArgb())), false);
}
}
}

View File

@@ -109,10 +109,8 @@ World:
G: 0
B: 0
A: 180
ShroudPalette@shroud:
ShroudPalette@fog:
IsFog: yes
Name: fog
ShroudPalette:
FogPalette:
Country@gdi:
Name: GDI
Race: gdi

View File

@@ -304,10 +304,8 @@ World:
G: 0
B: 0
A: 180
ShroudPalette@shroud:
ShroudPalette@fog:
IsFog: yes
Name: fog
ShroudPalette:
FogPalette:
Country@Atreides:
Name: Atreides
Race: atreides

View File

@@ -259,10 +259,8 @@ World:
G: 0
B: 0
A: 180
ShroudPalette@shroud:
ShroudPalette@fog:
IsFog: yes
Name: fog
ShroudPalette:
FogPalette:
Country@0:
Name: Allies
Race: allies