private void GenPixelTiles(GraphicsDeviceEventArgs e)
        {
            if (_wvm != null)
            {
                if (_wvm.PixelMap != null)
                {
                    if (_tileMap == null || _tileMap.Length != _wvm.PixelMap.ColorBuffers.Length)
                    {
                        _tileMap = new Texture2D[_wvm.PixelMap.ColorBuffers.Length];
                    }

                    for (int i = 0; i < _tileMap.Length; i++)
                    {
                        if (!Check2DFrustrum(i))
                            continue;

                        // Make a new texture for nulls
                        bool init = _tileMap[i] == null;
                        if (init || _tileMap[i].Width != _wvm.PixelMap.TileWidth || _tileMap[i].Height != _wvm.PixelMap.TileHeight)
                            _tileMap[i] = new Texture2D(e.GraphicsDevice, _wvm.PixelMap.TileWidth, _wvm.PixelMap.TileHeight);

                        if (_wvm.PixelMap.BufferUpdated[i] || init)
                        {
                            _tileMap[i].SetData(_wvm.PixelMap.ColorBuffers[i]);
                            _wvm.PixelMap.BufferUpdated[i] = false;
                        }
                    }
                }
            }
        }
        private void Update(GraphicsDeviceEventArgs e)
        {
            // Update
            _gameTimer.Update();

            ScrollWorld();
        }
        private void Render(GraphicsDeviceEventArgs e)
        {
            // Clear the graphics device and texture buffer
            //e.GraphicsDevice.Clear(TileColor.Black);
            e.GraphicsDevice.Textures[0] = null;

            GenPixelTiles(e);

            // Start SpriteBatch
            _spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);

            DrawPixelTiles();
            _spriteBatch.End();

            _spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);


            // Draw sprite overlays
            if (_wvm.ShowTextures && _textureDictionary.Valid)
                DrawSprites();

            if (_wvm.ShowGrid)
                DrawGrid();

            if (_wvm.ShowPoints)
                DrawPoints();

            if (_wvm.Selection.IsActive)
                DrawSelection();

            DrawToolPreview();

            // End SpriteBatch
            _spriteBatch.End();
        }
        private void xnaViewport_RenderXna(object sender, GraphicsDeviceEventArgs e)
        {
            // Abort rendering if in design mode or if gameTimer is not running
            if (!_gameTimer.IsRunning || _wvm.CurrentWorld == null || ViewModelBase.IsInDesignModeStatic)
                return;

            // Clear the graphics device and texture buffer
            e.GraphicsDevice.Clear(_backgroundColor);



            Update(e);
            Render(e);
        }
        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);

                            if (source.Height <= 0 || source.Width <= 0) continue;

                            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);
                }
            }

        }
 private void LoadResourceTextures(GraphicsDeviceEventArgs e)
 {
     _textures.Add("Spawn"          , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.spawn_marker.png", e.GraphicsDevice));
     _textures.Add("Dungeon"        , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.dungeon_marker.png", e.GraphicsDevice));
     _textures.Add("Old Man"        , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.npc_old_man.png", e.GraphicsDevice));
     _textures.Add("Arms Dealer"    , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.npc_arms_dealer.png", e.GraphicsDevice));
     _textures.Add("Clothier"       , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.npc_clothier.png", e.GraphicsDevice));
     _textures.Add("Demolitionist"  , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.npc_demolitionist.png", e.GraphicsDevice));
     _textures.Add("Dryad"          , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.npc_dryad.png", e.GraphicsDevice));
     _textures.Add("Guide"          , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.npc_guide.png", e.GraphicsDevice));
     _textures.Add("Merchant"       , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.npc_merchant.png", e.GraphicsDevice));
     _textures.Add("Nurse"          , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.npc_nurse.png", e.GraphicsDevice));
     _textures.Add("Goblin Tinkerer", WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.npc_goblin.png", e.GraphicsDevice));
     _textures.Add("Wizard"         , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.npc_wizard.png", e.GraphicsDevice));
     _textures.Add("Mechanic"       , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.npc_mechanic.png", e.GraphicsDevice));
     _textures.Add("Santa Claus"    , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.npc_santa_claus.png", e.GraphicsDevice));
     _textures.Add("Grid"           , WriteableBitmapEx.ResourceToTexture2D("TEditXna.Images.Overlays.grid.png", e.GraphicsDevice));
 }
        private async void xnaViewport_LoadContent(object sender, GraphicsDeviceEventArgs e)
        {
            // Abort rendering if in design mode or if gameTimer is already running
            if (ViewModelBase.IsInDesignModeStatic || _gameTimer.IsRunning)
            {
                return;
            }
            InitializeGraphicsComponents(e);


            if (_textureDictionary.Valid)
                LoadTerrariaTextures(e);

            _selectionTexture = new Texture2D(e.GraphicsDevice, 1, 1);
            LoadResourceTextures(e);

            _selectionTexture.SetData(new[] { Color.FromNonPremultiplied(0, 128, 255, 128) }, 0, 1);
            // Start the Game Timer
            _gameTimer.Start();
        }
        private void InitializeGraphicsComponents(GraphicsDeviceEventArgs e)
        {
            // Load services, textures and initialize spritebatch
            _serviceProvider = new SimpleProvider(xnaViewport.GraphicsService);
            _spriteBatch = new SpriteBatch(e.GraphicsDevice);
            _textureDictionary = new Textures(_serviceProvider, e.GraphicsDevice);

            System.Windows.Media.Matrix m = PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice;
            _dpiScale = new Vector2((float)m.M11, (float)m.M22);
        }