Categorygithub.com/faiface/pixel
modulepackage
0.11.0-beta
Repository: https://github.com/faiface/pixel.git
Documentation: pkg.go.dev

# README

Pixel Build Status GoDoc Go Report Card Join the chat at https://gitter.im/pixellib/Lobby Discord Chat

A hand-crafted 2D game library in Go. Take a look into the features to see what it can do.

go get github.com/faiface/pixel

If you are using Modules (Go 1.11 or higher) and want a mutable copy of the source code:

git clone https://github.com/faiface/pixel # clone outside of $GOPATH
cd pixel
go install ./...

See requirements for the list of libraries necessary for compilation.

All significant changes are documented in CHANGELOG.md.

Tutorial

The Wiki of this repo contains an extensive tutorial covering several topics of Pixel. Here's the content of the tutorial parts so far:

Examples

The examples repository contains a few examples demonstrating Pixel's functionality.

To run an example, navigate to it's directory, then go run the main.go file. For example:

$ cd pixel-examples/platformer
$ go run main.go

Here are some screenshots from the examples!

LightsPlatformer
LightsPlatformer
SmokeTypewriter
SmokeTypewriter
RaycasterGizmo
RaycasterGizmo

Features

Here's the list of the main features in Pixel. Although Pixel is still under heavy development, there should be no major breakage in the API. This is not a 100% guarantee, though.

  • Fast 2D graphics
    • Sprites
    • Primitive shapes with immediate mode style IMDraw (circles, rectangles, lines, ...)
    • Optimized drawing with Batch
    • Text drawing with text package
  • Audio through a separate Beep library.
  • Simple and convenient API
    • Drawing a sprite to a window is as simple as sprite.Draw(window, matrix)
    • Wanna know where the center of a window is? window.Bounds().Center()
    • ...
  • Full documentation and tutorial
  • Works on Linux, macOS and Windows
  • Window creation and manipulation (resizing, fullscreen, multiple windows, ...)
  • Keyboard (key presses, text input) and mouse input without events
  • Well integrated with the Go standard library
    • Use "image" package for loading pictures
    • Use "time" package for measuring delta time and FPS
    • Use "image/color" for colors, or use Pixel's own color.Color format, which supports easy multiplication and a few more features
    • Pixel uses float64 throughout the library, compatible with "math" package
  • Geometry transformations with Matrix
    • Moving, scaling, rotating
    • Easy camera implementation
  • Off-screen drawing to Canvas or any other target (Batch, IMDraw, ...)
  • Fully garbage collected, no Close or Dispose methods
  • Full Porter-Duff composition, which enables
    • 2D lighting
    • Cutting holes into objects
    • Much more...
  • Pixel let's you draw stuff and do your job, it doesn't impose any particular style or paradigm
  • Platform and backend independent core
  • Core Target/Triangles/Picture pattern makes it easy to create new drawing targets that do arbitrarily crazy stuff (e.g. graphical effects)
  • Small codebase, ~5K lines of code, including the backend glhf package

Related repositories

Here are some packages which use Pixel:

  • TilePix Makes handling TMX files built with Tiled trivially easy to work with using Pixel.
  • spriteplus Basic SpriteSheet and Animation implementations
  • pixelutils Variety of game related utilities (sprite packer, id generator, ticker, sprite loader, voronoia diagrams)

Missing features

Pixel is in development and still missing few critical features. Here're the most critical ones.

  • Audio
  • Drawing text
  • Antialiasing (filtering is supported, though)
  • Advanced window manipulation (cursor hiding, window icon, ...)
  • Better support for Hi-DPI displays
  • Mobile (and perhaps HTML5?) backend
  • More advanced graphical effects (e.g. blur) (solved with the addition of GLSL effects)
  • Tests and benchmarks
  • Vulkan support

Implementing these features will get us to the 1.0 release. Contribute, so that it's as soon as possible!

Requirements

If you're using Windows and having trouble building Pixel, please check this guide on the wiki.

PixelGL backend uses OpenGL to render graphics. Because of that, OpenGL development libraries are needed for compilation. The dependencies are same as for GLFW.

The OpenGL version used is OpenGL 3.3.

  • On macOS, you need Xcode or Command Line Tools for Xcode (xcode-select --install) for required headers and libraries.
  • On Ubuntu/Debian-like Linux distributions, you need libgl1-mesa-dev and xorg-dev packages.
  • On CentOS/Fedora-like Linux distributions, you need libX11-devel libXcursor-devel libXrandr-devel libXinerama-devel mesa-libGL-devel libXi-devel libXxf86vm-devel packages.
  • See here for full details.

The combination of Go 1.8, macOS and latest XCode seems to be problematic as mentioned in issue #7. This issue is probably not related to Pixel. Upgrading to Go 1.8.1 fixes the issue.

Contributing

Join us in the Discord Chat!

Pixel is in, let's say, mid-stage of development. Many of the important features are here, some are missing. That's why contributions are very important and welcome! All alone, I will be able to finish the library, but it'll take a lot of time. With your help, it'll take much less. I encourage everyone to contribute, even with just an idea. Especially welcome are issues and pull requests.

However, I won't accept everything. Pixel is being developed with thought and care. Each component was designed and re-designed multiple times. Code and API quality is very important here. API is focused on simplicity and expressiveness.

When contributing, keep these goals in mind. It doesn't mean that I'll only accept perfect pull requests. It just means that I might not like your idea. Or that your pull requests could need some rewriting. That's perfectly fine, don't let it put you off. In the end, we'll just end up with a better result.

Take a look at CONTRIBUTING.md for further information.

License

MIT

# Packages

Package imdraw implements a basic primitive geometry shape and pictured polygon drawing for Pixel with a nice immediate-mode-like API.
Package pixelgl implements efficient OpenGL targets and utilities for the Pixel game development library, specifically Window and Canvas.
Package text implements efficient text drawing for the Pixel library.

# Functions

Alpha returns a white RGBA color with the given alpha component.
C returns a new Circle with the given radius and center coordinates.
Clamp returns x clamped to the interval [min, max].
L creates and returns a new Line.
Lerp returns a linear interpolation between vectors a and b.
MakePictureData creates a zero-initialized PictureData covering the given rectangle.
MakeTrianglesData creates TrianglesData of length len initialized with default property values.
NewBatch creates an empty Batch with the specified Picture and container.
NewSprite creates a Sprite from the supplied frame of a Picture.
PictureDataFromImage converts an image.Image into PictureData.
PictureDataFromPicture converts an arbitrary Picture into PictureData (the conversion may be lossy, because PictureData works with unit-sized pixels).
R returns a new Rect with given the Min and Max coordinates.
RGB returns a fully opaque RGBA color with the given RGB values.
ToRGBA converts a color to RGBA format.
Unit returns a vector of length 1 facing the given angle.
V returns a new 2D vector with the given coordinates.

# Constants

Here's the list of all available Porter-Duff composition methods.
Here's the list of all available Porter-Duff composition methods.
Here's the list of all available Porter-Duff composition methods.
Here's the list of all available Porter-Duff composition methods.
Here's the list of all available Porter-Duff composition methods.
Here's the list of all available Porter-Duff composition methods.
Here's the list of all available Porter-Duff composition methods.
Here's the list of all available Porter-Duff composition methods.
Here's the list of all available Porter-Duff composition methods.
Here's the list of all available Porter-Duff composition methods.
Here's the list of all available Porter-Duff composition methods.

# Variables

IM stands for identity matrix.
RGBAModel converts colors to RGBA format.
ZR is a zero rectangle.
ZV is a zero vector.

# Structs

Batch is a Target that allows for efficient drawing of many objects with the same Picture.
Circle is a 2D circle.
Drawer glues all the fundamental interfaces (Target, Triangles, Picture) into a coherent and the only intended usage pattern.
Line is a 2D line segment, between points A and B.
PictureData specifies an in-memory rectangular area of pixels and implements Picture and PictureColor.
Rect is a 2D rectangle aligned with the axes of the coordinate system.
RGBA represents an alpha-premultiplied RGBA color with components within range [0, 1].
Sprite is a drawable frame of a Picture.
Vec is a 2D vector type with X and Y coordinates.

# Interfaces

BasicTarget is a Target with additional basic adjustment methods.
ComposeTarget is a BasicTarget capable of Porter-Duff composition.
Picture represents a rectangular area of raster data, such as a color.
PictureColor specifies Picture with Color property, so that every position inside the Picture's Bounds has a color.
Target is something that can be drawn onto, such as a window, a canvas, and so on.
TargetPicture is a Picture generated by a Target using MakePicture method.
TargetTriangles are Triangles generated by a Target with MakeTriangles method.
Triangles represents a list of vertices, where each three vertices form a triangle.
TrianglesClipped specifies Triangles with Clipping Rectangle property.
TrianglesColor specifies Triangles with Color property.
TrianglesPicture specifies Triangles with Picture property.
TrianglesPosition specifies Triangles with Position property.

# Type aliases

Anchor is a vector used to define anchors, such as `Center`, `Top`, `TopRight`, etc.
ComposeMethod is a Porter-Duff composition method.
Matrix is a 2x3 affine matrix that can be used for all kinds of spatial transforms, such as movement, scaling and rotations.
TrianglesData specifies a list of Triangles vertices with three common properties: TrianglesPosition, TrianglesColor and TrianglesPicture.