gamekit

package module
v0.0.0-...-251eadb Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2016 License: MIT Imports: 6 Imported by: 6

README

go-gamekit

go-gamekit is a simple game utility library built on top of SDL2.

The design of go-gamekit follows a reactive approach. Events are handled via channels that are wrapped as subscribable data types:

mouse.Position.Subscribe()                  // Mouse position changes
button.Clicked.Subscribe()                  // UI button click changes (clicked/unclicked)
window.Size.Subscribe()                     // Window Resizing
windowManager.CurrentWindowID.Subscribe()   // Focused window changes

We can use Get to get the values synchronously:

windowID := windowManager.CurrentWindowID.Get()

In the example below, mouse.Position has a Subscribe method that has a channel. This channel emits an Int32Pair, where we've decided that L is X and R is Y. Once the message comes in, we move the MyObject to follow:

var o *MyObject

sub := mouse.Position.Subscribe()
defer sub.Close()

for {
	select {
		case coords := <-sub.C:
			o.Move(coords.L, coords.R)
		case <-ctx.Done():
			return
	}
}

This general structure has lots of power. We can define our game objects as a series of goroutines which listen on channels and update their internal state approprietely.

We can also make pieces of code very generic:

// Moveable is an interface for an entity which can be moved around the screen
type Moveable interface {

	// Move moves the entity to the given x and y position
	Move(x, y int32)
}

// Follow moves the given Moveable everytime the subscribed coordinates change
func Follow(o Moveable, coords *pair.RxInt32PairSubscriber ) {
	for pos := range coords.C {
		o.Move(pos.L, pos.R)
	}
}

In this code, we do not care about the concrete type of Moveable; nor do we care if coords represents our mouse X and Y positions, values coming from a game server for a sprites location, or anything else imaginable.

Features

The following features are done or planned:

  • Multi-Window Management
  • Reactive Data Types
  • TODO: Scriptable console (needs migrating to new codebase)
  • TODO: Scripting support via Javascript/otto
  • TODO: Multiple game loop implementations
  • TODO: High level graphics API (maybe)
  • TODO: Networking

See Also

Documentation

Overview

Package gamekit is a group of utilities for building games in golang

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init()

Init initializes the gamekit engine

Types

type CountHistogram

type CountHistogram struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

CountHistogram renders a time series of data points

func (*CountHistogram) Render

func (c *CountHistogram) Render(r *sdl.Renderer)

Render draws the histogram to the SDL renderer

func (*CountHistogram) Run

func (c *CountHistogram) Run(ctx context.Context, interval time.Duration, fn func() int)

Run runs the count histogram

type Mouse

type Mouse struct {
	Position         *pair.RxInt32Pair
	LeftButtonState  *rx.Bool
	RightButtonState *rx.Bool
}

Mouse is the representation of our mouse input on the screen

func NewMouse

func NewMouse() *Mouse

NewMouse creates a new object object

type Window

type Window struct {
	ID       uint32
	Size     *pair.RxInt32Pair
	Window   *sdl.Window
	Renderer *sdl.Renderer
	Mouse    *Mouse
	// contains filtered or unexported fields
}

Window represents a managed window

type WindowManager

type WindowManager struct {
	WindowCount     *rx.Uint32
	CurrentWindowID *rx.Uint32
	// contains filtered or unexported fields
}

The WindowManager is responsible for handling a group of windows with different event handlers

func NewWindowManager

func NewWindowManager() *WindowManager

NewWindowManager builds a new window manager

func (*WindowManager) Destroy

func (wm *WindowManager) Destroy(id uint32)

Destroy destroys the window

func (*WindowManager) DispatchEvents

func (wm *WindowManager) DispatchEvents()

DispatchEvents handles events and sends them to the required window event handlers.

func (*WindowManager) NewWindow

func (wm *WindowManager) NewWindow(title string, w int, h int, extraFlags int) (*Window, error)

NewWindow creates a new window, returning the window

func (*WindowManager) SetHandler

func (wm *WindowManager) SetHandler(id uint32, h func(e sdl.Event))

SetHandler sets the event handler for the given window id

Directories

Path Synopsis
_examples
Package gfx2 is the 2D graphics abstraction library
Package gfx2 is the 2D graphics abstraction library
Package loop provides game loop implementations
Package loop provides game loop implementations
Package pair defines pair/tuple types
Package pair defines pair/tuple types
Package ui provides some simple user interface objects
Package ui provides some simple user interface objects

Jump to

Keyboard shortcuts

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