Skip to content

Commit df3b02a

Browse files
committed
Bug exporting attributes with mask
1 parent 4ea3227 commit df3b02a

3 files changed

Lines changed: 104 additions & 30 deletions

File tree

ZXBStudio/DocumentEditors/ZXGraphics/SpriteExportDialog.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<CheckBox Name="chkAttr" Grid.Row="5" Grid.Column="1" Classes="dialog"></CheckBox>
5353
</Grid>
5454

55-
<TextBlock Name="txtError" FontSize="14" Foreground="Red" Margin="0,16,0,16" IsVisible="False">
55+
<TextBlock Name="txtError" FontSize="14" Foreground="Red" Margin="0,16,0,16" TextWrapping="Wrap" IsVisible="False">
5656
You have not selected any sprite to export. Check the "Include in the export" box of the sprites you want to include in the export.
5757
</TextBlock>
5858
<TextBlock FontSize="14" Margin="0,16,0,0">

ZXBStudio/DocumentEditors/ZXGraphics/SpriteExportDialog.axaml.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ private void CreateExample_PutChars()
178178
txtCode.Text = "";
179179
}
180180

181+
var sprite = sprites.ElementAt(0);
182+
var res = ExportManager.Export_Sprite_PutChars(exportConfig, sprites);
183+
if (res.StartsWith("ERROR:"))
184+
{
185+
txtError.Text = res;
186+
txtError.IsVisible = true;
187+
return;
188+
}
189+
else
190+
{
191+
txtError.IsVisible = false;
192+
}
193+
181194
var sb = new StringBuilder();
182195
switch (exportConfig.ExportDataType)
183196
{
@@ -188,11 +201,10 @@ private void CreateExample_PutChars()
188201
sb.AppendLine("");
189202
sb.AppendLine(string.Format("' Can use: #INCLUDE \"{0}\"",
190203
Path.GetFileName(exportConfig.ExportFilePath)));
191-
sb.AppendLine(ExportManager.Export_Sprite_PutChars(exportConfig, sprites));
204+
sb.AppendLine(res);
192205
sb.AppendLine("");
193206
sb.AppendLine("'- Draw sprite --------------------------------------------");
194207

195-
var sprite = sprites.ElementAt(0);
196208
sb.AppendLine(string.Format(
197209
"putChars(10,5,{0},{1},@{2}{3}({4}))",
198210
sprite.Width / 8,
@@ -210,7 +222,6 @@ private void CreateExample_PutChars()
210222
sb.AppendLine("#INCLUDE <putchars.bas>");
211223
sb.AppendLine("");
212224
sb.AppendLine("'- Draw sprite --------------------------------------------");
213-
var sprite = sprites.ElementAt(0);
214225
sb.AppendLine(string.Format(
215226
"putChars(10,5,{0},{1},@{2}{3})",
216227
sprite.Width / 8,
@@ -221,7 +232,7 @@ private void CreateExample_PutChars()
221232
sb.AppendLine("' This section must not be executed");
222233
sb.AppendLine(string.Format("' Can use: #INCLUDE \"{0}\"",
223234
Path.GetFileName(exportConfig.ExportFilePath)));
224-
sb.AppendLine(ExportManager.Export_Sprite_PutChars(exportConfig, sprites));
235+
sb.AppendLine(res);
225236
}
226237
break;
227238

@@ -231,7 +242,6 @@ private void CreateExample_PutChars()
231242
sb.AppendLine("#INCLUDE <putchars.bas>");
232243
sb.AppendLine("");
233244
sb.AppendLine("'- Draw sprite --------------------------------------------");
234-
var sprite = sprites.ElementAt(0);
235245
sb.AppendLine(string.Format(
236246
"putChars(10,5,{0},{1},@{2})",
237247
sprite.Width / 8,
@@ -258,7 +268,6 @@ private void CreateExample_PutChars()
258268
sb.AppendLine("LOAD \"\" CODE");
259269
sb.AppendLine("");
260270
sb.AppendLine("'- Draw sprite --------------------------------------------");
261-
var sprite = sprites.ElementAt(0);
262271
sb.AppendLine(string.Format(
263272
"putChars(10,5,{0},{1},@{2})",
264273
sprite.Width / 8,

ZXBStudio/DocumentEditors/ZXGraphics/log/ExportManager.cs

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ public bool Build(string BuildPath, ZXBuildStage Stage, ZXBuildType BuildType, Z
8282
case FileTypes.Sprite:
8383
{
8484
var sprites = CreateSprites(fileData);
85-
if(!ExportSprites(exportConfig, sprites))
85+
if (!ExportSprites(exportConfig, sprites))
8686
{
8787
return false;
88-
}
88+
}
8989
}
9090
break;
9191

@@ -447,7 +447,7 @@ public bool ExportSprites(ExportConfig exportConfig, IEnumerable<Sprite> sprites
447447
break;
448448
case ExportTypes.MaskedSprites:
449449
exportedData = Export_Sprite_MaskedSprites(exportConfig, sprites);
450-
if(exportedData.StartsWith("ERROR:"))
450+
if (exportedData.StartsWith("ERROR:"))
451451
{
452452
return false;
453453
}
@@ -482,6 +482,15 @@ public bool ExportSprites(ExportConfig exportConfig, IEnumerable<Sprite> sprites
482482
/// <returns>string with the conversion commands for the export dialog samble textbox</returns>
483483
public static string Export_Sprite_PutChars(ExportConfig exportConfig, IEnumerable<Sprite> sprites)
484484
{
485+
string res = Export_Sprite_PutChars_Check(exportConfig, sprites);
486+
if (res.StartsWith("ERROR:"))
487+
{
488+
if (OutputLog != null)
489+
{
490+
OutputLog.WriteLine(res);
491+
}
492+
return res;
493+
}
485494
switch (exportConfig.ExportDataType)
486495
{
487496
case ExportDataTypes.DIM:
@@ -497,6 +506,26 @@ public static string Export_Sprite_PutChars(ExportConfig exportConfig, IEnumerab
497506
}
498507
}
499508

509+
private static string Export_Sprite_PutChars_Check(ExportConfig exportConfig, IEnumerable<Sprite> sprites)
510+
{
511+
foreach (var sprite in sprites)
512+
{
513+
if (sprite == null)
514+
{
515+
continue;
516+
}
517+
if (sprite.Masked)
518+
{
519+
if ((sprite.Patterns.Count % 2) != 0)
520+
{
521+
var name = sprite.Name.Replace(' ', '_');
522+
return $"ERROR: The number of frames in the sprite \"{name}\" must be even if it has a mask.";
523+
}
524+
}
525+
}
526+
return "OK";
527+
}
528+
500529

501530
#region DIM
502531

@@ -674,19 +703,31 @@ public static string Export_Sprite_PutChars_DIM(ExportConfig exportConfig, IEnum
674703
}
675704
else
676705
{
677-
// Mulriple sprites attributes
706+
// Multiple sprites attributes
678707
// Header
708+
int fr = sprite.Masked ? (sprite.Frames / 2) : (sprite.Frames - 1);
679709
sb.AppendLine(string.Format(
680710
"DIM {0}{1}_Attr({2},{3}) AS UByte => {{ _",
681711
exportConfig.LabelName,
682712
sprite.Name.Replace(" ", "_"),
683-
sprite.Frames - 1 + min,
713+
fr - 1 + min,
684714
((sprite.Width / 8) * (sprite.Height / 8)) - 1 + min));
685715
// Data
686-
for (int n = 0; n < sprite.Patterns.Count; n++)
716+
if (sprite.Masked)
687717
{
688-
var data = Export_Sprite_PutChars_Attribute(sprite, n, exportConfig, 0);
689-
sb.Append(data);
718+
for (int n = 0; n < sprite.Patterns.Count; n += 2)
719+
{
720+
var data = Export_Sprite_PutChars_Attribute(sprite, n, exportConfig, 0);
721+
sb.Append(data);
722+
}
723+
}
724+
else
725+
{
726+
for (int n = 0; n < sprite.Patterns.Count; n++)
727+
{
728+
var data = Export_Sprite_PutChars_Attribute(sprite, n, exportConfig, 0);
729+
sb.Append(data);
730+
}
690731
}
691732
// Footer
692733
sb.Append(" _\r\n}");
@@ -775,19 +816,37 @@ private static string Export_Sprite_PutChars_Attribute(Sprite sprite, int n, Exp
775816
return "";
776817
}
777818

778-
if (sprite.Frames > 1)
819+
if (sprite.Masked)
779820
{
780-
if (n > firstItem)
821+
if (sprite.Frames > 2)
781822
{
782-
sb.AppendLine(", _");
823+
if (n > firstItem)
824+
{
825+
sb.AppendLine(", _");
826+
}
827+
sb.AppendLine("\t{ _");
783828
}
784-
sb.AppendLine("\t{ _");
785829
}
830+
else
831+
if (sprite.Frames > 1)
832+
{
833+
if (n > firstItem)
834+
{
835+
sb.AppendLine(", _");
836+
}
837+
sb.AppendLine("\t{ _");
838+
}
786839

787840
int col = 0;
788841
int row = 0;
842+
int max = (sprite.Width / 8) * (sprite.Height / 8);
843+
int idx = 0;
789844
foreach (var d in pattern.Attributes)
790845
{
846+
if (idx++ >= max)
847+
{
848+
break;
849+
}
791850
if (col == 0)
792851
{
793852
if (row == 0)
@@ -984,8 +1043,14 @@ private static string Export_Sprite_PutChars_Attribute_ASM(Sprite sprite, int n,
9841043

9851044
int col = 0;
9861045
int row = 0;
1046+
int max = (sprite.Width / 8) * (sprite.Height / 8);
1047+
int idx = 0;
9871048
foreach (var d in pattern.Attributes)
9881049
{
1050+
if (idx++ >= max)
1051+
{
1052+
break;
1053+
}
9891054
if (col == 0)
9901055
{
9911056
if (row == 0)
@@ -1074,9 +1139,9 @@ private static byte[] Export_Sprite_PutChars_GetBinaryData(IEnumerable<Sprite> s
10741139
/// <returns>string with the conversion commands for the export dialog samble textbox</returns>
10751140
public static string Export_Sprite_MaskedSprites(ExportConfig exportConfig, IEnumerable<Sprite> sprites)
10761141
{
1077-
var res= Export_Sprite_MaskedSprites_Check(exportConfig, sprites);
1078-
if(res.StartsWith("ERROR:"))
1079-
{
1142+
var res = Export_Sprite_MaskedSprites_Check(exportConfig, sprites);
1143+
if (res.StartsWith("ERROR:"))
1144+
{
10801145
return res;
10811146
}
10821147
switch (exportConfig.ExportDataType)
@@ -1110,28 +1175,28 @@ public static string Export_Sprite_MaskedSprites(ExportConfig exportConfig, IEnu
11101175
private static string Export_Sprite_MaskedSprites_Check(ExportConfig exportConfig, IEnumerable<Sprite> sprites)
11111176
{
11121177
string txt = "";
1113-
foreach(var sprite in sprites)
1178+
foreach (var sprite in sprites)
11141179
{
11151180
if (sprite != null && sprite.Export)
11161181
{
11171182
if (!sprite.Masked)
11181183
{
1119-
txt=$"ERROR: Sprite {sprite.Name} is not masked.";
1184+
txt = $"ERROR: Sprite {sprite.Name} is not masked.";
11201185
}
11211186
if (sprite.Frames % 2 != 0)
11221187
{
1123-
txt=$"ERROR: Sprite {sprite.Name} has an odd number of frames.";
1188+
txt = $"ERROR: Sprite {sprite.Name} has an odd number of frames.";
11241189
}
1125-
if(sprite.Width!=16 || sprite.Height != 16)
1190+
if (sprite.Width != 16 || sprite.Height != 16)
11261191
{
1127-
txt=$"ERROR: Sprite {sprite.Name} has a size different than 16x16.";
1192+
txt = $"ERROR: Sprite {sprite.Name} has a size different than 16x16.";
11281193
}
11291194
if (!string.IsNullOrEmpty(txt))
11301195
{
1131-
if(OutputLog != null)
1196+
if (OutputLog != null)
11321197
{
11331198
OutputLog.WriteLine(txt);
1134-
}
1199+
}
11351200
return txt;
11361201
}
11371202
}
@@ -1202,7 +1267,7 @@ public static string Export_Sprite_MaskedSprites_DIM(ExportConfig exportConfig,
12021267
else
12031268
{
12041269
sb.AppendLine(
1205-
$"DIM {exportConfig.LabelName}{sprite.Name.Replace(" ", "_")}({(sprite.Frames/2)-1},63) AS UByte => {{ _");
1270+
$"DIM {exportConfig.LabelName}{sprite.Name.Replace(" ", "_")}({(sprite.Frames / 2) - 1},63) AS UByte => {{ _");
12061271
}
12071272
var rawData = Export_Sprite_MaskedSprites_GenerateData(exportConfig, sprite);
12081273
int i = 0;

0 commit comments

Comments
 (0)