/// <summary> /// Implement dynamic hardware palette sizing /// /// The HardwarePalette will now grow its palette buffer and texture in power-of-2 increments.. /// This avoids it having to allocate memory for a full 256*256 texture up front. /// In practice the default mods use 22 or 23 paleetes so a 32X256 texture is used. /// This means both the buffer and texture save neatly on memory. /// Additionally, HarewarePalette.ApplyModifiers sees a nice speedup as it has to transfer a much smaller amount of memory from the buffer to the texture /// /// to facilitate this change,the MaxPalettes constant is no more. /// instead the PaletteReference deals with the calculation of the index and this is passed into the appropriate methods. /// /// 实现动态的调色板寄存 /// /// HardwarePalette 现在以2的幂次增加其调色板的缓冲区和纹理大小。这避免了它必须先分配内存以获得完整的256x256纹理。 /// 实际上默认的mod 使用22或者23种调色板,因此使用了32x256的纹理。这意味着缓冲区和纹理整齐的保存在内存当中。 /// 此外HardwarePalette.ApplyModifiers 有着一个不错的加速,因为它必须从字节缓冲区传递少量的内存到纹理。 /// /// 为了促进这种变化。MaxPalettes 常数不再。PaletteReference处理调色板索引的计算,并将其传递给适当的方法。 /// </summary> /// <param name="name"></param> /// <param name="p"></param> /// <param name="allowModifiers"></param> public void AddPalette(string name, ImmutablePalette p, bool allowModifiers) { if (palettes.ContainsKey(name)) { throw new InvalidOperationException("Palette {0} has already benn defined.".F(name)); } int index = palettes.Count; indices.Add(name, index); palettes.Add(name, p); if (palettes.Count > Height) { //以2的增量增加调色板缓冲区和纹理, Height = Exts.NextPowerOf2(palettes.Count); Array.Resize(ref buffer, Height * Palette.Size * 4); } if (allowModifiers) { modifiablePalettes.Add(name, new MutablePalette(p)); } else { CopyPaletteToBuffer(index, p); } }
/// <summary> /// 添加调色板 /// </summary> /// <param name="name"></param> /// <param name="pal"></param> /// <param name="allowModifiers"></param> /// <param name="allowOverwrite"></param> public void AddPalette(string name, ImmutablePalette pal, bool allowModifiers = false, bool allowOverwrite = false) { if (allowOverwrite && palette.Contains(name)) { ReplacePalette(name, pal); } else { var oldHeight = palette.Height; palette.AddPalette(name, pal, allowModifiers); if (oldHeight != palette.Height && PaletteInvalidated != null) { PaletteInvalidated(); } } }
/// <summary> /// 替换调色板 /// </summary> /// <param name="name"></param> /// <param name="p"></param> public void ReplacePalette(string name, IPalette p) { if (modifiablePalettes.ContainsKey(name)) { CopyPaletteToBuffer(indices[name], modifiablePalettes[name] = new MutablePalette(p)); } else if (palettes.ContainsKey(name)) { CopyPaletteToBuffer(indices[name], palettes[name] = new ImmutablePalette(p)); } else { throw new InvalidOperationException("Palette '{0}' does not exist".F(name)); } CopyBufferToTexture(); }