This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
SpriteImageParser.cs
executable file
·260 lines (202 loc) · 7.63 KB
/
SpriteImageParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// Copyright (c) Jesse Freeman, Pixel Vision 8. All rights reserved.
//
// Licensed under the Microsoft Public License (MS-PL) except for a few
// portions of the code. See LICENSE file in the project root for full
// license information. Third-party libraries used by Pixel Vision 8 are
// under their own licenses. Please refer to those libraries for details
// on the license they use.
//
// Contributors
// --------------------------------------------------------
// This is the official list of Pixel Vision 8 contributors:
//
// Jesse Freeman - @JesseFreeman
// Christina-Antoinette Neofotistou @CastPixel
// Christer Kaitila - @McFunkypants
// Pedro Medeiros - @saint11
// Shawn Rakowski - @shwany
//
using System;
using System.Collections.Generic;
using System.Linq;
using PixelVision8.Player;
namespace PixelVision8.Runner
{
public class SpriteImageParser : ImageParser
{
protected ColorChip colorChip;
protected int cps;
protected int index;
protected int maxSprites;
protected SpriteChip spriteChip;
protected int[] spriteData;
protected int spriteHeight = 8;
protected int spritesAdded;
protected int spriteWidth = 8;
protected int totalSprites;
protected int x, y;
public ImageData ImageData;
public SpriteImageParser(string sourceFile, IImageParser parser, ColorChip colorChip, SpriteChip spriteChip = null) : base(parser)
{
SourcePath = sourceFile;
Parser = parser;
// this.chips = chips;
this.spriteChip = spriteChip;
this.colorChip = colorChip;
// Read the color chip's mask color
MaskHex = colorChip.maskColor;
if (spriteChip != null)
{
spriteWidth = this.spriteChip.width;
spriteHeight = this.spriteChip.height;
}
}
public override void CalculateSteps()
{
base.CalculateSteps();
_steps.Add(CreateImage);
if (spriteChip != null)
{
_steps.Add(PrepareSprites);
_steps.Add(CutOutSprites);
_steps.Add(PostCutOutSprites);
}
}
public virtual void PrepareSprites()
{
cps = spriteChip.colorsPerSprite;
totalSprites = ImageData.TotalSprites;
//TODO this needs to be double checked at different size sprites
var cols = Utilities.FloorToInt(spriteChip.textureWidth / spriteWidth);
var rows = Utilities.FloorToInt(spriteChip.textureHeight / spriteHeight);
maxSprites = cols * rows;
// // Keep track of number of sprites added
spritesAdded = 0;
StepCompleted();
}
public virtual void CreateImage()
{
// Get the chip colors and replace any transparent ones with the first color so we don't parse transparency
var colorData = Utilities.ConvertColors(colorChip.hexColors, colorChip.maskColor);
// colorData = colorChip.colors;
var colorRefs = colorData.Select(c => Utilities.RgbToHex(c.R, c.G, c.B)).ToArray();
// Remove the colors that are not supported
Array.Resize(ref colorRefs, colorChip.totalUsedColors);
var imageColors = Parser.colorPalette.Select(c => Utilities.RgbToHex(c.R, c.G, c.B)).ToArray();
var colorMap = new string[colorRefs.Length];
var orphanColors = new List<string>();
var uniqueColorIDs = new List<int>();
var totalImageColors = imageColors.Length;
// var totalColorRefs = colorRefs.Length;
for (int i = 0; i < totalImageColors; i++)
{
// var color = imageColors[i];
var color = imageColors[i];
if (color == colorChip.maskColor) continue;
var id = Array.IndexOf(colorRefs, color);
// Keep track of colors in and out of range
if (uniqueColorIDs.IndexOf(id) == -1)
{
uniqueColorIDs.Add(id);
}
if (id > -1)
{
colorMap[id] = color;
}
else
{
orphanColors.Add(color);
}
}
// Sort colors
uniqueColorIDs.Sort();
var indexes = new List<int>();
// find open slots
for (int i = 0; i < colorMap.Length; i++)
{
if (colorMap[i] == null)
{
indexes.Add(i);
}
}
var totalOrphanColors = orphanColors.Count;
for (int i = 0; i < indexes.Count; i++)
{
if (i < totalOrphanColors)
{
colorMap[indexes[i]] = orphanColors[i];
}
}
// clean up the color map
for (int i = 0; i < colorMap.Length; i++)
{
if (colorMap[i] == null)
{
colorMap[i] = colorRefs[i];
}
}
// Convert all of the pixels into color ids
var pixelIDs = Parser.colorPixels.Select(c => Array.IndexOf(colorMap, Utilities.RgbToHex(c.R, c.G, c.B))).ToArray();
ImageData = new ImageData(Parser.width, Parser.height, pixelIDs, colorMap);
StepCompleted();
}
public virtual void CutOutSprites()
{
for (var i = 0; i < totalSprites; i++)
{
// Convert sprite to color index
ConvertColorsToIndexes(cps);
ProcessSpriteData();
index++;
}
StepCompleted();
}
protected virtual void PostCutOutSprites()
{
// Add custom logic
StepCompleted();
}
protected virtual void ProcessSpriteData()
{
if (spritesAdded < maxSprites)
{
// TODO need to deprecate this since the sprite file should load up exactly how it is read
if (spriteChip.unique)
{
if (spriteChip.FindSprite(spriteData) == -1)
{
spriteChip.UpdateSpriteAt(spritesAdded, spriteData);
spritesAdded++;
}
}
else
{
if (spriteChip.IsEmpty(spriteData) == false)
{
spriteChip.UpdateSpriteAt(index, spriteData);
spritesAdded++;
}
}
}
}
public virtual void ConvertColorsToIndexes(int totalColors)
{
spriteData = ImageData.GetSpriteData(index, totalColors);
}
public override void Dispose()
{
base.Dispose();
colorChip = null;
spriteChip = null;
}
}
public partial class Loader
{
[FileParser("sprite.png")]
public void ParseSprites(string file, IPlayerChips engine)
{
AddParser(new SpriteImageParser(file, _imageParser, engine.ColorChip, engine.SpriteChip));
}
}
}