summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--SpriteEd/Colour.cs61
-rw-r--r--SpriteEd/Palette.cs111
-rw-r--r--SpriteEd/Sprite.cs125
-rw-r--r--SpriteEd/SpriteEd.csproj8
-rw-r--r--SpriteEd/SpriteEdException.cs34
-rw-r--r--SpriteEd/SpriteSet.cs158
-rw-r--r--SpriteEd/Util.cs80
7 files changed, 577 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+//
+using System;
+
+namespace SpriteEd
+{
+ public class Colour
+ {
+ /// <summary>
+ /// Red component of colour in range 0 to 255.
+ /// </summary>
+ public Byte Red {get;set;}
+
+ /// <summary>
+ /// Green component of colour in range 0 to 255.
+ /// </summary>
+ public Byte Green {get;set;}
+
+ /// <summary>
+ /// Blue component of colour in range 0 to 255.
+ /// </summary>
+ public Byte Blue {get;set;}
+
+ /// <summary>
+ /// Create a default black colour.
+ /// </summary>
+ public Colour()
+ {
+ Red = 0;
+ Green = 0;
+ Blue = 0;
+ }
+
+ /// <summary>
+ /// Create a colour given the passed colour components.
+ /// </summary>
+ /// <param name="red">The red component.</param>
+ /// <param name="green">The green component.</param>
+ /// <param name="blue">The blue component.</param>
+ 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 <http://www.gnu.org/licenses/>.
+//
+using System;
+using System.IO;
+
+namespace SpriteEd
+{
+ public class Palette
+ {
+ private Colour[] m_palette;
+
+ /// <summary>
+ /// Create a palette of the passed size.
+ /// </summary>
+ /// <param name="size"></param>
+ public Palette(uint size)
+ {
+ Size = size;
+ m_palette = new Colour[size];
+
+ for(int f = 0; f < size; f++)
+ {
+ m_palette[f] = new Colour();
+ }
+ }
+
+ /// <summary>
+ /// Get the size of the palette.
+ /// </summary>
+ public uint Size {get; private set;}
+
+ /// <summary>
+ /// Get/set the colour in the palette.
+ /// </summary>
+ /// <param name="index">The index to set/get.</param>
+ /// <returns>The colour, or black if the index is out of range.</returns>
+ 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;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Load a palette from a stream.
+ /// </summary>
+ /// <param name="s">The stream.</param>
+ /// <returns>The read palette.</returns>
+ 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;
+ }
+
+ /// <summary>
+ /// Save the palette to a stream.
+ /// </summary>
+ /// <param name="s">The stream.</param>
+ 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 <http://www.gnu.org/licenses/>.
+//
+using System;
+using System.IO;
+
+namespace SpriteEd
+{
+ /// <summary>
+ /// Represents a sprite.
+ /// </summary>
+ public class Sprite
+ {
+ private uint[,] m_data;
+
+ /// <summary>
+ /// The width of the sprite.
+ /// </summary>
+ public uint Width {get; private set;}
+
+ /// <summary>
+ /// The height of the sprite.
+ /// </summary>
+ public uint Height {get; private set;}
+
+ /// <summary>
+ /// Create a sprite of a specified size
+ /// </summary>
+ /// <param name="width">The width of the sprite.</param>
+ /// <param name="height">The height of the sprite.</param>
+ public Sprite(uint width, uint height)
+ {
+ Width = width;
+ Height = height;
+ m_data = new uint[width, height];
+ }
+
+ /// <summary>
+ /// Set/get a pixel value in the sprite.
+ /// </summary>
+ /// <param name="x">The X co-ord of the pixel.</param>
+ /// <param name="y">The Y co-ord of the pixel.</param>
+ /// <returns></returns>
+ 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;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Load a sprite from a stream.
+ /// </summary>
+ /// <param name="s">The stream.</param>
+ /// <returns></returns>
+ 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;
+ }
+
+ /// <summary>
+ /// Save a sprite to a stream.
+ /// </summary>
+ /// <param name="s">The stream.</param>
+ 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 @@
<IncludeMonoRuntime>false</IncludeMonoRuntime>
<UseSGen>true</UseSGen>
<UseRefCounting>true</UseRefCounting>
+ <AOTMode>None</AOTMode>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@@ -42,6 +43,7 @@
<UseRefCounting>true</UseRefCounting>
<CodeSignEntitlements>Entitlements.plist</CodeSignEntitlements>
<LinkMode>SdkOnly</LinkMode>
+ <AOTMode>None</AOTMode>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@@ -77,6 +79,12 @@
<DependentUpon>ViewController.cs</DependentUpon>
</Compile>
<Compile Include="NSSpriteEdit.cs" />
+ <Compile Include="Colour.cs" />
+ <Compile Include="Palette.cs" />
+ <Compile Include="Util.cs" />
+ <Compile Include="Sprite.cs" />
+ <Compile Include="SpriteSet.cs" />
+ <Compile Include="SpriteEdException.cs" />
</ItemGroup>
<ItemGroup>
<InterfaceDefinition Include="Main.storyboard" />
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 <http://www.gnu.org/licenses/>.
+//
+using System;
+
+namespace SpriteEd
+{
+ /// <summary>
+ /// An exception class for the sprite editor.
+ /// </summary>
+ public class SpriteEdException : Exception
+ {
+ /// <summary>
+ /// Create a new exception.
+ /// </summary>
+ /// <param name="message">The message for the exception.</param>
+ 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 <http://www.gnu.org/licenses/>.
+//
+using System;
+using System.IO;
+
+namespace SpriteEd
+{
+ /// <summary>
+ /// Defines a set of sprites.
+ /// </summary>
+ public class SpriteSet
+ {
+ private const uint SET_SIZE = 256;
+ private const uint MAGIC = 0x5d75537d + SET_SIZE;
+
+ private Sprite[] m_sprite;
+
+ /// <summary>
+ /// The palette associated with the sprite set.
+ /// </summary>
+ public Palette Palette {get; private set;}
+
+ /// <summary>
+ /// The width of each sprite in the set.
+ /// </summary>
+ public uint Width {get; private set;}
+
+ /// <summary>
+ /// The height of each sprite in the set.
+ /// </summary>
+ public uint Height {get; private set;}
+
+ /// <summary>
+ /// Whether the sprite has a double width pixel.
+ /// </summary>
+ public bool DoubleWidth {get; private set;}
+
+ /// <summary>
+ /// Whether the sprite has a double height pixel.
+ /// </summary>
+ public bool DoubleHeight {get; private set;}
+
+ /// <summary>
+ /// Construct a sprite set.
+ /// </summary>
+ /// <param name="width">The width of each sprite.</param>
+ /// <param name="height">The height of each sprite.</param>
+ /// <param name="pal">The palette for the sprites.</param>
+ /// <param name="double_width">Whether the psrite has a double width pixel.</param>
+ /// <param name="double_height">Whether the sprite has a double height pixel.</param>
+ 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);
+ }
+ }
+
+ /// <summary>
+ /// Get a sprite in the set.
+ /// </summary>
+ /// <param name="index">The index in the set.</param>
+ /// <returns></returns>
+ public Sprite this[byte index]
+ {
+ get
+ {
+ return m_sprite[index];
+ }
+ }
+
+ /// <summary>
+ /// Load a sprite set from a stream.
+ /// </summary>
+ /// <param name="s">The stream.</param>
+ /// <returns>The sprite set.</returns>
+ 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);
+ }
+ }
+
+ /// <summary>
+ /// Save a sprite set to a stream.
+ /// </summary>
+ /// <param name="s">The stream.</param>
+ 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 <http://www.gnu.org/licenses/>.
+//
+using System;
+using System.IO;
+
+namespace SpriteEd
+{
+ /// <summary>
+ /// Utility functions.
+ /// </summary>
+ public static class Util
+ {
+ /// <summary>
+ /// Write an unsigned int to a stream.
+ /// </summary>
+ /// <param name="s">The stream.</param>
+ /// <param name="v">The value to write</param>
+ public static void WriteUint(Stream s, uint v)
+ {
+ for(int f = 0; f < 4; f++)
+ {
+ s.WriteByte((byte)(v & 0xff));
+ v >>= 8;
+ }
+ }
+
+ /// <summary>
+ /// Read a uint from a stream.
+ /// </summary>
+ /// <param name="s">The stream.</param>
+ /// <returns>The uint from the stream.</returns>
+ 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;
+ }
+
+ /// <summary>
+ /// Write a bool to a stream.
+ /// </summary>
+ /// <param name="s">The stream.</param>
+ /// <param name="b">The boolean.</param>
+ public static void WriteBool(Stream s, bool b)
+ {
+ s.WriteByte(b ? (byte)255 : (byte)0);
+ }
+
+ /// <summary>
+ /// Read a bool from a stream.
+ /// </summary>
+ /// <param name="s">The stream.</param>
+ /// <returns>The bool from the stream.</returns>
+ public static bool ReadBool(Stream s)
+ {
+ return s.ReadByte() == 255;
+ }
+ }
+}