Generating smooth animations on different computer systems using C# can be a challenging task. However, there is an easy way to adjust the program behavior to the number of target frames per second.
Setting object animations with C# can mean many different things.
The first is to animate a series of image images, such as an animated GIF. In this case, there is already some existing support in the .Net Framework, but you may want to process the drawing frame by frame.
Another form of animation is an image generated using code. GDI%2B can draw some very complex graphics, and smooth animation can bring an extra quality dimension to your .Net application.
Finally, programmers sometimes want to animate form behaviors such as size, position, text, and more. Although these are object properties, we want them to change at regular intervals, regardless of the speed of the computer's CPU.
The first step in writing a constant FPS animation in C# is to determine how to measure the CPU cycle. The most common objects for measuring time are the Timer object and the DateTime class. Although both are easy to use, they lack accuracy. Instead, we will use the System.Diagonistics.StopWatch class.
The StopWatch class uses the API to call QueryPerformanceTimer to track CPU cycles, which is more accurate. Higher accuracy in this case means a more constant animation
Basically you will want to track three values, two of which will change consistently:
- Interval = stopwatch. Frequency / [target FPS [eg 30.0]]
- currentTicks = Stopwatch.GetTimestamp[]
- lastTicks = same as currentTicks, but the last animation
No matter how fast the computer is, the result is a continuous animation. Simply adjust FPS in your own system, which will be perceived across systems.
The reason it works is because animations don't run without a brain in a simple while/for loop. Cycle through the CPU cycles of the host to adjust the interval between animation refreshes.
Orignal From: Smooth animation in C#
No comments:
Post a Comment