We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am working through Shiffman's Learning Processing book. I notice that when using functions or classes, the function that deals with "displaying" an object always appear last in draw(). Anything that deals with movement appears first. Does anyone know a reason for that? Is it a rule or just a standard? I hope I've given enough information.
Answers
In any animation there are two parts
1) Updating state of the objects - calculating new positions, collision detection etc.
2) Displaying the current state of the objects.
They are usually done in that order because then each frame represents the current state of the animation.
Position gets calculated
Object gets displayed
Now, we have the object’s variables set to some numerals and the object displayed exactly according to these numerals. Think: what will happen if it occurred the other way around:
Object gets displayed (at the old values of its variables)
Position gets calculated
Now, we have an object displayed according to the old values of its variables and we have the object’s varibles set to numerals that do not correspond perfectly to the object’s actual location.
Obviously this can cause issues.
Edge case: There are binary conditions at low frameRates where you might want to update variables in the draw loop after drawing -- especially for interactive sketches, where the events are processed in the gap between frame updates.
For example, consider this sketch: it displays either red or green, once a second.
We decide to turn it into a simple game by adding a keyPressed check. Press a key while the sketch is green, get a hit.
However, weirdly, everything is backwards! Pressing a key while the sketch is red (off) prints "YES", pressing a key while green (on) prints "NO." That's not what the code appears to say. What is going on?
Moving the update of variables (
on=!on;
) to the bottom of draw, below the actual drawing, would be one way to solve this problem.https://stackoverflow.com/questions/10974922/what-is-the-basic-difference-between-stack-and-queue
the display func is the first on the stack becourse it's the last you put in a stack :) (is this english ?)
in a minimal program it would be the exit status - return success / failure
additional links:
http://www.geeksforgeeks.org/memory-layout-of-c-program/ ( 4. Stack ) https://www.hackerearth.com/practice/notes/memory-layout-of-c-program/
Thanks to quark, randomdude, jeremydouglass, & nabr for answering the question. I apologize for taking so long to comment.
I am not experienced enough to understand the Nov. 29 answer fully, but I'm glad to be exposed stack & heap.
The other 3 answers combined help me to understand why the status with the new numbers should be updated before display. The "edge case" situation really makes the point poignant. BIG THANKS!
@DCrish -- very glad it was helpful!
@nabr -- it makes sense, although instead of in/on I think you mean on/off or in/out?
I wasn't quite sure I understood what you were saying about how LIFO relates to the order of commands in a typical draw function.
Yes, I want introduce the concept of stacks, you can imagine a program as kind of array:
in some cases it is important where you place your instructions.
myarr[3] = show count, // first iteration count value == initial value
should be executed before
myarr[5]= reset count, //
other wise the count value would stay the same.
Just use a debugger and see how the values are changing.
Ask yourself what would happen, if you place a function, method, value, -whatever, to execute to a earlier point in the program flow etc.
Processing Debugger Tutorial