2016-11-25 06:05:29 +00:00
|
|
|
|
namespace SpriteCompiler
|
|
|
|
|
{
|
2016-12-05 05:14:51 +00:00
|
|
|
|
using Fclp;
|
|
|
|
|
using SpriteCompiler.Problem;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2016-12-06 03:55:04 +00:00
|
|
|
|
using System.Drawing;
|
2016-12-05 05:14:51 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
public class ApplicationArguments
|
|
|
|
|
{
|
|
|
|
|
public List<string> Data { get; set; }
|
|
|
|
|
public List<string> Mask { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-25 06:05:29 +00:00
|
|
|
|
public class Program
|
|
|
|
|
{
|
2016-12-05 05:14:51 +00:00
|
|
|
|
public static void WriteOutSolution(IEnumerable<SpriteGeneratorSearchNode> solution)
|
|
|
|
|
{
|
|
|
|
|
foreach (var step in solution.Skip(1))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(step.Action.Emit());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(string.Format("; Total Cost = {0} cycles", (int)solution.Last().PathCost));
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-25 06:05:29 +00:00
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
2016-12-06 03:55:04 +00:00
|
|
|
|
IEnumerable<SpriteByte> data = null;
|
2016-12-05 05:14:51 +00:00
|
|
|
|
|
2016-12-06 03:55:04 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
data = args.Select((s, i) => new SpriteByte(Convert.ToByte(s, 16), (ushort)i));
|
|
|
|
|
}
|
|
|
|
|
catch (FormatException e)
|
|
|
|
|
{
|
|
|
|
|
// If there is only one or two arguments, them marybe the user passed in a file
|
|
|
|
|
if (args.Length <= 2)
|
|
|
|
|
{
|
|
|
|
|
var palette = new Dictionary<Color, int>();
|
|
|
|
|
int nextIndex = 1;
|
|
|
|
|
|
|
|
|
|
// Convert the image / mask to a paletted image
|
|
|
|
|
var bitmap = new Bitmap(args[0]);
|
|
|
|
|
int[,] buffer = new int[bitmap.Width, bitmap.Height];
|
|
|
|
|
|
|
|
|
|
for (int r = 0; r < bitmap.Height; r++)
|
|
|
|
|
{
|
|
|
|
|
for (int w = 0; w < bitmap.Width; w++)
|
|
|
|
|
{
|
|
|
|
|
var rgb = bitmap.GetPixel(w, r);
|
2016-12-05 05:14:51 +00:00
|
|
|
|
|
2016-12-06 03:55:04 +00:00
|
|
|
|
if (!palette.ContainsKey(rgb))
|
|
|
|
|
{
|
|
|
|
|
if (palette.Count >= 15)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Image cannot have more than 15 unique colors");
|
|
|
|
|
}
|
|
|
|
|
palette[rgb] = nextIndex++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer[w, r] = palette[rgb];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pair up pixles to build bytes
|
|
|
|
|
var sprite = new List<SpriteByte>();
|
|
|
|
|
|
|
|
|
|
for (int r = 0; r < bitmap.Height; r++)
|
|
|
|
|
{
|
|
|
|
|
for (int w = 0; w < bitmap.Width; w += 2)
|
|
|
|
|
{
|
|
|
|
|
sprite.Add(new SpriteByte((byte)((buffer[w, r] << 4) + buffer[w + 1, r]), (ushort)(r * 160 + (w / 2))));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data = sprite;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-05 05:14:51 +00:00
|
|
|
|
/*
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var p = new FluentCommandLineParser<ApplicationArguments>();
|
|
|
|
|
|
|
|
|
|
// specify which property the value will be assigned too.
|
|
|
|
|
p.Setup<List<string>>(arg => arg.Data)
|
|
|
|
|
.As('d', "data") // define the short and long option name
|
|
|
|
|
.Required() // using the standard fluent Api to declare this Option as required.
|
|
|
|
|
.Callback(d => d.Select(s => Convert.ToByte(s, 16)).ToArray());
|
|
|
|
|
|
|
|
|
|
p.Setup<List<string>>(arg => arg.Mask)
|
|
|
|
|
.As('m', "mask");
|
|
|
|
|
|
|
|
|
|
var result = p.Parse(args);
|
|
|
|
|
|
|
|
|
|
if (!result.HasErrors)
|
|
|
|
|
{
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
var problem = SpriteGeneratorSearchProblem.CreateSearchProblem();
|
|
|
|
|
var search = SpriteGeneratorSearchProblem.Create();
|
|
|
|
|
|
|
|
|
|
var solution = search.Search(problem, new SpriteGeneratorState(data));
|
|
|
|
|
|
|
|
|
|
WriteOutSolution(solution);
|
|
|
|
|
//}
|
2016-11-25 06:05:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|