Screen flickers while using background() or clear() in layered drawing of PGraphics using Threads.

edited April 2014 in Questions about Code

Hi, I'm trying to write a code, which uses very high amount of particles of different types. So, I decided to draw each category of particles in different layers using PGraphics technique. Also, I've used thread to maintain the speed of animation ( not to slow down ). The structure of my code is in this way: >

1) Created objects of PGraphics, as, pg,pg1...

2) Wrote Animation code for particle category 1 in the main draw() function (Used arrayList and Particle classes as usual) using pg.

3) Wrote Animation code for particle category 2 in Thread1 (Used arrayList and Particle classes as usual for particles, in the thread class and used while(true) to loop the thread's run() function) using pg1. This thread class then triggered from setup() function of main code.

4) Then I've wrote image() function for both pg & pg1 in draw() function. (Drawn pg first and pg1 later).

5) To clear the screen of pg, I've used background(0,0) in it. and worked fine.

6) When I've tried pg1.background() and later pg1.clear(), the pg1 layer gets cleared. But, the screen started flickering. Some white, blue, or red colours appears on the screen. When pg1 finishes its drawing (drawing of particles, but still while loop and background() are working, because, I've wrote the code to remove particle class from ArrayList when it goes out of screen or it dies), the flickering stops.

>

I've searched for hours, about this problem, and failed to find the solution. The results I've got are, one is VSync. problem. But I cant find the updated code for processing 2+. Only I found is the outdated code. I don't know much about openGL part of processing, so only thing I can do in this step is just copy and paste the code given by geeks.. :( Also I've found that is, using Multiple renders, but I wonder whether I can use one over another. I've seen only the code to render them apart.

I'm almost tired., and this is my last option to find a solution. Could you please help me? :(

My code:

        setup(){
        SmallParticlesManager sp = new SmallParticlesManager();
          sp.start();
        }
        draw(){
           pg1.beginDraw();
           pg.beginDraw();
           pg.endDraw();
           pg1.endDraw();
           image(pg1,0,0);
           image(pg,0,0);
        }
        class SmallParticlesManager extends Thread{
          SmallParticlesManager(){
          }
          void run(){
            while(true){
          }}}}}

Untitled

Sign In or Register to comment.