Flicker in C # WinForms program on screen

Okay, so I did some research on this topic, and most of the solutions I found fix the problem, but I believe that they do not work correctly. I am in the early stages of introducing a simple engine of small particles, nothing crazy, I just do it out of boredom. Previously, I did not do anything similar with WinForms, I certainly with C / C ++, but for me this is a new thing. Below is the code that I use to draw particles on the screen, the boiler plate code for particles does not matter, since it works great, I'm more curious to know about my real game loop.

Here is the main code for updates and redraws

public MainWindow() { this.DoubleBuffered = true; InitializeComponent(); Application.Idle += HandleApplicationIdle; } void HandleApplicationIdle(object sender, EventArgs e) { Graphics g = CreateGraphics(); while (IsApplicationIdle()) { UpdateParticles(); RenderParticles(g); g.Dispose(); } } //Variables for drawing the particle Pen pen = new Pen(Color.Black, 5); Brush brush = new SolidBrush(Color.Blue); public bool emmiter = false; private void EmitterBtn_Click(object sender, EventArgs e) { //Determine which emitter to use if (emmiter == true) { //Creates a new particle Particle particle = new Particle(EmitterOne.Left, EmitterOne.Top, .5f, .5f, 20, 20); emmiter = false; } else if(emmiter == false) { Particle particle = new Particle(EmitterTwo.Left, EmitterTwo.Top, -.5f, .5f, 20, 20); emmiter = true; } } public void RenderParticles(Graphics renderer) { Invalidate(); Thread.Sleep(0); //Iterate though the static list of particles for (int i = 0; i < Particle.activeParticles.Count; i++) { //Draw Particles renderer.DrawRectangle(pen, Particle.activeParticles[i].x, Particle.activeParticles[i].y, Particle.activeParticles[i].w, Particle.activeParticles[i].h); } } public void UpdateParticles() { for (int i = 0; i < Particle.activeParticles.Count; i++) { //Move particles Particle.activeParticles[i].MoveParticle(); } } 

The problem I am facing is that at any time when the screen is cleaned and updated, it receives this terrible flicker, and not only that, but sometimes it will not when I emit a particle.

The form basically uses shortcuts as invisible places on the screen to tell where to display each particle.

In any case, I have seen this topic before, but nothing has fixed anything, the current implementation is the least flickering / lagging, but does not solve the problem.

Any help is appreciated, thanks!

EDIT * I ​​realized that I never shot a graphic object from each cycle, so I did it and there is no delay when I press the emitter button, however the flicker is still there, I updated the code accordingly.

+6
source share
1 answer

Getting rid of visible paint artifacts requires double buffering. In other words, visualize the scene in the back buffer, which, when ready, quickly turns into the surface of the screen in one step. What is a built-in function in Winforms, just set the DoubleBuffered property to true in the form constructor. You must use the Paint event to take advantage of this. Override OnPaint () and call RenderParticles (e.Graphics).

You need to take care of the timing, right now your user interface stream is on 100%, and the animation speed depends entirely on the number of particles and the speed of the machine. Instead of Application.Idle, cancel the timer from the toolbar to your form. In the Tick event handler, call UpdateParticles () and this.Invalidate () to fire the Paint event again. The value of the Interval property of the timer is critical, you get the maximum refresh rate by choosing 15 or 31 ms (64 or 32 FPS).

You will not always get the desired FPS speed, the timer simply delays or skips the Tick event if the machine becomes busy or too slow or other code needs to be executed in the user interface thread. To make sure that this does not affect the animation, you should measure the actual elapsed time, and not move the particles by a fixed amount. Either Environment.TickCount, DateTime.UtcNow, or a stopwatch are suitable ways to measure true elapsed time.

+5
source

All Articles