Second point: About the usage of static functions in several classes
(especially CoreSystem and Stage) :
Same remark as above, the following is just my point of view that aims
to be
discussed. Please don't be offended if some sentences look dogmatic.
It's not
their goal at all.
My feeling is that current usage of static functions was appropriated
as long as the framework is intended to develop applets. Indeed, in
that case, there is always only one Scene displayed at a time. Thus,
there is only one animation thread and all the processing is initiated
by event coming from this thread.
But in the case of pulpcore's integration in Swing, the context
changes, because:
- code's execution happens in several thread (main thread, EDT thread,
pulp's animation thread, other custom threads)
- the framework must handle the usage of several scenes at a time.
That's why I think the static functions will become a problem. Indeed:
- most of them retrive - in backstage - the context according to the
current thread. This can result in a mis-understanding for the
developper, as their name don't mention explicitely they are thread
dependant.
- moreover, if some of the proposed changes made above are done (ie.
my threading thought), relying on the current thread loose its sense,
as the framework would be aimed to be used from any thread.
- finally, the usage of singleton (static functions are close to
singleton's pattern) is often seen as an anti-pattern, mostly because
it freezes further changes (derivation of the singleton class, further
need for multiple instance of this class, ...).
Thus, I think the functions that rely on current thread should be
removed as much as possible.
I did some list of the current key classes that are needed in several
places : Platform, Input, AppContext, SoundEngine, Surface, Stage,
Scene, CoreApplet (and their respective derivated classes).
In those, we can see that :
- CoreSystem (singleton) holds the unique reference to the Platform.
- Platform holds a reference on the SoundEngine
- AppContext holds references on CoreApplet, Input, Surface, Stage
- Stage holds references on the list of scenes, Input, AppContext (its
manager), Surface & the animation thread.
That means, given an access to the instances of Platform and
AppContext, all other key parts of the framework can be accessed.
That's why, to my mind, only a few static functions could be kept,
mostly for convenience. I think too that when a function's result
depends on the current thread, its should explicitely mention it. (I
like the current 'getThisxxx()' naming). The two staitc functions
remaining could be:
- AppletPlatform.getInstance() and PlupPanelPlatform.getInstance()
(this is a singleton and even further needs won't involve the need of
multiple instances).
- AppContext.getThisAppContext() : for convenience (but its use would
be discouraged). To easily get the context associated to a thread.
To achieve that, we need to replace those functions by non-static
access in key places of the framework. I propose:
- to add a Scene2D pointer in each class of the package
pulpcore.sprite. That means each sprite will belong to one specific
Scene2D. In that case, the constructor of the sprite should take at
least a Scene pointer as parameter.
- to add a Context pointer in Scene2D, to be able to retrieve the
context owning this scene. As porposed in my previous code poted
above, I think a Scene2D could belong to *zero* or one Context at a
time.
More globally: As for the threading toughts, such changes would break
the compatibility with previous versions of pulpcore, but I think it
would be a great enhancement of the framework and make it even more
robust and flexible.
If my suggestions look no too bad for you, I could manage such change
and propose concrete things.
What do think of those ideas ?
I've got other ideas about moving a few responsabilities from one
class to another, but it's minor compared to the two suggestions
above.
Hope my (too) long posts are not too much boring to read, but it's
quite hard to propose changes and their reasons without being precise.
Best regards,
Nouk²