From 40f02a62412772353933360996b0225e1be3f925 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sun, 21 Jun 2020 20:10:30 +0000 Subject: Added base classes for sprite data. --- SpriteEd/Colour.cs | 61 ++++++++++++++++ SpriteEd/Palette.cs | 111 +++++++++++++++++++++++++++++ SpriteEd/Sprite.cs | 125 +++++++++++++++++++++++++++++++++ SpriteEd/SpriteEd.csproj | 8 +++ SpriteEd/SpriteEdException.cs | 34 +++++++++ SpriteEd/SpriteSet.cs | 158 ++++++++++++++++++++++++++++++++++++++++++ SpriteEd/Util.cs | 80 +++++++++++++++++++++ 7 files changed, 577 insertions(+) create mode 100644 SpriteEd/Colour.cs create mode 100644 SpriteEd/Palette.cs create mode 100644 SpriteEd/Sprite.cs create mode 100644 SpriteEd/SpriteEdException.cs create mode 100644 SpriteEd/SpriteSet.cs create mode 100644 SpriteEd/Util.cs diff --git a/SpriteEd/Colour.cs b/SpriteEd/Colour.cs new file mode 100644 index 0000000..b0c97d2 --- /dev/null +++ b/SpriteEd/Colour.cs @@ -0,0 +1,61 @@ +// SpriteEd - Simple sprite editor +// Copyright 2020 Ian Cowburn +// +// This program is free software: you can redistribute it and/or modify +// it 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. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +using System; + +namespace SpriteEd +{ + public class Colour + { + /// + /// Red component of colour in range 0 to 255. + /// + public Byte Red {get;set;} + + /// + /// Green component of colour in range 0 to 255. + /// + public Byte Green {get;set;} + + /// + /// Blue component of colour in range 0 to 255. + /// + public Byte Blue {get;set;} + + /// + /// Create a default black colour. + /// + public Colour() + { + Red = 0; + Green = 0; + Blue = 0; + } + + /// + /// Create a colour given the passed colour components. + /// + /// The red component. + /// The green component. + /// The blue component. + public Colour(Byte red, Byte green, Byte blue) + { + Red = red; + Green = green; + Blue = blue; + } + } +} diff --git a/SpriteEd/Palette.cs b/SpriteEd/Palette.cs new file mode 100644 index 0000000..1a46a17 --- /dev/null +++ b/SpriteEd/Palette.cs @@ -0,0 +1,111 @@ +// SpriteEd - Simple sprite editor +// Copyright 2020 Ian Cowburn +// +// This program is free software: you can redistribute it and/or modify +// it 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. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +using System; +using System.IO; + +namespace SpriteEd +{ + public class Palette + { + private Colour[] m_palette; + + /// + /// Create a palette of the passed size. + /// + /// + public Palette(uint size) + { + Size = size; + m_palette = new Colour[size]; + + for(int f = 0; f < size; f++) + { + m_palette[f] = new Colour(); + } + } + + /// + /// Get the size of the palette. + /// + public uint Size {get; private set;} + + /// + /// Get/set the colour in the palette. + /// + /// The index to set/get. + /// The colour, or black if the index is out of range. + public Colour this[uint index] + { + get + { + if (index < Size) + { + return m_palette[index]; + } + else + { + return new Colour(); + } + } + + set + { + if (index < Size) + { + m_palette[index] = value; + } + } + } + + /// + /// Load a palette from a stream. + /// + /// The stream. + /// The read palette. + public static Palette Load(Stream s) + { + uint size = Util.ReadUint(s); + + Palette p = new Palette(size); + + for(uint f = 0; f < p.Size; f++) + { + p.m_palette[f].Red = (byte)s.ReadByte(); + p.m_palette[f].Green = (byte)s.ReadByte(); + p.m_palette[f].Blue = (byte)s.ReadByte(); + } + + return p; + } + + /// + /// Save the palette to a stream. + /// + /// The stream. + public void Save(Stream s) + { + Util.WriteUint(s, Size); + + for(uint f = 0; f < Size; f++) + { + s.WriteByte(m_palette[f].Red); + s.WriteByte(m_palette[f].Green); + s.WriteByte(m_palette[f].Blue); + } + } + } +} diff --git a/SpriteEd/Sprite.cs b/SpriteEd/Sprite.cs new file mode 100644 index 0000000..09fed6b --- /dev/null +++ b/SpriteEd/Sprite.cs @@ -0,0 +1,125 @@ +// SpriteEd - Simple sprite editor +// Copyright 2020 Ian Cowburn +// +// This program is free software: you can redistribute it and/or modify +// it 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. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +using System; +using System.IO; + +namespace SpriteEd +{ + /// + /// Represents a sprite. + /// + public class Sprite + { + private uint[,] m_data; + + /// + /// The width of the sprite. + /// + public uint Width {get; private set;} + + /// + /// The height of the sprite. + /// + public uint Height {get; private set;} + + /// + /// Create a sprite of a specified size + /// + /// The width of the sprite. + /// The height of the sprite. + public Sprite(uint width, uint height) + { + Width = width; + Height = height; + m_data = new uint[width, height]; + } + + /// + /// Set/get a pixel value in the sprite. + /// + /// The X co-ord of the pixel. + /// The Y co-ord of the pixel. + /// + public uint this[uint x, uint y] + { + get + { + if (x < Width && y < Height) + { + return m_data[x,y]; + } + else + { + return 0; + } + } + + set + { + if (x < Width && y < Height) + { + m_data[x,y] = value; + } + } + } + + /// + /// Load a sprite from a stream. + /// + /// The stream. + /// + public static Sprite Load(Stream s) + { + Sprite sprite; + + uint width,height; + + width = Util.ReadUint(s); + height = Util.ReadUint(s); + + sprite = new Sprite(width, height); + + for(uint x = 0; x < width; x++) + { + for(uint y = 0; y < height; y++) + { + sprite[x,y] = Util.ReadUint(s); + } + } + + return sprite; + } + + /// + /// Save a sprite to a stream. + /// + /// The stream. + public void Save(Stream s) + { + Util.WriteUint(s, Width); + Util.WriteUint(s, Height); + + for(uint x = 0; x < Width; x++) + { + for(uint y = 0; y < Height; y++) + { + Util.WriteUint(s, this[x,y]); + } + } + } + } +} diff --git a/SpriteEd/SpriteEd.csproj b/SpriteEd/SpriteEd.csproj index afb2a85..5a8d003 100644 --- a/SpriteEd/SpriteEd.csproj +++ b/SpriteEd/SpriteEd.csproj @@ -27,6 +27,7 @@ false true true + None pdbonly @@ -42,6 +43,7 @@ true Entitlements.plist SdkOnly + None @@ -77,6 +79,12 @@ ViewController.cs + + + + + + diff --git a/SpriteEd/SpriteEdException.cs b/SpriteEd/SpriteEdException.cs new file mode 100644 index 0000000..2a4cc33 --- /dev/null +++ b/SpriteEd/SpriteEdException.cs @@ -0,0 +1,34 @@ +// SpriteEd - Simple sprite editor +// Copyright 2020 Ian Cowburn +// +// This program is free software: you can redistribute it and/or modify +// it 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. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +using System; + +namespace SpriteEd +{ + /// + /// An exception class for the sprite editor. + /// + public class SpriteEdException : Exception + { + /// + /// Create a new exception. + /// + /// The message for the exception. + public SpriteEdException(string message) : base(message) + { + } + } +} diff --git a/SpriteEd/SpriteSet.cs b/SpriteEd/SpriteSet.cs new file mode 100644 index 0000000..cbc5c05 --- /dev/null +++ b/SpriteEd/SpriteSet.cs @@ -0,0 +1,158 @@ +// SpriteEd - Simple sprite editor +// Copyright 2020 Ian Cowburn +// +// This program is free software: you can redistribute it and/or modify +// it 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. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +using System; +using System.IO; + +namespace SpriteEd +{ + /// + /// Defines a set of sprites. + /// + public class SpriteSet + { + private const uint SET_SIZE = 256; + private const uint MAGIC = 0x5d75537d + SET_SIZE; + + private Sprite[] m_sprite; + + /// + /// The palette associated with the sprite set. + /// + public Palette Palette {get; private set;} + + /// + /// The width of each sprite in the set. + /// + public uint Width {get; private set;} + + /// + /// The height of each sprite in the set. + /// + public uint Height {get; private set;} + + /// + /// Whether the sprite has a double width pixel. + /// + public bool DoubleWidth {get; private set;} + + /// + /// Whether the sprite has a double height pixel. + /// + public bool DoubleHeight {get; private set;} + + /// + /// Construct a sprite set. + /// + /// The width of each sprite. + /// The height of each sprite. + /// The palette for the sprites. + /// Whether the psrite has a double width pixel. + /// Whether the sprite has a double height pixel. + public SpriteSet(uint width, uint height, Palette pal, bool double_width, bool double_height) + { + Palette = pal; + Width = width; + Height = height; + DoubleWidth = double_width; + DoubleHeight = double_height; + + m_sprite = new Sprite[SET_SIZE]; + + for(uint f = 0; f < SET_SIZE; f++) + { + m_sprite[f] = new Sprite(Width, Height); + } + } + + /// + /// Get a sprite in the set. + /// + /// The index in the set. + /// + public Sprite this[byte index] + { + get + { + return m_sprite[index]; + } + } + + /// + /// Load a sprite set from a stream. + /// + /// The stream. + /// The sprite set. + public static SpriteSet Load(Stream s) + { + try + { + SpriteSet sprset; + + if (Util.ReadUint(s) != MAGIC) + { + throw new SpriteEdException("Not a sprite set file"); + } + + Palette pal = Palette.Load(s); + uint width = Util.ReadUint(s); + uint height = Util.ReadUint(s); + bool double_width = Util.ReadBool(s); + bool double_height = Util.ReadBool(s); + + sprset = new SpriteSet(width, height, pal, double_width, double_height); + + for(uint f = 0; f < SET_SIZE; f++) + { + sprset.m_sprite[f] = Sprite.Load(s); + } + + return sprset; + } + catch (IOException e) + { + throw new SpriteEdException(e.Message); + } + } + + /// + /// Save a sprite set to a stream. + /// + /// The stream. + public void Save(Stream s) + { + try + { + Util.WriteUint(s, MAGIC); + + Palette.Save(s); + Util.WriteUint(s, Width); + Util.WriteUint(s, Height); + Util.WriteBool(s, DoubleWidth); + Util.WriteBool(s, DoubleHeight); + + for(uint f = 0; f < SET_SIZE; f++) + { + m_sprite[f].Save(s); + } + } + catch (IOException e) + { + throw new SpriteEdException(e.Message); + } + } + } +} diff --git a/SpriteEd/Util.cs b/SpriteEd/Util.cs new file mode 100644 index 0000000..4bba750 --- /dev/null +++ b/SpriteEd/Util.cs @@ -0,0 +1,80 @@ +// SpriteEd - Simple sprite editor +// Copyright 2020 Ian Cowburn +// +// This program is free software: you can redistribute it and/or modify +// it 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. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +using System; +using System.IO; + +namespace SpriteEd +{ + /// + /// Utility functions. + /// + public static class Util + { + /// + /// Write an unsigned int to a stream. + /// + /// The stream. + /// The value to write + public static void WriteUint(Stream s, uint v) + { + for(int f = 0; f < 4; f++) + { + s.WriteByte((byte)(v & 0xff)); + v >>= 8; + } + } + + /// + /// Read a uint from a stream. + /// + /// The stream. + /// The uint from the stream. + public static uint ReadUint(Stream s) + { + uint v = 0; + + for(int f = 0; f < 4; f++) + { + int b = s.ReadByte(); + + v |= (uint)b << (f * 8); + } + + return v; + } + + /// + /// Write a bool to a stream. + /// + /// The stream. + /// The boolean. + public static void WriteBool(Stream s, bool b) + { + s.WriteByte(b ? (byte)255 : (byte)0); + } + + /// + /// Read a bool from a stream. + /// + /// The stream. + /// The bool from the stream. + public static bool ReadBool(Stream s) + { + return s.ReadByte() == 255; + } + } +} -- cgit v1.2.3