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.