Skip to content

Commit 41d3f0c

Browse files
authored
Merge pull request #509 from twpol/feature/particle-colour
fix: Swap colour bytes from MSTS/XNA order to Monogame/DX order
2 parents e873707 + 976d15c commit 41d3f0c

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

Source/Orts.Formats.Msts/LightCollection.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace Orts.Formats.Msts
3131
public class LightState
3232
{
3333
public float Duration;
34-
public uint Color;
34+
public Color Color;
3535
public Vector3 Position;
3636
public float Radius;
3737
public Vector3 Azimuth;
@@ -44,22 +44,14 @@ public LightState(STFReader stf)
4444
stf.MustMatch("(");
4545
stf.ParseBlock(new[] {
4646
new STFReader.TokenProcessor("duration", ()=>{ Duration = stf.ReadFloatBlock(STFReader.UNITS.None, null); }),
47-
new STFReader.TokenProcessor("lightcolour", ()=>{ Color = stf.ReadHexBlock(null); }),
47+
new STFReader.TokenProcessor("lightcolour", ()=>{ Color = stf.ReadColorBlock(null); }),
4848
new STFReader.TokenProcessor("position", ()=>{ Position = stf.ReadVector3Block(STFReader.UNITS.None, Vector3.Zero); }),
4949
new STFReader.TokenProcessor("radius", ()=>{ Radius = stf.ReadFloatBlock(STFReader.UNITS.Distance, null); }),
5050
new STFReader.TokenProcessor("azimuth", ()=>{ Azimuth = stf.ReadVector3Block(STFReader.UNITS.None, Vector3.Zero); }),
5151
new STFReader.TokenProcessor("elevation", ()=>{ Elevation = stf.ReadVector3Block(STFReader.UNITS.None, Vector3.Zero); }),
5252
new STFReader.TokenProcessor("transition", ()=>{ Transition = 1 <= stf.ReadFloatBlock(STFReader.UNITS.None, 0); }),
5353
new STFReader.TokenProcessor("angle", ()=>{ Angle = stf.ReadFloatBlock(STFReader.UNITS.None, null); }),
5454
});
55-
// Color byte order changed in XNA 4 from BGRA to RGBA
56-
Color = new Color()
57-
{
58-
B = (byte)(Color),
59-
G = (byte)(Color >> 8),
60-
R = (byte)(Color >> 16),
61-
A = (byte)(Color >> 24)
62-
}.PackedValue;
6355
}
6456

6557
public LightState(LightState state, bool reverse)

Source/Orts.Parsers.Msts/STFReader.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,20 @@ public string ReadStringBlock(string defaultValue)
11691169
return defaultValue;
11701170
}
11711171

1172+
/// <summary>Read a hexadecimal encoded color from the STF format '( {color_constant} ... )'
1173+
/// </summary>
1174+
/// <param name="defaultValue">the default value if the constant is not found in the block.</param>
1175+
/// <returns>The STF block with the first {item} converted to a color constant.</returns>
1176+
public Color ReadColorBlock(Color? defaultValue)
1177+
{
1178+
var hex = this.ReadHexBlock(STFReader.SwapColorBytes(defaultValue.GetValueOrDefault(Color.Black).PackedValue));
1179+
return new Color() { PackedValue = STFReader.SwapColorBytes(hex) };
1180+
}
1181+
1182+
static uint SwapColorBytes(uint color) {
1183+
return (color & 0xFF00FF00) + (byte)(color >> 16) + (uint)((byte)color << 16);
1184+
}
1185+
11721186
/// <summary>Read an hexidecimal encoded number from the STF format '( {int_constant} ... )'
11731187
/// </summary>
11741188
/// <param name="defaultValue">the default value if the constant is not found in the block.</param>

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/DieselEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,8 @@ public virtual void Parse(STFReader stf)
842842
case "maxexhaust": MaxExhaust = stf.ReadFloatBlock(STFReader.UNITS.None, 0);initLevel |= SettingsFlags.MaxExhaust; break;
843843
case "exhaustdynamics": ExhaustAccelIncrease = stf.ReadFloatBlock(STFReader.UNITS.None, 0); initLevel |= SettingsFlags.ExhaustDynamics; break;
844844
case "exhaustdynamicsdown": ExhaustDecelReduction = stf.ReadFloatBlock(STFReader.UNITS.None, null); initLevel |= SettingsFlags.ExhaustDynamics; break;
845-
case "exhaustcolor": ExhaustSteadyColor.PackedValue = stf.ReadHexBlock(Color.Gray.PackedValue); initLevel |= SettingsFlags.ExhaustColor; break;
846-
case "exhausttransientcolor": ExhaustTransientColor.PackedValue = stf.ReadHexBlock(Color.Black.PackedValue);initLevel |= SettingsFlags.ExhaustTransientColor; break;
845+
case "exhaustcolor": ExhaustSteadyColor = stf.ReadColorBlock(Color.Gray); initLevel |= SettingsFlags.ExhaustColor; break;
846+
case "exhausttransientcolor": ExhaustTransientColor = stf.ReadColorBlock(Color.Black);initLevel |= SettingsFlags.ExhaustTransientColor; break;
847847
case "dieselpowertab": DieselPowerTab = new Interpolator(stf);initLevel |= SettingsFlags.DieselPowerTab; break;
848848
case "dieselconsumptiontab": DieselConsumptionTab = new Interpolator(stf);initLevel |= SettingsFlags.DieselConsumptionTab; break;
849849
case "throttlerpmtab":

Source/RunActivity/Viewer3D/Lights.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public static void CalculateLightCone(LightState lightState, out Vector3 positio
202202
angle = MathHelper.ToRadians(lightState.Angle) / 2;
203203
radius = lightState.Radius / 2;
204204
distance = (float)(radius / Math.Sin(angle));
205-
color = new Color() { PackedValue = lightState.Color }.ToVector4();
205+
color = lightState.Color.ToVector4();
206206
}
207207

208208
#if DEBUG_LIGHT_STATES
@@ -535,11 +535,11 @@ public LightGlowPrimitive(LightViewer lightViewer, RenderProcess renderProcess,
535535

536536
var position1 = state1.Position; position1.Z *= -1;
537537
var normal1 = Vector3.Transform(Vector3.Transform(-Vector3.UnitZ, Matrix.CreateRotationX(MathHelper.ToRadians(-state1.Elevation.Y))), Matrix.CreateRotationY(MathHelper.ToRadians(-state1.Azimuth.Y)));
538-
var color1 = new Color() { PackedValue = state1.Color }.ToVector4();
538+
var color1 = state1.Color.ToVector4();
539539

540540
var position2 = state2.Position; position2.Z *= -1;
541541
var normal2 = Vector3.Transform(Vector3.Transform(-Vector3.UnitZ, Matrix.CreateRotationX(MathHelper.ToRadians(-state2.Elevation.Y))), Matrix.CreateRotationY(MathHelper.ToRadians(-state2.Azimuth.Y)));
542-
var color2 = new Color() { PackedValue = state2.Color }.ToVector4();
542+
var color2 = state2.Color.ToVector4();
543543

544544
vertexData[6 * state + 0] = new LightGlowVertex(new Vector2(1, 1), position1, position2, normal1, normal2, color1, color2, state1.Radius, state2.Radius);
545545
vertexData[6 * state + 1] = new LightGlowVertex(new Vector2(0, 0), position1, position2, normal1, normal2, color1, color2, state1.Radius, state2.Radius);

0 commit comments

Comments
 (0)