We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Image Update (Read 833 times)
Image Update
Mar 24th, 2010, 5:30am
 
Hi!
I'm using the Processing lib with mt4j (a multitouch java framework)
Atm I'm trying to display an image on the  background and reloaded it every few milliseconds so that it looks like a video.
The image file is updated and saved from another program every 30 milliseconds(and works fine).


my code so far looks like this:

the main class
Code:

public OTscene(MTApplication mtApplication, String name) {
super(mtApplication, name);
this.pa = mtApplication;

im =  pa.loadImage(System.getProperty("user.dir") + File.separator  + "1.jpg");

               // Mt4j bit to add the image in the backgorund
img = new MTBackgroundImage(pa, im, true);
backgroundLayer = new MTComponent(pa);
backgroundLayer.addChild(img);
canvas = this.getCanvas();
canvas.addChild(backgroundLayer);

               //Thread Class to reload the image
BackgroundUpdate updateBack = new  BackgroundUpdate(this);
               Thread a = new Thread(updateBack);
               a.start();



and the run method
Code:

public void run(){
while(true){
                       //reload the updated image
im =  pa.loadImage(System.getProperty("user.dir") + File.separator  + "1.jpg");

                       //mt4j bit
       img = new MTBackgroundImage(pa, im, true);
       backgroundLayer.removeChild(0);
       backgroundLayer.addChild(img);
                       
                       //Wait a bit
                       try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
               


the first time, the image loads without any problem and it shows up, then when it tries to load from the second time onwards it just gives me a blank screen..

Does anybody know what I'm doing wrong or if there is any other better way of doing this thing?

cheers!
Re: Image Update
Reply #1 - Mar 24th, 2010, 6:56am
 
Hi,
the problem is that you create the image in another thread the second time and you are probably using opengl as a renderer. The creation of the image uses opengl methods which can only be called from the rendering thread (when the opengl context is valid). So the loading can be done in the other thread but the creation of the backgroundimage has to happen in the main rendering thread.
Here's how it should work. The rigersterpredrawaction will be exectuted the next time the rendering thread runs, which is why it works then. Also I've optimized the rest a bit.

Code:

public void run() {
while(true){
try {
//reload the updated image
im =  pa.loadImage(System.getProperty("user.dir") + File.separator  + "1.jpg");
//mt4j bit
registerPreDrawAction(new IPreDrawAction() {
public void processAction() {
if (img.getTexture() instanceof GLTexture){
GLTexture tex = (GLTexture)img.getTexture();
tex.putPixelsIntoTexture(im);
}else{
img.setTexture(im);
}
}
public boolean isLoop() {
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
//Wait a bit
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Page Index Toggle Pages: 1