About ofSetBackgroundAuto, flickering and CPU

I’m new in oF and also in this community. I’m working in a physical game to mix samples using body movements. I did the first prototype in Processing but I decided to learn oF and write it again. It’s almost done but i have a problem with the code. I’ll try to explain it.

In my code I have two loops operations:

  • I need to execute the code inside update with a high “framerate” let’s say 40 fps to read data with low latency from an arduino board.

  • But, I only need to redraw the graphics with 4 fps

If I set the fps to 40 everything works fine but it eats all my processor without need.
So what I do Is create a condition inside draw()

	if(drawing==0){//just 1/10 of the times			  
		DrawSequencer();	//Here I have all my graphic-related code  

To make it work I need to turn off the autoclean at the beginning of every draw() call:


Otherwise it cleans the screen before every draw() and I only see the output during 1/10 seconds.

Using this it works fine but if I move the window, record the screen or put another window over my program window, the next happens:

Is seems that it stores the past image and mixes it with the current one creating that strange flickering.
I tried to make use of

		ofBackground(0, 0, 0);  

at the end of the update method to clean the screen only before writing the new screen, but it doesn’t work.
Any clue about what to do?
In my mind the solution would be execute update() and draw() with different frequencies but I don’t think it’s possible. Any alternatives?

Operating system: Snow Leopard
Thank you so much

I think the ideal solution would be to talk to the arduino board in a thread. Look at the ofxThread addon example to get an idea of how it works. just keep in mind that you can’t do any graphics calls from within the thread itself, or you’ll get a crash.

The best part about this is that the thread will run as fast as possible, potentially way faster than 40fps. You can then set the framerate to 10fps in the main app if you want to. I assume you’ve got a dual core anyway so this should be no problem.

The other route I can see would be to draw to an offscreen texture, or FBO every 30 frames or so, and then in the between frames simply draw the pre-rendered texture to screen. I think threading would be easier and more efficient in this case though.

Hope this helps,

I tried the FBO solution and works fine: 10% CPU and I can keep the delay low enough.

Thank you so much!