> For the complete documentation index, see [llms.txt](https://docs.perfectcs2.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.perfectcs2.com/documentacao/api/render.md).

# render

Desenhar na tela.

As funções `render.*` desenham por cima do jogo e **só funcionam dentro do callback `draw`**, que roda a cada frame. Fora dele, dão erro.

```lua
callbacks.add("draw", function()
    local w, h = render.screen_size()
    render.rect_filled(w / 2 - 50, 20, 100, 30, color(0, 0, 0, 160), 6)
    render.text(w / 2, 26, "Perfect", color(255, 140, 40), { size = 16, font = "bold", align = "center" })
end)
```

* Posições e tamanhos são em **pixels**; `(0, 0)` é o canto superior esquerdo.
* Cores são [color](/documentacao/tipos/color.md). Uma tabela simples também serve: `{ 255, 0, 0 }` ou `{ r = 255, g = 0, b = 0, a = 128 }`.
* Por padrão o desenho fica **atrás do menu**. Para desenhar por cima, use `render.set_layer("foreground")`.

***

## Tela

### render.screen\_size

```lua
local width, height = render.screen_size()
```

### render.world\_to\_screen

```lua
local x, y = render.world_to_screen(x, y, z)   -- or render.world_to_screen(vec3)
```

Converte uma posição do mapa para a tela. Devolve `nil` se o ponto não está visível, por exemplo atrás da câmera. É a mesma função de `engine.world_to_screen` e de `vec3:to_screen()`.

***

## Formas

| Função                                                               | Desenha                                                                                        |
| -------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| `render.line(x1, y1, x2, y2, color, [thickness=1])`                  | Linha                                                                                          |
| `render.rect(x, y, w, h, color, [rounding=0], [thickness=1])`        | Contorno de retângulo                                                                          |
| `render.rect_filled(x, y, w, h, color, [rounding=0])`                | Retângulo preenchido                                                                           |
| `render.gradient(x, y, w, h, color1, color2, [horizontal=false])`    | Degradê de cima (`color1`) para baixo (`color2`); com `horizontal`, da esquerda para a direita |
| `render.circle(x, y, radius, color, [thickness=1], [segments=auto])` | Contorno de círculo                                                                            |
| `render.circle_filled(x, y, radius, color, [segments=auto])`         | Círculo preenchido                                                                             |
| `render.arc(x, y, radius, from, to, color, [thickness=1])`           | Arco. Ângulos em graus: 0 aponta para a direita, crescendo no sentido horário.                 |
| `render.triangle(x1, y1, x2, y2, x3, y3, color, [thickness=1])`      | Contorno de triângulo                                                                          |
| `render.triangle_filled(x1, y1, x2, y2, x3, y3, color)`              | Triângulo preenchido                                                                           |
| `render.polyline(points, color, [thickness=1], [closed=false])`      | Linha por vários pontos: `{ x1, y1, x2, y2, … }`                                               |
| `render.polygon(points, color)`                                      | Polígono convexo preenchido                                                                    |
| `render.shadow(x, y, w, h, [rounding=0], [size=16], [opacity=0.5])`  | Sombra suave em volta de um retângulo, como a dos cards do menu                                |

```lua
-- rounded progress bar
render.rect_filled(20, 20, 200, 8, color(255, 255, 255, 30), 4)
render.rect_filled(20, 20, 200 * progress, 8, menu.accent(), 4)
```

***

## Texto

### render.text

```lua
render.text(x, y, text, color, [options])
```

`y` é o topo do texto. `options` é uma tabela:

| Campo     | Padrão      |                                                                                                    |
| --------- | ----------- | -------------------------------------------------------------------------------------------------- |
| `size`    | `14`        | Altura em pixels (6 a 128)                                                                         |
| `font`    | `"regular"` | `"regular"`, `"bold"`, `"mono"` (Consolas) ou `"icons"` (para [icons](/documentacao/api/icons.md)) |
| `align`   | `"left"`    | `"left"`, `"center"` ou `"right"`, em relação a `x`                                                |
| `shadow`  | `false`     | Sombra escura de 1 px                                                                              |
| `outline` | `false`     | Contorno escuro, que lê bem em qualquer fundo                                                      |

### render.measure\_text

```lua
local width, height = render.measure_text(text, [options])
```

Tamanho que o texto vai ocupar com essas opções. Funciona também fora do `draw`. Útil para desenhar uma caixa do tamanho certo:

```lua
local label = "Perfect V4"
local style = { size = 15, font = "bold" }
local w, h = render.measure_text(label, style)
render.rect_filled(18, 18, w + 16, h + 8, color(10, 10, 12, 200), 6)
render.text(26, 22, label, color(255, 255, 255), style)
```

***

## Imagens

### render.load\_image

```lua
local img = render.load_image("images/logo.png")
```

Carrega uma imagem **PNG** de dentro da pasta de scripts. Carregue uma vez, ao iniciar o script, e não dentro do `draw`. `img:size()` devolve largura e altura.

### render.image

```lua
render.image(img, x, y, [w], [h], [tint], [rounding=0])
```

Sem `w` e `h`, desenha no tamanho original. `tint` multiplica a imagem; por exemplo, `color(255, 255, 255, 128)` deixa ela meio transparente.

***

## Recorte e camada

| Função                         |                                                                                                           |
| ------------------------------ | --------------------------------------------------------------------------------------------------------- |
| `render.push_clip(x, y, w, h)` | Só o que estiver dentro desse retângulo aparece, até o `pop_clip`                                         |
| `render.pop_clip()`            | Desfaz o último `push_clip`                                                                               |
| `render.set_layer(layer)`      | `"background"` (padrão, atrás do menu) ou `"foreground"` (na frente de tudo). Vale até o fim do callback. |

Recortes que ficarem abertos são fechados sozinhos no fim do callback.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.perfectcs2.com/documentacao/api/render.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
