**This is an old revision of the document!**

Component: GPU

This is the component provided by graphics cards. For simple programs the term API will usually all you need. For more complex operations, or to get a bit more performance, you may wish to interact with the GPU directly, though.

As of OC 1.3 screens of tier 2 and 3 have a 16 color palette. For tier two this palette contains all colors the screen can possibly display, and is initialized to the standard Minecraft colors. As a side-effect you can specify the colors using gpu.setBackground(colors.red, true), for example. Keep in mind this only works on tier two screens. For tier three, the palette is initialized to grayscale values and the remaining 240 colors are stored as truncated RGB values as was the case in older versions of OpenComputers.

Component name: gpu.
Callbacks:

  • bind(address: string): boolean[, string]
    Tries to bind the GPU to a screen with the specified address. Returns true on success, false and an error message on failure.
    A GPU can only be bound to one screen at a time. All operations on it will work on the bound screen. If you wish to control multiple screens at once, you'll need to put more than one graphics card into your computer.
  • getScreen():string
    Get the address of the screen the GPU is bound to. Since 1.3.2.
  • getBackground(): number, boolean
    Gets the current background color. This background color is applied to all “pixels” that get changed by other operations.
    Note that the returned number is either an RGB value in hexadecimal format, i.e. 0xRRGGBB, or a palette index. The second returned value indicates which of the two it is (true for palette color, false for RGB value).
  • setBackground(color: number[, isPaletteIndex: boolean]): number[, index]
    Sets the background color to apply to “pixels” modified by other operations from now on. The returned value is the old background color, as the actual value it was set to (i.e. not compressed to the color space currently set). The first value is the previous color as an RGB value. If the color was from the palette, the second value will be the index in the palette. Otherwise it will be nil. Note that the color is expected to be specified in hexadecimal RGB format, i.e. 0xRRGGBB. This is to allow uniform color operations regardless of the color depth supported by the screen and GPU.
  • getForeground(): number, boolean
    Like getBackground, but for the foreground color.
  • setForeground(color: number[, isPaletteIndex: boolean]): number[, index]
    Like setBackground, but for the foreground color.
  • getPaletteColor(index: number): number
    Gets the RGB value of the color in the palette at the specified index.
  • setPaletteColor(index: number, value: number): number
    Sets the RGB value of the color in the palette at the specified index.
  • maxDepth(): number
    Gets the maximum supported color depth supported by the GPU and the screen it is bound to (minimum of the two).
  • getDepth(): number
    The currently set color depth of the GPU/screen, in bits. Can be 1, 4 or 8.
  • setDepth(bit: number): boolean
    Sets the color depth to use. Can be up to the maximum supported color depth. If a larger or invalid value is provided it will throw an error. Returns true if the depth was set, false otherwise.
  • maxResolution(): number, number
    Gets the maximum resolution supported by the GPU and the screen it is bound to (minimum of the two).
  • getResolution(): number, number
    Gets the currently set resolution.
  • setResolution(width: number, height: number): boolean
    Sets the specified resolution. Can be up to the maximum supported resolution. If a larger or invalid resolution is provided it will throw an error. Returns true if the resolution was set, false otherwise.
  • getSize(): number, number
    Gets the size in blocks of the screen the graphics card is bound to. For simple screens and robots this will be one by one.
    Deprecated, use screen.getAspectRatio() instead.
  • get(x: number, y: number): string, number, number, number or nil, number or nil
    Gets the character currently being displayed at the specified coordinates. The second and third returned values are the fore- and background color, as hexvalues. If the colors are from the palette, the fourth and fifth values specify the palette index of the color, otherwise they are nil.
  • set(x: number, y: number, value: string[, vertical:boolean]): boolean
    Writes a string to the screen, starting at the specified coordinates. The string will be copied to the screen's buffer directly, in a single row. This means even if the specified string contains line breaks, these will just be printed as special characters, the string will not be displayed over multiple lines. Returns true if the string was set to the buffer, false otherwise.
    The optional fourth argument makes the specified text get printed vertically instead, if true.
  • copy(x: number, y: number, width: number, height: number, tx: number, ty: number): boolean
    Copies a portion of the screens buffer to another location. The source rectangle is specified by the x, y, width and height parameters. The target rectangle is defined by x + tx, y + ty, width and height. Returns true on success, false otherwise.
  • fill(x: number, y: number, width: number, height: number, char: string): boolean
    Fills a rectangle in the screen buffer with the specified character. The target rectangle is specified by the x and y coordinates and the rectangle's width and height. The fill character char must be a string of length one, i.e. a single character. Returns true on success, false otherwise.
    Note that filling screens with spaces ( ) is usually less expensive, i.e. consumes less energy, because it is considered a “clear” operation (see config).

Example use:

snippet.lua
local component = require("component")
local gpu = component.gpu -- get primary gpu component
local w, h = gpu.getResolution()
gpu.fill(1, 1, w, h, " ") -- clears the screen
gpu.setForeground(0x000000)
gpu.setBackground(0xFFFFFF)
gpu.fill(1, 1, w/2, h/2, "X") -- fill top left quarter of screen
gpu.copy(1, 1, w/2, h/2, w/2, h/2) -- copy top left quarter of screen to lower right