Surfaces

This section deals with the functions related to creating and manipulating surfaces.

In the normal draw events, GameMaker: Studio doesn't actually draw directly to the screen, but rather draws to a surface called the application surface. This surface is basically a blank "canvas" that can then later be manipulated before being drawn to the screen when needed, and in most cases GameMaker: Studio handles this for you (although you can also manipulate it yourself in code for shaders, scaling and many other things - further details are given below).

However, apart from this application surface, you can also create your own surfaces and use them to create stunning or subtle special effects in your game. For example, you can use surfaces to "catch" instances, which can then be destroyed, and in this way you can create a decal effect where the sprite for the instance is displayed on the surface as if it still existed, permitting graphical effects like debris, blood, etc... without any real processing overhead. Another thing you can do with surfaces is use them as textures to be manipulated, or to create sprites "on the fly", or to create complex overlays. in fact, the uses for surfaces are endless!

Normal surfaces are quite easy to use, but there are a few basic rules to be followed when you use them:

  • First, you should realise that surfaces (except the application surface) are "volatile". This means that if the device or window loses focus or is minimised (good examples are when a screensaver comes up in Windows, or on an Android device when the app loses focus due to a call) then the surface may be destroyed. This is because it is stored in the texture memory and may be overwritten when the target platform needs that memory for something else which means that you should always have some type of fail-safe code in place, usually with the surface_exists function.

  • Second, you should note that surfaces require large quantities of texture memory to be used, and so you should attempt to keep them as small as possible. Normally you would try and keep them no larger than the size of the view or display window.

  • Third, you should try to draw to surfaces in the draw event. This is not always possible, but due to the optimised way in which GameMaker: Studio draws to the screen, it is recommended that you keep all draw functions within the draw event.

  • Fourth, when drawing to a surface manually, the surface is always at the position of (0,0). This means that you may need to convert absolute coordinates into local coordinates for the surface. For example, if you have a view-sized surface and wish to draw something that is currently in view to that surface, you should subtract the view x and y coordinates from the actual x and y coordinates to get a relative position to the surface (0,0) position. So, the code would look something like this:

    GML Code Example

    if view_current = 0
        {
        surface_set_target(surf);
        with (obj_Effect)
           {
           draw_sprite(sprite_index, image_index, x - view_xview[1], y - view_yview[1]);
           }
        surface_reset_target();
        }
     else
        {
        draw_surface(surf, 0, 0);
        }
     

The basic use of a surface would be as follows - You first create a surface and assign its index to a variable. You would then set the drawing target to the surface rather than the display and draw the things you wish as well as perform any other manipulations. Once you are done you reset the drawing target so that all further drawing happens on the screen again. One thing to note is that should you require drawing the whole display to a surface (including tiles, backgrounds etc...) you can simply access the application surface itself (see below for further details) or you could assign a surface to a view using the variable view_surface_id[0..7]as with this all that is visible in that view will be drawn to the corresponding surface.

The following functions exist to deal with surfaces:

These functions are specific for creating and manipulating surfaces, but to actually draw them to the screen you should be using the specific draw functions that can be found in the section on Drawing Surfaces.

The Application Surface

As mentioned above, all normal drawing in your game is done on the application surface, and this surface can be changed and manipulated just like a normal surface. However, it is not a normal surface and advanced users will find that it enables complete control over how and when things are drawn in GameMaker: Studio. For more details, as well as specific functions for this feature, please see the following section:


Back: Reference



No Examples Submitted

Does this page need better examples? Be the first to Submit

These are page edits suggested by the community. Cast your vote to help improve the manual.

Suggested by /u/gerahmurov on December 12, 2016

Please add the thing about drawing half-transparent sprites to the surface as it is here at the end: http://www.yoyogames.com/blog/57
It is not a simple task as half-transparent things always are more transparent after drawn to surface.

One of the example of how to draw the half transparency to surface is using clear white and special blending mode:
draw_set_blend_mode_ext(bm_one, bm_zero);
surface_set_target (Surf);
draw_clear (c_white);
draw_self();
surface_reset_target ();

Edited On December 12, 2016

No Questions Have Been Ask

Do you have a question about this page? Ask it Here