├── .gitattributes ├── .gitignore ├── Pixel-Sprite-Generator-CSharp.sln ├── Pixel-Sprite-Generator-CSharp ├── App.config ├── App.xaml ├── App.xaml.cs ├── Pixel-Sprite-Generator-CSharp.csproj ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings ├── SpriteGen │ ├── Mask.cs │ └── Sprite.cs ├── Window1.xaml └── Window1.xaml.cs └── readme.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opensdf 80 | *.sdf 81 | *.cachefile 82 | 83 | # Visual Studio profiler 84 | *.psess 85 | *.vsp 86 | *.vspx 87 | *.sap 88 | 89 | # TFS 2012 Local Workspace 90 | $tf/ 91 | 92 | # Guidance Automation Toolkit 93 | *.gpState 94 | 95 | # ReSharper is a .NET coding add-in 96 | _ReSharper*/ 97 | *.[Rr]e[Ss]harper 98 | *.DotSettings.user 99 | 100 | # JustCode is a .NET coding add-in 101 | .JustCode 102 | 103 | # TeamCity is a build add-in 104 | _TeamCity* 105 | 106 | # DotCover is a Code Coverage Tool 107 | *.dotCover 108 | 109 | # NCrunch 110 | _NCrunch_* 111 | .*crunch*.local.xml 112 | nCrunchTemp_* 113 | 114 | # MightyMoose 115 | *.mm.* 116 | AutoTest.Net/ 117 | 118 | # Web workbench (sass) 119 | .sass-cache/ 120 | 121 | # Installshield output folder 122 | [Ee]xpress/ 123 | 124 | # DocProject is a documentation generator add-in 125 | DocProject/buildhelp/ 126 | DocProject/Help/*.HxT 127 | DocProject/Help/*.HxC 128 | DocProject/Help/*.hhc 129 | DocProject/Help/*.hhk 130 | DocProject/Help/*.hhp 131 | DocProject/Help/Html2 132 | DocProject/Help/html 133 | 134 | # Click-Once directory 135 | publish/ 136 | 137 | # Publish Web Output 138 | *.[Pp]ublish.xml 139 | *.azurePubxml 140 | # TODO: Comment the next line if you want to checkin your web deploy settings 141 | # but database connection strings (with potential passwords) will be unencrypted 142 | *.pubxml 143 | *.publishproj 144 | 145 | # NuGet Packages 146 | *.nupkg 147 | # The packages folder can be ignored because of Package Restore 148 | **/packages/* 149 | # except build/, which is used as an MSBuild target. 150 | !**/packages/build/ 151 | # Uncomment if necessary however generally it will be regenerated when needed 152 | #!**/packages/repositories.config 153 | 154 | # Windows Azure Build Output 155 | csx/ 156 | *.build.csdef 157 | 158 | # Windows Azure Emulator 159 | efc/ 160 | rfc/ 161 | 162 | # Windows Store app package directory 163 | AppPackages/ 164 | 165 | # Visual Studio cache files 166 | # files ending in .cache can be ignored 167 | *.[Cc]ache 168 | # but keep track of directories ending in .cache 169 | !*.[Cc]ache/ 170 | 171 | # Others 172 | ClientBin/ 173 | [Ss]tyle[Cc]op.* 174 | ~$* 175 | *~ 176 | *.dbmdl 177 | *.dbproj.schemaview 178 | *.pfx 179 | *.publishsettings 180 | node_modules/ 181 | orleans.codegen.cs 182 | 183 | # RIA/Silverlight projects 184 | Generated_Code/ 185 | 186 | # Backup & report files from converting an old project file 187 | # to a newer Visual Studio version. Backup files are not needed, 188 | # because we have git ;-) 189 | _UpgradeReport_Files/ 190 | Backup*/ 191 | UpgradeLog*.XML 192 | UpgradeLog*.htm 193 | 194 | # SQL Server files 195 | *.mdf 196 | *.ldf 197 | 198 | # Business Intelligence projects 199 | *.rdl.data 200 | *.bim.layout 201 | *.bim_*.settings 202 | 203 | # Microsoft Fakes 204 | FakesAssemblies/ 205 | 206 | # GhostDoc plugin setting file 207 | *.GhostDoc.xml 208 | 209 | # Node.js Tools for Visual Studio 210 | .ntvs_analysis.dat 211 | 212 | # Visual Studio 6 build log 213 | *.plg 214 | 215 | # Visual Studio 6 workspace options file 216 | *.opt 217 | 218 | # Visual Studio LightSwitch build output 219 | **/*.HTMLClient/GeneratedArtifacts 220 | **/*.DesktopClient/GeneratedArtifacts 221 | **/*.DesktopClient/ModelManifest.xml 222 | **/*.Server/GeneratedArtifacts 223 | **/*.Server/ModelManifest.xml 224 | _Pvt_Extensions 225 | 226 | # Paket dependency manager 227 | .paket/paket.exe 228 | 229 | # FAKE - F# Make 230 | .fake/ 231 | 232 | # ========================= 233 | # Operating System Files 234 | # ========================= 235 | 236 | # OSX 237 | # ========================= 238 | 239 | .DS_Store 240 | .AppleDouble 241 | .LSOverride 242 | 243 | # Thumbnails 244 | ._* 245 | 246 | # Files that might appear in the root of a volume 247 | .DocumentRevisions-V100 248 | .fseventsd 249 | .Spotlight-V100 250 | .TemporaryItems 251 | .Trashes 252 | .VolumeIcon.icns 253 | 254 | # Directories potentially created on remote AFP share 255 | .AppleDB 256 | .AppleDesktop 257 | Network Trash Folder 258 | Temporary Items 259 | .apdisk 260 | 261 | # Windows 262 | # ========================= 263 | 264 | # Windows image file caches 265 | Thumbs.db 266 | ehthumbs.db 267 | 268 | # Folder config file 269 | Desktop.ini 270 | 271 | # Recycle Bin used on file shares 272 | $RECYCLE.BIN/ 273 | 274 | # Windows Installer files 275 | *.cab 276 | *.msi 277 | *.msm 278 | *.msp 279 | 280 | # Windows shortcuts 281 | *.lnk 282 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.24720.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pixel-Sprite-Generator-CSharp", "Pixel-Sprite-Generator-CSharp\Pixel-Sprite-Generator-CSharp.csproj", "{DA4352AB-6A4A-43D1-8D82-AF8719C4C2E7}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {DA4352AB-6A4A-43D1-8D82-AF8719C4C2E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {DA4352AB-6A4A-43D1-8D82-AF8719C4C2E7}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {DA4352AB-6A4A-43D1-8D82-AF8719C4C2E7}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {DA4352AB-6A4A-43D1-8D82-AF8719C4C2E7}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/App.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | using System.Windows; 8 | 9 | namespace Pixel_Sprite_Generator 10 | { 11 | /// 12 | /// Interaction logic for App.xaml 13 | /// 14 | public partial class App : Application 15 | { 16 | Window1 whatever = new Window1(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/Pixel-Sprite-Generator-CSharp.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {DA4352AB-6A4A-43D1-8D82-AF8719C4C2E7} 8 | WinExe 9 | Properties 10 | Pixel_Sprite_Generator_CSharp 11 | Pixel-Sprite-Generator-CSharp 12 | v4.5.2 13 | 512 14 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 15 | 4 16 | true 17 | 18 | 19 | AnyCPU 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | 28 | 29 | AnyCPU 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE 34 | prompt 35 | 4 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 4.0 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | MSBuild:Compile 56 | Designer 57 | 58 | 59 | 60 | 61 | Window1.xaml 62 | 63 | 64 | App.xaml 65 | Code 66 | 67 | 68 | MSBuild:Compile 69 | Designer 70 | 71 | 72 | 73 | 74 | Code 75 | 76 | 77 | True 78 | True 79 | Resources.resx 80 | 81 | 82 | True 83 | Settings.settings 84 | True 85 | 86 | 87 | ResXFileCodeGenerator 88 | Resources.Designer.cs 89 | 90 | 91 | SettingsSingleFileGenerator 92 | Settings.Designer.cs 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 108 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Resources; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | using System.Windows; 6 | 7 | // General Information about an assembly is controlled through the following 8 | // set of attributes. Change these attribute values to modify the information 9 | // associated with an assembly. 10 | [assembly: AssemblyTitle("Pixel-Sprite-Generator-CSharp")] 11 | [assembly: AssemblyDescription("")] 12 | [assembly: AssemblyConfiguration("")] 13 | [assembly: AssemblyCompany("")] 14 | [assembly: AssemblyProduct("Pixel-Sprite-Generator-CSharp")] 15 | [assembly: AssemblyCopyright("Copyright © 2016")] 16 | [assembly: AssemblyTrademark("")] 17 | [assembly: AssemblyCulture("")] 18 | 19 | // Setting ComVisible to false makes the types in this assembly not visible 20 | // to COM components. If you need to access a type in this assembly from 21 | // COM, set the ComVisible attribute to true on that type. 22 | [assembly: ComVisible(false)] 23 | 24 | //In order to begin building localizable applications, set 25 | //CultureYouAreCodingWith in your .csproj file 26 | //inside a . For example, if you are using US english 27 | //in your source files, set the to en-US. Then uncomment 28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in 29 | //the line below to match the UICulture setting in the project file. 30 | 31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 32 | 33 | 34 | [assembly: ThemeInfo( 35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 36 | //(used if a resource is not found in the page, 37 | // or application resource dictionaries) 38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 39 | //(used if a resource is not found in the page, 40 | // app, or any theme specific resource dictionaries) 41 | )] 42 | 43 | 44 | // Version information for an assembly consists of the following four values: 45 | // 46 | // Major Version 47 | // Minor Version 48 | // Build Number 49 | // Revision 50 | // 51 | // You can specify all the values or you can default the Build and Revision Numbers 52 | // by using the '*' as shown below: 53 | // [assembly: AssemblyVersion("1.0.*")] 54 | [assembly: AssemblyVersion("1.0.0.0")] 55 | [assembly: AssemblyFileVersion("1.0.0.0")] 56 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace Pixel_Sprite_Generator_CSharp.Properties 12 | { 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources 26 | { 27 | 28 | private static global::System.Resources.ResourceManager resourceMan; 29 | 30 | private static global::System.Globalization.CultureInfo resourceCulture; 31 | 32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 33 | internal Resources() 34 | { 35 | } 36 | 37 | /// 38 | /// Returns the cached ResourceManager instance used by this class. 39 | /// 40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 41 | internal static global::System.Resources.ResourceManager ResourceManager 42 | { 43 | get 44 | { 45 | if ((resourceMan == null)) 46 | { 47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Pixel_Sprite_Generator_CSharp.Properties.Resources", typeof(Resources).Assembly); 48 | resourceMan = temp; 49 | } 50 | return resourceMan; 51 | } 52 | } 53 | 54 | /// 55 | /// Overrides the current thread's CurrentUICulture property for all 56 | /// resource lookups using this strongly typed resource class. 57 | /// 58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 59 | internal static global::System.Globalization.CultureInfo Culture 60 | { 61 | get 62 | { 63 | return resourceCulture; 64 | } 65 | set 66 | { 67 | resourceCulture = value; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace Pixel_Sprite_Generator_CSharp.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/SpriteGen/Mask.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Pixel_Sprite_Generator.SpriteGen 8 | { 9 | /** 10 | * The Mask class defines a 2D template form which sprites can be generated. 11 | * 12 | * @class Mask 13 | * @constructor 14 | * @param {data} Integer array describing which parts of the sprite should be 15 | * empty, body, and border. The mask only defines a semi-ridgid stucture 16 | * which might not strictly be followed based on randomly generated numbers. 17 | * 18 | * -1 = Always border (black) 19 | * 0 = Empty 20 | * 1 = Randomly chosen Empty/Body 21 | * 2 = Randomly chosen Border/Body 22 | * 23 | * @param {width} Width of the mask data array 24 | * @param {height} Height of the mask data array 25 | * @param {mirrorX} A boolean describing whether the mask should be mirrored on the x axis 26 | * @param {mirrorY} A boolean describing whether the mask should be mirrored on the y axis 27 | */ 28 | class Mask 29 | { 30 | public int width; 31 | public int height; 32 | public int[] data; 33 | public bool mirrorX; 34 | public bool mirrorY; 35 | 36 | public Mask(int[] data, int width, int height, bool mirrorX = false, bool mirrorY = false) 37 | { 38 | this.width = width; 39 | this.height = height; 40 | this.data = data; 41 | this.mirrorX = mirrorX; 42 | this.mirrorY = mirrorY; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/SpriteGen/Sprite.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Windows; 3 | 4 | namespace Pixel_Sprite_Generator.SpriteGen 5 | { 6 | /** 7 | * The Sprite class makes use of a Mask instance to generate a 2D sprite on a 8 | * HTML canvas. 9 | *

10 | * var options = { 11 | * colored : true, // boolean 12 | * edgeBrightness : 0.3, // value from 0 to 1 13 | * colorVariations : 0.2, // value from 0 to 1 14 | * brightnessNoise : 0.3, // value from 0 to 1 15 | * saturation : 0.5 // value from 0 to 1 16 | * } 17 | * 18 | * @param {mask} 19 | * @param {options} 20 | * @class Sprite 21 | * @constructor 22 | */ 23 | class Sprite 24 | { 25 | public int width; 26 | public int height; 27 | public Mask mask; 28 | public int[] data; 29 | public bool colored; 30 | double edgeBrightness; 31 | double colorVariations; 32 | double brightnessNoise; 33 | double saturation; 34 | Random random; 35 | int SEED; 36 | 37 | public Sprite(int width, int height, Mask mask, 38 | bool colored = true, 39 | double edgeBrightness = 0.3, 40 | double colorVariations = 0.2, 41 | double brightnessNoise = 0.3, 42 | double saturation = 0.5, 43 | int SEED = 0) 44 | { 45 | this.width = mask.width * (mask.mirrorX ? 2 : 1); 46 | this.height = mask.height * (mask.mirrorY ? 2 : 1); 47 | this.mask = mask; 48 | this.data = new int[this.width * this.height]; 49 | this.colored = colored; 50 | this.edgeBrightness = edgeBrightness; 51 | this.colorVariations = colorVariations; 52 | this.brightnessNoise = brightnessNoise; 53 | this.saturation = saturation; 54 | if (SEED == 0) 55 | this.SEED = Environment.TickCount; 56 | else 57 | this.SEED = SEED; 58 | this.init(); 59 | } 60 | 61 | /** 62 | * The init method calls all functions required to generate the sprite. 63 | * 64 | * @method init 65 | * @returns {undefined} 66 | */ 67 | private void init() 68 | { 69 | this.initData(); 70 | this.applyMask(); 71 | this.generateRandomSample(); 72 | 73 | if (this.mask.mirrorX) 74 | { 75 | this.mirrorX(); 76 | } 77 | 78 | if (this.mask.mirrorY) 79 | { 80 | this.mirrorY(); 81 | } 82 | 83 | this.generateEdges(); 84 | } 85 | 86 | public int getWidth() 87 | { 88 | return width; 89 | } 90 | public int getHeight() 91 | { 92 | return height; 93 | } 94 | 95 | /** 96 | * The getData method returns the sprite template data at location (x, y) 97 | *

98 | * -1 = Always border (black) 99 | * 0 = Empty 100 | * 1 = Randomly chosen Empty/Body 101 | * 2 = Randomly chosen Border/Body 102 | * 103 | * @param {x} 104 | * @param {y} 105 | * @method getData 106 | * @returns {undefined} 107 | */ 108 | public int getData(int x, int y) 109 | { 110 | return this.data[y * this.width + x]; 111 | } 112 | 113 | 114 | /** 115 | * The setData method sets the sprite template data at location (x, y) 116 | *

117 | * -1 = Always border (black) 118 | * 0 = Empty 119 | * 1 = Randomly chosen Empty/Body 120 | * 2 = Randomly chosen Border/Body 121 | * 122 | * @param {x} 123 | * @param {y} 124 | * @param {value} 125 | * @method setData 126 | * @returns {undefined} 127 | */ 128 | public void setData(int x, int y, int value) 129 | { 130 | this.data[y * this.width + x] = value; 131 | } 132 | 133 | /** 134 | * The initData method initializes the sprite data to completely solid. 135 | * 136 | * @method initData 137 | * @returns {undefined} 138 | */ 139 | public void initData() 140 | { 141 | int h = this.height; 142 | int w = this.width; 143 | for (int y = 0; y < h; y++) 144 | { 145 | for (int x = 0; x < w; x++) 146 | { 147 | this.setData(x, y, -1); 148 | } 149 | } 150 | } 151 | 152 | /** 153 | * The mirrorX method mirrors the template data horizontally. 154 | * 155 | * @method mirrorX 156 | * @returns {undefined} 157 | */ 158 | public void mirrorX() 159 | { 160 | int h = this.height; 161 | int w = (int)Math.Floor(this.width / (double)2); 162 | for (int y = 0; y < h; y++) 163 | { 164 | for (int x = 0; x < w; x++) 165 | { 166 | this.setData(this.width - x - 1, y, this.getData(x, y)); 167 | } 168 | } 169 | } 170 | 171 | /** 172 | * The mirrorY method mirrors the template data vertically. 173 | * 174 | * @method 175 | * @returns {undefined} 176 | */ 177 | public void mirrorY() 178 | { 179 | int h = (int)Math.Floor(this.height / (double)2); 180 | int w = this.width; 181 | for (int y = 0; y < h; y++) 182 | { 183 | for (int x = 0; x < w; x++) 184 | { 185 | this.setData(x, this.height - y - 1, this.getData(x, y)); 186 | } 187 | } 188 | } 189 | 190 | /** 191 | * The applyMask method copies the mask data into the template data array at 192 | * location (0, 0). 193 | *

194 | * (note: the mask may be smaller than the template data array) 195 | * 196 | * @method applyMask 197 | * @returns {undefined} 198 | */ 199 | public void applyMask() 200 | { 201 | int h = this.mask.height; 202 | int w = this.mask.width; 203 | 204 | for (int y = 0; y < h; y++) 205 | { 206 | for (int x = 0; x < w; x++) 207 | { 208 | this.setData(x, y, this.mask.data[y * w + x]); 209 | } 210 | } 211 | } 212 | 213 | /** 214 | * Apply a random sample to the sprite template. 215 | *

216 | * If the template contains a 1 (internal body part) at location (x, y), then 217 | * there is a 50% chance it will be turned empty. If there is a 2, then there 218 | * is a 50% chance it will be turned into a body or border. 219 | *

220 | * (feel free to play with this logic for interesting results) 221 | * 222 | * @method generateRandomSample 223 | * @returns {undefined} 224 | */ 225 | public void generateRandomSample() 226 | { 227 | random = new Random(SEED); 228 | int h = this.height; 229 | int w = this.width; 230 | 231 | for (int y = 0; y < h; y++) 232 | { 233 | for (int x = 0; x < w; x++) 234 | { 235 | int val = this.getData(x, y); 236 | if (val == 1) 237 | { 238 | val = random.Next(0, 2); 239 | } 240 | else if (val == 2) 241 | { 242 | if (random.NextDouble() > 0.5) 243 | { 244 | val = 1; 245 | } 246 | else { 247 | val = -1; 248 | } 249 | } 250 | this.setData(x, y, val); 251 | } 252 | } 253 | } 254 | 255 | /** 256 | * This method applies edges to any template location that is positive in 257 | * value and is surrounded by empty (0) pixels. 258 | * 259 | * @method generateEdges 260 | * @returns {undefined} 261 | */ 262 | public void generateEdges() 263 | { 264 | int h = this.height; 265 | int w = this.width; 266 | for (int y = 0; y < h; y++) 267 | { 268 | for (int x = 0; x < w; x++) 269 | { 270 | if (this.getData(x, y) > 0) 271 | { 272 | if (y - 1 >= 0 && this.getData(x, y - 1) == 0) 273 | { 274 | this.setData(x, y - 1, -1); 275 | } 276 | if (y + 1 < this.height && this.getData(x, y + 1) == 0) 277 | { 278 | this.setData(x, y + 1, -1); 279 | } 280 | if (x - 1 >= 0 && this.getData(x - 1, y) == 0) 281 | { 282 | this.setData(x - 1, y, -1); 283 | } 284 | if (x + 1 < this.width && this.getData(x + 1, y) == 0) 285 | { 286 | this.setData(x + 1, y, -1); 287 | } 288 | } 289 | } 290 | } 291 | } 292 | 293 | /** 294 | * This method converts HSL color values to RGB color values. 295 | * 296 | * @param {h} 297 | * @param {s} 298 | * @param {l} 299 | * @param {result} 300 | * @method hslToRgb 301 | * @returns {result} 302 | */ 303 | public double[] hslToRgb(double h, double s, double l) 304 | { 305 | double f, p, q, t; 306 | int i = (int)Math.Floor(h * (double)6); 307 | f = h * 6 - i; 308 | p = l * (1 - s); 309 | q = l * (1 - f * s); 310 | t = l * (1 - (1 - f) * s); 311 | switch (i % 6) 312 | { 313 | case 0: return new double[] { l, t, p }; 314 | case 1: return new double[] { q, l, p }; 315 | case 2: return new double[] { p, l, t }; 316 | case 3: return new double[] { p, q, l }; 317 | case 4: return new double[] { t, p, l }; 318 | case 5: return new double[] { l, p, q }; 319 | default: return null; 320 | } 321 | } 322 | 323 | /** 324 | * This method renders out the template data to a HTML canvas to finally 325 | * create the sprite. 326 | *

327 | * (note: only template locations with the values of -1 (border) are rendered) 328 | * 329 | * @method renderPixelData 330 | * @returns {undefined} 331 | */ 332 | public int[] renderPixelData() 333 | { 334 | random = new Random(SEED); 335 | bool isVerticalGradient = random.NextDouble() > 0.5; 336 | double saturation = Math.Max(Math.Min(random.NextDouble() * this.saturation, 1), 0); 337 | double hue = random.NextDouble(); 338 | int[] pixels = new int[height * width * 4]; 339 | 340 | int ulen, vlen; 341 | if (isVerticalGradient) 342 | { 343 | ulen = this.height; 344 | vlen = this.width; 345 | } 346 | else { 347 | ulen = this.width; 348 | vlen = this.height; 349 | } 350 | 351 | for (int u = 0; u < ulen; u++) 352 | { 353 | // Create a non-uniform random number between 0 and 1 (lower numbers more likely) 354 | double isNewColor = Math.Abs(((random.NextDouble() * 2 - 1) 355 | + (random.NextDouble() * 2 - 1) 356 | + (random.NextDouble() * 2 - 1)) / 3); 357 | // Only change the color sometimes (values above 0.8 are less likely than others) 358 | if (isNewColor > (1 - this.colorVariations)) 359 | { 360 | hue = random.NextDouble(); 361 | } 362 | 363 | //MessageBox.Show(this.toString()); 364 | 365 | for (int v = 0; v < vlen; v++) 366 | { 367 | int val, index; 368 | if (isVerticalGradient) 369 | { 370 | val = this.getData(v, u); 371 | index = (u * vlen + v) * 4; 372 | } 373 | else { 374 | val = this.getData(u, v); 375 | index = (v * ulen + u) * 4; 376 | } 377 | 378 | double[] rgb = new double[] { 1, 1, 1 }; 379 | 380 | if (val != 0) 381 | { 382 | if (this.colored) 383 | { 384 | // Fade brightness away towards the edges 385 | var brightness = Math.Sin(((double)u / (double)ulen) * Math.PI) * (1 - this.brightnessNoise) 386 | + random.NextDouble() * this.brightnessNoise; 387 | 388 | // Get the RGB color value 389 | rgb = this.hslToRgb(hue, saturation, brightness); 390 | 391 | // If this is an edge, then darken the pixel 392 | if (val == -1) 393 | { 394 | rgb[0] *= this.edgeBrightness; 395 | rgb[1] *= this.edgeBrightness; 396 | rgb[2] *= this.edgeBrightness; 397 | } 398 | 399 | } 400 | else { 401 | // Not colored, simply output black 402 | if (val == -1) 403 | { 404 | rgb = new double[] { 0, 0, 0 }; 405 | } 406 | } 407 | } 408 | 409 | pixels[index + 0] = (int)(rgb[0] * 255); 410 | pixels[index + 1] = (int)(rgb[1] * 255); 411 | pixels[index + 2] = (int)(rgb[2] * 255); 412 | if (val != 0) 413 | { 414 | pixels[index + 3] = 255; 415 | } 416 | else { 417 | pixels[index + 3] = 0; 418 | } 419 | } 420 | } 421 | 422 | return pixels; 423 | } 424 | 425 | public String toString() 426 | { 427 | int h = this.height; 428 | int w = this.width; 429 | String output = ""; 430 | for (int y = 0; y < h; y++) 431 | { 432 | for (int x = 0; x < w; x++) 433 | { 434 | var val = this.getData(x, y); 435 | output += val >= 0 ? " " + val : "" + val; 436 | } 437 | output += '\n'; 438 | } 439 | return output; 440 | } 441 | } 442 | } 443 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/Window1.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 | 29 | 30 | 31 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Pixel-Sprite-Generator-CSharp/Window1.xaml.cs: -------------------------------------------------------------------------------- 1 | using Pixel_Sprite_Generator.SpriteGen; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using System.Windows; 8 | using System.Windows.Controls; 9 | using System.Windows.Data; 10 | using System.Windows.Documents; 11 | using System.Windows.Input; 12 | using System.Windows.Media; 13 | using System.Windows.Media.Imaging; 14 | using System.Windows.Shapes; 15 | 16 | namespace Pixel_Sprite_Generator 17 | { 18 | public partial class Window1 : Window 19 | { 20 | Mask spaceship = new Mask(new int[]{ 21 | 0, 0, 0, 0, 0, 0, 22 | 0, 0, 0, 0, 1, 1, 23 | 0, 0, 0, 0, 1,-1, 24 | 0, 0, 0, 1, 1,-1, 25 | 0, 0, 0, 1, 1,-1, 26 | 0, 0, 1, 1, 1,-1, 27 | 0, 1, 1, 1, 2, 2, 28 | 0, 1, 1, 1, 2, 2, 29 | 0, 1, 1, 1, 2, 2, 30 | 0, 1, 1, 1, 1,-1, 31 | 0, 0, 0, 1, 1, 1, 32 | 0, 0, 0, 0, 0, 0 33 | }, 6, 12, true, false); 34 | Sprite sprite; 35 | 36 | public Window1() 37 | { 38 | InitializeComponent(); 39 | newBitmap(); 40 | } 41 | 42 | public void newBitmap() { 43 | int width = 12; 44 | int height = 12; 45 | int stride = width * 4; 46 | 47 | this.sprite = new Sprite(12, 12, spaceship, true); 48 | int[] spritePixels = this.sprite.renderPixelData(); 49 | 50 | // Create a writeable bitmap (which is a valid WPF Image Source 51 | WriteableBitmap bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null); 52 | 53 | // Create an array of pixels to contain pixel color values 54 | uint[] pixels = new uint[width * height]; 55 | 56 | int red; 57 | int green; 58 | int blue; 59 | int alpha; 60 | 61 | for (int x = 0; x < width; ++x) 62 | { 63 | for (int y = 0; y < height; ++y) 64 | { 65 | int i = width * y + x; 66 | int index = (width * y + x)*4; 67 | 68 | red = spritePixels[index]; 69 | green = spritePixels[index + 1]; 70 | blue = spritePixels[index + 2]; 71 | alpha = spritePixels[index + 3]; 72 | 73 | pixels[i] = (uint)((alpha << 24) + (red << 16) + (green << 8) + blue); 74 | } 75 | } 76 | 77 | // apply pixels to bitmap 78 | bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * 4, 0); 79 | 80 | // set image source to the new bitmap 81 | this.MainImage.Source = bitmap; 82 | } 83 | 84 | private void Button_Click(object sender, RoutedEventArgs e) { 85 | newBitmap(); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | All I did here was a quick lazy port of [zfedoran's procedural sprite generator](https://github.com/zfedoran/pixel-sprite-generator) to C# as a WPF app in Visual Studio 2015. 2 | 3 | The point of it was only to familiarize myself a little with C# basics. 4 | 5 | It should be easy to take what's in SpriteGen and put it in any game engine that uses C#. 6 | 7 | See also: [Shogan's port of this same algorithm for Unity](https://github.com/Shogan/PixelSpriteGenerator-Unity) and [my other port for Java + libGDX](https://github.com/BenMcLean/pixel-sprite-generator-libgdx) 8 | --------------------------------------------------------------------------------