render

package
v0.3.1-0...-ca91996 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2021 License: MIT Imports: 4 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Canvas

type Canvas interface {
	// Finish resets the target of the underlying renderer and returns the
	// rendered image.
	Finish() Image
	// Close resets the target of the underlying renderer and deletes the
	// underlying texture if it was not yet finished.
	// It is idempotent and does nothing if Finish() was called before.
	Close()
}

Canvas is a facility to render content into a rectangular canvas. use it to pre-render content into a texture you can later copy to the renderer when rendering a scene.

Creation of a Canvas sets the target of the underlying Renderer to the texture of this Canvas. Ensure that you either Close or Finish a Canvas object after creating it to reset the renderer's target. It is recommended to use `defer canvas.Close()` right after creation, which will do nothing if you explicitly call Finish() in the following code.

To draw into the canvas, simply use the Renderer API.

type Directions

type Directions uint8

Directions is a bitset of directions.

const (
	// Nowhere contains no direction at all.
	Nowhere Directions = 0
	// North is the northern direction
	North Directions = 1 << iota
	// East is the eastern direction
	East
	// South is the southern direction
	South
	// West is the western direction
	West
	// Everywhere contains all directions.
	Everywhere = North | East | South | West
)

type HAlign

type HAlign int

HAlign defines horizontal alignment

const (
	// Left is horizontal alignment to the left
	Left HAlign = iota
	// Center is horizontal alignment in the center
	Center
	// Right is horizontal alignment to the right
	Right
	// HStretch stretches width to the maximum
	HStretch
)

type Image

type Image struct {
	TextureID     uint32
	Width, Height int32
	// Flipped describes the row order in the image data.
	//   true  -> first datapoint in image data is lower left corner.
	//   false -> first datapoint in image data is upper left corner.
	// This is used because loading image files returns the top row first,
	// while internally OpenGL places the bottom row first (e.g. when rendering
	// to a texture).
	Flipped bool
	// true iff the texture has an alpha channel.
	HasAlpha bool
}

Image is a rectangular image stored as OpenGL texture `id` is the name of an OpenGL texture iff width!=0. any Image width width==0 is considered the empty image.

In the context of this API, this type should be considered opaque with only Width and Height being read-accessible. The fields are public so that plugins can use the data for custom OpenGL processing.

func EmptyImage

func EmptyImage() Image

EmptyImage returns an image that has no linked OpenGL texture.

func (Image) Draw

func (i Image) Draw(r Renderer, area Rectangle, alpha uint8)

Draw draws the image to the given rectangular area. The image will be stretched to fit the whole area.

func (Image) IsEmpty

func (i Image) IsEmpty() bool

IsEmpty tests whether the image is empty (i.e. does not link to an OpenGL texture)

type Rectangle

type Rectangle struct {
	// coordinate of the lower left corner
	X, Y          int32
	Width, Height int32
}

Rectangle describes a rectangle positioned on the screen.

The screen coordinates are (0,0) at the lower left corner and (width, height) at the upper right corner.

Rectangle is the high-level API for positioning stuff on the screen. It provides functionality to position one rectangle in another one and so on. You can use it in conjunction with the lower-level Transform API which can do rotations by using Rectangle's Translation() / Transformation() funcs.

func (Rectangle) Carve

func (r Rectangle) Carve(edge Directions,
	length int32) (carved Rectangle, rest Rectangle)

Carve removes a rectangle of the given length starting at the given edge from the current rectangle.

edge must be North, East, South or West. The carved rectangle is returned as `carved`, the remaining rectangle as `rest`

func (Rectangle) Fill

func (r Rectangle) Fill(renderer Renderer, color api.RGBA)

Fill fills the rectangle with the given color.

func (Rectangle) Move

func (r Rectangle) Move(dx, dy int32) Rectangle

Move moves the rectangle by the given delta

func (Rectangle) Position

func (r Rectangle) Position(width, height int32, horiz HAlign,
	vert VAlign) Rectangle

Position returns a rectangle with the given width and height, which is position in the current rectangle according to the given flags.

giving HStretch and VStretch will override the given width and height respectively, the other positioning flags will only set the position.

func (Rectangle) Scale

func (r Rectangle) Scale(factor float32) Rectangle

Scale scales the rectangle's width and height by the given factor, repositioning it so that the center stays the same.

func (Rectangle) Shrink

func (r Rectangle) Shrink(dw, dh int32) Rectangle

Shrink removes dw from the rectangles width and dh from its height, repositioning it so that the center stays the same.

func (Rectangle) Transformation

func (r Rectangle) Transformation() Transform

Transformation returns the transformation needed to transform a square with edge length of 1.0 centered around the origin to the subject rectangle.

func (Rectangle) Translation

func (r Rectangle) Translation() Transform

Translation returns the transformation needed to move an object centered on the origin to the center of the rectangle.

type Renderer

type Renderer interface {
	// OutputSize returns a rectangle that describes the dimensions in pixels
	// of the current rendering area. X and Y are always 0.
	//
	// This rectangle will be the whole OpenGL surface by default, but if a Canvas
	// is active, it returns the Canvas' size.
	OutputSize() Rectangle
	// Unit is the scaled smallest unit in pixels. It is defined as being
	// 1/144 of the screen's width or height, whichever is smaller.
	// Borders typically have the size of one Unit.
	//
	// Use this value as base for calculating sizes to make your renderer output
	// scale depending on the target display size. Not using lengths smaller than
	// Unit avoids making stuff too small for your users to see.
	//
	// Font sizes depend directly on this size.
	Unit() int32
	// FillRect fills a rectangle with the specified color.
	// The rectangle is a square with edge length of 1.0 centered around the
	// origin, transformed with the given transformation.
	//
	// For the high-level API, use Rectangle's Fill() instead.
	FillRect(t Transform, color api.RGBA)
	// DrawImage renders the given image if it is not empty on a square with
	// edge length of 1.0 centered around the origin, transformed with the given
	// transformation. alpha modifies the image's opacity.
	//
	// For the high-level API, use Image's Draw() instead.
	DrawImage(image Image, t Transform, alpha uint8)
	// RenderText renders the given text with the given font into an image with
	// transparent background.
	// Returns an empty image if it wasn't able to create the texture.
	RenderText(text string, font api.Font) Image
	// CreateCanvas creates a canvas to draw content into, and fills it with the
	// given background. The returned content rectangle is the canvas area minus
	// the borders.
	//
	// Borders are added in each given direction. Border width/height is added to
	// the given innerWidth / innerHeight values.
	//
	// The texture created by the canvas will have an alpha channel only if the
	// primary color has an alpha value other than 255, or if a mask is set and
	// the secondary color has an alpha value other than 255.
	CreateCanvas(innerWidth, innerHeight int32, bg api.Background,
		borders Directions) (canvas Canvas, content Rectangle)
	// LoadImageFile loads an image file from the specified URL.
	// if an error is returned, the returned image is empty.
	//
	// if scaleDownToOutput is true, the image is scaled down to the output
	// context's dimensions so that it completely fits into the display while
	// preserving aspect ratio.
	//
	// The image will always be scaled down to GL_MAX_TEXTURE_SIZE if its
	// dimensions exceed it.
	LoadImageFile(path *url.URL, scaleDownToOutput bool) (Image, error)
	// LoadImageMem loads an image from data in memory.
	// if an error is returned, the returned image is empty.
	//
	// scaleDownToOutput work like for LoadImageFile.
	LoadImageMem(data []byte, scaleDownToOutput bool) (Image, error)
	// FreeImage destroys the texture associated with the image (if one exists)
	// and sets i to be the empty image. Does nothing on empty images.
	FreeImage(i *Image)
}

Renderer describes an object providing functions for rendering objects.

type Transform

type Transform [6]float32

Transform encodes a 2D affine transformation matrix

func Identity

func Identity() Transform

Identity returns the identity transformation

func (Transform) Compose

func (t Transform) Compose(other Transform) Transform

Compose adds the given transformation to the current transformation

func (Transform) Invert

func (t Transform) Invert() Transform

Invert inverts the transformation

func (Transform) Rotate

func (t Transform) Rotate(angle float32) Transform

Rotate adds a rotation to the transformation. The given angle is in radian.

func (Transform) Scale

func (t Transform) Scale(x float32, y float32) Transform

Scale adds scaling to the transformation.

func (Transform) Translate

func (t Transform) Translate(x float32, y float32) Transform

Translate adds a translation to the transformation. A translation moves by (x,y).

type TransitionCurve

type TransitionCurve struct {
	// Duration sets the length of the transition
	Duration time.Duration
}

TransitionCurve provides various curves useful for smooth transitions. All its methods return a float between 0.0 and 1.0 calculated from the given elapsed time in relation to the total duration.

func (TransitionCurve) Cubic

func (tc TransitionCurve) Cubic(elapsed time.Duration) float32

Cubic implements a cubic transition curve.

func (TransitionCurve) Linear

func (tc TransitionCurve) Linear(elapsed time.Duration) float32

Linear implements a linear transition curve.

type VAlign

type VAlign int

VAlign defines vertical alignment

const (
	// Top is vertical alignment to the top
	Top VAlign = iota
	// Middle is vertical alignment in the middle
	Middle
	// Bottom is vertical alignment to the bottom
	Bottom
	// VStretch stretches height to the maximum
	VStretch
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL