• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

How to clear all existing surfaces?

Hi all!
I'm trying to figure out a way for GMS to adapt to toggling from fullscreen to windowed mode while displaying surfaces. I can't seem to find a way to tell GMS to just clear all surfaces from memory (regardless of the object they're associated with or name) so that it can re-create them after the player switches from fullscreen to windowed. Is there a way to achieve this?

Thanks for reading
 
B

Bayesian

Guest
I think you'd have to register every object that has a surface and its surfaces in a global data structure or array and then go through it
 

TsukaYuriko

☄️
Forum Staff
Moderator
Surfaces are not indexed by default. You could create this index yourself, for example through a global list and adding every surface that gets created to it as well as removing every surface that is freed or happens to whoops out of existence.

Technically, keeping this list accurate isn't required since you can check whether any surface in the list actually exists before freeing it in order to avoid crashes. You don't even have to keep a list if you're willing to loop from anywhere between 1 to your best guess at how many surfaces might have been declared up to that point, checking if they exist and wiping them if they do so, but that's merely a cop-out that relies on surfaces being indexed through incremental integers rather than being their own data type... so it's anything but best practice.
 
Surfaces are not indexed by default. You could create this index yourself, for example through a global list and adding every surface that gets created to it as well as removing every surface that is freed or happens to whoops out of existence.

Technically, keeping this list accurate isn't required since you can check whether any surface in the list actually exists before freeing it in order to avoid crashes. You don't even have to keep a list if you're willing to loop from anywhere between 1 to your best guess at how many surfaces might have been declared up to that point, checking if they exist and wiping them if they do so, but that's merely a cop-out that relies on surfaces being indexed through incremental integers rather than being their own data type... so it's anything but best practice.
Interesting! I was actually having issues with assigning a surface to a global variable (such as global.surf1=create_surface) so I opted to just have an object make it locally (the documentation also seems to imply this is the way to go). I suppose I could try to also create an entry in some kind of global array? Maybe it just comes down to a data structure kind of thing, but I wanted to at least make sure there wasn't a way of just telling game maker "get rid of all surfaces" before I proceeded. I'm going to try a few different ways to have a global list I can just loop through since (as best I can tell) all surfaces have to be freed when toggling fullscreen/windowed.

Thanks for replying!
 

TsukaYuriko

☄️
Forum Staff
Moderator
I was actually having issues with assigning a surface to a global variable (such as global.surf1=create_surface) so I opted to just have an object make it locally (the documentation also seems to imply this is the way to go).
It makes no difference from a technical standpoint whether surfaces are under global or local scope. It would appear more natural to me to have them local, though. (This does not exclude also storing them in a global list for easier removal, though.)

I suppose I could try to also create an entry in some kind of global array? Maybe it just comes down to a data structure kind of thing, but I wanted to at least make sure there wasn't a way of just telling game maker "get rid of all surfaces" before I proceeded.
Whether you use an array, list or other data structure is up to personal preference. Either will work.
There is no clear all button.

I'm going to try a few different ways to have a global list I can just loop through since (as best I can tell) all surfaces have to be freed when toggling fullscreen/windowed.
If toggling full screen mode and surface freeing have any correlation at all, that would be an "is being freed" one, not a "has to be freed" one. The surfaces manual page provides additional examples. Exactly when or why surfaces go missing should not be a concern, though - there should always be a fail-safe in place.

Wouldn't surface_free() work?
That works for freeing one surface. The topic's question is how to free all of them.
 
Last edited by a moderator:
Top