Using Surfaces

First I'll just explain what a surface is. A surface is a canvas that can be drawn on and then either saved, added as a sprite/background, or just shown later in the game. That's it at its basics.

Why use surfaces?
Surfaces are very powerful if used correctly. With the technology of surfaces, objects can be drawn onto the surface, and the surface is stored in the memory until the surface is freed. Many applications of surfaces exist but here are some fundamental uses:


 * Motion blur or bluring in general.
 * Rendering sprites and backgrounds in-game without the player realising.
 * Speeding up gameplay by sticking immobile sprites together.
 * Taking an in-game screenshot and displaying it without needing to save it as a file.
 * Plus many more…

Surfaces don't stop there, in the advanced side of things surfaces can be used to take a text string and an image and then draw the text but with that image as the texture which I'll explain later. So heard enough? You want to know how to do it yourself? Well then it's your lucky day because I'm going to tell you how to do just that.

How do I use surfaces?
I've just foolishly assumed just now that you know how to use the in built GML editor and are capable of programming in GML. If you don't know how to do those things I have just said, your not going to understand the rest of this article.

To start using surfaces you need to create one by using the function surface_create(width,height) < now this function actually returns a negative number if it fails to create a surface that big. This is usually because the graphics card of the computer running the game or program does not have enough memory to store the surface in. Now heres the interesting part: all you need to do then is set the surface as the drawing target and draw away with normal GML draw_ functions. But don't forget to reset the target of drawing to the screen after you have drawn everything you need on a surface. Don't worry you can always come back and edit a surface later on. Below is some helpful code to get you started.  Creating and drawing on a surface

var n; n=surface_create(w,h); // creates a surface with width of w and height of h if n<0 exit; // checks if the surface has been successfully created surface_set_target(n); // sets the drawing target to the surface // Draw code goes here, remember // Draw functions are exactly the same surface_reset_target; // resets the drawing target to the screen

Showing a surface on screen

draw_surface(id,x,y);

Adding a surface as a sprite

sprite_create_from_surface(id,x,y,w,h,precise,transparent,smooth,preload,xorig,yorig)

Saving a surface to file

surface_save(id,fname) // saves a surface to a file in BMP format

Surfaces are very easy to use once you've learned the basics which I've explained above. Now I'm going to explain how to apply the technologies of surfaces to your game. In this example I'm going to explain how to draw textured text. Two surfaces will be needed for this, one of the image, and one for the text alpha mask, all we do is draw a black rectangle over one surface, then draw white text over it; and then with the other surface just draw the image on the surface, and then create sprites from both of them apply sprite one as an alpha mask for sprite two and then draw sprite two. Here's how it's done:

Create Event

// Create both of the surfaces su1=surface_create(200,50); if su1<0 exit; su2=surface_create(200,50); if su2<0 exit; // Draw on surface one surface_set_target(su1); draw_set_color(c_black); draw_rectangle(0,0,200,50,0); draw_set_color(c_white); draw_text(10,10,"Textured Text"); surface_reset_target; // Draw on surface two surface_set_target(su2); draw_sprite(spr_texture,0,0,0); surface_reset_target; // Create sprites out of the surfaces sp1=sprite_create_from_surface(su1,0,0,200,50,0,0,0,1,0,0); sp2=sprite_create_from_surface(su2,0,0,200,50,0,0,0,1,0,0); sprite_set_alpha_from_sprite(sp2,sp1);

Draw Event

draw_sprite(sp2,0,0,0);

There you have it, a nice text texture example. There are lots of other uses of textures as I pointed out earlier which can have very nice graphics touches to a game. This concludes my article thanks for reading.