jueves, 21 de mayo de 2015

Smooth Animation in C#

Creating smooth animations with C# across unique computer system systems can be a difficult job. Nevertheless there is a easy technique to change a plan behavior to a target quantity of frames per second.

Animating objects with C# can imply a quantity of diverse issues.

Initial would be animating a sequence of image pictures like an animated GIF. In this case there is currently some current assistance in the .World wide web Framework, but maybe you want to deal with drawing on a frame-by-frame basis.

An additional type of animation is with code-generated pictures. GDI%2B can draw some extremely complicated graphics and animating them smoothly can bring an additional dimension of high quality to your .Web applications.

Lastly occasionally programmers want to animate Window Type behavior, such as size, position, text, and so on. Whilst these are object properties, we want them to alter inside a normal interval, no matter what the speed of the laptop CPU is.

The Initial step for writing continuous FPS animation in C# is to determine the way to measure CPU cycles. The most generally utilized objects to measure time is the Timer object and the DateTime class. Whilst each are simple to use, they lack accuracy. Rather, we are going to make use of the Method.Diagonistics.StopWatch class.

The StopWatch class utilizes the API contact QueryPerformanceTimer to maintain track of CPU cycles, which is much more correct. Higher accuracy in this case indicates a much more continuous animation.

Essentially you will want to maintain track of 3 values, two of which will alter continuously:

  • Interval = Stopwatch.Frequency / [target FPS (30.0 for instance)]
  • currentTicks = Stopwatch.GetTimestamp()
  • lastTicks = exact same as currentTicks but taken the last time the animation was drawn
The logic behind the C# algorithm is not as well complex. In nutshell, a loop will be continuously operating, but you only want the animation to execute/refresh While the last quantity of CPU cycles and the current quantity of cycles has at least a gap of the Interval you previously calculated. Then and only then is the animation refreshed.

The outcome is a continual animation no matter how rapidly a personal computer is. Just change the FPS in your personal approach and that will be the perceived speed across systems.

The cause it operates is due to the fact animations are not run on a easy Even though/for loop brainlessly. The loop utilizes the host laptop or computer's CPU cycles to modify the interval the animation is refreshed.

Fortunately the net has a lot of examples of C# applications that draw smooth animations with FPS

Understand far more sophisticated C# tactics at Visual C# Kicks => vckicks.110mb.com

No hay comentarios:

Publicar un comentario