Clearing surfaces (not actually clearing)

Freddy Jones

Your Main Detective
My surfaces seem to be corroded with old corrupted images, after freeing them in the previous room, and clearing the newly created ones in the next.

Is there something I'm doing wrong?


Code:
    surface_set_target( newSurf );
        draw_clear_alpha( c_white, 0);
    surface_reset_target();
This has usually worked for me!
 

Phil Strahl

Member
So you free each surface, then re-create it and clear it with that code, correct? Or do you keep the surface and just clear it?
 

Freddy Jones

Your Main Detective
Hey, Phil!
In the previous room, I free(destroy) every surface that was made. In the next room, I create new surfaces, and as a precautionary measure I clear the surface. I don't keep it.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Clear with c_black and not c_white? Not sure if that'll make a difference, but I've always used black and it always works... would be curious to see if it makes any difference in this case.
 

Freddy Jones

Your Main Detective
Hey, Nocturne! I've tried many colours including black. I tried multiple colors at once. Nothing really made a difference.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Have you run the game with the debugger? You can see what is on each and every surface from that... you can also see if you are actually removing/creating/etc... surfaces as you think you are. Also, instead of using draw_clear_alpha() try simply setting the alpha to 0 (draw_set_alpha) and then drawing a coloured rectangle over the surface and see what results that gives.
 

Freddy Jones

Your Main Detective
Thanks for letting me know about this!
I've done some digging, fixed a small leak, but still no luck with the corroded surfaces.

There are no hanging surfaces left around, but what I have noticed is that there is an exceptionally large amount of textures that are left which are exactly what was left on the surfaces from the previous room. The first 3 textures are the texture pages for the graphics, and after that there's a lot of texture pages of old surfaces that were freed. No clue at all how this could happen.

Doing:
Code:
    surface_set_target( newSurf );
        draw_set_alpha( 0 );
        draw_clear( c_black );
    surface_reset_target();
    draw_set_alpha( 1 );
Does not change the results.
 
P

PlayLight

Guest
I believe Nocturne was refering to the following to clear the surface of any static or anomalies.
Code:
surface_set_target( newSurf );
draw_set_alpha(0);
draw_rectangle( 0, 0, surface_get_width(newSurf), surface_get_height(newSurf), false );
surface_reset_target();
draw_set_alpha(1);
as an alternative to > draw_clear_alpha( col, alpha );
 
Didn't see for sure in my brief glance through, but are you executing the code in one of the Draw Events? Drawing to surfaces in any event other then one of the Draw Events has no guarantee of working and the surface can easily get re-corrupted.
 

Freddy Jones

Your Main Detective
Didn't see for sure in my brief glance through, but are you executing the code in one of the Draw Events? Drawing to surfaces in any event other then one of the Draw Events has no guarantee of working and the surface can easily get re-corrupted.
Hey stainedofmind! That looks to be the real issue apparently. The surfaces were being recreated in the create event. So instead, I changed the code to newSurface = -1, that way it would just be created in the draw event when it knew it didn't exist. That seemed to fix it! Do you know of any way around this (for future reference)?

I also tried just creating the new surface, in the create event, and not clearing it and that also resulted in corrupted surfaces.

But I'm still slightly worried about all the textures that are being "left over" from the surfaces that were previously destroyed. Wouldn't that be a memory leak?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
try calling draw_texture_flush() at the end of the room and see if that clears them... although I agree that they probably shouldn't be there.
 
The surfaces were being recreated in the create event. So instead, I changed the code to newSurface = -1, that way it would just be created in the draw event when it knew it didn't exist. That seemed to fix it! Do you know of any way around this (for future reference)?
Not really a way around it. That's exactly how I would handle the situation.
 
P

ParodyKnaveBob

Guest
Howdy, $:^ J
Do you know of any way around this (for future reference)?

...

But I'm still slightly worried about all the textures that are being "left over" from the surfaces that were previously destroyed. Wouldn't that be a memory leak?
  1. Legacy GameMaker (before Studio that is) iirc warned against drawing to surfaces during the Draw Event, whereas Studio's instructions, tutorials, etc. all warn to draw to surfaces only during the Draw Event (and to always test that they exist before targeting them); note also (last I checked) that surface-targeting and resetting now stacks, meaning you need to reset as many times as you target in order to get back to the "base" drawing. Therefore, in the past, drawing to a surface in the Draw Event would've been a terrible workaround of a non-solution, whereas now, this is the official solution.
  2. As I understand it, the volatile surface memory just kind of gets thrown around and not especially deleted or anything once memory pointers are freed; therefore, you might get accidental artefacts of picturey junk (or just junky junk, lol,) splattering across your newly created surfaces (before clearing them), but the memory addresses should nonetheless be free and clear for future use, not leaking any bit of it in that sense.
Now, let's see how much my memory and/or current understanding fails me. $E^ ]

Either way, I do hope this helps,
Bob

P.S. Say howdy to Norville for us, eh? $:^ b ( $E^ b )
 
Top