• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Draw surface to self?

Neptune

Member
Is something like this legal? Or do you have to create a separate "middle man" surface to apply an effect at that point?
Any info is appreciated :)
GML:
if !surface_exists(surf) {surf = surface_create(100,100);}

surface_set_target(surf);

    //set a shader
    draw_surface(surf,0,0);

surface_reset_target();
 

SoapSud39

Member
It might work but it's not recommended because, according to documentation, surfaces are unstable, so their memory gets overwritten really easily. So if you're trying to redraw the surface from a previous step something funky might happen with the graphics. And if you've just created the surface there's nothing to draw using this code. Keeping surfaces past a step can also result in memory issues.

If you're trying to apply shaders to images created as surfaces there are better ways to do it.
e.g.
GML:
//create surface
//surface set target
//draw sprites
//surface reset target
//set shader
//draw surface
//reset shader
or
GML:
//surface set target
//draw sprites
//surface reset target
sprite_create_from_surface();
//set shader
//draw sprite
//reset shader
If you create a sprite, you can use it outside of the step.
In either case, you'll want to delete the info at the end of the step to avoid memory leaks and crashes. For surfaces, use surface_free(), and for created sprites, especially if it is being created every step, use if (sprite_exists(sprite_name)) sprite_delete(sprite_name) in the draw end or draw GUI end event (do it in one of these, because if you do it in the draw or draw GUI event it'll make the sprite a white box).

If you have something specific you're trying to achieve, I or many others would be happy to help.
 
Top