I've given you this article a day early because I am ver y busy tommorow, enjoy =P
Why And How To Use Surfaces
First I'll just explain what a surface is, a surface is a canvas that can be drawn on and, saved, added as a sprite/background, or just shown later in the game. Thats 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 surfaced, and the surfaced is stored in the memory until the surface is freed. Many applications of surfaces exist but here are some fundamental uses:
-Motion blur or blur 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.
-And many more...
Surfaces don't stop there, in the advanced side of things surfaces can be used to take a text string, take an image, and draw the text but with that image as the text which I'll explain later. So heard enough? You want to know how to do it yourself, well it's your lucky day cause 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.
Heres some nifty code to get you started:
Creating and drawing on a surface:
Showing a surface on screen:
Adding a surface as a sprite:
Saving a surface to file:
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 tqo. And heres how it's done:
Create Event:
Draw Event:
And there you have it, a nice text texture example. They're 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.
The following is an extract from the Game Maker Help file and very useful information:
Some care must be taken when using these functions. In particular please notice the following:
-You should never change the drawing target while you are actually drawing on the screen, that is, never use it in drawing events. This will cause serious problems with the projection and viewport.
-Surfaces do not work correctly with 3D mode. You can use them while not in 3D mode (by calling d3d_end() before using them, but once you start 3D mode again the surfaces will be destroyed.
-For reasons of speed, the surface is maintained in videomemory only. As a result, you might loose the surface when e.g. the screen resolution changes or the screensaver pops up.
-Surfaces will not be saved when saving a game.
Why And How To Use Surfaces
First I'll just explain what a surface is, a surface is a canvas that can be drawn on and, saved, added as a sprite/background, or just shown later in the game. Thats 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 surfaced, and the surfaced is stored in the memory until the surface is freed. Many applications of surfaces exist but here are some fundamental uses:
-Motion blur or blur 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.
-And many more...
Surfaces don't stop there, in the advanced side of things surfaces can be used to take a text string, take an image, and draw the text but with that image as the text which I'll explain later. So heard enough? You want to know how to do it yourself, well it's your lucky day cause 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.
Heres some nifty code to get you started:
Creating and drawing on a surface:
{
var n;
n=surface_create(w,h); // creates a surface with a width of w and a 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
// all 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 tqo. And heres 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);
}
And there you have it, a nice text texture example. They're 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.
The following is an extract from the Game Maker Help file and very useful information:
Some care must be taken when using these functions. In particular please notice the following:
-You should never change the drawing target while you are actually drawing on the screen, that is, never use it in drawing events. This will cause serious problems with the projection and viewport.
-Surfaces do not work correctly with 3D mode. You can use them while not in 3D mode (by calling d3d_end() before using them, but once you start 3D mode again the surfaces will be destroyed.
-For reasons of speed, the surface is maintained in videomemory only. As a result, you might loose the surface when e.g. the screen resolution changes or the screensaver pops up.
-Surfaces will not be saved when saving a game.
0 Comments On This Entry
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Tags
My Blog Links
Recent Entries
-
Theory Of Online Gamingon Mar 02 2008 05:30 AM
-
Why And How To Use Surfaceson Feb 22 2008 09:09 AM
-
Announcing...on Feb 18 2008 02:29 PM
Search My Blog
3 user(s) viewing
3 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



Leave Comment








|