Skip to content

Commit

Permalink
Use types for room sprites (LostArtefacts#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahm86 authored Apr 30, 2024
1 parent 7fc5ddf commit d97ddf9
Show file tree
Hide file tree
Showing 29 changed files with 149 additions and 192 deletions.
Binary file modified Deps/TRGE.Coord.dll
Binary file not shown.
122 changes: 38 additions & 84 deletions TREnvironmentEditor/Model/Types/Mirroring/EMMirrorFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,35 +298,8 @@ private void MirrorRooms(TR1Level level)
room.Info.X = FlipWorldX(oldRoomX);
room.Info.X -= room.NumXSectors * TRConsts.Step4;
Debug.Assert(room.Info.X >= 0);
// Flip room sprites separately as they don't sit on tile edges
List<TR1RoomVertex> processedVerts = new();
foreach (TRRoomSprite sprite in room.Mesh.Sprites)
{
TR1RoomVertex roomVertex = room.Mesh.Vertices[sprite.Vertex];

// Flip the old world coordinate, then subtract the new room position
int x = oldRoomX + roomVertex.Vertex.X;
x = FlipWorldX(x);
x -= room.Info.X;
roomVertex.Vertex.X = (short)x;

Debug.Assert(roomVertex.Vertex.X >= 0);
processedVerts.Add(roomVertex);
}

// Flip the face vertices
foreach (TR1RoomVertex vert in room.Mesh.Vertices)
{
if (processedVerts.Contains(vert))
{
continue;
}

int sectorX = vert.Vertex.X / TRConsts.Step4;
int newSectorX = room.NumXSectors - sectorX;
vert.Vertex.X = (short)(newSectorX * TRConsts.Step4);
Debug.Assert(vert.Vertex.X >= 0);
}
MirrorRoomMesh(room.Mesh, oldRoomX, room.Info.X, room.NumXSectors);

// Change visibility portal vertices and flip the normal for X
foreach (TRRoomPortal portal in room.Portals)
Expand Down Expand Up @@ -359,35 +332,8 @@ private void MirrorRooms(TR2Level level)
room.Info.X = FlipWorldX(oldRoomX);
room.Info.X -= room.NumXSectors * TRConsts.Step4;
Debug.Assert(room.Info.X >= 0);
// Flip room sprites separately as they don't sit on tile edges
List<TR2RoomVertex> processedVerts = new();
foreach (TRRoomSprite sprite in room.Mesh.Sprites)
{
TR2RoomVertex roomVertex = room.Mesh.Vertices[sprite.Vertex];

// Flip the old world coordinate, then subtract the new room position
int x = oldRoomX + roomVertex.Vertex.X;
x = FlipWorldX(x);
x -= room.Info.X;
roomVertex.Vertex.X = (short)x;

Debug.Assert(roomVertex.Vertex.X >= 0);
processedVerts.Add(roomVertex);
}

// Flip the face vertices
foreach (TR2RoomVertex vert in room.Mesh.Vertices)
{
if (processedVerts.Contains(vert))
{
continue;
}

int sectorX = vert.Vertex.X / TRConsts.Step4;
int newSectorX = room.NumXSectors - sectorX;
vert.Vertex.X = (short)(newSectorX * TRConsts.Step4);
Debug.Assert(vert.Vertex.X >= 0);
}
MirrorRoomMesh(room.Mesh, oldRoomX, room.Info.X, room.NumXSectors);

// Change visibility portal vertices and flip the normal for X
foreach (TRRoomPortal portal in room.Portals)
Expand Down Expand Up @@ -420,35 +366,8 @@ private void MirrorRooms(TR3Level level)
room.Info.X = FlipWorldX(oldRoomX);
room.Info.X -= room.NumXSectors * TRConsts.Step4;
Debug.Assert(room.Info.X >= 0);
// Flip room sprites separately as they don't sit on tile edges
List<TR3RoomVertex> processedVerts = new();
foreach (TRRoomSprite sprite in room.Mesh.Sprites)
{
TR3RoomVertex roomVertex = room.Mesh.Vertices[sprite.Vertex];

// Flip the old world coordinate, then subtract the new room position
int x = oldRoomX + roomVertex.Vertex.X;
x = FlipWorldX(x);
x -= room.Info.X;
roomVertex.Vertex.X = (short)x;

Debug.Assert(roomVertex.Vertex.X >= 0);
processedVerts.Add(roomVertex);
}

// Flip the face vertices
foreach (TR3RoomVertex vert in room.Mesh.Vertices)
{
if (processedVerts.Contains(vert))
{
continue;
}

int sectorX = vert.Vertex.X / TRConsts.Step4;
int newSectorX = room.NumXSectors - sectorX;
vert.Vertex.X = (short)(newSectorX * TRConsts.Step4);
Debug.Assert(vert.Vertex.X >= 0);
}
MirrorRoomMesh(room.Mesh, oldRoomX, room.Info.X, room.NumXSectors);

// Change visibility portal vertices and flip the normal for X
foreach (TRRoomPortal portal in room.Portals)
Expand All @@ -473,6 +392,41 @@ private void MirrorRooms(TR3Level level)
}
}

private void MirrorRoomMesh<T, V>(TRRoomMesh<T, V> mesh, int oldRoomX, int newRoomX, ushort roomWidth)
where T : Enum
where V : TRRoomVertex
{
// Flip room sprites separately as they don't sit on tile edges
List<V> processedVerts = new();
foreach (TRRoomSprite<T> sprite in mesh.Sprites)
{
V roomVertex = mesh.Vertices[sprite.Vertex];

// Flip the old world coordinate, then subtract the new room position
int x = oldRoomX + roomVertex.Vertex.X;
x = FlipWorldX(x);
x -= newRoomX;
roomVertex.Vertex.X = (short)x;

Debug.Assert(roomVertex.Vertex.X >= 0);
processedVerts.Add(roomVertex);
}

// Flip the face vertices
foreach (V vert in mesh.Vertices)
{
if (processedVerts.Contains(vert))
{
continue;
}

int sectorX = vert.Vertex.X / TRConsts.Step4;
int newSectorX = roomWidth - sectorX;
vert.Vertex.X = (short)(newSectorX * TRConsts.Step4);
Debug.Assert(vert.Vertex.X >= 0);
}
}

private void MirrorRoomStaticMeshes<T>(IEnumerable<TRRoomStaticMesh<T>> meshes)
where T : Enum
{
Expand Down
6 changes: 3 additions & 3 deletions TREnvironmentEditor/Model/Types/Rooms/EMCopyRoomFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public override void ApplyToLevel(TR1Level level)
{
newRoom.Mesh.Sprites.Add(new()
{
Texture = baseRoom.Mesh.Sprites[i].Texture,
ID = baseRoom.Mesh.Sprites[i].ID,
Vertex = baseRoom.Mesh.Sprites[i].Vertex
});
}
Expand Down Expand Up @@ -254,7 +254,7 @@ public override void ApplyToLevel(TR2Level level)
{
newRoom.Mesh.Sprites.Add(new()
{
Texture = baseRoom.Mesh.Sprites[i].Texture,
ID = baseRoom.Mesh.Sprites[i].ID,
Vertex = baseRoom.Mesh.Sprites[i].Vertex
});
}
Expand Down Expand Up @@ -398,7 +398,7 @@ public override void ApplyToLevel(TR3Level level)
{
newRoom.Mesh.Sprites.Add(new()
{
Texture = baseRoom.Mesh.Sprites[i].Texture,
ID = baseRoom.Mesh.Sprites[i].ID,
Vertex = baseRoom.Mesh.Sprites[i].Vertex
});
}
Expand Down
4 changes: 2 additions & 2 deletions TREnvironmentEditor/Model/Types/Rooms/EMImportRoomFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public override void ApplyToLevel(TR2Level level)
{
newRoom.Mesh.Sprites.Add(new()
{
Texture = roomDef.Room.Mesh.Sprites[i].Texture,
ID = roomDef.Room.Mesh.Sprites[i].ID,
Vertex = roomDef.Room.Mesh.Sprites[i].Vertex
});
}
Expand Down Expand Up @@ -402,7 +402,7 @@ public override void ApplyToLevel(TR3Level level)
{
newRoom.Mesh.Sprites.Add(new()
{
Texture = roomDef.Room.Mesh.Sprites[i].Texture,
ID = roomDef.Room.Mesh.Sprites[i].ID,
Vertex = roomDef.Room.Mesh.Sprites[i].Vertex
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace TREnvironmentEditor.Model.Types;

public class EMAddRoomSpriteFunction : BaseEMFunction
{
public short Texture { get; set; }
public uint ID { get; set; }
public EMRoomVertex Vertex { get; set; }
public List<EMLocation> Locations { get; set; }

Expand All @@ -17,7 +17,7 @@ public override void ApplyToLevel(TR1Level level)
TR1Room room = level.Rooms[data.ConvertRoom(location.Room)];
room.Mesh.Sprites.Add(new()
{
Texture = Texture,
ID = (TR1Type)ID,
Vertex = (short)room.Mesh.Vertices.Count
});
room.Mesh.Vertices.Add(new()
Expand All @@ -41,7 +41,7 @@ public override void ApplyToLevel(TR2Level level)
TR2Room room = level.Rooms[data.ConvertRoom(location.Room)];
room.Mesh.Sprites.Add(new()
{
Texture = Texture,
ID = (TR2Type)ID,
Vertex = (short)room.Mesh.Vertices.Count
});
room.Mesh.Vertices.Add(new()
Expand All @@ -67,7 +67,7 @@ public override void ApplyToLevel(TR3Level level)
TR3Room room = level.Rooms[data.ConvertRoom(location.Room)];
room.Mesh.Sprites.Add(new()
{
Texture = Texture,
ID = (TR3Type)ID,
Vertex = (short)room.Mesh.Vertices.Count
});
room.Mesh.Vertices.Add(new()
Expand Down
2 changes: 1 addition & 1 deletion TRLevelControl/Build/Room/TR1RoomBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TRLevelControl.Build;

public class TR1RoomBuilder : TRRoomBuilder<TR1RoomVertex>
public class TR1RoomBuilder : TRRoomBuilder<TR1Type, TR1RoomVertex>
{
public TR1RoomBuilder()
: base(TRGameVersion.TR1) { }
Expand Down
2 changes: 1 addition & 1 deletion TRLevelControl/Build/Room/TR2RoomBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TRLevelControl.Build;

public class TR2RoomBuilder : TRRoomBuilder<TR2RoomVertex>
public class TR2RoomBuilder : TRRoomBuilder<TR2Type, TR2RoomVertex>
{
public TR2RoomBuilder()
: base(TRGameVersion.TR2) { }
Expand Down
2 changes: 1 addition & 1 deletion TRLevelControl/Build/Room/TR3RoomBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TRLevelControl.Build;

public class TR3RoomBuilder : TRRoomBuilder<TR3RoomVertex>
public class TR3RoomBuilder : TRRoomBuilder<TR3Type, TR3RoomVertex>
{
public TR3RoomBuilder()
: base(TRGameVersion.TR3) { }
Expand Down
2 changes: 1 addition & 1 deletion TRLevelControl/Build/Room/TR4RoomBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TRLevelControl.Build;

public class TR4RoomBuilder : TRRoomBuilder<TR3RoomVertex>
public class TR4RoomBuilder : TRRoomBuilder<TR4Type, TR3RoomVertex>
{
public TR4RoomBuilder()
: base(TRGameVersion.TR4) { }
Expand Down
15 changes: 8 additions & 7 deletions TRLevelControl/Build/TRRoomBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace TRLevelControl.Build;

public abstract class TRRoomBuilder<V>
public abstract class TRRoomBuilder<T, V>
where T : Enum
where V : TRRoomVertex
{
protected readonly TRGameVersion _version;
Expand All @@ -22,7 +23,7 @@ public void ReadRawMesh(TRLevelReader reader)
protected abstract List<V> ReadVertices(TRLevelReader reader);
protected abstract void WriteVertices(TRLevelWriter writer, List<V> vertices);

public TRRoomMesh<V> BuildMesh(int roomIndex)
public TRRoomMesh<T, V> BuildMesh(int roomIndex, ISpriteProvider<T> spriteProvider)
{
ushort[] rawData = _rawMeshes[roomIndex];
byte[] target = new byte[rawData.Length * sizeof(short)];
Expand All @@ -31,7 +32,7 @@ public TRRoomMesh<V> BuildMesh(int roomIndex)
using MemoryStream ms = new(target);
using TRLevelReader reader = new(ms);

TRRoomMesh<V> mesh = new()
TRRoomMesh<T, V> mesh = new()
{
Vertices = ReadVertices(reader)
};
Expand Down Expand Up @@ -66,14 +67,14 @@ public TRRoomMesh<V> BuildMesh(int roomIndex)
mesh.Sprites.Add(new()
{
Vertex = reader.ReadInt16(),
Texture = reader.ReadInt16()
ID = spriteProvider.FindSpriteType(reader.ReadInt16())
});
}

return mesh;
}

public void WriteMesh(TRLevelWriter writer, TRRoomMesh<V> mesh)
public void WriteMesh(TRLevelWriter writer, TRRoomMesh<T, V> mesh, ISpriteProvider<T> spriteProvider)
{
using MemoryStream stream = new();
using TRLevelWriter meshWriter = new(stream);
Expand All @@ -95,10 +96,10 @@ public void WriteMesh(TRLevelWriter writer, TRRoomMesh<V> mesh)
}

meshWriter.Write((short)mesh.Sprites.Count);
foreach (TRRoomSprite sprite in mesh.Sprites)
foreach (TRRoomSprite<T> sprite in mesh.Sprites)
{
meshWriter.Write(sprite.Vertex);
meshWriter.Write(sprite.Texture);
meshWriter.Write(spriteProvider.GetSpriteOffset(sprite.ID));
}

byte[] data = stream.ToArray();
Expand Down
24 changes: 13 additions & 11 deletions TRLevelControl/Control/TR1LevelControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ namespace TRLevelControl;

public class TR1LevelControl : TRLevelControlBase<TR1Level>
{
private readonly TRObjectMeshBuilder<TR1Type> _meshBuilder;
private readonly TRSpriteBuilder<TR1Type> _spriteBuilder;
private readonly TR1RoomBuilder _roomBuilder;
private TRObjectMeshBuilder<TR1Type> _meshBuilder;
private TRSpriteBuilder<TR1Type> _spriteBuilder;
private TR1RoomBuilder _roomBuilder;

public TR1LevelControl(ITRLevelObserver observer = null)
: base(observer)
{
_meshBuilder = new(TRGameVersion.TR1, _observer);
_spriteBuilder = new(TRGameVersion.TR1);
_roomBuilder = new();
}
: base(observer) { }

protected override TR1Level CreateLevel(TRFileVersion version)
{
Expand All @@ -34,6 +29,13 @@ protected override TR1Level CreateLevel(TRFileVersion version)
return level;
}

protected override void Initialise()
{
_meshBuilder = new(TRGameVersion.TR1, _observer);
_spriteBuilder = new(TRGameVersion.TR1);
_roomBuilder = new();
}

protected override void Read(TRLevelReader reader)
{
uint numImages = reader.ReadUInt32();
Expand Down Expand Up @@ -237,7 +239,7 @@ private void WriteRooms(TRLevelWriter writer)
{
writer.Write(room.Info, _level.Version.Game);

_roomBuilder.WriteMesh(writer, room.Mesh);
_roomBuilder.WriteMesh(writer, room.Mesh, _spriteBuilder);

writer.Write((ushort)room.Portals.Count);
writer.Write(room.Portals);
Expand Down Expand Up @@ -315,7 +317,7 @@ private void ReadSprites(TRLevelReader reader)

for (int i = 0; i < _level.Rooms.Count; i++)
{
_level.Rooms[i].Mesh = _roomBuilder.BuildMesh(i);
_level.Rooms[i].Mesh = _roomBuilder.BuildMesh(i, _spriteBuilder);
}
}

Expand Down
Loading

0 comments on commit d97ddf9

Please sign in to comment.