surface_create

该函数创建一个新表面并返回它。

首次创建表面时,它可能包含 " 噪声",因为基本上它只是为此目的而保留的内存区域 (并且该内存可能仍然包含信息),因此您可能需要在使用之前清除表面类似 draw_clear_alpha 的函数。

强烈建议所有表面的大小均为 2 的幂,例如大小为 16、128、512 或 1024 像素。这在某些平台 (如 Windows 和 MacOS) 上并不是完全必要的,但它肯定会提高这些目标的兼容性,而对于 HTML5 和设备来说,这是必不可少的,并且记住这一点非常重要,否则以后可能会遇到问题。

格式

您可以选择指定将用于在内存中存储表面 数据的格式。默认格式为 surface_rgba8unorm

创建表面 时,可以使用以下任何一种格式:

Surface Format Constant
常量描述
surface_rgba8unorm(Default) This format supports 4 channels (red, green, blue, alpha) with 8 bits each, meaning each channel has a 0-255 range

"unorm" refers to these values being normalised into a 0-1 range when read in shaders
surface_r8unormThis format supports a single channel (red) with an 8-bit value (0-255)

Since it only contains one channel, it takes a quarter of the space compared to the format above (which stores RGBA)

When read in a shader, all channels except red will be 0
surface_rg8unormThis is similar to the format above, however it contains two channels: red and green
surface_rgba4unormThis format supports 4 channels (red, green, blue, alpha) with 4 bits each, meaning each channel has a 0-15 range
surface_rgba16floatThis format supports 4 channels (red, green, blue, alpha) with each channel being a 16-bit float, providing a higher precision

An example use case is HDR, as this format would allow you to use values past the default 0-255 colour range
surface_r16floatThis format supports a single channel (red) with a 16-bit floating point value

Since it only contains one channel, it takes a quarter of the space compared to the format above (which stores RGBA)

When read in a shader, all channels except red will be 0
surface_rgba32floatThis format supports 4 channels (red, green, blue, alpha) with each channel being a 32-bit float, providing the highest precision, however this is slower to render to than 16-bit formats and is not as widely supported
surface_r32floatThis format supports a single channel (red) with a 32-bit floating point value

 

 

语法:

surface_create(w, h, [format]);

参数类型描述
wReal要创建的表面的宽度
hReal要创建的表面的高度
formatSurface Format Constant可选 用于存储表面数据的格式,默认为 surface_rgba8unorm

 

返回:

Surface (or -1 if anything went wrong)

 

例子:

if (!surface_exists(surf))
{
    surf = surface_create(1024, 1024);
    surface_set_target(surf);
    draw_clear_alpha(c_black, 0);
    surface_reset_target();
    view_surface_id[0] = surf;
}

上面的代码检查表面是否存在,如果不存在,则会创建一个宽 1024 像素、高 1024 像素的表面,并将其分配给变量 "surf"。然后将绘图目标设置为新表面,在将绘图目标重置为显示器之前,该表面将被清除并变得透明。最后将表面分配给视图。