non-working palette fail

This commit is contained in:
Paul Chote
2010-08-14 21:07:19 +12:00
parent 5c1408d4d7
commit dda6556e17
7 changed files with 73 additions and 60 deletions

View File

@@ -71,7 +71,7 @@ namespace OpenRA.FileFormats.Graphics
public interface ITexture public interface ITexture
{ {
void SetData( Bitmap bitmap ); void SetData( Bitmap bitmap );
void SetData(int[,] colors); void SetData(uint[,] colors);
} }
public interface IFont public interface IFont

View File

@@ -9,7 +9,6 @@
#endregion #endregion
using System; using System;
using System.Drawing;
using System.IO; using System.IO;
namespace OpenRA.FileFormats namespace OpenRA.FileFormats
@@ -30,7 +29,7 @@ namespace OpenRA.FileFormats
byte cbParts; byte cbParts;
int2 blocks; int2 blocks;
UInt32[] offsets; UInt32[] offsets;
int[] palette; uint[] palette;
// Stores a list of subpixels, referenced by the VPTZ chunk // Stores a list of subpixels, referenced by the VPTZ chunk
byte[] cbf; byte[] cbf;
@@ -42,7 +41,7 @@ namespace OpenRA.FileFormats
byte[] origData; byte[] origData;
// Final frame output // Final frame output
int[,] frameData; uint[,] frameData;
byte[] audioData; // audio for this frame: 22050Hz 16bit mono pcm, uncompressed. byte[] audioData; // audio for this frame: 22050Hz 16bit mono pcm, uncompressed.
public byte[] AudioData { get { return audioData; } } public byte[] AudioData { get { return audioData; } }
@@ -89,9 +88,9 @@ namespace OpenRA.FileFormats
var frameSize = NextPowerOf2(Math.Max(Width,Height)); var frameSize = NextPowerOf2(Math.Max(Width,Height));
cbf = new byte[Width*Height]; cbf = new byte[Width*Height];
cbp = new byte[Width*Height]; cbp = new byte[Width*Height];
palette = new int[numColors]; palette = new uint[numColors];
origData = new byte[2*blocks.X*blocks.Y]; origData = new byte[2*blocks.X*blocks.Y];
frameData = new int[frameSize,frameSize]; frameData = new uint[frameSize,frameSize];
var type = new String(reader.ReadChars(4)); var type = new String(reader.ReadChars(4));
Console.WriteLine(type); Console.WriteLine(type);
@@ -140,7 +139,7 @@ namespace OpenRA.FileFormats
while (reader.BaseStream.Position < end) while (reader.BaseStream.Position < end)
{ {
var type = new String(reader.ReadChars(4)); var type = new String(reader.ReadChars(4));
var length = Swap(reader.ReadUInt32()); var length = int2.Swap(reader.ReadUInt32());
switch (type) switch (type)
{ {
@@ -181,7 +180,7 @@ namespace OpenRA.FileFormats
while(reader.BaseStream.Position < end) while(reader.BaseStream.Position < end)
{ {
var type = new String(reader.ReadChars(4)); var type = new String(reader.ReadChars(4));
var length = Swap(reader.ReadUInt32()); var length = int2.Swap(reader.ReadUInt32());
switch(type) switch(type)
{ {
@@ -207,7 +206,7 @@ namespace OpenRA.FileFormats
// Chunks are aligned on even bytes; may be padded with a single null // Chunks are aligned on even bytes; may be padded with a single null
if (reader.PeekChar() == 0) reader.ReadByte(); if (reader.PeekChar() == 0) reader.ReadByte();
var type = new String(reader.ReadChars(4)); var type = new String(reader.ReadChars(4));
int subchunkLength = (int)Swap(reader.ReadUInt32()); int subchunkLength = (int)int2.Swap(reader.ReadUInt32());
switch(type) switch(type)
{ {
@@ -243,10 +242,10 @@ namespace OpenRA.FileFormats
case "CPL0": case "CPL0":
for (int i = 0; i < numColors; i++) for (int i = 0; i < numColors; i++)
{ {
byte r = reader.ReadByte(); byte r = (byte)(reader.ReadByte() << 2);
byte g = reader.ReadByte(); byte g = (byte)(reader.ReadByte() << 2);
byte b = reader.ReadByte(); byte b = (byte)(reader.ReadByte() << 2);
palette[i] = Color.FromArgb(255,ToColorByte(r),ToColorByte(g),ToColorByte(b)).ToArgb(); palette[i] = (uint)(255 << 24 | (r << 16) | (g << 8) | b);
} }
break; break;
@@ -262,7 +261,7 @@ namespace OpenRA.FileFormats
} }
int cachedFrame = -1; int cachedFrame = -1;
public int[,] FrameData { get public uint[,] FrameData { get
{ {
if (cachedFrame != currentFrame) if (cachedFrame != currentFrame)
{ {
@@ -294,16 +293,5 @@ namespace OpenRA.FileFormats
++v; ++v;
return v; return v;
} }
public byte ToColorByte(byte b)
{
return (byte)((b & 63) * 255 / 63);
}
// Change endianness of a uint32
public UInt32 Swap(UInt32 orig)
{
return (UInt32)((orig & 0xff000000) >> 24) | ((orig & 0x00ff0000) >> 8) | ((orig & 0x0000ff00) << 8) | ((orig & 0x000000ff) << 24);
}
} }
} }

View File

@@ -11,20 +11,27 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq;
namespace OpenRA.FileFormats namespace OpenRA.FileFormats
{ {
public class Palette public class Palette
{ {
List<Color> colors = new List<Color>(); uint[] colors;
public Color GetColor(int index) public Color GetColor(int index)
{ {
return colors[index]; return Color.FromArgb((int)colors[index]);
}
public uint[] Values
{
get { return colors; }
} }
public Palette(Stream s, bool remapTransparent) public Palette(Stream s, bool remapTransparent)
{ {
colors = new uint[256];
using (BinaryReader reader = new BinaryReader(s)) using (BinaryReader reader = new BinaryReader(s))
{ {
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++)
@@ -33,23 +40,25 @@ namespace OpenRA.FileFormats
byte g = (byte)(reader.ReadByte() << 2); byte g = (byte)(reader.ReadByte() << 2);
byte b = (byte)(reader.ReadByte() << 2); byte b = (byte)(reader.ReadByte() << 2);
colors.Add(Color.FromArgb(r, g, b)); colors[i] = (uint)Color.FromArgb(r,g,b).ToArgb();//(uint)(((byte)255 << 0) | (r << 16));// | (g << 8) | b);
} }
} }
colors[0] = Color.FromArgb(0, 0, 0, 0); colors[0] = 0;//Color.FromArgb(0, 0, 0, 0);
if (remapTransparent) if (remapTransparent)
{ {
colors[3] = Color.FromArgb(178, 0, 0, 0); colors[3] = (uint)178 << 24;//Color.FromArgb(178, 0, 0, 0);
colors[4] = Color.FromArgb(140, 0, 0, 0); colors[4] = (uint)140 << 24;//Color.FromArgb(140, 0, 0, 0);
} }
} }
public Palette(Palette p, IPaletteRemap r) public Palette(Palette p, IPaletteRemap r)
{ {
for (int i = 0; i < 256; i++) colors = p.colors;
colors.Add(r.GetRemappedColor(p.GetColor(i), i));
//for (int i = 0; i < 256; i++)
// colors.Add(r.GetRemappedColor(p.GetColor(i), i));
} }
} }

View File

@@ -53,5 +53,11 @@ namespace OpenRA
public float2 ToFloat2() { return new float2(X, Y); } public float2 ToFloat2() { return new float2(X, Y); }
public override string ToString() { return string.Format("{0},{1}", X, Y); } public override string ToString() { return string.Format("{0},{1}", X, Y); }
// Change endianness of a uint32
public static uint Swap(uint orig)
{
return (uint)((orig & 0xff000000) >> 24) | ((orig & 0x00ff0000) >> 8) | ((orig & 0x0000ff00) << 8) | ((orig & 0x000000ff) << 24);
}
} }
} }

View File

@@ -77,11 +77,20 @@ namespace OpenRA.Graphics
public void Update(IEnumerable<IPaletteModifier> paletteMods) public void Update(IEnumerable<IPaletteModifier> paletteMods)
{ {
var b = new Bitmap(Bitmap); //var b = new Bitmap(Bitmap);
foreach (var mod in paletteMods) //foreach (var mod in paletteMods)
mod.AdjustPalette(b); // mod.AdjustPalette(b);
Texture.SetData(b); var data = new uint[256,MaxPalettes];
foreach (var pal in palettes)
{
var j = indices[pal.Key];
var c = pal.Value.Values;
for (var i = 0; i < 256; i++)
data[i,j] = c[i];
}
Texture.SetData(data);
Game.Renderer.PaletteTexture = Texture; Game.Renderer.PaletteTexture = Texture;
} }
} }

View File

@@ -23,7 +23,7 @@ namespace OpenRA.Widgets
string cachedVideo; string cachedVideo;
float invLength; float invLength;
float2 videoOrigin, videoSize; float2 videoOrigin, videoSize;
int[,] overlay; uint[,] overlay;
bool stopped; bool stopped;
bool paused; bool paused;
@@ -56,8 +56,8 @@ namespace OpenRA.Widgets
if (!DrawOverlay) if (!DrawOverlay)
return; return;
overlay = new int[2*textureSize, 2*textureSize]; overlay = new uint[2*textureSize, 2*textureSize];
var black = Color.Black.ToArgb(); uint black = (uint)255 << 24;
for (var y = 0; y < video.Height; y++) for (var y = 0; y < video.Height; y++)
for (var x = 0; x < video.Width; x++) for (var x = 0; x < video.Width; x++)
overlay[2*y,x] = black; overlay[2*y,x] = black;

View File

@@ -28,8 +28,8 @@ namespace OpenRA.GlRenderer
SetData(bitmap); SetData(bitmap);
} }
// An array of Color.ToARGB()'s // An array of RGBA
public void SetData(int[,] colors) public void SetData(uint[,] colors)
{ {
int width = colors.GetUpperBound(0) + 1; int width = colors.GetUpperBound(0) + 1;
int height = colors.GetUpperBound(1) + 1; int height = colors.GetUpperBound(1) + 1;
@@ -37,12 +37,11 @@ namespace OpenRA.GlRenderer
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));
IntPtr intPtr;
unsafe unsafe
{ {
fixed (int* ptr = colors) fixed (uint* ptr = colors)
intPtr = new IntPtr((void *) ptr); {
} IntPtr intPtr = new IntPtr((void *) ptr);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture); Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
GraphicsDevice.CheckGlError(); GraphicsDevice.CheckGlError();
@@ -54,6 +53,8 @@ namespace OpenRA.GlRenderer
0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, intPtr); 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, intPtr);
GraphicsDevice.CheckGlError(); GraphicsDevice.CheckGlError();
} }
}
}
public void SetData(Bitmap bitmap) public void SetData(Bitmap bitmap)
{ {