The Surface Function of GameMaker: Studio
In GameMaker: Studio, a Surface provides you another layer, so to speak, on which you can draw. At first, you probably only used Sprites and background colors to create a background effect in a Room. Essentially, by using Surfaces, you can draw on top of your background (the screen) if you want to add effects during the game.
For example, say you have a shooter game in which the player can toss grenades. When the player tosses the grenade, an explosion occurs. When the explosion occurs, you want a portion of the screen to appear damaged.
You create the damage effect by using Surfaces, in that the damage appears on the screen after the grenade explodes. Other uses for Surfaces include adding shadows and lights to complete the look and feel of your game.
Creating Surfaces requires writing code using the GameMaker Language.
Events and Actions in GameMaker: Studio include Events and Actions for drawing. The basic drawing function draws straight to the screen. That is, if you have a draw_circle function in the Draw Event, you see a circle appear on the screen.
If you want to draw an effect on top of the screen, you need to change the drawing target to be a Surface. Then all subsequent drawing will be drawn on the Surface layer instead of the screen. To change the drawing target, you can use draw_surface() functions; these functions also determine when the effect will be drawn.
GameMaker draws the whole game to a Surface, known as the application Surface. The application Surface is the only Surface that is not volatile and will always exist while your game is running. However, you can create your own Surface.
You want to keep your Surface drawings small — the bigger they are, the more memory they’ll use. And the more memory you use, the greater the odds that your game will lag or even crash. To make Surfaces even more complex, GameMaker stores Surfaces as a power of two textures, so that a 640-x-480-pixel Surface is treated as a 1,024-x-512-pixel texture in memory.
Try to keep your Surface sizes small so as not to use all the VRAM of the player’s computer or device.
The default position for Surfaces is always at the (0,0) coordinate within the Room. If you have a large Room with Views, or if you only want a small Surface for a special effect, you need to specify the drawing coordinates, offset within the Room (as discussed in the following procedures).