README

Oak

A pure Go game engine

GoDoc Go Report Card Build Status Code Coverage badge

Installation

go get -u github.com/oakmound/oak/...

On linux, for audio, see klangsynthese for audio installation requirements

Usage

This is an example of the most basic oak program:

oak.AddScene("firstScene",
    // Initialization function
    func(prevScene string, inData interface{}) {}, 
    // Loop to continue or stop current scene
    func()bool{return true}, 
    // Exit to transition to next scene
    func()(nextScene string, result *oak.SceneResult){return "firstScene", nil}) 
oak.Init("firstScene")

See the examples folder for longer demos.

Motivation

The initial version of oak was made to support Oakmound Studio's game, Agent Blue and was developed in parallel. Oak supports Windows with no dependencies and Linux with limited audio dependencies. We hope that users will be able to make great pure Go games with oak and welcome improvements.

Because Oak wants to have as few dependencies as possible, Oak does not use OpenGL or GLFW. We're open to adding support for these in the future for performance gains, but we always want an alternative that requires zero or near-zero dependencies. (We are very sad about the linux audio dependency and are considering writing an audio driver just to get rid of it.)

Features

  1. Window Rendering
    • Windows and key events through shiny
    • Logical frame rate distinct from Draw rate
  2. Image Management
    • render.Renderable interface
    • TileSheet Batch Loading
    • Manipulation
      • render.Modifiable interface
      • Built in Shaping, Coloring, Shading, ...
      • Some built ins via gift
      • extensible Modification syntax func(image.Image) *image.RGBA
      • Copying
    • Built in Renderable types
      • Sprite
      • Sheet Animation
      • Sequence, Compound, Composite
      • History-tracking Reverting
    • Primarily 2D
  3. Particle System
  4. Mouse Handling
    • Click Collision
    • MouseEnter / MouseExit reaction events
    • Drag Handling
  5. Audio Support
    • From klangsynthese
    • Batch Loading
    • Positional filters to pan and scale audio based on a listening position
  6. Collision
    • Collision R-Tree from rtreego
    • 2D Raycasting
    • Collision Spaces
      • Attachable to Objects
      • Auto React to collisions through events
      • OnHit bindings func(s1,s2 *collision.Space)
      • Start/Stop collision with targeted objects
  7. Physics System
    • Vectors
      • Attachable to Objects / Renderables
      • Momentum
      • Friction
      • Force / Pushing
  8. Event Handler, Bus
    • PubSub system
    • event.CID can Bind(fn,eventName) and selectively Trigger(eventName) events
    • GlobalBind and event.Trigger for entity-independent
  9. Timing utilities
    • Smoothed draw rate, frame rate tracking
    • FPS conversion to time.Duration
    • Manipulatable time.Ticker to readily change frame rate
  10. Shaping
    • Shapes from func(x float64) (y float64) equations
    • Shapes from func(x,y,w,h int) bool containment
    • Convert shapes into:
      • Containment checks
      • Outlines
      • 2D arrays
  11. Custom Console Commands
  12. Logging
    • Controlled by config files
    • Filterable by string, debug level

Package-specific Usage

... Pending! See examples or godoc!

Expand ▾ Collapse ▴

Documentation

Overview

Package oak is a game engine. It provides scene control, control over windows and what is drawn to them, propagates regular events to evaluate game logic, and so on.

Index

Constants

View Source
const (
	// DefaultSeed is a key int64 sent in to SeedRNG
	// used to indicate that the seed function should just
	// do the default operation for seeding, using the current
	// time.
	DefaultSeed int64 = iota
)

Variables

View Source
var (
	Background = image.Black
	// DrawTicker is an unused parallel to LogicTicker to set the draw framerate
	DrawTicker *timing.DynamicTicker
)
View Source
var (

	// ScreenWidth is the width of the screen
	ScreenWidth int
	// ScreenHeight is the height of the screen
	ScreenHeight int

	// FrameRate is the current logical frame rate.
	// Changing this won't directly effect frame rate, that
	// requires changing the LogicTicker, but it will take
	// effect next scene
	FrameRate int

	// DrawFrameRate is the unused equivalent to FrameRate
	DrawFrameRate int

	// CurrentScene is the scene currently running in oak
	CurrentScene string
)
View Source
var (
	// ColorPalette is the current color palette oak is set to conform to. Modification of this
	// value directly will not effect oak's palette, use SetPalette instead. If SetPallete is never called,
	// this is the zero value ([]Color of length 0).
	ColorPalette color.Palette
)
View Source
var (
	// DefShaker is the global default shaker, used when oak.Shake is called.
	DefShaker = ScreenShaker{false, floatgeom.Point2{1.0, 1.0}}
)
View Source
var (

	// LoadingR is a renderable that is displayed during loading screens.
	LoadingR render.Renderable
)
View Source
var (
	// LogicTicker is exposed so that games can manually change the speed
	// at which EnterFrame events are produced
	LogicTicker *timing.DynamicTicker
)
View Source
var (
	// UseAspectRatio determines whether new window changes will distort or
	// maintain the relative width to height ratio of the screen buffer.
	UseAspectRatio = false
)
View Source
var (
	// ViewPos represents the point in the world which the viewport is anchored at.
	ViewPos = image.Point{}
)

Functions

func AddCommand

func AddCommand(s string, fn func([]string))

AddCommand adds a console command to call fn when 'c <s> <args>' is input to the console

func AddScene

func AddScene(name string, start SceneStart, loop SceneUpdate, end SceneEnd) error

AddScene adds a scene with the given name and functions to the scene map

func BindKey

func BindKey(key string, binding string)

BindKey binds a name to be triggered when this key is triggered

func BindKeyBindings

func BindKeyBindings(r io.Reader) error

BindKeyBindings loads and binds KeyBindings at once. It maintains existing keybindings not a part of the input reader.

func BindKeys

func BindKeys(bindings KeyBindings)

BindKeys loops over and binds all pairs in the input KeyBindings

func ChangeWindow

func ChangeWindow(width, height int)

ChangeWindow sets the width and height of the game window. Although exported, calling it without a size event will probably not act as expected.

func ClearScreenFilter

func ClearScreenFilter()

ClearScreenFilter resets the draw function to no longer filter the screen before publishing it to the window.

func FramesElapsed

func FramesElapsed() int

FramesElapsed returns the number of logical frames that have been processed in the current scene. This value is also passed in to all EnterFrame bindings.

func GetKeyBind

func GetKeyBind(key string) string

GetKeyBind returns either whatever name has been bound to a key or the key if nothing has been bound to it. Todo: this should be a var function that starts out as "return key", and only becomes this function when a binding is made.

func GetScreen

func GetScreen() *image.RGBA

GetScreen returns the current screen as an rgba buffer

func Init

func Init(firstScene string)

Init initializes the oak engine. It spawns off an event loop of several goroutines and loops through scenes after initialization.

func IsDown

func IsDown(key string) (k bool)

IsDown returns whether a key is held down

func IsHeld

func IsHeld(key string) (k bool, d time.Duration)

IsHeld returns whether a key is held down, and for how long

func LoadConf

func LoadConf(filePath string) error

LoadConf loads a config file, that could exist inside oak's binary data storage (see fileutil), to SetupConfig

func LoadConfData

func LoadConfData(r io.Reader) error

LoadConfData takes in an io.Reader and decodes it to SetupConfig

func ScreenShot

func ScreenShot() *image.RGBA

ScreenShot takes a snap shot of the window's image content. ScreenShot is not safe to call while an existing ScreenShot call has yet to finish executing. This could change in the future.

func SeedRNG

func SeedRNG(inSeed int64)

SeedRNG seeds go's random number generator and logs the seed set to file.

func SetAspectRatio

func SetAspectRatio(xToY float64)

SetAspectRatio will enforce that the displayed window does not distort the input screen away from the given x:y ratio. The screen will not use these settings until a new size event is received from the OS.

func SetBinaryPayload

func SetBinaryPayload(payloadFn func(string) ([]byte, error), dirFn func(string) ([]string, error))

SetBinaryPayload just sets some public fields on packages that require access to binary functions as alternatives to os file functions. This is no longer necessary, as a single package uses these now.

func SetKeyBindings

func SetKeyBindings(r io.Reader) error

SetKeyBindings removes all existing keybindings and then binds all bindings within the input reader.

func SetLang

func SetLang(s string)

SetLang parses a string as a language

func SetPalette

func SetPalette(palette color.Palette)

SetPalette tells oak to conform the screen to the input color palette before drawing.

func SetScreen

func SetScreen(x, y int)

SetScreen sends a signal to the draw loop to set the viewport to be at x,y

func SetScreenFilter

func SetScreenFilter(screenFilter mod.Filter)

SetScreenFilter will filter the screen by the given modification function prior to publishing the screen's rgba to be displayed.

func SetViewportBounds

func SetViewportBounds(x1, y1, x2, y2 int)

SetViewportBounds sets the minimum and maximum position of the viewport, including screen dimensions

func ShakeScreen

func ShakeScreen(dur time.Duration)

ShakeScreen will Shake using the package global DefShaker

func TransitionFade

func TransitionFade(rate float32, frames int) func(*image.RGBA, int) bool

TransitionFade is a scene transition that fades to black at a given rate for a total of frames frames

func TransitionZoom

func TransitionZoom(xPerc, yPerc float64, frames int, zoomRate float64) func(*image.RGBA, int) bool

TransitionZoom transitions a scene by zooming in at a relative position of the screen at some defined rate. Reasonable values are < .01 for zoomRate.

func UnbindAllKeys

func UnbindAllKeys()

UnbindAllKeys clears the contents of the oak's keybindings.

func UnbindKey

func UnbindKey(key string)

UnbindKey removes the binding for the given key in oak's keybindings. Does nothing if the key is not already bound.

func ViewVector

func ViewVector() physics.Vector

ViewVector returns ViewPos as a Vector

Types

type Assets

type Assets struct {
	AssetPath string `json:"assetPath"`
	AudioPath string `json:"audioPath"`
	ImagePath string `json:"imagePath"`
	FontPath  string `json:"fontPath"`
}

Assets is a json type storing paths to different asset folders

type Config

type Config struct {
	Assets         Assets `json:"assets"`
	Debug          Debug  `json:"debug"`
	Screen         Screen `json:"screen"`
	Font           Font   `json:"font"`
	FrameRate      int    `json:"frameRate"`
	DrawFrameRate  int    `json:"drawFrameRate"`
	Language       string `json:"language"`
	Title          string `json:"title"`
	BatchLoad      bool   `json:"batchLoad"`
	GestureSupport bool   `json:"gestureSupport"`
	// DisableKeyhold is deprecated. Keyhold functionality
	// no longer has a significant performance impact and so can't
	// be disabled.
	DisableKeyhold bool `json:"disableKeyHold"`
}

Config stores initialization settings for oak.

var (
	// SetupConfig is the config struct read from at initialization time
	// when oak starts. When oak.Init() is called, the variables behind
	// SetupConfig are passed to their appropriate places in the engine, and
	// afterword the variable is unused.
	SetupConfig Config
)

type Debug

type Debug struct {
	Filter string `json:"filter"`
	Level  string `json:"level"`
}

Debug is a json type storing the starting debug filter and level

type Font

type Font struct {
	Hinting string  `json:"hinting"`
	Size    float64 `json:"size"`
	DPI     float64 `json:"dpi"`
	File    string  `json:"file"`
	Color   string  `json:"color"`
}

Font is a json type storing the default font settings

type KeyBindings

type KeyBindings map[string]string

KeyBindings map input keys to meaningful names, so code can be built around those meaningufl names and users can easily rebind which keys do what.

func LoadKeyBindings

func LoadKeyBindings(r io.Reader) (KeyBindings, error)

LoadKeyBindings converts a reader into a map of keys to meaningful names. It expects a simple .toml syntax, of key = "value" pairs per line. The resulting KeyBindings will have the keys and values reversed, so `MoveUp = "W"` will correspond to kb["W"] = "MoveUp"

type Language

type Language int

Language is hypothetically something games might care about in their text

const (
	ENGLISH Language = iota
	GERMAN
)

Lang enumerator

var (
	// Lang is the current langugae
	Lang Language
)

type Scene

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

A Scene is a set of functions defining what needs to happen when a scene starts, loops, and ends.

func GetScene

func GetScene(s string) *Scene

GetScene returns the scene struct with the given name

type SceneEnd

type SceneEnd func() (string, *SceneResult)

SceneEnd is a function returning the next scene and a SceneResult

type SceneResult

type SceneResult struct {
	NextSceneInput interface{}
	Transition     transitionFunction
}

A SceneResult is a set of options for what should be passed into the next scene and how the next scene should be transitioned to.

type SceneStart

type SceneStart func(prevScene string, data interface{})

SceneStart is a function taking in a previous scene and a payload of data from the previous scene's end

type SceneUpdate

type SceneUpdate func() bool

SceneUpdate is a function that returns whether or not the current scene should continue to loop.

type Screen

type Screen struct {
	Height int `json:"height"`
	Width  int `json:"width"`
	Scale  int `json:"scale"`
}

Screen is a json type storing the starting screen width and height

type ScreenShaker

type ScreenShaker struct {
	Random    bool
	Magnitude floatgeom.Point2
}

A ScreenShaker knows how to shake a screen by a (or up to a) given magnitude. If Random is true, the Shaker will shake up to the (negative or positive) magnitude of each the X and Y axes. Otherwise, it will oscillate between negative magnitude and positive magnitude.

func (*ScreenShaker) Shake

func (ss *ScreenShaker) Shake(dur time.Duration)

Shake shakes the screen based on this shaker's attributes. See ScreenShaker.

Directories

Path Synopsis
alg Package alg stores useful algorithms and math functions
alg/floatgeom Package floatgeom stores primitives for floating point geometry
alg/intgeom Package intgeom stores primitives for integer geometry
audio Package audio provides audio types, font types for filtering audio reactively, and channels to allow constant audio play signals to be restricted to play at variable frequencies.
collision Package collision provides collision tree and space structures, raycasting utilities and hit detection functions on spaces.
dlog Package dlog provides logging functions with caller file and line information, logging levels and level and text filters.
entities Package entities stores useful object and entity types, such as positions and renderables, collision spaces and renderables, and delta / speed vectors built into the above types.
event Package event propagates events through entities with given caller IDs.
examples/bezier
examples/flappyBird
examples/pong
fileutil Package fileutil provides functionality to subvert os and ioutil calls when needed for particular operating systems (js) or runtimes (asset data packaged into a binary)
key
mouse Package mouse handles the propagation of mouse events though clickable regions.
oakerr Package oakerr stores errors returned throughout oak.
physics Package physics provides vector types and operations to perform math and simple physics on those types.
render Package render provides several types of renderable entities which are used throughout the code base In addition to entities the package also provides utilities to load images from files and load images from parts of files as well as draw them.
render/mod
render/particle Package particle provides options for generating renderable particle sources.
shape Package shape provides types to satisfy the Shape interface, which allows for containment and outline checks on two dimensional shapes.
timing Package timing provides utilities for time