matrigo

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

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

Go to latest
Published: Jul 23, 2018 License: MIT Imports: 10 Imported by: 1

README

MatriGo

Documentation

CircleCI

Coverage Status

Go Report Card

Codacy Badge

MatriGo is an abstraction layer library over go-sdl2 for matrix-based games

API Documentation: https://godoc.org/github.com/tommyblue/matrigo

Features

  • Draw a custom size matrix of images
  • Preload images (cache)
  • Receive mouse input from the user (left and right click) and send it to the client by callbacks

To do

  • Make simple shapes
  • Color shapes (also support gradients)
  • Write stuff
  • Use different fonts for text (now it only loads different ttf files)
  • Fullscreen window
  • Keyboard events
  • Draw the matrix by an offset (e.g. move it) into the window and manage the mouse click accordingly

Setup

Run ./script/setup, that's all. The file will install sdl2 dependencies for Mac or Debian-based distros.

Other o.s.

go-sdl2 package is the only dependency, check its README file to know how to install it and its dependencies (sdl2): https://github.com/veandco/go-sdl2

Usage

TODO

Examples

import "github.com/tommyblue/matrigo"

func InitMyUI() *UI {
    matrigoConf := &matrigo.Conf{
		TileSize: 40,
		Window: &matrigo.Window{
			Width:  800,
			Height: 600,
		},
		Debug: true,
		Title: "Minesweeper",
		Fonts: map[string]matrigo.FontConfig{
			"mono": {
				Path: getAbsolutePath("../assets/fonts/mono.ttf"),
				Size: 14,
			},
		},
		BackgroundColor: &[4]uint8{255, 255, 255, 255},
		BackgroundImage: getAbsolutePath("../assets/images/bg.jpg"),
		ImagesToCache:   getImagesToCache(),
		SyncFPS:         true,
    }
    err := matrigo.Init(matrigoConf)
	if err != nil {
		panic(err)
    }

    ui := &UI{}
	mapInputToFn(ui)

	return ui
}

var tileImages = map[Tile]string{
	Empty:     "empty",
	Mine:      "mine",
	Flag:      "flag",
}

func getImagesToCache() *map[string]string {
	ret := make(map[string]string)
	for _, v := range tileImages {
		ret[v] = getAbsolutePath(fmt.Sprintf("../assets/images/%s.png", v))
	}
	return &ret
}

type matrigoInputInterface struct {
	mouseLeftClickDownFn  func(x, y int32)
	mouseRightClickDownFn func(x, y int32)
	quitFn                func()
}

var input *matrigoInputInterface

func mapInputToFn(ui *UI) {
	if input == nil {
		input = &matrigoInputInterface{
			quitFn:                ui.stopRunning,
			mouseLeftClickDownFn:  ui.mouseLeftClickAt,
			mouseRightClickDownFn: ui.mouseRightClickAt,
		}
	}
}

func (i *matrigoInputInterface) MouseLeftClickDown(x, y int32) {
	i.mouseLeftClickDownFn(x, y)
}
func (i *matrigoInputInterface) MouseRightClickDown(x, y int32) {
	i.mouseRightClickDownFn(x, y)
}
func (i *matrigoInputInterface) Quit() {
	i.quitFn()
}

Changelog

  • 2018-06-29: super-mega-alpha

Documentation

Overview

Package matrigo provides an abstraction layer over go-sdl2.

It works for tile-based words, like famous games Sokoban, Minesweeper and Tetris.

The client sends a configuration object during the initialization and this package exposes few methods to draw tiles and manage input from the user.

For usage examples see the Readme file in the package repository: https://github.com/tommyblue/matrigo

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() error

Close everything. To be called by the client before quitting.

func Draw

func Draw(matrix *Matrix)

Draw what needs to be drawn down. It draws the background image and color, all the images and then syncs the frame rate to the value in the fpsTarget variable

func Init

func Init(c *Conf) error

Init the package. The client must call this function with a valid configuration at the beginning of the ui setup

func ManageInput

func ManageInput(inputInterface Input)

ManageInput listens for events from the user and calls the callbacks provided by the Input interface

Types

type Conf

type Conf struct {
	TileSize        int32                 // Size of the side of the tiles (tiles are squared)
	Title           string                // Title of the window
	Fonts           map[string]FontConfig // Fonts to use
	Debug           bool                  // Enable or disable debugging
	Window          *Window               // Window to draw
	BackgroundColor *[4]uint8             // Background color of the window
	BackgroundImage string                // Background image. Must be of the same size of the Window (optional)
	ImagesToCache   *map[string]string    // All the images to add to the cache, as key => abs path of the image
	SyncFPS         bool                  // Sync the frame rate to 60fps
}

Conf is the global configuration of the library

type FontConfig

type FontConfig struct {
	Path string // Absolute path to a ttf font file
	Size int    // Font size in points
}

FontConfig is used to configure the fonts to use. Each struct must contain the absolute path to a ttf font file and its size (in points)

type Input

type Input interface {
	// x and y passed to the mouse click functions are the position, in pixels, based on the Window
	// where (0, 0) is the top-left (NW) corner. The receiver should then convert the position to
	// the clicked tile
	MouseLeftClickDown(x, y int32)
	MouseRightClickDown(x, y int32)
	// TODO: keyboard event
	Quit()
}

Input is the interface that must be implemented by clients to receive input callbacks. The implementation must then be passed to ManageInput that will call the callbacks when the corresponding event will happen

type Matrix

type Matrix struct {
	Tiles []*Tile // Array of tiles
}

Matrix represents the whole tiles (i.e. images) to draw in the window

type Tile

type Tile struct {
	ImageID string // Absolute path to the image file
	PosX    int32  // X position (column) of the tile in the matrix
	PosY    int32  // Y position (row) of the tile in the matrix
	OffsetX int32  // Offset in the X direction in pixels
	OffsetY int32  // Offset in the Y direction in pixels
}

Tile is an image to be drawn down. Each Tile has an absolute path to the image file and its position (x and y) in the matrix

type Window

type Window struct {
	Width  int32 // Width of the window
	Height int32 // Height of the window
}

Window defines the size of the window to create (in pixels)

Jump to

Keyboard shortcuts

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