此组件由显卡提供。对简单程序而言只需使用term(终端) API即可满足需求。而为了实现更复杂的操作或提高运行性能,你可能会需要直接与GPU交互。
OC 1.3中的2级与3级屏幕有16色的调色板。调色板的作用是决定显示RGB颜色时使用的具体颜色。
对二级屏幕而言,调色板包含的颜色即为屏幕能显示的所有颜色,默认为Minecraft的16种标准颜色。这种特性带来的副作用是你可以使用例如这样的写法:gpu.setBackground(colors.red, true)
(使用color API而不是16进制颜色,见函数的描述)。请注意这种写法仅可用于2级屏幕。
3级屏幕也有可编辑的16色调色板,同时还有240色的固定调色板。可编辑调色板默认为灰度值,其余的240色为缩减过的RGB颜色值,与OC模组早期版本的情况相同。
组件名:gpu
。
回调函数:
bind(address: string[, reset: boolean=true]): boolean[, string]
true
,若失败返回false
和一条报错信息。若reset
参数为true
则重置屏幕的设置。
一个GPU同一时间只能被绑定到一块屏幕。在其上进行的一切操作都会在所绑定的屏幕上进行。如果你希望同时控制多块屏幕,则需要在电脑中安装多张显卡。getScreen():string
getBackground(): number, boolean
0xRRGGBB
,也可能是调色板索引号。第二个返回值代表了第一个返回值是上述哪一种(true
代表调色板颜色, false
代表RGB颜色值)。setBackground(color: number[, isPaletteIndex: boolean]): number[, index]
nil
。0xRRGGBB
。这是为了能统一颜色操作,而无需考虑屏幕或GPU支持的色深。getForeground(): number, boolean
getBackground
,但是为前景色。setForeground(color: number[, isPaletteIndex: boolean]): number[, index]
setBackground
,但是为前景色。getPaletteColor(index: number): number
setPaletteColor(index: number, value: number): number
maxDepth(): number
getDepth(): number
setDepth(bit: number): string
OneBit
、FourBit
或EightBit
。maxResolution(): number, number
getResolution(): number, number
setResolution(width: number, height: number): boolean
true
(尝试将分辨率设定为与当前分辨率相同也可能会返回false
),若失败则返回false
。getViewport(): number, number
setViewport(width: number, height: number): boolean
true
(尝试将分辨率设定为与当前分辨率相同也可能会返回false
),若失败则返回false
。此操作会使得屏幕分辨率看上去降低,但实际分辨率仍然不变。超出指定区域左上角的字符只是隐藏不显示,此功能是为了能在屏幕外渲染或显示内容,并在需要时将它们复制到可视区域内。改动分辨率会将可见区域修改为与分辨率相同。getSize(): number, number
screen.getAspectRatio()
。get(x: number, y: number): string, number, number, number or nil, number or nil
nil
。set(x: number, y: number, value: string[, vertical:boolean]): boolean
true
,若失败则返回false
。true
可以使给定文本竖行显示。copy(x: number, y: number, width: number, height: number, tx: number, ty: number): boolean
x
、y
、width
与height
参数决定。目标位置由x + tx
、y + ty
、width
与height
决定(即tx
和ty
为相对坐标)。若成功返回true
,若失败返回false
。fill(x: number, y: number, width: number, height: number, char: string): boolean
x
与y
坐标以及矩形的width
(宽度)和height
(高度)决定。填充字符参数char
必须是长度为1的字符串,即单个字符。若成功返回true
,若失败返回false
。
)填充屏幕通常耗能更少,因为此操作被视为“清除”操作(见配置文件)。使用例:
local component = require("component") local gpu = component.gpu --获取首选GPU组件 local w, h = gpu.getResolution() gpu.fill(1, 1, w, h, " ") --清空屏幕 gpu.setForeground(0x000000) gpu.setBackground(0xFFFFFF) gpu.fill(1, 1, w/2, h/2, "X") --填充屏幕的左上四分之一区域 gpu.copy(1, 1, w/2, h/2, w/2, h/2) --将屏幕的左上四分之一复制到右下
Color Depth (see gpu.setDepth
and gpu.getDepth
) can be 1, 4, or 8 bits separately for foreground and background. These depths provide 2, 16, and 256 colors respectively.
The color value (the number
passed to gpu.setBackground
and gpu.setForeground
) is interpreted either as a 8 bits per channel rgb value (24 bit color) or a palette index.
The background and foreground colors, as set by calling setBackground
and setForeground
, are defined by a value
(number) and is_palette
(boolean) pair (the boolean being optional).
When is_palette
is false (or nil
), value
is interpreted as a 24 bit rgb color (0xRRGGBB), regardless of depth. However, the color is approximated to the closest available color in the given depth. In monochrome, zero rounds to zero and all nonzero values round to 1 (and the configured monochrome color is used). In 4 bit color, the closest available color in the palette is selected. In 8 bit color the closest color of the available 256 colors is used. The available 256 colors are described in the following table:
Image by Eunomiac
When is_palette
is true
, value
is interpreted as palette index [0, 16). If you switch from a higher bit density to monochrome note that the color value from the palette is used to determine zero vs the nonzero monochrome color. It is an error to specify a paletted color (i.e. an index value and true
) in 1 bit depth.
Note that the original color pair (the value number
and palette bool
) are preserved (background and foreground each) even when switching bit depths. The actual rendering on the screen will update to respect the new depth, but the original 24bit rgb value (or palette index) is not lost. For example, calling gpu.getBackground
while in 1 bit mode will return the original 24 bit rgb value specified from any previous color depth.
A GPU card has internal memory that you can allocate into pages. You can specify a custom page size (width and height each must be greater than zero). The total memory of a GPU is reduced by the width*height of an allocation. Each tier of gpu has more total memory than the last. Each page buffer acts like an offscreen Screen with its own width, height, and color. The max color depth of a gpu buffer is based on the gpu tier. Rebooting a machine releases all bufffers.
Each page buffer has its own index; the gpu finds the next available index. Index zero (0) has a special meaning, it is reserved for the screen. Whether a gpu is bound to a screen or not, you can allocate pages, set them active, and read/write to them. Attaching and detaching a screen, even binding to a new screen, does not release the gpu pages. When a computer shuts off or reboots, the pages are released. Each GPU has its own video memory and pages.
Updates to vram (set, copy, fill, etc) are nearly free. They have no energy cost and no additional budget cost. Every direct component invoke (and these gpu methods are direct) has a tiny system minimum budget cost, but the gpu itself in these vram updates adds no additional cost. When bitblt'ing the vram to the screen there is some cost, similar to how updates to the screen normally incur a cost. A dirty (modified) vram back buffer has a one time budget cost that increases with the size of the source buffer. Subsequent bitblts from a clean back buffer to the screen have extremely low costs.
getActiveBuffer(): number
setActiveBuffer(index: number): number
index
. 0 is reserved for the screen and can be set even when there is no screen. Returns nil for an invalid index (0 is valid even with no screen)buffers(): table
allocateBuffer([width: number, height: number]): number
freeBuffer([index: number]): boolean
index
(default: current buffer index). Returns true if the buffer was removed. When you remove the currently selected buffer, the gpu automatically switches back to index 0 (reserved for a screen)freeAllBuffers()
totalMemory(): number
freeMemory(): number
getBufferSize([index: number]): number, number
index
(default: current buffer index). Returns the screen resolution for index 0. Returns nil for invalid indexesbitblt([dst: number, col: number, row: number, width: number, height: number, src: number, fromCol: number, fromRow: number])