Fix palettes and all palettemods. Remap palettes broken.

This commit is contained in:
Paul Chote
2010-08-14 23:29:01 +12:00
parent 0a2d39f15b
commit d0da9d11bf
8 changed files with 106 additions and 88 deletions

View File

@@ -12,6 +12,7 @@ using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System;
namespace OpenRA.FileFormats namespace OpenRA.FileFormats
{ {
@@ -23,6 +24,16 @@ namespace OpenRA.FileFormats
return Color.FromArgb((int)colors[index]); 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 public uint[] Values
{ {
get { return colors; } get { return colors; }
@@ -43,21 +54,22 @@ namespace OpenRA.FileFormats
} }
} }
colors[0] = 0;//Color.FromArgb(0, 0, 0, 0); colors[0] = 0;
if (remapTransparent) if (remapTransparent)
{ {
colors[3] = (uint)178 << 24;//Color.FromArgb(178, 0, 0, 0); colors[3] = (uint)178 << 24;
colors[4] = (uint)140 << 24;//Color.FromArgb(140, 0, 0, 0); colors[4] = (uint)140 << 24;
} }
} }
public Palette(Palette p, IPaletteRemap r) public Palette(Palette p, IPaletteRemap r)
{ {
colors = p.colors; colors = p.colors;
}
//for (int i = 0; i < 256; i++) public Palette(Palette p)
// colors.Add(r.GetRemappedColor(p.GetColor(i), i)); {
colors = (uint[])p.colors.Clone();
} }
} }

View File

@@ -14,6 +14,7 @@ using System.Drawing;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Traits; using OpenRA.Traits;
using OpenRA.FileFormats.Graphics; using OpenRA.FileFormats.Graphics;
using System.Linq;
namespace OpenRA.Graphics namespace OpenRA.Graphics
{ {
@@ -21,10 +22,8 @@ namespace OpenRA.Graphics
{ {
public const int MaxPalettes = 64; public const int MaxPalettes = 64;
int allocated = 0; int allocated = 0;
ITexture texture;
// We need to store the Palettes themselves for the remap palettes to work ITexture texture;
// We should probably try to fix this somehow
Dictionary<string, Palette> palettes; Dictionary<string, Palette> palettes;
Dictionary<string, int> indices; Dictionary<string, int> indices;
@@ -37,54 +36,41 @@ namespace OpenRA.Graphics
public Palette GetPalette(string name) public Palette GetPalette(string name)
{ {
try { return palettes[name]; } Palette ret;
catch (KeyNotFoundException) if (!palettes.TryGetValue(name,out ret))
{ throw new InvalidOperationException("Palette `{0}` does not exist".F(name));
throw new InvalidOperationException( return ret;
"Palette `{0}` does not exist".F(name));
}
} }
public int GetPaletteIndex(string name) public int GetPaletteIndex(string name)
{ {
try { return indices[name]; } int ret;
catch (KeyNotFoundException) if (!indices.TryGetValue(name,out ret))
{ throw new InvalidOperationException("Palette `{0}` does not exist".F(name));
throw new InvalidOperationException( return ret;
"Palette `{0}` does not exist".F(name));
}
} }
public int AddPalette(string name, Palette p) public void AddPalette(string name, Palette p)
{ {
Console.WriteLine("Adding palette "+name);
palettes.Add(name, p); palettes.Add(name, p);
indices.Add(name, allocated); indices.Add(name, allocated++);
/*for (int i = 0; i < 256; i++)
{
this[new Point(i, allocated)] = p.GetColor(i);
}*/
return allocated++;
} }
public void UpdatePalette(string name, Palette p) public void UpdatePalette(string name, Palette p)
{ {
palettes[name] = 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<IPaletteModifier> paletteMods) public void Update(IEnumerable<IPaletteModifier> paletteMods)
{ {
//var b = new Bitmap(Bitmap); var copy = palettes.ToDictionary(p => p.Key, p => new Palette(p.Value));
//foreach (var mod in paletteMods)
// mod.AdjustPalette(b); foreach (var mod in paletteMods)
mod.AdjustPalette(copy);
var data = new uint[MaxPalettes,256]; var data = new uint[MaxPalettes,256];
foreach (var pal in palettes) foreach (var pal in copy)
{ {
var j = indices[pal.Key]; var j = indices[pal.Key];
var c = pal.Value.Values; var c = pal.Value.Values;
@@ -94,16 +80,6 @@ namespace OpenRA.Graphics
// Doesn't work // Doesn't work
texture.SetData(data); 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; Game.Renderer.PaletteTexture = texture;
} }
} }

View File

@@ -12,6 +12,7 @@ using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using OpenRA.GameRules; using OpenRA.GameRules;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.FileFormats;
namespace OpenRA.Traits namespace OpenRA.Traits
{ {
@@ -95,7 +96,7 @@ namespace OpenRA.Traits
public interface ISpeedModifier { float GetSpeedModifier(); } public interface ISpeedModifier { float GetSpeedModifier(); }
public interface IPowerModifier { float GetPowerModifier(); } public interface IPowerModifier { float GetPowerModifier(); }
public interface IFirepowerModifier { float GetFirepowerModifier(); } public interface IFirepowerModifier { float GetFirepowerModifier(); }
public interface IPaletteModifier { void AdjustPalette(Bitmap b); } public interface IPaletteModifier { void AdjustPalette(Dictionary<string,Palette> b); }
public interface IPips { IEnumerable<PipType> GetPips(Actor self); } public interface IPips { IEnumerable<PipType> GetPips(Actor self); }
public interface ITags { IEnumerable<TagType> GetTags(); } public interface ITags { IEnumerable<TagType> GetTags(); }

View File

@@ -37,8 +37,8 @@ namespace OpenRA.GlRenderer
// An array of RGBA // An array of RGBA
public void SetData(uint[,] colors) public void SetData(uint[,] colors)
{ {
int width = colors.GetUpperBound(0) + 1; int width = colors.GetUpperBound(1) + 1;
int height = colors.GetUpperBound(1) + 1; int height = colors.GetUpperBound(0) + 1;
if (!IsPowerOf2(width) || !IsPowerOf2(height)) if (!IsPowerOf2(width) || !IsPowerOf2(height))
throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width,height)); throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width,height));

View File

@@ -10,6 +10,8 @@
using System.Drawing; using System.Drawing;
using OpenRA.Traits; using OpenRA.Traits;
using OpenRA.FileFormats;
using System.Collections.Generic;
namespace OpenRA.Mods.RA namespace OpenRA.Mods.RA
{ {
@@ -31,22 +33,27 @@ namespace OpenRA.Mods.RA
remainingFrames--; remainingFrames--;
} }
public void AdjustPalette(Bitmap b) public void AdjustPalette(Dictionary<string,Palette> palettes)
{ {
if (remainingFrames == 0) if (remainingFrames == 0)
return; return;
var frac = (float)remainingFrames / chronoEffectLength; var frac = (float)remainingFrames / chronoEffectLength;
System.Console.WriteLine("{0}",frac);
var excludePalettes = new List<string>(){"cursor", "chrome", "colorpicker"};
foreach (var pal in palettes)
{
if (excludePalettes.Contains(pal.Key))
continue;
// TODO: Fix me to only affect "world" palettes
for( var y = 0; y < b.Height; y++ )
for (var x = 0; x < 256; x++) 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 lum = (int)(255 * orig.GetBrightness());
var desat = Color.FromArgb(orig.A, lum, lum, lum); 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));
} }
}
} }
} }
} }

View File

@@ -10,6 +10,8 @@
using System.Drawing; using System.Drawing;
using OpenRA.Traits; using OpenRA.Traits;
using System.Collections.Generic;
using OpenRA.FileFormats;
namespace OpenRA.Mods.RA namespace OpenRA.Mods.RA
{ {
@@ -22,15 +24,20 @@ namespace OpenRA.Mods.RA
t += .5f; t += .5f;
} }
public void AdjustPalette(Bitmap b) public void AdjustPalette(Dictionary<string,Palette> palettes)
{ {
var rotate = (int)t % 18; var excludePalettes = new List<string>(){"cursor", "chrome", "colorpicker"};
if (rotate > 9) foreach (var pal in palettes)
rotate = 18 - rotate; {
if (excludePalettes.Contains(pal.Key))
continue;
using (var bitmapCopy = new Bitmap(b)) var rotate = (int)t % 18;
for (int j = 0; j < b.Height; j++) if (rotate > 9)
b.SetPixel(0x67, j, b.GetPixel(230+rotate, j)); rotate = 18 - rotate;
pal.Value.SetColor(0x67, pal.Value.GetColor(230+rotate));
}
} }
} }
} }

View File

@@ -10,6 +10,8 @@
using System.Drawing; using System.Drawing;
using OpenRA.Traits; using OpenRA.Traits;
using System.Collections.Generic;
using OpenRA.FileFormats;
namespace OpenRA.Mods.RA namespace OpenRA.Mods.RA
{ {
@@ -31,21 +33,26 @@ namespace OpenRA.Mods.RA
remainingFrames--; remainingFrames--;
} }
public void AdjustPalette(Bitmap b) public void AdjustPalette(Dictionary<string,Palette> palettes)
{ {
if (remainingFrames == 0) if (remainingFrames == 0)
return; return;
var frac = (float)remainingFrames / nukeEffectLength; var frac = (float)remainingFrames / nukeEffectLength;
// TODO: Fix me to only affect "world" palettes var excludePalettes = new List<string>(){"cursor", "chrome", "colorpicker"};
for( var y = 0; y < b.Height; y++ ) foreach (var pal in palettes)
{
if (excludePalettes.Contains(pal.Key))
continue;
for (var x = 0; x < 256; x++) for (var x = 0; x < 256; x++)
{ {
var orig = b.GetPixel(x, y); var orig = pal.Value.GetColor(x);
var white = Color.FromArgb(orig.A, 255, 255, 255); var white = Color.FromArgb(orig.A, 255, 255, 255);
b.SetPixel(x, y, OpenRA.Graphics.Util.Lerp(frac, orig, white)); pal.Value.SetColor(x, OpenRA.Graphics.Util.Lerp(frac,orig,white));
} }
}
} }
} }
} }

View File

@@ -10,6 +10,8 @@
using System.Drawing; using System.Drawing;
using OpenRA.Traits; using OpenRA.Traits;
using System.Collections.Generic;
using OpenRA.FileFormats;
namespace OpenRA.Mods.RA namespace OpenRA.Mods.RA
{ {
@@ -33,18 +35,24 @@ namespace OpenRA.Mods.RA
t += .25f; t += .25f;
} }
public void AdjustPalette(Bitmap b) public void AdjustPalette(Dictionary<string,Palette> palettes)
{ {
var rotate = (int)t % 7; var excludePalettes = new List<string>(){"cursor", "chrome", "colorpicker"};
using (var bitmapCopy = new Bitmap(b)) foreach (var pal in palettes)
for (int j = 0; j < b.Height; j++) {
for (int i = 0; i < 7; i++) if (excludePalettes.Contains(pal.Key))
{ continue;
if (cncmode)
b.SetPixel(0x20 + (rotate + i) % 7, j, bitmapCopy.GetPixel(0x20 + i, j)); var copy = (uint[])pal.Value.Values.Clone();
else var rotate = (int)t % 7;
b.SetPixel(0x60 + (rotate + i) % 7, j, bitmapCopy.GetPixel(0x60 + i, j)); 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]);
}
}
} }
} }
} }