Documentation
¶
Overview ¶
Package pi provides the core Pi functions for the game loop, screen, color palette, and drawing pixels, shapes, and sprites.
This package and all other pi* packages are not thread-safe. This is an intentional design choice to significantly improve performance. You should not call Pi's API from any goroutine other than the one running your `pi.Update` and `pi.Draw` functions. You can still create your own goroutines in your game, but they must not call any Pi functions or access Pi state.
Index ¶
- Constants
- Variables
- func Blit(src Canvas, x int, y int)
- func Circ(cx, cy, r int)
- func CircFill(centerX, centerY, radius int)
- func Cls()
- func Line(x0, y0, x1, y1 int)
- func Pal(fromColor, toColor Color)
- func Palt(color Color, t bool)
- func Rect(x0, y0, x1, y1 int)
- func RectFill(x0 int, y0 int, x1 int, y1 int)
- func ResetColorTables()
- func ResetPalette()
- func ResetPaletteMapping()
- func SetPixel(x, y int)
- func SetScreenSize(width, height int)
- func Spr(sprite Sprite, dx, dy int)
- func Stretch(sprite Sprite, dx, dy, dw, dh int)
- type Area
- func (a Area[T]) ClippedBy(clip Area[T]) (_ Area[T], dx, dy T)
- func (a Area[T]) Contains(x, y T) bool
- func (a Area[T]) MovedBy(dx, dy T) Area[T]
- func (a Area[T]) Size() T
- func (a Area[T]) WithH(h T) Area[T]
- func (a Area[T]) WithW(w T) Area[T]
- func (a Area[T]) WithX(x T) Area[T]
- func (a Area[T]) WithY(y T) Area[T]
- type Canvas
- type Color
- type ColorTable
- type IntArea
- type Number
- type PaletteArray
- type PaletteMap
- type Position
- type RGB
- type Sprite
- type Surface
- func (m Surface[T]) Clear(v T)
- func (m Surface[T]) Clone() Surface[T]
- func (m Surface[T]) CloneArea(area IntArea) Surface[T]
- func (m Surface[T]) Data() []T
- func (m Surface[T]) EntireArea() IntArea
- func (m Surface[T]) FlatIndex(x, y int) int
- func (m Surface[T]) Get(x, y int) T
- func (m Surface[T]) Get2(x, y int) (T, T)
- func (m Surface[T]) Get3(x, y int) (T, T, T)
- func (m Surface[T]) GetLine(y int) []T
- func (m Surface[T]) H() int
- func (m Surface[T]) LinesIterator(area IntArea) iter.Seq2[Position, []T]
- func (m Surface[T]) Set(x, y int, value T)
- func (m Surface[T]) SetAll(values ...T)
- func (m Surface[T]) SetArea(area IntArea, values ...T)
- func (m Surface[T]) SetData(data []T)
- func (m Surface[T]) SetMany(x, y int, values ...T)
- func (m Surface[T]) SetSurface(x int, y int, src Surface[T])
- func (m Surface[T]) String() string
- func (m Surface[T]) W() int
Constants ¶
const MaxColors = 64
MaxColors is the maximum number of colors that can be used simultaneously on the screen.
Variables ¶
var ( // Update is a user-provided function called every frame. // // This function handles user input, performs calculations, and updates // the game state. Typically, it does not draw anything on the screen. Update = func() {} // Draw is a user-provided function called up to once per frame. // Pi may skip calling this function if the previous frame took too long. // // The purpose of this function is to draw to the screen. Draw = func() {} )
Game-loop function callbacks.
var ( // Frame is the current game frame number. // // It is automatically incremented by the backend at the start of each game frame. Frame int // Time is the current game time in seconds. // // It is automatically incremented by the backend at the start of each frame. Time float64 )
var ColorTables [4]ColorTable
ColorTables defines 4 different ColorTable entries, selected based on bits 6 and 7 in the Color value.
When drawing a draw (source) color over a target (background) color, the following operation is performed:
(source | target) >> 6
The result of this operation determines the index of the ColorTable to use.
var TPS = 30
TPS defines the ticks per second.
You can change TPS while the game is running.
Functions ¶
func Blit ¶ added in v0.30.0
Blit draws the contents of the src Canvas at (x, y) on the current draw target.
It takes into account the camera position, clipping region, color tables, and masks.
func Circ ¶ added in v0.10.0
func Circ(cx, cy, r int)
Circ draws the outline of a circle with center at (cx, cy) and radius r.
It takes into account the camera position, clipping region, color tables, and masks.
func CircFill ¶ added in v0.10.0
func CircFill(centerX, centerY, radius int)
CircFill draws a filled circle with center at (centerX, centerY) and the given radius.
It takes into account the camera position, clipping region, color tables, and masks.
func Line ¶ added in v0.9.0
func Line(x0, y0, x1, y1 int)
Line draws a line on the screen between (x0, y0) and (x1, y1), inclusive.
It takes into account the camera position, clipping region, color tables, and masks.
func Rect ¶ added in v0.8.0
func Rect(x0, y0, x1, y1 int)
Rect draws the outline of a rectangle between (x0, y0) and (x1, y1), inclusive.
It takes into account the camera position, clipping region, color tables, and masks.
func RectFill ¶ added in v0.8.0
RectFill draws a filled rectangle between (x0, y0) and (x1, y1), inclusive.
It takes into account the camera position, clipping region, color tables, and masks.
func ResetColorTables ¶ added in v0.30.0
func ResetColorTables()
func ResetPalette ¶ added in v0.30.0
func ResetPalette()
ResetPalette resets the palette to the default Picotron palette.
func ResetPaletteMapping ¶ added in v0.30.0
func ResetPaletteMapping()
func SetPixel ¶ added in v0.30.0
func SetPixel(x, y int)
SetPixel sets the draw color at the given coordinates.
It takes into account the camera position, clipping region, color tables, and masks.
func SetScreenSize ¶ added in v0.19.0
func SetScreenSize(width, height int)
SetScreenSize sets the screen size in pixels.
The current screen will be replaced with a new, cleared screen. The new screen also automatically becomes the current draw target.
The maximum number of pixels is 131072 (128 KB).
func Spr ¶
Spr draws the given sprite at (dx, dy) on the current draw target.
It takes into account the camera position, clipping region, color tables, and masks.
func Stretch ¶ added in v0.30.0
Stretch draws the given sprite stretched to the specified size.
dx, dy specify the position on the current draw target. dw, dh specify the width and height on the current draw target.
It takes into account the camera position, clipping region, color tables, and masks.
Types ¶
type Area ¶ added in v0.30.0
type Area[T Number] struct { X, Y, W, H T }
Area specifies rectangular boundaries on a Surface.
It is used by Sprite, SetClip, Surface.CloneArea, Surface.LinesIterator, and others.
func (Area[T]) ClippedBy ¶ added in v0.30.0
ClippedBy clips the area so it does not extend beyond the given clip region.
In addition to the new area, it also returns how much it was shifted. The output parameters dx and dy are always positive.
func (Area[T]) Contains ¶ added in v0.30.0
Contains reports whether the point (x, y) is inside the Area.
func (Area[T]) Size ¶ added in v0.30.0
func (a Area[T]) Size() T
Size returns the number of values in the area.
type Canvas ¶ added in v0.30.0
Canvas is used to store pixels
func DecodeCanvas ¶ added in v0.30.0
DecodeCanvas decodes a PNG file into a Canvas using the current Palette.
Must be called after the Palette has been set.
This function can be slow for large images that do not use indexed color mode, or that use indexed color mode with a different palette. Whenever possible, use images with indexed color mode and the same Palette. Doing so will also simplify your workflow, since you can inspect color indexes directly in your graphics editor.
func DecodeCanvasOrErr ¶ added in v0.30.0
DecodeCanvasOrErr is like DecodeCanvas but returns an error.
This function is useful when you need to validate user-provided PNG data. It returns an error if the input is not a valid PNG file or if the image contains more than 256 colors.
func DrawTarget ¶ added in v0.30.0
func DrawTarget() Canvas
func SetDrawTarget ¶ added in v0.30.0
SetDrawTarget sets c as the target Canvas for all subsequent drawing, including functions like Spr, SetPixel, Line, etc.
This function also automatically sets the clip region to cover the entire area of c.
type Color ¶
type Color = uint8
Color represents a pixel color value in the range 0..63 (first 6 bits). Bits 6 and 7 specify the ColorTable index.
var ( // ReadMask is the mask applied when reading the draw color. ReadMask Color = MaxColors - 1 // TargetMask is the mask used when reading the target color in sprite operations. TargetMask Color = MaxColors - 1 // ShapeTargetMask is the mask used when reading the target color in shape operations. ShapeTargetMask Color = MaxColors - 1 )
Masks used by drawing operations.
These define which bits of the color value are considered when reading or writing pixels.
type ColorTable ¶ added in v0.30.0
ColorTable defines the color mapping rules used during drawing.
The first index is the draw (source) color index, and the second is the target (background) color index.
For example:
colorTable[7][0] = 6
means drawing color 7 over color 0 will result in color 6.
See more about color tables:
https://www.lexaloffle.com/bbs/?tid=149249
func (ColorTable) String ¶ added in v0.30.0
func (p ColorTable) String() string
type Number ¶ added in v0.30.0
type Number interface { ~int | ~float64 | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~uint | ~byte | ~uint16 | ~uint32 | ~uint64 }
Number describes any numeric type in Go.
Includes signed integers, unsigned integers, and floating-point types.
type PaletteArray ¶ added in v0.30.0
PaletteArray maps each Color to an RGB value.
The array index is the Color, and the value is the RGB color, e.g., 0xFF00FF.
var Palette PaletteArray = defaultPalette()
Palette is the global color palette used before the final rendering of the Screen contents to the game window. It maps Color values to RGB.
The array index is the Color value, and the element is the RGB color, e.g., 0xFF00FF.
func DecodePalette ¶ added in v0.30.0
func DecodePalette(pngFile []byte) PaletteArray
DecodePalette extracts a palette from a PNG file.
- If the file uses an indexed palette, colors are read directly with their assigned indices.
- Otherwise, all unique colors are read and assigned indices automatically. Scanning starts from the top-left pixel and proceeds line by line.
func DecodePaletteOrErr ¶ added in v0.30.0
func DecodePaletteOrErr(pngFile []byte) (PaletteArray, error)
DecodePaletteOrErr works like DecodePalette but returns an error if the PNG file is invalid or contains too many colors.
func (PaletteArray) String ¶ added in v0.30.0
func (p PaletteArray) String() string
type PaletteMap ¶ added in v0.30.0
PaletteMap defines the color mapping for display.
The array index is the original color, and the value is the mapped color.
var PaletteMapping PaletteMap = notSwappedPaletteMapping()
PaletteMapping defines the color mapping for display.
Colors are remapped at the end of each frame, just before rendering the contents of the Screen to the game window.
The array index is the original color, and the value is the mapped color.
func (PaletteMap) String ¶ added in v0.30.0
func (p PaletteMap) String() string
type Position ¶ added in v0.19.0
type Position struct{ X, Y int }
Position represents a 2D integer coordinate.
It stores X and Y values in a 2D grid.
var Camera Position
Camera is the camera offset applied to all subsequent draw operations.
func (Position) Add ¶ added in v0.30.0
Add returns a new Position after adding coordinates with other
func (Position) Subtract ¶ added in v0.30.0
Subtract returns a new Position after subtracting coordinates with other
type RGB ¶ added in v0.30.0
type RGB uint32
RGB represents a color with three components: R (Red), G (Green), and B (Blue).
For example: black is 0x000000, white is 0xFFFFFF, green is 0x00FF00.
func FromRGBf ¶ added in v0.30.0
FromRGBf creates an RGB value from normalized floating-point components.
The r, g, and b parameters should be in the range [0.0, 1.0]. Values are clamped, converted to 8-bit, and combined into a single RGB value.
type Sprite ¶ added in v0.30.0
Sprite represents a portion of a Canvas.
func CanvasSprite ¶ added in v0.30.0
CanvasSprite returns a Sprite covering the entire canvas.
func SpriteFrom ¶ added in v0.30.0
SpriteFrom creates a Sprite from the specified region of the canvas.
func (Sprite) WithFlipX ¶ added in v0.30.0
WithFlipX returns a new Sprite with the FlipX value updated.
func (Sprite) WithFlipY ¶ added in v0.30.0
WithFlipY returns a new Sprite with the FlipY value updated.
func (Sprite) WithSize ¶ added in v0.30.0
WithSize returns a new Sprite with its size set to the given w and h.
func (Sprite) WithSizeScaled ¶ added in v0.30.0
WithSizeScaled returns a new Sprite scaled by w and h.
func (Sprite) WithSource ¶ added in v0.30.0
WithSource returns a new Sprite with its Source set to the given Canvas.
type Surface ¶ added in v0.30.0
type Surface[T any] struct { // contains filtered or unexported fields }
Surface represents a 2D grid for storing arbitrary data.
Surface is a generic container that maps 2D coordinates (X, Y) to values of any type T. It can be used to represent game maps, tile layers, simulation grids, or any other 2D spatial data.
Surface is already used in Pi to store pixels in the Canvas type.
Example usage:
s := NewSurface[int](10, 10) s.Set(3, 4, 42) value := s.Get(3, 4)
func NewSurface ¶ added in v0.30.0
NewSurface creates a new Surface with the specified dimensions.
func (Surface[T]) CloneArea ¶ added in v0.30.0
CloneArea clones the specified area of the surface.
If the area extends outside the bounds, the missing regions are filled with zero values.
func (Surface[T]) EntireArea ¶ added in v0.30.0
func (Surface[T]) FlatIndex ¶ added in v0.30.0
FlatIndex returns the index in Surface.Data for the given coordinates.
func (Surface[T]) Get ¶ added in v0.30.0
Get returns the value at the given coordinates.
If the coordinates are out of bounds, it returns the zero value of T.
func (Surface[T]) LinesIterator ¶ added in v0.30.0
LinesIterator returns an iterator for reading and writing data in lines.
This is a very efficient way to process regions in 2D space. LinesIterator enables writing advanced, low-level code.
The area must be clipped by m; otherwise, it will panic.
func (Surface[T]) SetSurface ¶ added in v0.30.0
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
_examples
|
|
gui
This example demonstrates building a simple GUI hierarchy with pigui.
|
This example demonstrates building a simple GUI hierarchy with pigui. |
shapes
Example showing how to draw shapes and use a mouse.
|
Example showing how to draw shapes and use a mouse. |
snake
A minimal snake (worm) game implementation.
|
A minimal snake (worm) game implementation. |
Package picofont provides the Pico-8 font created by Zep.
|
Package picofont provides the Pico-8 font created by Zep. |
Package pidebug provides an API for pausing the game for debugging purposes.
|
Package pidebug provides an API for pausing the game for debugging purposes. |
Package piebiten enables running your game using the [Ebitengine] backend.
|
Package piebiten enables running your game using the [Ebitengine] backend. |
Package pievent provides a simple observer pattern implementation for decoupled communication.
|
Package pievent provides a simple observer pattern implementation for decoupled communication. |
Package pifont provides functionality for rendering text using bitmap fonts.
|
Package pifont provides functionality for rendering text using bitmap fonts. |
Package pigui offers a minimal API for building GUIs.
|
Package pigui offers a minimal API for building GUIs. |
Package pikey provides functionality for a virtual keyboard.
|
Package pikey provides functionality for a virtual keyboard. |
Package piloop defines events published during the game loop.
|
Package piloop defines events published during the game loop. |
Package pipad provides functionality for a virtual gamepad inspired by controllers from the 1990s.
|
Package pipad provides functionality for a virtual gamepad inspired by controllers from the 1990s. |
Package pipool provides an extremely simple, non-thread-safe pool that can be used to reduce heap memory allocations.
|
Package pipool provides an extremely simple, non-thread-safe pool that can be used to reduce heap memory allocations. |
Package piring provides a ring buffer implementation.
|
Package piring provides a ring buffer implementation. |
Package piroutine provides functionality similar to coroutines found in other languages (e.g., Lua).
|
Package piroutine provides functionality similar to coroutines found in other languages (e.g., Lua). |
Package piscope provides developer tools.
|
Package piscope provides developer tools. |
Package pisnap provides functions for taking screenshots.
|
Package pisnap provides functions for taking screenshots. |
Package pistat provides information about current resource usage, such as CPU and RAM.
|
Package pistat provides information about current resource usage, such as CPU and RAM. |
Package pitest provides helper functions for writing unit tests.
|
Package pitest provides helper functions for writing unit tests. |