# High-performance update of a bitmap grid

I need to visualize a fast paced process in .NET in 2D. I am planning to use writeable bitmap 800x40px.

The idea is like this - there are objects which are represented by very basic colours and sizes anything from 1x1 to 4x4. each takes 4x4 "cell" - they all are fixed like a chess board.

The objects move between cells and change size. Most importantly they move independently, so I cant (I can but I would like to not) have a sort of refresh rate.. I need to animate each object state change - like say pixels pouring from cell to cell. the transition time should be around 500ms.

In this period of time the states can change multiple times so I should be able to abort outdated animation, move object "instantly" to new cell and start more recent object animation which could have taken over the "old" cell..

The accuracy should be in 1-2ms intervals.

The host application is WPF .NET Framework

• Welcome to Game Development! Here, requesting for tutorials is off-topic, so I removed that part of the question. – Vaillancourt Mar 30 at 14:50
• Most games operate with a "sort of refresh rate" that we call a game-loop. That's how we drive even our fastest-paced animations and gameplay. Typically we display visible updates around a rate of 60 Hz, though some games will go into the hundreds of Hz. That's still milliseconds between repaints though, not microseconds. If you need microsecond precision with no discrete update rate then (first, I'd ask why - displays or human observers won't be able to show/discern the difference - and second) game developers might not be the right folks to ask about that. – DMGregory Mar 30 at 15:12
• yeah may be microseconds is bad choice of word, lets say 1ms.. why - when you draw a seemingly chaotic process the more precise it is - the better "big picture" you get. The half-millisecond inaccuracy on each move might accumulate errors into something perceptive if you draw a thousand objects which are in constant motion.. so i think it is justified. – Boppity Bop Mar 30 at 15:55
• (other way to see it - it is important to try and catch the relative moves between object - so they are moved in realistic sequence, since each can move at its own time - i dont see how to make it work - only by reducing this refresh rate or what i call accuracy) – Boppity Bop Mar 30 at 15:57
• Can you edit your question to tell us more about what this simulation represents? It's tickling my spidey-sense that there's a better way to accomplish the determinism you want, which we can then render and display at a rate decoupled from the timing precision of the underlying sim. – DMGregory Mar 30 at 16:59

the accuracy should be in 1-2ms intervals

You are out of luck here, getting high and steady frame-rate in WPF is impossible by design. Still, you can get decent 60Hz, and anything above requires high-frequency display anyways — I expect those to be rarity in a business environment.

I am planning to use writeable bitmap 800x40px.

D3DImage should be a better fit, especially for performance reasons. Drawing a NxN pixels rectangle is easy with either API.

Most importantly they move independently, so I cant (I can but I would like to not) have a sort of refresh rate.

Objects' independence is irrelevant. So many objects could be (most probably will be) updating at same time, allocating a separate timer for each object is just unreasonable.

To set WPF to continious rendering mode, you need to subscribe for a certain event. Then in each frame you have to update animation states and redraw each particle.

In this period of time the states can change multiple times so I should be able to abort outdated animation, move object "instantly" to new cell and start more recent object animation which could have taken over the "old" cell.

That can be easily achieved by associating a particle ("object" of yours) with an animation. Animation instructs how to interpolate particle's properties (location and size) between 2 states, it consists of 2 key-frames and a time range. When assigning new animation to a particle with existing animation, don't forget to force previous animation (copy last key-frame's properties as is, no interpolation is necessary).

On each frame a particle should re-calculate it's state from it's animation. First, calculate the animation alpha (capped at 100%) based on current time (a = (now - Start) / Duration), then use it to interpolate particle's properties. Once 100% alpha is achieved, delete (or deactivate) the animation.

where would you start?

Let the code speak:

    record Keyframe(Point Location, int Size);

record Animation(Keyframe From, Keyframe To, DateTime Start, TimeSpan Duration);

class Particle
{
public int Id { get; }
public Point Location { get; private set; }
public int Size { get; private set; }

public Animation Animation { get; set; } // null = disabled
public void Update(DateTime now); // recalculate itself from the animation
}

class SimulationViewModel
{
List<Particle> particles;
void Update(DateTime now); // for each particle do ...
void Move(int ParticleId, Keyframe from, Keyframe to); // setup the animation; duration is constant, start time is now
}

• p.s. yes - 200hz monitor would cut down expectations to 5ms in theory.. but the prod system - they say they can do better and who am i to question :) – Boppity Bop Mar 31 at 15:18