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.IO;
using System.Linq;
using System;
namespace OpenRA.FileFormats
{
@@ -23,6 +24,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
{
get { return colors; }
@@ -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();
}
}

View File

@@ -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
ITexture texture;
Dictionary<string, Palette> palettes;
Dictionary<string, int> 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<IPaletteModifier> 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;
}
}

View File

@@ -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<string,Palette> b); }
public interface IPips { IEnumerable<PipType> GetPips(Actor self); }
public interface ITags { IEnumerable<TagType> GetTags(); }

View File

@@ -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));

View File

@@ -10,6 +10,8 @@
using System.Drawing;
using OpenRA.Traits;
using OpenRA.FileFormats;
using System.Collections.Generic;
namespace OpenRA.Mods.RA
{
@@ -31,21 +33,26 @@ namespace OpenRA.Mods.RA
remainingFrames--;
}
public void AdjustPalette(Bitmap b)
public void AdjustPalette(Dictionary<string,Palette> palettes)
{
if (remainingFrames == 0)
return;
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++)
{
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));
}
}
}
}

View File

@@ -10,6 +10,8 @@
using System.Drawing;
using OpenRA.Traits;
using System.Collections.Generic;
using OpenRA.FileFormats;
namespace OpenRA.Mods.RA
{
@@ -22,15 +24,20 @@ namespace OpenRA.Mods.RA
t += .5f;
}
public void AdjustPalette(Bitmap b)
public void AdjustPalette(Dictionary<string,Palette> palettes)
{
var excludePalettes = new List<string>(){"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;
using (var bitmapCopy = new Bitmap(b))
for (int j = 0; j < b.Height; j++)
b.SetPixel(0x67, j, b.GetPixel(230+rotate, j));
pal.Value.SetColor(0x67, pal.Value.GetColor(230+rotate));
}
}
}
}

View File

@@ -10,6 +10,8 @@
using System.Drawing;
using OpenRA.Traits;
using System.Collections.Generic;
using OpenRA.FileFormats;
namespace OpenRA.Mods.RA
{
@@ -31,20 +33,25 @@ namespace OpenRA.Mods.RA
remainingFrames--;
}
public void AdjustPalette(Bitmap b)
public void AdjustPalette(Dictionary<string,Palette> 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++ )
var excludePalettes = new List<string>(){"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 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 OpenRA.Traits;
using System.Collections.Generic;
using OpenRA.FileFormats;
namespace OpenRA.Mods.RA
{
@@ -33,17 +35,23 @@ namespace OpenRA.Mods.RA
t += .25f;
}
public void AdjustPalette(Bitmap b)
public void AdjustPalette(Dictionary<string,Palette> palettes)
{
var excludePalettes = new List<string>(){"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;
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));
pal.Value.SetColor(0x20 + (rotate + i) % 7, copy[0x20 + i]);
else
b.SetPixel(0x60 + (rotate + i) % 7, j, bitmapCopy.GetPixel(0x60 + i, j));
pal.Value.SetColor(0x60 + (rotate + i) % 7, copy[0x60 + i]);
}
}
}
}