Skip to content

Commit bf93fcb

Browse files
committed
Fix crashes when drawing base nodes that have "remapable bibs" with no actual remap on them, fix remap of base nodes being shifted towards pink
1 parent 0c09617 commit bf93fcb

File tree

2 files changed

+58
-36
lines changed

2 files changed

+58
-36
lines changed

src/TSMapEditor/Rendering/MapView.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,13 +1143,19 @@ private void RecordBaseNode(GraphicalBaseNode graphicalBaseNode)
11431143
return;
11441144
}
11451145

1146-
const float opacity = 0.35f;
1146+
const float opacity = 0.5f;
11471147

11481148
ShapeImage bibGraphics = TheaterGraphics.BuildingBibTextures[graphicalBaseNode.BuildingType.Index];
11491149
ShapeImage graphics = TheaterGraphics.BuildingTextures[graphicalBaseNode.BuildingType.Index];
11501150
Color replacementColor = Color.DarkBlue;
11511151
string iniName = graphicalBaseNode.BuildingType.ININame;
1152-
Color remapColor = (graphicalBaseNode.BuildingType.ArtConfig.Remapable ? graphicalBaseNode.Owner.XNAColor : Color.White) * opacity;
1152+
Color remapColor;
1153+
Color nonRemapColor = new Color(128, 128, 128, 255);
1154+
1155+
if (!graphicalBaseNode.BuildingType.ArtConfig.Remapable)
1156+
remapColor = nonRemapColor;
1157+
else
1158+
remapColor = new Color((byte)graphicalBaseNode.Owner.XNAColor.R, (byte)graphicalBaseNode.Owner.XNAColor.G, (byte)graphicalBaseNode.Owner.XNAColor.B, (byte)255) * opacity;
11531159

11541160
int yDrawOffset = Constants.CellSizeY / -2;
11551161
int frameIndex = 0;
@@ -1180,14 +1186,19 @@ private void RecordBaseNode(GraphicalBaseNode graphicalBaseNode)
11801186
objectSpriteRecord.AddGraphicsEntry(new ObjectSpriteEntry(bibGraphics.GetPaletteTexture(), bibFrame,
11811187
new Rectangle(bibFinalDrawPointX, bibFinalDrawPointY,
11821188
bibFrame.SourceRectangle.Width, bibFrame.SourceRectangle.Height),
1183-
remapColor, false, false, new DepthRectangle(1f, 1f)));
1189+
nonRemapColor * opacity, false, false, new DepthRectangle(1f, 1f)));
11841190

11851191
if (bibGraphics.HasRemapFrames())
11861192
{
1187-
objectSpriteRecord.AddGraphicsEntry(new ObjectSpriteEntry(bibGraphics.GetPaletteTexture(), bibGraphics.GetRemapFrame(0),
1188-
new Rectangle(bibFinalDrawPointX, bibFinalDrawPointY,
1189-
bibFrame.SourceRectangle.Width, bibFrame.SourceRectangle.Height),
1190-
remapColor, true, false, new DepthRectangle(1f, 1f)));
1193+
var remapFrame = bibGraphics.GetRemapFrame(0);
1194+
1195+
if (remapFrame != null)
1196+
{
1197+
objectSpriteRecord.AddGraphicsEntry(new ObjectSpriteEntry(bibGraphics.GetPaletteTexture(), bibGraphics.GetRemapFrame(0),
1198+
new Rectangle(bibFinalDrawPointX, bibFinalDrawPointY,
1199+
bibFrame.SourceRectangle.Width, bibFrame.SourceRectangle.Height),
1200+
remapColor, true, false, new DepthRectangle(1f, 1f)));
1201+
}
11911202
}
11921203
}
11931204
}
@@ -1205,12 +1216,17 @@ private void RecordBaseNode(GraphicalBaseNode graphicalBaseNode)
12051216
int y = drawPoint.Y - frame.ShapeHeight / 2 + frame.OffsetY + Constants.CellSizeY / 2 + yDrawOffset;
12061217
Rectangle drawRectangle = new Rectangle(x, y, frame.SourceRectangle.Width, frame.SourceRectangle.Height);
12071218

1208-
objectSpriteRecord.AddGraphicsEntry(new ObjectSpriteEntry(graphics.GetPaletteTexture(), texture, frame.SourceRectangle, drawRectangle, remapColor, false, false, new DepthRectangle(1f, 1f)));
1219+
objectSpriteRecord.AddGraphicsEntry(new ObjectSpriteEntry(graphics.GetPaletteTexture(), texture, frame.SourceRectangle, drawRectangle, nonRemapColor * opacity, false, false, new DepthRectangle(1f, 1f)));
12091220

12101221
if (graphics.HasRemapFrames())
12111222
{
1212-
objectSpriteRecord.AddGraphicsEntry(new ObjectSpriteEntry(graphics.GetPaletteTexture(), graphics.GetRemapFrame(frameIndex).Texture,
1213-
graphics.GetRemapFrame(frameIndex).SourceRectangle, drawRectangle, remapColor, true, false, new DepthRectangle(1f, 1f)));
1223+
var remapFrame = graphics.GetRemapFrame(frameIndex);
1224+
1225+
if (remapFrame != null)
1226+
{
1227+
objectSpriteRecord.AddGraphicsEntry(new ObjectSpriteEntry(graphics.GetPaletteTexture(), graphics.GetRemapFrame(frameIndex).Texture,
1228+
graphics.GetRemapFrame(frameIndex).SourceRectangle, drawRectangle, remapColor, true, false, new DepthRectangle(1f, 1f)));
1229+
}
12141230
}
12151231

12161232
objectSpriteRecord.AddTextEntry(new TextEntry("#" + baseNodeIndex, baseNodeIndexColor, drawPoint));

src/TSMapEditor/Rendering/ShapeImage.cs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -131,43 +131,49 @@ public void RenderToGraphicsPreparationObject(GraphicsPreparationClass graphicsP
131131
if (frameInfo == null || frameData == null)
132132
continue;
133133

134-
var positionedTexture = new PositionedTexture(shpFile.Width, shpFile.Height, frameInfo.XOffset, frameInfo.YOffset, null, Rectangle.Empty);
135-
136-
Point offset = graphicsPreparationObject.AddImage(frameInfo.Width, frameInfo.Height, frameData, positionedTexture);
137-
positionedTexture.SourceRectangle = new Rectangle(offset.X, offset.Y, frameInfo.Width, frameInfo.Height);
138-
Frames[i] = positionedTexture;
134+
byte[] remapColorArray = null;
135+
bool hasRemap = false;
139136

140137
if (remapable)
141138
{
142-
byte[] remapColorArray = frameData.Select(b =>
139+
remapColorArray = new byte[frameData.Length];
140+
141+
// If the sprite is remapable, copy the remapped areas to the remap sprite data
142+
// and erase them from the non-remapped sprite data.
143+
// We do this because the remapped areas are pink by default, causing sprites'
144+
// team colors to "shift" towards pink if sprites are drawn as transparent
145+
// (for example, in base nodes) without erasing the remap data from the regular
146+
// sprite, since sprites are drawn so that the regular sprite is drawn first,
147+
// and the teamcolored sprite is layered on top of the regular sprite.
148+
149+
// Also, we record whether the sprite actually has remap data at all - some
150+
// objects declare themselves as remapable, while actually having 0 remapable pixels.
151+
// We can save (V)RAM by not generating remap textures for those objects.
152+
for (int pixelIndex = 0; pixelIndex < frameData.Length; pixelIndex++)
143153
{
154+
byte b = frameData[pixelIndex];
144155
if (b >= 0x10 && b <= 0x1F)
145156
{
146-
// This is a remap color
147-
return (byte)b;
148-
}
149-
150-
return (byte)0;
151-
}).ToArray();
152-
153-
bool hasRemap = false;
154-
for (int b = 0; b < remapColorArray.Length; b++)
155-
{
156-
if (remapColorArray[b] != 0)
157-
{
157+
remapColorArray[pixelIndex] = b;
158158
hasRemap = true;
159-
break;
159+
frameData[pixelIndex] = 0;
160160
}
161161
}
162+
}
162163

163-
if (hasRemap)
164-
{
165-
var remapTexture = new PositionedTexture(shpFile.Width, shpFile.Height, frameInfo.XOffset, frameInfo.YOffset, null, Rectangle.Empty);
164+
var positionedTexture = new PositionedTexture(shpFile.Width, shpFile.Height, frameInfo.XOffset, frameInfo.YOffset, null, Rectangle.Empty);
166165

167-
offset = graphicsPreparationObject.AddImage(frameInfo.Width, frameInfo.Height, remapColorArray, remapTexture);
168-
remapTexture.SourceRectangle = new Rectangle(offset.X, offset.Y, frameInfo.Width, frameInfo.Height);
169-
RemapFrames[i] = remapTexture;
170-
}
166+
Point offset = graphicsPreparationObject.AddImage(frameInfo.Width, frameInfo.Height, frameData, positionedTexture);
167+
positionedTexture.SourceRectangle = new Rectangle(offset.X, offset.Y, frameInfo.Width, frameInfo.Height);
168+
Frames[i] = positionedTexture;
169+
170+
if (hasRemap)
171+
{
172+
var remapTexture = new PositionedTexture(shpFile.Width, shpFile.Height, frameInfo.XOffset, frameInfo.YOffset, null, Rectangle.Empty);
173+
174+
offset = graphicsPreparationObject.AddImage(frameInfo.Width, frameInfo.Height, remapColorArray, remapTexture);
175+
remapTexture.SourceRectangle = new Rectangle(offset.X, offset.Y, frameInfo.Width, frameInfo.Height);
176+
RemapFrames[i] = remapTexture;
171177
}
172178
}
173179
}

0 commit comments

Comments
 (0)