Hi jbucks, NoChinDeluxe and blindfish
Where to start
blindfish said
Quote:I've always worked on the basis that conditions should catch the most likely case first. So I'd be tempted to change the order of the conditions that Quark copied into the Class's draw method. Since the most likely state is for 'over' and 'pressed' to be false it would make sense to change it to:
This is quite correct. In fact
if statements can and do reduce performance as modern CPU use pipelines where the instructions 'flow' through the processor and when the boolean condition involves calculation sometimes the processor has to guess the result so later instructions can follow into the pipeline - if it guesses wrong then the pipeline has to be flushed. (This is the Blue Peter version my friend who writes chess programs for computer vs computer games would be able to explain it in in much greater
over my head detail). Sorry that was a long aside.
NoChinDeluxe no problem with your description and the code example looked good.
Now I get to quote myself
Quote:There is always an alternative way but not always a better way
We now have 3 ways, the original pasted by jbucks, threads posted by NoChinDeluxe, and my way using a single thread but utilizing a little known Processing feature with blindfish finding those hard to find logical errors (mouseY should have been mouseX) and providing good programming practice.
So which way is best
jbucks original post rules that way out so what about threads
Well threads are very good for
time consuming background tasks and would work well here but as NoChinDeluxe pointed out the update and draw threads need to access the same data i.e. the ArrayList of buttons and this can cause problems because as stated the threads are not synchronized and can cause problems if the draw thread attempts to use data that is currently being modified by the update thread. Unless you are going to have thousands of buttons having the update and draw methods in the same thread is not likely to have a serious effect on performance.
This can be seen here
http://www.lagers.org.uk/g4p/examples.htmlthis is a demo of my G4P library for Processing and it probably doesn't surprise you that I used the approach I described earlier in this thread.
Each cycle of the Processing loop calls a number of methods
pre() - called once for each registered object
draw() - called once for each registered object
mouseEvent() - this might be called many times as it empties the event queue
post() - called once for each registered object
Iif we want to use these methods it is not enough to just to add them to our sketch we have to register them with e.g.
Code:registerPre(object);
where object is the Java object that has the pre() method. (The only exception is the draw()method in our main sketch code)
Including the following in our sketch
Code:registerPre(this);
means we want to use the pre() method defined in our main sketch.
In my previous post I simply registered each button in turn for mouseEvent and draw. i.e.
Code:registerDraw(b);
registerMouseEvent(b);
So its back to you jbucks whether you go for the single thread or multiple threads, both will do want you need and in this particular situation you will not notice any differences in performance between them. I strongly suggest that you pick the one you feel most comfortable with.