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.
IndexProgramming Questions & HelpPrograms › Overwriting text that has motion OR Clearing State
Page Index Toggle Pages: 1
Overwriting text that has motion OR Clearing State (Read 632 times)
Overwriting text that has motion OR Clearing State
Apr 24th, 2009, 10:01am
 
Hello - first I apologize if this is not the correct forum.  On to the topic:

I am reading from a text file in my program.  The text is accurately displayed on screen (black background, white text).  I currently have noLoop() in setup() but have a boolean toggle turning this to loop upon a mouseclick.  I draw the text with an x value partially based on a deltaX which increments by 10 when drawToggle is true.

This all works correctly BUT the text written from the previous iteration still appears in white.  I have tried 2 things to fix: write the same text in black before writing the white text (this still produces poor results) AND simply rendering a black image at the start of each loop that covers the length of the text.  The latter "works" but feels like a very poor solution and though isn't a problem here, rendering an image in more intensive programs when not necessary could become a tax on the system.

Is there a better way of "overwriting" the past text without having to render a black image (perhaps a "clearLastDraw()" or something which clears the state after each draw call)?
Re: Overwriting text that has motion OR Clearing State
Reply #1 - Apr 24th, 2009, 11:24am
 
Found at least a slightly better solution for those interested - I just drew a rectangle the size of the whole window (instead of importing an image file).
Re: Overwriting text that has motion OR Clearing State
Reply #2 - Apr 24th, 2009, 11:27am
 
Clearing an image is usually done by filling the screen, see
http://processing.org/reference/background_.html
I think that's the equivalent of glClear(), and that's the fastest way I know to clean the screen with one color (no need to display an image).
Re: Overwriting text that has motion OR Clearing State
Reply #3 - Apr 24th, 2009, 7:12pm
 
Clearing the entire display using background(0) is fast and not worth worrying about.

This is great for a simple object (in your case some text) on a solid background. If you're worried about what to do when you have a complicated background, consider this:

The display is like a mosaic. When you change the image (display text, lines, circles, whatever), you are removing the existing mosaic tiles and replacing them with new ones. If you want to go back to what was there before, you need to either start over, or remember just the ones you changed and change them back.

If you have a complicated background that takes time to compute, but once computed doesn't change (eg if you had a fractal in the background that didn't zoom or anything), you can save a copy of the image and blast that onto the screen for each frame (instead of painting black, for instance). The only other option is to do the calculations again.

The "undo" operation in most types of program (graphical or otherwise) requires memory to be reserved for the thing (eg picture) you are changing. That's just the way things are.

Having said that, your idea to draw an image over the place where the text was is a good notion - if you had a complex image in the background (such as the fractal example), redrawing only the part that needed to be redrawn is a good optimisation. How you manage that is, of course a different matter. Saving the entire image and selectively redrawing just part of it is one option, and will generally be faster.

One thing you might like to try is drawing a black rectangle at less than 100% opacity. The result will give you a sort of trail after text.

-spxl
Page Index Toggle Pages: 1