Skip to content

Commit f67138f

Browse files
committed
Fix bug where some buildings' remap sprites were off by a pixel
1 parent 36c9fd9 commit f67138f

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/TSMapEditor/Rendering/ObjectRenderers/BuildingRenderer.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,6 @@ protected override void Render(Structure gameObject, Point2D drawPoint, in Commo
307307
nonRemapColor, true, gameObject.GetRemapColor(), affectedByLighting, affectedByAmbient,
308308
drawPoint, depthAddition);
309309
}
310-
311-
//buildingAnimRenderer.BuildingAnimDepthAddition = depthAddition - Constants.DepthEpsilon;
312-
//buildingAnimRenderer.Draw(anim, false);
313310
}
314311
}
315312

@@ -419,6 +416,16 @@ private void DrawBuildingImage(Structure gameObject, ShapeImage image, int frame
419416
depthRectangleRight += depthAddition;
420417
depthRectangleRight += GetDepthAddition(gameObject);
421418

419+
// Check for truncation due to integer-based source rectangle math
420+
// For example, a texture width of 91 pixels will give us two 45-pixel wide rectangles,
421+
// or an even-width texture like 104 might give us one uneven rectangle due to truncating
422+
// in source rectangle computation
423+
if (sourceRectangleLeft.Width + sourceRectangleRight.Width != drawingBounds.Width)
424+
{
425+
sourceRectangleRight.X = sourceRectangleRight.X - 1;
426+
sourceRectangleRight.Width = sourceRectangleRight.Width + 1;
427+
}
428+
422429
color = new Color((color.R / 255.0f) * lighting.X / 2f,
423430
(color.B / 255.0f) * lighting.Y / 2f,
424431
(color.B / 255.0f) * lighting.Z / 2f, color.A);
@@ -431,17 +438,11 @@ private void DrawBuildingImage(Structure gameObject, ShapeImage image, int frame
431438

432439
if (drawRemap && remapFrame != null)
433440
{
434-
remapColor = new Color(
435-
(remapColor.R / 255.0f),
436-
(remapColor.G / 255.0f),
437-
(remapColor.B / 255.0f),
438-
color.A);
439-
440-
sourceRectangleLeft = new Rectangle(remapFrame.SourceRectangle.X, remapFrame.SourceRectangle.Y, sourceRectangleLeft.Width, remapFrame.SourceRectangle.Height);
441-
sourceRectangleRight = new Rectangle(remapFrame.SourceRectangle.X + sourceRectangleLeft.Width, remapFrame.SourceRectangle.Y, sourceRectangleRight.Width, remapFrame.SourceRectangle.Height);
441+
Rectangle remapSourceRectangleLeft = sourceRectangleLeft with { X = remapFrame.SourceRectangle.X, Y = remapFrame.SourceRectangle.Y };
442+
Rectangle remapSourceRectangleRight = sourceRectangleRight with { X = remapFrame.SourceRectangle.X + sourceRectangleLeft.Width, Y = remapFrame.SourceRectangle.Y };
442443

443-
RenderDependencies.ObjectSpriteRecord.AddGraphicsEntry(new ObjectSpriteEntry(image.GetPaletteTexture(), remapFrame.Texture, sourceRectangleLeft, drawingBoundsLeft, remapColor, true, false, depthRectangleLeft + Constants.DepthEpsilon));
444-
RenderDependencies.ObjectSpriteRecord.AddGraphicsEntry(new ObjectSpriteEntry(image.GetPaletteTexture(), remapFrame.Texture, sourceRectangleRight, drawingBoundsRight, remapColor, true, false, depthRectangleRight + Constants.DepthEpsilon));
444+
RenderDependencies.ObjectSpriteRecord.AddGraphicsEntry(new ObjectSpriteEntry(image.GetPaletteTexture(), remapFrame.Texture, remapSourceRectangleLeft, drawingBoundsLeft, remapColor, true, false, depthRectangleLeft + Constants.DepthEpsilon));
445+
RenderDependencies.ObjectSpriteRecord.AddGraphicsEntry(new ObjectSpriteEntry(image.GetPaletteTexture(), remapFrame.Texture, remapSourceRectangleRight, drawingBoundsRight, remapColor, true, false, depthRectangleRight + Constants.DepthEpsilon));
445446
}
446447
}
447448

0 commit comments

Comments
 (0)