bento

package module
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2022 License: MIT Imports: 12 Imported by: 1

README

Bento 🍱

A game framework for Ebitengine

DISCLAIMER: Bento is still in the early stages of development! The API is subject to breaking changes.

Rationale

TL;DR My original intent was to make a game, but I ended up creating a game framework ¯\_(ツ)_/¯

Ebitengine is great for making cross platform games because it abstracts away the low-level stuff. However, I think that a game framework would be beneficial for simplifying some aspects of game development with it.

To this end, Bento aims to provide a modular framework of utilities.

Requirements

The minimum Go version is 1.18 for versions of Bento past 0.5.0.

Getting Started

Installation

go get github.com/ongyx/bento should be sufficient to add it as a go.mod dependency.

Documentation

The API documentation is a good place to start.

General tutorials/how-tos can be found in the docs directory. You may want to take a look at the examples too.

Build Tags

Bento can be configured by specifying build tags when compiling or testing.

Some features require CGo to be enabled, which is the default for native builds. If you are cross compiling, some setup is needed.

Feature Description CGo needed?
discretegpu Prefer using the discrete GPU on Windows Y
pprof Enable pprof server on localhost:6060 N
ecs.debug Enable debug logging to stdout for ECS N
Cross Compiling

To cross compile pure Go code, it's generally as easy as setting GOOS and GOARCH to your target platform, such as windows/amd64. However, features using CGo require some setup to work properly.

First, find and install the appropriate cross compiler for your target platform. For Linux -> Windows, this is usually MinGW. (Consult your package manager for this.)

Next, export the GOOS/GOARCH from earlier, CGO_ENABLED=1, and CC to the cross compiler:

$ # this is for windows/amd64, change the variables accordingly for your platform
$ export GOOS=windows
$ export GOARCH=amd64
$ export CGO_ENABLED=1
$ export CC=x86_64-w64-mingw32-gcc
$ go build

Credits

Hajime Hoshi for creating Ebiten.

License

Bento is licensed under the MIT License.

Documentation

Overview

Package bento is a game framework for Ebitengine.

Index

Constants

View Source
const (
	// Right moves an image to the right of a point.
	Right Align = 1 << iota
	// HCenter moves an image to the horizontal center of a point.
	HCenter
	// Left moves an image to the left of a point.
	Left
	// Top moves an image above a point.
	Top
	// VCenter moves an image to the vertical center of a point.
	VCenter
	// Bottom moves an image below a point.
	Bottom

	// Default is the default alignment of an image (to the right and below).
	Default = BottomRight

	TopLeft   = Top | Left
	TopCenter = Top | HCenter
	TopRight  = Top | Right

	CenterLeft  = VCenter | Left
	Center      = VCenter | HCenter
	CenterRight = VCenter | Right

	BottomLeft   = Bottom | Left
	BottomCenter = Bottom | Center
	BottomRight  = Bottom | Right
)
View Source
const (
	// Deg90 is a 90 degree turn in radians.
	Deg90 = math.Pi / 2

	// Deg180 is a 180 degree turn in radians.
	Deg180 = math.Pi

	// Deg270 is a 270 degree turn in radians.
	Deg270 = 1.5 * math.Pi
)

Variables

View Source
var Clock = clock{}

Clock is a monotonically increasing tick counter.

Functions

func Bound

func Bound(point, size image.Point) image.Rectangle

Bound calculates a bound, given its top-left point and its size.

func Coordinate added in v0.3.0

func Coordinate(point image.Point) (x, y float64)

Coordinate returns the given point as a float64 pair.

func DPIScale

func DPIScale(res int) float64

DPIScale scales the given resolution by the device's scale factor. This allows high-DPI rendering.

func Keypress

func Keypress(keys []ebiten.Key) bool

Keypress checks if at least one of the keys are pressed.

func Pad

func Pad(bound image.Rectangle, pad image.Point) image.Rectangle

Pad adds padding to the bound by a fixed amount.

func Point added in v0.3.0

func Point(m *ebiten.GeoM) image.Point

Point returns the point of a geometry matrix.

func Poll added in v0.4.0

func Poll[T any](ch <-chan T) (value T, ok bool)

Poll attempts to read a value from the channel without blocking. If the channel is empty, ok is false.

func Radian

func Radian(degree float64) float64

Radian converts an angle in degrees to radians.

func SecondToTick

func SecondToTick(seconds float64) int

SecondToTick converts seconds to ticks.

func TickToSecond

func TickToSecond(ticks int) float64

TickToSecond converts ticks to seconds.

func Unpad

func Unpad(bound image.Rectangle, pad image.Point) image.Rectangle

Unpad removes padding from a bound by a fixed amount.

Types

type Align

type Align int

Align specifies the alignment to render an image at a point (x, y). Align must have at most one horizontal (AlignRight, AlignHCenter, AlignLeft) and vertical (AlignTop, AlignVCenter, AlignBottom) flag.

func (Align) Align

func (a Align) Align(point, size image.Point) image.Point

Align adjusts the point so that it will be the top-left point of an image given its size. The adjusted point can then be passed to ebiten.Image.DrawImage so the image will be in the correct position.

func (Align) Has

func (a Align) Has(flag Align) bool

Has checks if the alignment flag is set.

func (Align) Point

func (a Align) Point(img image.Image) image.Point

Point calculates a point in an image.

type Atlas added in v0.5.4

type Atlas struct {
	// contains filtered or unexported fields
}

Atlas is a tile-based texture atlas for sprites.

func NewAtlas added in v0.5.4

func NewAtlas(img *ebiten.Image, tile image.Point) *Atlas

NewAtlas creates a new atlas with an image and tile size. The tile size for each axis must satisfy (1 <= size <= image_size).

func (*Atlas) Composite added in v0.5.4

func (a *Atlas) Composite(img *ebiten.Image, layers ...Layer) *ebiten.Image

Composite draws several layers to an image in order.

func (*Atlas) Draw added in v0.5.4

func (a *Atlas) Draw(img *ebiten.Image, layer Layer) *ebiten.Image

Draw draws a layer to an image. If img is nil, a new image large enough to display all the tiles is created.

func (*Atlas) Size added in v0.5.4

func (a *Atlas) Size() image.Point

Size returns the size of the atlas in tiles. The size should be consider a closed interval, i.e (size.X * size.Y) is the maximum texture index.

func (*Atlas) Texture added in v0.5.4

func (a *Atlas) Texture(index int) *ebiten.Image

Texture returns the tile at index as a subimage of the atlas. Index is the tile column multiplied by the tile row. If index is negative, an empty image the same size as a tile is returned instead.

func (*Atlas) Tile added in v0.5.4

func (a *Atlas) Tile() image.Point

Tile returns the size of a tile.

type Delta

type Delta struct {
	// contains filtered or unexported fields
}

Delta is a change in value per time unit.

func NewDelta

func NewDelta(
	algo DeltaAlgorithm,
	delta image.Point,
	period float64,
) *Delta

NewDelta creates an delta with the total delta, and the period over which to increase the current delta.

func (*Delta) Delta

func (d *Delta) Delta() (x, y float64)

Delta returns the current delta.

func (*Delta) DeltaPt

func (d *Delta) DeltaPt() image.Point

func (*Delta) Done

func (d *Delta) Done() bool

Done checks if the current delta is equal to the total delta.

func (*Delta) Update

func (d *Delta) Update()

Update updates the delta.

type DeltaAlgorithm

type DeltaAlgorithm int

DeltaAlgorithm specifies the algorithm to use when generating deltas.

const (
	// Linear specifies a delta is constant.
	Linear DeltaAlgorithm = iota
	// Exponential specifies a delta in exponential (e^x) space.
	Exponential
)

type Event added in v0.5.0

type Event[T any] struct {
	// contains filtered or unexported fields
}

Event notifies one or more callbacks when a event value is sent over the channel.

ch := make(chan string)

e := NewEvent(ch)

e.Notify(func(s string) {
	fmt.Println(s)
})

e.Listen()

ch <- "Hello World!"

func NewEvent added in v0.5.0

func NewEvent[T any](ch <-chan T) *Event[T]

NewEvent creates a new event backed by the read-only channel ch.

func (*Event[T]) Notify added in v0.5.0

func (e *Event[T]) Notify(fn func(T))

Notify registers the callback to the event.

type Font

type Font struct {
	Face font.Face
}

Font is a wrapper around a fontface, used to render text to images.

func (*Font) Draw

func (f *Font) Draw(
	str string,
	clr color.Color,
	img *ebiten.Image,
	point image.Point,
)

Draw renders the text on an image at the point as-is (i.e without any alignment). NOTE: point is the bottom-left point of the text.

func (*Font) Load

func (f *Font) Load(src []byte, opts *opentype.FaceOptions) error

Load loads an OpenType fontface from a source.

func (*Font) Write

func (f *Font) Write(
	str string,
	clr color.Color,
	img *ebiten.Image,
	point image.Point,
	align Align,
) image.Rectangle

Write renders the text on an image at the point with alignment and returns its bounds. point is the top-left point of the text.

func (*Font) WriteCenter

func (f *Font) WriteCenter(str string, clr color.Color, img *ebiten.Image) image.Rectangle

WriteCenter renders the text in the center of an image.

type Layer added in v0.5.3

type Layer [][]int

Layer is a 2D slice of tile indices.

type Scroll

type Scroll struct {
	Font  *Font
	Point image.Point
	Color color.Color
	// contains filtered or unexported fields
}

Scroll allows several pieces of text to be scrolled across an image. point is the bottom-left point of the scroll.

func NewScroll

func NewScroll(font *Font, tx string) *Scroll

NewScroll creates a new scroll with the initial text.

func (*Scroll) Done

func (s *Scroll) Done() bool

Done checks if the scrolling has finished.

func (*Scroll) Draw

func (s *Scroll) Draw(img *ebiten.Image)

Draw renders the scroll on a new image.

func (*Scroll) SetSpeed added in v0.3.0

func (s *Scroll) SetSpeed(n float64)

SetSpeed changes the speed of scrolling text, where n is the number of seconds to wait between scrolling each character.

func (*Scroll) SetText

func (s *Scroll) SetText(tx string)

SetText changes the text currently scrolling.

func (*Scroll) Size

func (s *Scroll) Size() image.Point

Size returns the total size of the scroll.

func (*Scroll) Skip

func (s *Scroll) Skip()

Skip causes the next render to render the whole text instead of waiting for scrolling.

func (*Scroll) Text

func (s *Scroll) Text() string

Text returns the current text in the scroll.

func (*Scroll) Update

func (s *Scroll) Update()

Update updates the state of the scroll.

type Timer added in v0.3.0

type Timer struct {
	// contains filtered or unexported fields
}

Timer is a tick-based scheduler for operations. One tick is equivalent to a single frame, where 1 second is 60 frames.

func NewTimer added in v0.3.0

func NewTimer(n float64, once bool) *Timer

NewTimer creates a new timer that triggers every n seconds. If once is true, the timer will only trigger once.

func (*Timer) Delta added in v0.3.0

func (t *Timer) Delta() uint64

Delta returns the number of ticks between each trigger.

func (*Timer) Done added in v0.3.0

func (t *Timer) Done() bool

Done checks if the timer has triggered.

type Vec

type Vec struct {
	Path vector.Path
}

Vec is a wrapper around ebiten's vector path for drawing operations.

func (*Vec) Arc

func (vec *Vec) Arc(center image.Point, radius int, from, to float32)

Arc draws a circular arc with a center and radius. from and to are angles in radians.

func (*Vec) Circle

func (vec *Vec) Circle(center image.Point, radius int)

Circle draws a circle with a center and radius.

func (*Vec) Draw

func (vec *Vec) Draw(
	clr color.Color,
	img *ebiten.Image,
	o *ebiten.DrawTrianglesOptions,
)

Draw renders the vector's path with color to an image.

func (*Vec) DrawShader

func (vec *Vec) DrawShader(
	clr color.Color,
	img *ebiten.Image,
	shader *ebiten.Shader,
	o *ebiten.DrawTrianglesShaderOptions,
)

DrawShader renders the vector's path with a shader to an image.

func (*Vec) Line

func (vec *Vec) Line(to image.Point)

Line draws a line from the current position to another position.

func (*Vec) Move

func (vec *Vec) Move(to image.Point)

Move moves the vec's currnet position to a new position. This doesn't draw anything.

func (*Vec) Rect

func (vec *Vec) Rect(bounds image.Rectangle)

Rect draws a rectangle with bounds.

Directories

Path Synopsis
ecs
examples

Jump to

Keyboard shortcuts

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