private void LoadTerrariaTextures(GraphicsDeviceEventArgs e) { // If the texture dictionary is valid (Found terraria and loaded content) load texture data foreach (var id in World.NpcIds) { _textureDictionary.GetNPC(id.Value); } //foreach (var tile in World.TileProperties.Where(t => t.IsFramed)) //{ // var tileTexture = _textureDictionary.GetTile(tile.Id); //} foreach (var sprite in World.Sprites) { if (sprite.Size.X == 0 || sprite.Size.Y == 0) continue; try { var tile = World.TileProperties[sprite.Tile]; if (tile.TextureGrid.X == 0 || tile.TextureGrid.Y == 0) continue; var texture = new Texture2D(e.GraphicsDevice, sprite.Size.X * tile.TextureGrid.X, sprite.Size.Y * tile.TextureGrid.Y); var tileTex = _textureDictionary.GetTile(sprite.Tile); for (int x = 0; x < sprite.Size.X; x++) { for (int y = 0; y < sprite.Size.Y; y++) { var source = new Rectangle(x * (tile.TextureGrid.X + 2) + sprite.Origin.X, y * (tile.TextureGrid.Y + 2) + sprite.Origin.Y, tile.TextureGrid.X, tile.TextureGrid.Y); if (source.Bottom > tileTex.Height) source.Height -= (source.Bottom - tileTex.Height); if (source.Right > tileTex.Width) source.Width -= (source.Right - tileTex.Width); var color = new Color[source.Height * source.Width]; var dest = new Rectangle(x * tile.TextureGrid.X, y * tile.TextureGrid.Y, source.Width, source.Height); tileTex.GetData(0, source, color, 0, color.Length); texture.SetData(0, dest, color, 0, color.Length); } } sprite.IsPreviewTexture = true; sprite.Preview = texture.Texture2DToWriteableBitmap(); } catch (Exception ex) { ErrorLogging.LogException(ex); } } }