Documentation ¶
Overview ¶
Package carol provides a framework for making pixel-art games in Go.
Index ¶
- func Error(context string, err error) error
- func FrameTime() float64
- func FrameTimeAverage() (t float64, overruns int)
- func HasFocus() bool
- func HasMouseFocus() bool
- func Log(format string, v ...interface{})
- func Now() float64
- func Path() string
- func Run(loop GameLoop) error
- func SetTimeStep(t float64)
- func ShowError(e error)
- func Stop()
- func TimeStep() float64
- func WindowSize() (width, height int32)
- type GameLoop
- type Handlers
- type State
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FrameTimeAverage ¶
FrameTimeAverage returns the average durations of frames; it is updated 4 times per second. It also returns the number of overruns (i.e. frame time longer than the threshold) during the last measurment interval.
func HasMouseFocus ¶
func HasMouseFocus() bool
HasMouseFocus returns true if the mouse is currently inside the game window.
func Now ¶
func Now() float64
Now returns the current time (elapsed since program start).
If called during the update callback, it corresponds to the current time step. If called during the draw callback, it corresponds to the current frame. And if called during an event callback, it corresponds to the event time stamp.
It shouldn't be used outside of these three contexts.
func Path ¶
func Path() string
Path returns the (slash-separated) path of the executable, with a trailing slash.
func Run ¶
Run starts the game loop.
The update callback is called with a fixed time step, while event handlers and the draw callback are called once for each frame displayed. The loop runs until Stop() is called.
Important: must be called from main.main, or at least from a function that is known to run on the main OS thread.
func SetTimeStep ¶
func SetTimeStep(t float64)
func ShowError ¶
func ShowError(e error)
ShowError shows an error to the user. In debug mode, it only prints to the standard error output, otherwise it also brings a dialog box.
func WindowSize ¶
func WindowSize() (width, height int32)
WindowSize returns the size of the window in (screen) pixels.
Types ¶
type GameLoop ¶
type GameLoop interface { // Loop setup Setup() error // The loop Update() error Draw(delta float64, lerp float64) error // Window events WindowShown() WindowHidden() WindowResized(width, height int32) WindowMinimized() WindowMaximized() WindowRestored() WindowMouseEnter() WindowMouseLeave() WindowFocusGained() WindowFocusLost() WindowQuit() // Keyboard events KeyDown(l key.Label, p key.Position) KeyUp(l key.Label, p key.Position) // Mouse events MouseMotion(deltaX, deltaY int32, posX, posY int32) MouseButtonDown(b mouse.Button, clicks int) MouseButtonUp(b mouse.Button, clicks int) MouseWheel(deltaX, deltaY int32) // Pixel events ScreenResized(width, height int16, pixel int32) }
GameLoop methods are called to setup the game, and during the main loop to process events, Update the game state and Draw it.
type Handlers ¶
Handlers implements default handlers for all events.
It's an empty struct intended to be embedded in the user-defined GameLoop:
type loop struct { carol.Handlers }
This way it's possible to implement the GameLoop interface without writing a method for each event.
type State ¶
type State func() State
State is used to implement a lightweight state machine. Each state is represented by a closure, that, when run, returns the next state (or nil to stop the machine).
Example ¶
package main import ( "fmt" "github.com/drakmaniso/carol" ) func main() { counter := 0 // Define the State Machine var s1, s2, s3 carol.State s1 = func() carol.State { fmt.Println("State 1") return s2 } s2 = func() carol.State { fmt.Println("State 2") return s3 } s3 = func() carol.State { fmt.Println("State 3") if counter > 6 { return nil } return s2 } // Run the State Machine m := s1 for m != nil { counter++ m.Update() } }
Output: State 1 State 2 State 3 State 2 State 3 State 2 State 3
Example (NoAllocations) ¶
package main import ( "fmt" "github.com/drakmaniso/carol" ) var counter int func state1() carol.State { fmt.Println("State 1") return state2 } func state2() carol.State { fmt.Println("State 2") return state3 } func state3() carol.State { fmt.Println("State 3") if counter > 6 { return nil } return state2 } func main() { m := carol.State(state1) for m != nil { counter++ m.Update() } }
Output: State 1 State 2 State 3 State 2 State 3 State 2 State 3
Directories ¶
Path | Synopsis |
---|---|
_examples
|
|
Package colour provides types and functions to manipulate colors.
|
Package colour provides types and functions to manipulate colors. |
formats
|
|
obj
Package obj implements a simple parser for Wavefront ".obj" file format.
|
Package obj implements a simple parser for Wavefront ".obj" file format. |
Package key provides keyboard support
|
Package key provides keyboard support |
Package mouse provides mouse support.
|
Package mouse provides mouse support. |
Package noise provides several noise algorithms.
|
Package noise provides several noise algorithms. |
Package pixel provides a virtual screen designed to display pixel art.
|
Package pixel provides a virtual screen designed to display pixel art. |
Package plane implements mathematical objects and operations in the euclidean plane.
|
Package plane implements mathematical objects and operations in the euclidean plane. |
Package poly will provide a 3D renderer to display polygonal art (aka low-poly).
|
Package poly will provide a 3D renderer to display polygonal art (aka low-poly). |
Package space implements mathematical objets and operations in three-dimensional euclidean space.
|
Package space implements mathematical objets and operations in three-dimensional euclidean space. |
x
|
|
atlas
Package atlas provides a packing algorithm for creating atlases of 2D textures.
|
Package atlas provides a packing algorithm for creating atlases of 2D textures. |
gl
Package gl provides simple abstractions over a modern subset of OpenGL.
|
Package gl provides simple abstractions over a modern subset of OpenGL. |
math32
Package math provides efficient float32 math functions.
|
Package math provides efficient float32 math functions. |