I am writing a GUI library that should draw some things on the current active context, but for doing this it has to sometimes create a texture first using a RenderTexture. The problem is the current context gets deactivated and I found no good way in SFML to reactivate the previous context.
I see a few possible solutions:
- Ideally I would be able to retrieve a reference to a sf::Context for the current active context inside the library from some static method and then later call setActive on it, but I see no way in SFML for doing this. (I think the static ensureGLContext method would just use some hidden context and that would not help me as it would be the wrong one.)
- Next best alternative would be giving the library function a parameter with a reference to an object of a SFML class with the setActive function. The problem there is the inheritance hierarchy in SFML does not allow for casting to a common base class with a virtual setActive function, because its missing in GlResource/RenderTarget and RenderTarget does not even derive from GlResource. That makes it not possible to transparently handle Context, Window, RenderWindow and RenderTexture, because they all freshly declare a new version of the identically named function.
- Another alternative would be if all of Window, RenderWindow and RenderTexture had a function to get a reference to their Context, but there is also no such methods.
- The awkward, last-I-can-think-of and only-one-I-can-implement-myself-without-changing-SFML alternative would be I create a function object class with a pure virtual operator(), then derive a template function-object class from it that gets a reference to one of those SFML objects in the constructor, saves it in a member variable of the templates parameter-type and overrides the operator() to call setActive on the member. Then I would have the library user need to create a function object from the derived template and give the library a casted-to-the-base-class reference or smart pointer to it and the library could then somehow get it into the resource creation function to use it at the appropriate moment, but that function would be needed to be changed into a function object too and then the resource cache using that function as a template parameter would hopefully still work with this after some change to it.
Now I dont really think or know if it was feasible to change SFML for my singular case, though maybe someone got another idea how I could design my library in any other way around this?