Quantcast
Viewing all articles
Browse latest Browse all 18858

Screen not updating quickly enough (probably problem with threads).

I have a 2,000 x 2,000 image inside of a Scrollviewer. As part of an animation sequence representing the location of animals on the map the image.source is changed to a 'clean' copy of the 3D map and then the new locations of the animals are redrawn (below is a series of extreme zoomed in screen captures to illustrate):

Image may be NSFW.
Clik here to view.

When I just 'step' the animation sequence ahead individually there are no problems (as you can see), the image is refreshed and the new locations (green lines) are plotted. The problem occurs when I put the animation into a while loop. For example if I try to step the animation 5 times what happens is that the first image 'freezes' and then about 7 seconds later the image redraws with the LAST position of the animals.

In other words, the image is not being refreshed correctly. I suspect this is a problem with threads.

Does anybody have a solution

The code that is called is below:

writeableBitmap = new WriteableBitmap(CleanVegMap);
image.Source = writeableBitmap;
DrawDinos2d();

public void DrawDinos2d()
{
for (int i = 0; i < MainWindow.Dinosaurs.Count(); i++)
    {
    for (int j = 0; j < MainWindow.Dinosaurs[i].Location.Count; j++)
    SetPixel(writeableBitmap, (int)MainWindow.Dinosaurs[i].Location[j].X, (int)MainWindow.Dinosaurs[i].Location[j].Y, System.Windows.Media.Color.FromArgb(127, 255, 255, 0));
            }

}// DrawDinos2d

Any ideas how to fix this animation update problem?

Thanks in advance!

Here's the code for SetPixel:

public void SetPixel(WriteableBitmap wb, int x, int y, System.Windows.Media.Color color)
    {
    var pixelRect = new Int32Rect(x, y, 1, 1);
    var pixelBuffer = new byte[4];
    wb.CopyPixels(pixelRect, pixelBuffer, 4, 0);
    pixelBuffer[0] = (byte)(pixelBuffer[0] * (1F - color.ScA) + color.B * color.ScA);
     pixelBuffer[1] = (byte)(pixelBuffer[1] * (1F - color.ScA) + color.G * color.ScA);
    pixelBuffer[2] = (byte)(pixelBuffer[2] * (1F - color.ScA) + color.R * color.ScA);
    wb.WritePixels(pixelRect, pixelBuffer, 4, 0);
        }

I'm not sure what you want to see, i.e. , "type of Dinosaurs." However, here's a link to my development web site which has a lot of information: Dinosaur-Island.com

As requested, here's Dinosaurs:

public static List<Dinosaur> Dinosaurs = new List<Dinosaur>();

 public class Dinosaur
    {
        public string Specie { get; set; }
        public int Age { get; set; }
        public int Weight { get; set; }
        public double Height { get; set; }
        public int Health { get; set; }
        public double Water { get; set; }
        public double FoodConsumed { get; set; }
        public bool Sex { get; set; }
        public string PersonalName { get; set; }
        public System.Windows.Point Head = new System.Windows.Point();
        public List<System.Windows.Point> Location { get; set; }
        public double Length { get; set; }
        public double Speed { get; set; }
        public byte State { get; set; }
        public System.Windows.Point Goal = new System.Windows.Point();

        public System.Windows.Point[] FoodLocation = new System.Windows.Point[5]; // The last five locations that the dino found food
        public System.Windows.Point[] WaterLocation = new System.Windows.Point[5]; // The last five locations that the dino found water


        // Constructor
        public Dinosaur()
        {
           
        }
    }


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>