carol

package module
v0.0.0-...-7a819e8 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2019 License: BSD-2-Clause Imports: 4 Imported by: 0

README

Merged into cozely, so this repository is archived.

Carol

GoDoc Go Report Card

Carol will be a minimalist framework for making games in Go, using 2D pixel art or 3D polygonal art (aka low-poly).

Work in Progress

Not yet in a usable state: the framework is very incomplete, and the API is subject to frequent changes.

Dependencies

The only dependancies are SDL 2 and OpenGL 4.5.

License

The code is under a simplified BSD license (see LICENSE file). When a sub-package is derived from another source, the directory contain the appropriate LICENSE file.

Credits

The Perlin and Simplex noise functions are adapted from "Simplex Noise Demystified" by Stefan Gustavson (code in the public domain).

The pixel font was originally based on "Pixel Operator Mono" by Jayvee Enayas, but has been so modified that it's now a completely different font. It is still licensed under the SIL OFL.

Some implementations of the single-precision math functions are derived from the Go source code (BSD-style license).

Documentation

Overview

Package carol provides a framework for making pixel-art games in Go.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(context string, err error) error

Error returns nil if err is nil, or a wrapped error otherwise.

func FrameTime

func FrameTime() float64

FrameTime returns the duration of the last frame

func FrameTimeAverage

func FrameTimeAverage() (t float64, overruns int)

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 HasFocus

func HasFocus() bool

HasFocus returns true if the game windows has focus.

func HasMouseFocus

func HasMouseFocus() bool

HasMouseFocus returns true if the mouse is currently inside the game window.

func Log

func Log(format string, v ...interface{})

Log logs a formated message.

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

func Run(loop GameLoop) error

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 Stop

func Stop()

Stop request the game loop to stop.

func TimeStep

func TimeStep() float64

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

type Handlers = internal.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

func (*State) Update

func (s *State) Update()

Update runs s (i.e. the current state), and replaces it with the result of that call. It's safe to call Update on a nil target.

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.

Jump to

Keyboard shortcuts

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