and it works, but not at the same moment you change it, it needs either restart or Fullscreen to be (de)activated. As I understood the change of window mode is done deeply on OGL level, and it is not simple , ofc.
The question is, can we use some workaround? for example by triggering some method of Window (similar to FS change, but somewhat nicer), and in my gameUI I would bind it to some Apply button.
Note that if you’re fine with “always having window with multi-sampling” and just want to toggle anti-aliasing on/off at run-time, you can use code like this:
if GLFeatures.Multisample then glEnable(GL_MULTISAMPLE)
if GLFeatures.Multisample then glDisable(GL_MULTISAMPLE);
Use units {$ifdef OpenGLES} CastleGLES, {$else} CastleGL, {$endif} to have the glDisable and GL_MULTISAMPLE identifiers.
The GL_MULTISAMPLE flag is by default “on” when context has multi-sampling, but can be disabled at run-time. The additional multi-sampling storage of the context will then lie unused.
For a proper answer to your question, how to make the changes to Window.AntiAliasing be applied:
Using multi-sampling requires special OpenGL(ES) context creation flags, so indeed the context needs to be recreated to apply the new Window.AntiAliasing.
To do it (without restarting the application or fullscreen switch) you can do this:
MyWindow.Close(false);
MyWindow.Open;
The false parameter to Close above means QuitWhenLastWindowClosed=false, so effectively such “closing the window” doesn’t change Application.Terminated.
Let me know how it works for you – admittedly there may be some adverse effects that I didn’t think about… It’s a valid feature request to make it better.
And it is definitely possible to “do it better”, since the above method will destroy + recreate more than it needs to. E.g. it hides and destroys the window, then creates a new one, maybe at different position and size. It is possible to do it better, at least with most backends, we could keep the OS window existing and only destroy+recreate OpenGL(ES) context. Most backends use now TGLContext descendants to manage contexts, so it should be possible to “only destroy + recreate context” with them.
So, let me know, above is just “the simplest way to do it now”, maybe we can improve it
here I implemented 2 option, looks OK, but ofc with reopen.
high-level approach with Window.Antialiasing looks more user-friendly, as it offers fine-tuning and exact text value.
But I also tried 1 option, it works without reopen, but as I understood, I need Antialiasing/Multisampling be set to some value upfront (at app start), and then I’m able to turn on and off GL_MULTISAMPLE without window reopen, so I need to set separately AA/MS value and glEnable and make use of it on the interface
and the fun fact, if you have saved AA value as none (or MS as 1) and set it up on start, then glEnable has no power on actual result
so I need to elaborate some sort of combination of these and put it to interface, presumably I will store it as direct AAvalue, I would use AA always at least minimal (but not None) to setup the window at start, and if AA is None I would make gldisable, otherwise enable. The only downside is that user is not able to change dynamically the quality of AA, only on/off, but maybe I would also keep an option to reopen window
Let’s see
Indeed, to make use of the 1st option (toggling glEnable(GL_MULTISAMPLE) / glDisable(GL_MULTISAMPLE)) you need to have context initialized with multi-sampling. These options do nothing if context doesn’t have multi-sampling.
So this approach is simplest to use if you set some particular Window.AntiAliasing value (not none) always, like aa4SamplesNicer. And then at run-time you just present to user checkbox “Anti-Aliasing”, yes/no, and toggle glEnable(GL_MULTISAMPLE) / glDisable(GL_MULTISAMPLE) based on this choice.
This is admittedly less functional than the “full control of anti-aliasing” like in option 2, which allows to choose anti-aliasing quality.
I’d say that if you want the “full control of anti-aliasing”, then my “option 1” may be just unnecessary hassle If you want to allow changing anti-aliasing quality, then you want to change Window.AntiAliasing and we need to recreate the context (which right now means: reopening the window).