在一般的绘制事件里, GameMaker Studio 2 不会真实的绘制屏幕上,而是绘制到 表面上,那个表面叫做应用表面。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 2 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).
此外,除了应用表面,也可以创建自己的表面层,游戏中用来设计一些华丽的特效。例如,你可以使用表面来“捕捉”实例,然后摧毁它,并以这种方式,在表面(surfaces)上,实例的精灵显示的位置,创建一个贴花效果,就好像它仍然存在一样,以此来制作一些图像效果,如碎片,血液等.. 这上面没有任何真实的处理进程。另外就是使用表面作为需要操作的纹理贴图,或者创建“在飞”的精灵,或者创建复杂的叠加。in fact, the uses for surfaces are endless!
表面使用很简单,但在使用的时候,也有几个应遵循的基本规则:
- 首先,应该认识到表面层(除了 应用表面层)是“不稳定的 ” are "volatile". 这意味着,如果设备或窗口失去焦点或最小化(很好的例子是,当Windows弹出屏幕保护程序或Android 设备来电话时应用程序失去焦点的情况),表面可能会被破坏。这是因为它储存在显存里,目标平台往内存里写入其它东西时就有可能会重写,所以你需要一直 在某些位置入置安全的代码,通常是 surface_exists 函数。
- 其次,你应该注意,使用表面需要大量的显存,所以你应该尽可能的让它们更小。通常不要超过视野或者显示窗口的大小。
- Third, you should only create surfaces in the draw event. If you create a surface in the Create Event of an instance, you could potentially get the same index as the application_surface. This then can cause lots of problems and confusion as you think your using your own surface, but you are actually using the current render target. You should also always try to limit drawing to a surface in the draw event too, since due to the optimised way in which GameMaker Studio 2 draws to the screen, it is recommended that you keep all draw functions within the draw event - this includes clearing a surface when it is first created, etc... Drawing to a surface outside of the draw event is possible and may even be necessary for some effects, but it's not how it should be done.
- 第四,手动绘制表面层时,表面 始终 在 (0,0)的位置。这意味着你可能需要为表面转换坐标,从绝对坐标转换到局部坐标系。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 camera view x and y coordinates from the actual x and y coordinates to get a relative position to the surface (0,0) position. 因此,代码可以这样写:
if view_current = 0
{
surface_set_target(surf);
with (obj_Effect)
{
var _vx = camera_get_view_x(view_camera[1]);
var _vy = camera_get_view_y(view_camera[1]);
draw_sprite(sprite_index, image_index, x - _vx, y - _vy);
}
surface_reset_target();
}
else
{
draw_surface(surf, 0, 0);
}
表面层的基本用法如下 - 首先创建一个表面,给它的索引分配给一个变量。然后你可以设置绘图目标到表面上,而不是显示器,往里面绘制内容并进行一些调整。一旦完成,重置绘制目标,再让进一步的绘制发生在屏幕上。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 port using the variable view_surface_id[0..7] as with this all that is visible in that view port 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 Surface Drawing, below):
- surface_exists
- surface_create
- surface_create_ext
- surface_resize
- surface_set_target
- surface_set_target_ext
- surface_get_target
- surface_get_target_ext
- surface_reset_target
- surface_copy
- surface_copy_part
- surface_depth_disable
- surface_get_height
- surface_get_width
- surface_get_texture
- surface_get_depth_disable
- surface_getpixel
- surface_getpixel_ext
- surface_free
- surface_save
- surface_save_part
正如上面提过的,所有普通绘制都是在应用表面层里进行的,这个表面可以像通常的表面层一样改变或控制。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 2. 更多具体的内容,以及一些特性函数,请查阅如下的章节:
The following functions and variable exists for referencing the application surface:
And you also have a few special functions that are designed only for use with the application surface:
Surfaces are an incredibly powerful graphic tool that GameMaker Studio 2 provides you for creating special effects and a whole host of other things. They are relatively simple to use as they are basically a canvas that you draw to instead of the display screen, and this canvas can then be manipulated and changed before being drawn with the functions in this section.
注:由于储存在显存里,表面层运行时存在随时停止并退出的可能性。在直接引用它们之前,你需要 一直使用 surface_exists检测表面层是否退出。The following functions exist for drawing surfaces: