From d0da9d11bfb457586fd8a25f3cc0816b4b5c9d81 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 14 Aug 2010 23:29:01 +1200 Subject: [PATCH] Fix palettes and all palettemods. Remap palettes broken. --- OpenRA.FileFormats/Palette.cs | 26 +++++++--- OpenRA.Game/Graphics/HardwarePalette.cs | 60 +++++++--------------- OpenRA.Game/Traits/TraitsInterfaces.cs | 3 +- OpenRA.Gl/Texture.cs | 4 +- OpenRA.Mods.RA/ChronoshiftPaletteEffect.cs | 19 ++++--- OpenRA.Mods.RA/LightPaletteRotator.cs | 25 +++++---- OpenRA.Mods.RA/NukePaletteEffect.cs | 27 ++++++---- OpenRA.Mods.RA/WaterPaletteRotation.cs | 30 +++++++---- 8 files changed, 106 insertions(+), 88 deletions(-) diff --git a/OpenRA.FileFormats/Palette.cs b/OpenRA.FileFormats/Palette.cs index 76d5155e9b..13ef0e2fc9 100644 --- a/OpenRA.FileFormats/Palette.cs +++ b/OpenRA.FileFormats/Palette.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; +using System; namespace OpenRA.FileFormats { @@ -22,6 +23,16 @@ namespace OpenRA.FileFormats { return Color.FromArgb((int)colors[index]); } + + public void SetColor(int index, Color color) + { + colors[index] = (uint)color.ToArgb(); + } + + public void SetColor(int index, uint color) + { + colors[index] = (uint)color; + } public uint[] Values { @@ -43,21 +54,22 @@ namespace OpenRA.FileFormats } } - colors[0] = 0;//Color.FromArgb(0, 0, 0, 0); - + colors[0] = 0; if (remapTransparent) { - colors[3] = (uint)178 << 24;//Color.FromArgb(178, 0, 0, 0); - colors[4] = (uint)140 << 24;//Color.FromArgb(140, 0, 0, 0); + colors[3] = (uint)178 << 24; + colors[4] = (uint)140 << 24; } } public Palette(Palette p, IPaletteRemap r) { colors = p.colors; - - //for (int i = 0; i < 256; i++) - // colors.Add(r.GetRemappedColor(p.GetColor(i), i)); + } + + public Palette(Palette p) + { + colors = (uint[])p.colors.Clone(); } } diff --git a/OpenRA.Game/Graphics/HardwarePalette.cs b/OpenRA.Game/Graphics/HardwarePalette.cs index af9521bfe0..aae51e669d 100644 --- a/OpenRA.Game/Graphics/HardwarePalette.cs +++ b/OpenRA.Game/Graphics/HardwarePalette.cs @@ -14,6 +14,7 @@ using System.Drawing; using OpenRA.FileFormats; using OpenRA.Traits; using OpenRA.FileFormats.Graphics; +using System.Linq; namespace OpenRA.Graphics { @@ -21,10 +22,8 @@ namespace OpenRA.Graphics { public const int MaxPalettes = 64; int allocated = 0; + ITexture texture; - - // We need to store the Palettes themselves for the remap palettes to work - // We should probably try to fix this somehow Dictionary palettes; Dictionary indices; @@ -37,54 +36,41 @@ namespace OpenRA.Graphics public Palette GetPalette(string name) { - try { return palettes[name]; } - catch (KeyNotFoundException) - { - throw new InvalidOperationException( - "Palette `{0}` does not exist".F(name)); - } + Palette ret; + if (!palettes.TryGetValue(name,out ret)) + throw new InvalidOperationException("Palette `{0}` does not exist".F(name)); + return ret; } public int GetPaletteIndex(string name) { - try { return indices[name]; } - catch (KeyNotFoundException) - { - throw new InvalidOperationException( - "Palette `{0}` does not exist".F(name)); - } + int ret; + if (!indices.TryGetValue(name,out ret)) + throw new InvalidOperationException("Palette `{0}` does not exist".F(name)); + return ret; } - public int AddPalette(string name, Palette p) + public void AddPalette(string name, Palette p) { + Console.WriteLine("Adding palette "+name); palettes.Add(name, p); - indices.Add(name, allocated); - /*for (int i = 0; i < 256; i++) - { - this[new Point(i, allocated)] = p.GetColor(i); - }*/ - return allocated++; + indices.Add(name, allocated++); } public void UpdatePalette(string name, Palette p) { palettes[name] = p; - var j = indices[name]; - /* - for (int i = 0; i < 256; i++) - { - this[new Point(i, j)] = p.GetColor(i); - }*/ } public void Update(IEnumerable paletteMods) { - //var b = new Bitmap(Bitmap); - //foreach (var mod in paletteMods) - // mod.AdjustPalette(b); + var copy = palettes.ToDictionary(p => p.Key, p => new Palette(p.Value)); + + foreach (var mod in paletteMods) + mod.AdjustPalette(copy); var data = new uint[MaxPalettes,256]; - foreach (var pal in palettes) + foreach (var pal in copy) { var j = indices[pal.Key]; var c = pal.Value.Values; @@ -94,16 +80,6 @@ namespace OpenRA.Graphics // Doesn't work texture.SetData(data); - /* - // Works - var foo = new Bitmap(256,MaxPalettes); - for (int j = 0; j < MaxPalettes; j++) - for (int i = 0; i < 256; i++) - foo.SetPixel(i,j,Color.FromArgb((int)data[i,j])); - - - Texture.SetData(foo); - */ Game.Renderer.PaletteTexture = texture; } } diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index af323f0420..79e5882d7a 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.Drawing; using OpenRA.GameRules; using OpenRA.Graphics; +using OpenRA.FileFormats; namespace OpenRA.Traits { @@ -95,7 +96,7 @@ namespace OpenRA.Traits public interface ISpeedModifier { float GetSpeedModifier(); } public interface IPowerModifier { float GetPowerModifier(); } public interface IFirepowerModifier { float GetFirepowerModifier(); } - public interface IPaletteModifier { void AdjustPalette(Bitmap b); } + public interface IPaletteModifier { void AdjustPalette(Dictionary b); } public interface IPips { IEnumerable GetPips(Actor self); } public interface ITags { IEnumerable GetTags(); } diff --git a/OpenRA.Gl/Texture.cs b/OpenRA.Gl/Texture.cs index fe8dc43682..d134e889ba 100644 --- a/OpenRA.Gl/Texture.cs +++ b/OpenRA.Gl/Texture.cs @@ -37,8 +37,8 @@ namespace OpenRA.GlRenderer // An array of RGBA public void SetData(uint[,] colors) { - int width = colors.GetUpperBound(0) + 1; - int height = colors.GetUpperBound(1) + 1; + int width = colors.GetUpperBound(1) + 1; + int height = colors.GetUpperBound(0) + 1; if (!IsPowerOf2(width) || !IsPowerOf2(height)) throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width,height)); diff --git a/OpenRA.Mods.RA/ChronoshiftPaletteEffect.cs b/OpenRA.Mods.RA/ChronoshiftPaletteEffect.cs index e835641d5a..717b5e659c 100644 --- a/OpenRA.Mods.RA/ChronoshiftPaletteEffect.cs +++ b/OpenRA.Mods.RA/ChronoshiftPaletteEffect.cs @@ -10,6 +10,8 @@ using System.Drawing; using OpenRA.Traits; +using OpenRA.FileFormats; +using System.Collections.Generic; namespace OpenRA.Mods.RA { @@ -31,22 +33,27 @@ namespace OpenRA.Mods.RA remainingFrames--; } - public void AdjustPalette(Bitmap b) + public void AdjustPalette(Dictionary palettes) { if (remainingFrames == 0) return; var frac = (float)remainingFrames / chronoEffectLength; - - // TODO: Fix me to only affect "world" palettes - for( var y = 0; y < b.Height; y++ ) + System.Console.WriteLine("{0}",frac); + var excludePalettes = new List(){"cursor", "chrome", "colorpicker"}; + foreach (var pal in palettes) + { + if (excludePalettes.Contains(pal.Key)) + continue; + for (var x = 0; x < 256; x++) { - var orig = b.GetPixel(x, y); + var orig = pal.Value.GetColor(x); var lum = (int)(255 * orig.GetBrightness()); var desat = Color.FromArgb(orig.A, lum, lum, lum); - b.SetPixel(x, y, OpenRA.Graphics.Util.Lerp(frac, orig, desat)); + pal.Value.SetColor(x, OpenRA.Graphics.Util.Lerp(frac, orig, desat)); } + } } } } diff --git a/OpenRA.Mods.RA/LightPaletteRotator.cs b/OpenRA.Mods.RA/LightPaletteRotator.cs index 6c89fe1448..af272ea8ba 100644 --- a/OpenRA.Mods.RA/LightPaletteRotator.cs +++ b/OpenRA.Mods.RA/LightPaletteRotator.cs @@ -10,6 +10,8 @@ using System.Drawing; using OpenRA.Traits; +using System.Collections.Generic; +using OpenRA.FileFormats; namespace OpenRA.Mods.RA { @@ -21,16 +23,21 @@ namespace OpenRA.Mods.RA { t += .5f; } - - public void AdjustPalette(Bitmap b) + + public void AdjustPalette(Dictionary palettes) { - var rotate = (int)t % 18; - if (rotate > 9) - rotate = 18 - rotate; - - using (var bitmapCopy = new Bitmap(b)) - for (int j = 0; j < b.Height; j++) - b.SetPixel(0x67, j, b.GetPixel(230+rotate, j)); + var excludePalettes = new List(){"cursor", "chrome", "colorpicker"}; + foreach (var pal in palettes) + { + if (excludePalettes.Contains(pal.Key)) + continue; + + var rotate = (int)t % 18; + if (rotate > 9) + rotate = 18 - rotate; + + pal.Value.SetColor(0x67, pal.Value.GetColor(230+rotate)); + } } } } diff --git a/OpenRA.Mods.RA/NukePaletteEffect.cs b/OpenRA.Mods.RA/NukePaletteEffect.cs index 034f30b6dc..4552ec55bc 100644 --- a/OpenRA.Mods.RA/NukePaletteEffect.cs +++ b/OpenRA.Mods.RA/NukePaletteEffect.cs @@ -10,6 +10,8 @@ using System.Drawing; using OpenRA.Traits; +using System.Collections.Generic; +using OpenRA.FileFormats; namespace OpenRA.Mods.RA { @@ -31,21 +33,26 @@ namespace OpenRA.Mods.RA remainingFrames--; } - public void AdjustPalette(Bitmap b) + public void AdjustPalette(Dictionary palettes) { if (remainingFrames == 0) return; var frac = (float)remainingFrames / nukeEffectLength; - - // TODO: Fix me to only affect "world" palettes - for( var y = 0; y < b.Height; y++ ) - for (var x = 0; x < 256; x++) - { - var orig = b.GetPixel(x, y); - var white = Color.FromArgb(orig.A, 255, 255, 255); - b.SetPixel(x, y, OpenRA.Graphics.Util.Lerp(frac, orig, white)); - } + + var excludePalettes = new List(){"cursor", "chrome", "colorpicker"}; + foreach (var pal in palettes) + { + if (excludePalettes.Contains(pal.Key)) + continue; + + for (var x = 0; x < 256; x++) + { + var orig = pal.Value.GetColor(x); + var white = Color.FromArgb(orig.A, 255, 255, 255); + pal.Value.SetColor(x, OpenRA.Graphics.Util.Lerp(frac,orig,white)); + } + } } } } diff --git a/OpenRA.Mods.RA/WaterPaletteRotation.cs b/OpenRA.Mods.RA/WaterPaletteRotation.cs index 69a99af285..40126b6def 100644 --- a/OpenRA.Mods.RA/WaterPaletteRotation.cs +++ b/OpenRA.Mods.RA/WaterPaletteRotation.cs @@ -10,6 +10,8 @@ using System.Drawing; using OpenRA.Traits; +using System.Collections.Generic; +using OpenRA.FileFormats; namespace OpenRA.Mods.RA { @@ -33,18 +35,24 @@ namespace OpenRA.Mods.RA t += .25f; } - public void AdjustPalette(Bitmap b) + public void AdjustPalette(Dictionary palettes) { - var rotate = (int)t % 7; - using (var bitmapCopy = new Bitmap(b)) - for (int j = 0; j < b.Height; j++) - for (int i = 0; i < 7; i++) - { - if (cncmode) - b.SetPixel(0x20 + (rotate + i) % 7, j, bitmapCopy.GetPixel(0x20 + i, j)); - else - b.SetPixel(0x60 + (rotate + i) % 7, j, bitmapCopy.GetPixel(0x60 + i, j)); - } + var excludePalettes = new List(){"cursor", "chrome", "colorpicker"}; + foreach (var pal in palettes) + { + if (excludePalettes.Contains(pal.Key)) + continue; + + var copy = (uint[])pal.Value.Values.Clone(); + var rotate = (int)t % 7; + for (int i = 0; i < 7; i++) + { + if (cncmode) + pal.Value.SetColor(0x20 + (rotate + i) % 7, copy[0x20 + i]); + else + pal.Value.SetColor(0x60 + (rotate + i) % 7, copy[0x60 + i]); + } + } } } }