From ab3f7de52ce75a4200602fac27ec19fa6f1a0c53 Mon Sep 17 00:00:00 2001 From: Brett Sanderson Date: Sat, 5 Mar 2016 17:01:16 -0500 Subject: [PATCH] Refactor --- CdgLib/GraphicsFile.cs | 20 ++++++++--------- CdgLib/SubCode/Command.cs | 4 ++-- CdgLib/SubCode/Instruction.cs | 2 +- CdgLib/SubCode/Packet.cs | 42 ++++++++++++++++++----------------- 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/CdgLib/GraphicsFile.cs b/CdgLib/GraphicsFile.cs index 6f61132..d019a2d 100644 --- a/CdgLib/GraphicsFile.cs +++ b/CdgLib/GraphicsFile.cs @@ -13,14 +13,14 @@ namespace CdgLib { private const int ColourTableSize = 16; - private const int CdgPacketSize = 24; + private const int PacketSize = 24; private const int TileHeight = 12; private const int TileWidth = 6; public const int FullWidth = 300; public const int FullHeight = 216; - private readonly int[] _mColourTable = new int[ColourTableSize]; - private readonly byte[,] _mPixelColours = new byte[FullHeight, FullWidth]; + private readonly int[] _colourTable = new int[ColourTableSize]; + private readonly byte[,] _pixelColours = new byte[FullHeight, FullWidth]; private long _previousPosition; @@ -31,13 +31,13 @@ namespace CdgLib public bool Transparent => true; - public long Duration => Length/CdgPacketSize*1000/300; + public long Duration => Length/PacketSize*1000/300; public async Task RenderAtTime(long position = -1) { if (position < 0) { - position = _previousPosition + CdgPacketSize; + position = _previousPosition + PacketSize; } if (position < _previousPosition) @@ -68,12 +68,12 @@ namespace CdgLib private async Task> ReadSubCodeAsync(long numberOfPackets) { var subCodePackets = new List(); - var buffer = new byte[CdgPacketSize*numberOfPackets]; + var buffer = new byte[PacketSize*numberOfPackets]; var bytesRead = await ReadAsync(buffer, 0, buffer.Length); - for (var i = 0; i < bytesRead/CdgPacketSize; i++) + for (var i = 0; i < bytesRead/PacketSize; i++) { - var subCodePacket = new Packet(buffer.Skip(i* CdgPacketSize).Take(CdgPacketSize).ToArray()); + var subCodePacket = new Packet(buffer.Skip(i* PacketSize).Take(PacketSize).ToArray()); subCodePackets.Add(subCodePacket); } return subCodePackets; @@ -92,11 +92,11 @@ namespace CdgLib if (ri < TileHeight || ri >= FullHeight - TileHeight || ci < TileWidth || ci >= FullWidth - TileWidth) { - // _mPSurface.RgbData[ri, ci] = _mColourTable[_mBorderColourIndex]; + // _mPSurface.RgbData[ri, ci] = _colourTable[_mBorderColourIndex]; } else { - _mPSurface.RgbData[ri, ci] = _mColourTable[_mPixelColours[ri + _mVOffset, ci + _mHOffset]]; + _mPSurface.RgbData[ri, ci] = _colourTable[_pixelColours[ri + _mVOffset, ci + _mHOffset]]; } } } diff --git a/CdgLib/SubCode/Command.cs b/CdgLib/SubCode/Command.cs index 1e9568e..dbb2568 100644 --- a/CdgLib/SubCode/Command.cs +++ b/CdgLib/SubCode/Command.cs @@ -1,7 +1,7 @@ namespace CdgLib.SubCode { - internal enum Command : byte + public enum Command { - Graphic = 0x9 + Graphic = 9 } } diff --git a/CdgLib/SubCode/Instruction.cs b/CdgLib/SubCode/Instruction.cs index ed0e1b6..d17622d 100644 --- a/CdgLib/SubCode/Instruction.cs +++ b/CdgLib/SubCode/Instruction.cs @@ -3,7 +3,7 @@ /// /// /// - internal enum Instruction + public enum Instruction { /// /// Set the screen to a particular color. diff --git a/CdgLib/SubCode/Packet.cs b/CdgLib/SubCode/Packet.cs index c2e7f38..4c89639 100644 --- a/CdgLib/SubCode/Packet.cs +++ b/CdgLib/SubCode/Packet.cs @@ -4,18 +4,21 @@ namespace CdgLib.SubCode { public class Packet { - public byte[] Command = new byte[1]; - public byte[] Data = new byte[16]; - public byte[] Instruction = new byte[1]; - public byte[] ParityP = new byte[4]; - public byte[] ParityQ = new byte[2]; + public Command Command { get; } + + public Instruction Instruction { get; } + + public byte[] ParityQ { get; } = new byte[2]; + + public byte[] Data { get; } = new byte[16]; + + public byte[] ParityP { get; } = new byte[4]; - private const byte Mask = 0x3f; public Packet(byte[] data) { - Array.Copy(data, 0, Command, 0, 1); - Array.Copy(data, 1, Instruction, 0, 1); + Command = (Command)(data[0] & 0x3F); + Instruction = (Instruction)(data[1] & 0x3F); Array.Copy(data, 2, ParityQ, 0, 2); Array.Copy(data, 4, Data, 0, 16); Array.Copy(data, 20, ParityP, 0, 4); @@ -23,35 +26,34 @@ namespace CdgLib.SubCode public void ApplyTransform(int[,] data) { - if ((Command[0] & Mask) != (int) SubCode.Command.Graphic) return; - var instructionCode = (Instruction)(Instruction[0] & Mask); - switch (instructionCode) + if (Command != Command.Graphic) return; + switch (Instruction) { - case SubCode.Instruction.MemoryPreset: + case Instruction.MemoryPreset: MemoryPreset(data); break; - case SubCode.Instruction.BorderPreset: + case Instruction.BorderPreset: BorderPreset(data); break; - case SubCode.Instruction.TileBlockNormal: + case Instruction.TileBlockNormal: TileBlock(data,false); break; - case SubCode.Instruction.ScrollPreset: + case Instruction.ScrollPreset: Scroll(data,false); break; - case SubCode.Instruction.ScrollCopy: + case Instruction.ScrollCopy: Scroll(data,true); break; - case SubCode.Instruction.DefineTransparentColor: + case Instruction.DefineTransparentColor: DefineTransparentColour(data); break; - case SubCode.Instruction.LoadColorTableLower: + case Instruction.LoadColorTableLower: LoadColorTable(data,0); break; - case SubCode.Instruction.LoadColorTableUpper: + case Instruction.LoadColorTableUpper: LoadColorTable(data,1); break; - case SubCode.Instruction.TileBlockXor: + case Instruction.TileBlockXor: TileBlock(data,true); break; }