Skip to content

Commit

Permalink
Use EmbeddedSpritePalette in D2k.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote committed Oct 1, 2018
1 parent f2ed76e commit d52fbbd
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 150 deletions.
3 changes: 1 addition & 2 deletions OpenRA.Mods.D2k/OpenRA.Mods.D2k.csproj
Expand Up @@ -74,8 +74,7 @@
<Compile Include="Traits\SpiceBloom.cs" />
<Compile Include="Traits\World\BuildableTerrainLayer.cs" />
<Compile Include="Traits\World\D2kResourceLayer.cs" />
<Compile Include="Traits\World\FogPaletteFromR8.cs" />
<Compile Include="Traits\World\PaletteFromR8.cs" />
<Compile Include="Traits\World\D2kFogPalette.cs" />
<Compile Include="Traits\World\PaletteFromScaledPalette.cs" />
<Compile Include="Traits\AttractsWorms.cs" />
<Compile Include="UtilityCommands\D2kMapImporter.cs" />
Expand Down
31 changes: 28 additions & 3 deletions OpenRA.Mods.D2k/SpriteLoaders/R8Loader.cs
Expand Up @@ -12,7 +12,9 @@
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Primitives;

namespace OpenRA.Mods.D2k.SpriteLoaders
Expand All @@ -27,6 +29,8 @@ class R8Frame : ISpriteFrame
public byte[] Data { get; set; }
public bool DisableExportPadding { get { return true; } }

public readonly uint[] Palette = null;

public R8Frame(Stream s)
{
// Scan forward until we find some data
Expand Down Expand Up @@ -58,9 +62,20 @@ public R8Frame(Stream s)

Data = s.ReadBytes(width * height);

// Ignore palette
// Palette palette
if (type == 1 && paletteOffset != 0)
s.Seek(520, SeekOrigin.Current);
{
// Skip header
s.ReadUInt32();
s.ReadUInt32();

Palette = new uint[256];
for (var i = 0; i < 256; i++)
{
var packed = s.ReadUInt16();
Palette[i] = (uint)((255 << 24) | ((packed & 0xF800) << 8) | ((packed & 0x7E0) << 5) | ((packed & 0x1f) << 3));
}
}
}
}

Expand Down Expand Up @@ -94,11 +109,21 @@ public bool TryParseSprite(Stream s, out ISpriteFrame[] frames, out TypeDictiona

var start = s.Position;
var tmp = new List<R8Frame>();
var palettes = new Dictionary<int, uint[]>();
while (s.Position < s.Length)
tmp.Add(new R8Frame(s));
{
var f = new R8Frame(s);
if (f.Palette != null)
palettes.Add(tmp.Count, f.Palette);
tmp.Add(f);
}

s.Position = start;

frames = tmp.ToArray();
if (palettes.Any())
metadata = new TypeDictionary { new EmbeddedSpritePalette(framePalettes: palettes) };

return true;
}
}
Expand Down
54 changes: 54 additions & 0 deletions OpenRA.Mods.D2k/Traits/World/D2kFogPalette.cs
@@ -0,0 +1,54 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion

using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;

namespace OpenRA.Mods.D2k.Traits
{
class D2kFogPaletteInfo : ITraitInfo
{
[FieldLoader.Require, PaletteDefinition]
[Desc("Internal palette name")]
public readonly string Name = null;

[FieldLoader.Require, PaletteReference]
[Desc("The name of the shroud palette to base off.")]
public readonly string BasePalette = null;

[Desc("Allow palette modifiers to change the palette.")]
public readonly bool AllowModifiers = true;

public object Create(ActorInitializer init) { return new D2kFogPalette(this); }
}

class D2kFogPalette : ILoadsPalettes, IProvidesAssetBrowserPalettes
{
readonly D2kFogPaletteInfo info;
public D2kFogPalette(D2kFogPaletteInfo info) { this.info = info; }

public void LoadPalettes(WorldRenderer wr)
{
var basePalette = wr.Palette(info.BasePalette).Palette;

// Bit twiddling is equivalent to unpacking RGB channels, dividing them by 2, subtracting from 255, then repacking
var fog = new uint[Palette.Size];
for (var i = 0; i < Palette.Size; i++)
fog[i] = ~((basePalette[i] >> 1) & 0x007F7F7F);

wr.AddPalette(info.Name, new ImmutablePalette(fog), info.AllowModifiers);
}

public IEnumerable<string> PaletteNames { get { yield return info.Name; } }
}
}
68 changes: 0 additions & 68 deletions OpenRA.Mods.D2k/Traits/World/FogPaletteFromR8.cs

This file was deleted.

66 changes: 0 additions & 66 deletions OpenRA.Mods.D2k/Traits/World/PaletteFromR8.cs

This file was deleted.

24 changes: 13 additions & 11 deletions mods/d2k/rules/palettes.yaml
Expand Up @@ -33,11 +33,15 @@
G: 255
B: 255
A: 128
PaletteFromR8@moveflash:
PaletteFromEmbeddedSpritePalette@moveflash-base:
Name: moveflash-base
Image: moveflsh
Sequence: idle
PaletteFromScaledPalette@moveflash:
Name: moveflash
Filename: DATA.R8
Offset: 2652107
InvertColor: true
BasePalette: moveflash-base
Scale: -1
Offset: 255
AllowModifiers: false
PaletteFromRGBA@disabled:
Name: disabled
Expand All @@ -58,15 +62,13 @@
BasePalette: d2k
AllowModifiers: false
Offset: -64
PaletteFromR8@shroud:
PaletteFromEmbeddedSpritePalette@shroud:
Name: shroud
Filename: DATA.R8
Offset: 12007
FogPaletteFromR8@fog:
Image: shroud
Sequence: palette
D2kFogPalette@fog:
Name: fog
Filename: DATA.R8
Offset: 12007
InvertColor: true
BasePalette: shroud
PlayerColorPalette:
BasePalette: d2k
RemapIndex: 255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240
Expand Down
4 changes: 4 additions & 0 deletions mods/d2k/sequences/misc.yaml
Expand Up @@ -438,6 +438,7 @@ moveflsh:
Tick: 80
BlendMode: Multiply
ZOffset: 2047
EmbeddedPalette: moveflash

resources:
spice: BLOXBASE.R8
Expand All @@ -450,6 +451,9 @@ shroud:
BlendMode: Subtractive
Offset: -16,-16
Length: 14
palette: DATA.R8
Start: 38
EmbeddedPalette: shroud
shrouda: DATA.R8
Start: 40
shroudb: DATA.R8
Expand Down

0 comments on commit d52fbbd

Please sign in to comment.