Surfaces Question

Xer0botXer0

Senpai
Hi guys,

I'm wondering if a surface has to be drawn.
Code:
surf = surface_create(w,h);
surface_set_target(surf);
draw_sprite()..
surface_reset_target();
Would this work ? I suspect I wouldn't be able to see the sprite that I drew on the surface because I didn't draw the surface, but yeah. Also in that regard, as long as I've set the target surface to surf, can I draw onto the surface before drawing the surface or do I have to draw content onto the surface after drawing it ?
 
D

dannyjenn

Guest
You draw onto the surface before drawing the surface.
Think of the surface as sort of second screen that's kept offscreen. You draw onto the surface exactly as you would draw onto the screen, but since its all offscreen you can't see any of it. To see it you must draw the surface itself onto the screen. But if you do that before drawing onto the surface, you won't see anything, because there's nothing to be seen. (Well, strictly speaking, you will see all of the surface's old contents, since surfaces do not clear themselves automatically.)
 
D

dannyjenn

Guest
This way is correct:
Code:
// in create event:
surf = surface_create(w,h);

// in draw event:
if(surface_exists(surf){ // <-- be sure you check this first
    surface_set_target(surf);
    draw_sprite() // you update the surface
    surface_reset_target();
    draw_surface() // you draw the [updated] surface to the screen
}
This other way will NOT work:
Code:
// in create event:
surf = surface_create(w,h);

// in draw event:
if(surface_exists(surf)){ // <-- be sure you check this first
    draw_surface() // you draw the [not yet updated] surface to the screen
    surface_set_target(surf);
    draw_sprite() // you update the surface
    surface_reset_target();
}
The reason is because the latter is not being done in the correct order. Analogy: The first way (the correct way) is like if I write an essay and print it out and give it to my teacher. My teacher will then be able to read my essay. The second way (the incorrect way) is like if I were to open a new document, print out a blank page, give the blank page to my teacher, and then write my essay afterwards. No matter what I write afterwards, the teacher isn't going to see any of it, because the only thing I've turned in is a blank page.


As for your primary question, that depends on what you're trying to make it do. If you just want to draw a sprite to the surface, then yes, it would work. But if you want to draw a sprite to the surface and see it on the screen, then no, it would not work, because you never drew the surface to the screen.

Another thing to keep in mind is, the screen is automatically cleared every step, but surfaces are not automatically cleared every step. It's also a good idea to clear the surface (using draw_clear or draw_clear_alpha) before drawing anything to it.
 
Last edited by a moderator:
Top