tie

package module
v0.0.0-...-ec9c00f Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2018 License: MIT Imports: 13 Imported by: 0

README

franeklubi/tie

GoDoc

logo

Package franeklubi/tie provides a Processing-like API for simple and fun drawing, game making, data and algorithm visualization, and generally - art :)



Installation

To install this package:

go get github.com/franeklubi/tie

You'll also need to install dependencies. (see Dependencies)

Dependencies

This package depends on two other packages:

  • gl v2.1
  • glfw v3.2

To install these, use:

go get github.com/go-gl/gl/v2.1/gl

then

go get github.com/go-gl/glfw/v3.2/glfw

and You should be all set! :)

Features

Main features:

  • it's beginner friendly,
  • has Processing-like API - no need to learn a new framework from the ground up,
  • provides easy image manipulation,
  • it's fun :D

Example

( For more examples visit franeklubi/tie-examples! )

package main

// import the package
import (
    "github.com/franeklubi/tie"
)

func main() {
    // initialize engine in main
    tie.Init(500, 500, "window_name", false)
    //   width, height, window_name, is_resizable

    // pass all the functions you want used by the engine
    tie.PassFunctions(
        preload,
        setup,
        draw,
    )

    // launch the engine
    tie.Launch()
}

var (
    img tie.Image
)

// called only once, before setup, nothing can be drawn here
func preload() {
    img = tie.LoadImage("/path/to/image.png")
}

// called only once, before draw, you can draw here
func setup() {
    tie.Background(255, 255, 255, 255)
}

// called once every frame
func draw() {
    // drawing loaded image
    tie.Fill(255, 255, 255, 255)
    tie.PastePixels(img, 0, 0, tie.Width, tie.Height)

    // drawing ellipse
    tie.Fill(0, 255, 0, 255)
    tie.Ellipse(tie.Width/2, tie.Height/2, 200, 200)

    // drawing rectangle
    tie.Fill(0, 255, 255, 255)
    tie.Rect(tie.Width/2-50, tie.Height/2-50, 100, 100)
}

Documentation

Overview

Package tie provides a Processing-like API for simple and fun drawing, game making, data and algorithm visualization, generally - art :).

To start writing a new sketch, You need to initialize the engine first:

tie.Init(width, height, title, is_window_resizable)

Then You need to pass the functions You want to act as the ones listed below, in the right order, but only preload, setup, and draw are necessary:

preload         // called only once, before setup, nothing can be drawn here
setup           // called only once, before draw, you can draw here
draw            // called once every frame
keyPressed      // called every time a key gets pressed
keyReleased     // called every time a key gets released
mouseMoved      // called every time the mouse is moved
mousePressed    // called every time a mouse key gets pressed
mouseReleased   // called every time a mouse key gets released
scrolledY       // called every time the mouse scroll Y-axis gets scrolled
scrolledX       // called every time the mouse scroll X-axis gets scrolled

To do that, call PassFunctions with the functions You want to use as arguments:

tie.PassFunctions(
    preload,
    setup,
    draw,
)

The only thing that's left is launching the engine with:

tie.Launch()

The whole sketch should look something like this:

package main

// import the package
import (
    "github.com/franeklubi/tie"
)

func main() {
    // initialize engine in main
    tie.Init(500, 500, "window_name", false)
    //   width, height, window_name, is_resizable

    // pass all the functions you want used by the engine
    tie.PassFunctions(
        preload,
        setup,
        draw,
    )

    // launch the engine
    tie.Launch()
}

var (
    img tie.Image
)

func preload() {
    img = tie.LoadImage("/path/to/image.png")
}

func setup() {
    tie.Background(255, 255, 255, 255)
}

func draw() {
    tie.Fill(0, 255, 255, 255)
    tie.Rect(tie.Width/2-50, tie.Height/2-50, 100, 100)
}

For more examples visit https://github.com/franeklubi/tie-examples/

Index

Constants

View Source
const (
	// keyboard special keys
	ENTER     string = "ā"
	ESC       string = "Ā"
	BACKSPACE string = "ă"
	CTRL      string = "ŕ"
	L_CTRL    string = "ŕ"
	R_CTRL    string = "ř"
	SHIFT     string = "Ŕ"
	L_SHIFT   string = "Ŕ"
	R_SHIFT   string = "Ř"
	ALT       string = "Ŗ"
	ALT_GR    string = "Ś"
	UP        string = "ĉ"
	DOWN      string = "Ĉ"
	LEFT      string = "ć"
	RIGHT     string = "Ć"

	// function keys
	F1  string = "Ģ"
	F2  string = "ģ"
	F3  string = "Ĥ"
	F4  string = "ĥ"
	F5  string = "Ħ"
	F6  string = "ħ"
	F7  string = "Ĩ"
	F8  string = "ĩ"
	F9  string = "Ī"
	F10 string = "ī"
	F11 string = "Ĭ"
	F12 string = "ĭ"

	// mouse keys
	MOUSE_LEFT   glfw.MouseButton = glfw.MouseButtonLeft
	MOUSE_RIGHT  glfw.MouseButton = glfw.MouseButtonRight
	MOUSE_MIDDLE glfw.MouseButton = glfw.MouseButtonMiddle

	// math consts
	PI float64 = math.Pi
	E  float64 = math.E

	// OpenGL consts
	POINTS         uint32 = gl.POINTS
	LINES          uint32 = gl.LINES
	LINE_STRIP     uint32 = gl.LINE_STRIP
	LINE_LOOP      uint32 = gl.LINE_LOOP
	POLYGON        uint32 = gl.POLYGON
	TRIANGLES      uint32 = gl.TRIANGLES
	TRIANGLE_STRIP uint32 = gl.TRIANGLE_STRIP
	TRIANGLE_FAN   uint32 = gl.TRIANGLE_FAN
	QUADS          uint32 = gl.QUADS
	QUAD_STRIP     uint32 = gl.QUAD_STRIP
)

Consts helpful when interfacing with tie

Variables

View Source
var (
	// Width and Height of a created window
	Width  float64
	Height float64
)
View Source
var (
	// when the respective events get triggered, these variables will change correspondingly
	Key_pressed bool    = false
	Key         string  = ""
	MouseX      float64 = 0
	MouseY      float64 = 0
	Mouse_key   glfw.MouseButton
	ScrollValue float64 = 0
)
View Source
var (
	// total frame count
	Frames float64 = 0
)

Functions

func Abs

func Abs(n float64) float64

Abs links to math.Abs

func Acos

func Acos(n float64) float64

Acos links to math.Acos

func Asin

func Asin(n float64) float64

Asin links to math.Asin

func Atan

func Atan(n float64) float64

Atan links to math.Atan

func Atan2

func Atan2(n1, n2 float64) float64

Atan2 links to math.Atan2

func Background

func Background(r, g, b, a byte)

Background fills the whole frame with one colour

func BeginShape

func BeginShape(mode uint32)

BeginShape (together with EndShape) provides a toolset for drawing more complex shapes

It should be called with one of the OpenGL consts (see Constants section)

To draw a shape - one of Vertex functions has to be called between the BeginShape and EndShape functions

func CaToGl

func CaToGl(x, y float64) (float64, float64)

CaToGl maps the tie coordinates ((0,0) at upper left) to OpenGL standard coordinates ((0,0) at the center)

func Ceil

func Ceil(n float64) float64

Ceil links to math.Ceil

func Cos

func Cos(n float64) float64

Cos links to math.Cos

func Cube

func Cube(size float64)

Cube draws a cube

func DegToRad

func DegToRad(degrees float64) float64

DegToRad converts degrees to radians

func DepthRefreshOff

func DepthRefreshOff()

DepthRefreshOff turns automatic depth refreshing off

Every time a shape is drawn the depth buffer is cleared to prevent Z-fighting

func DepthRefreshOn

func DepthRefreshOn()

DepthRefreshOn turns automatic depth refreshing on

func Ellipse

func Ellipse(x, y, w, h float64)

Ellipse draws an ellipse

func EndShape

func EndShape()

EndShape should be called after BeginShape

It draws a described shape to the buffer

func Fill

func Fill(r, g, b, a byte)

Fill sets colour used to fill shapes based on RGBA values

func Floor

func Floor(n float64) float64

Floor links to math.Floor

func GlToCa

func GlToCa(x, y float64) (float64, float64)

GlToCa maps OpenGL standard coordinates ((0,0) at the center) to the tie coordinates ((0,0) at upper left)

func HideMouse

func HideMouse()

HideMouse hides mouse

func HsvToRgb

func HsvToRgb(hue, saturation, value float64, alpha byte) (byte, byte, byte, byte)

Desired usage:

Fill(HsvToRgb(h, s, v, a))
Stroke(HsvToRgb(h, s, v, a))
Background(HsvToRgb(h, s, v, a))

RgbToHsv returns RGBA values based on HSV and Alpha values.

(hue=0-360, saturation=0-1, value=0-1, alpha=0-255)

func Init

func Init(width, height float64, title string, resizable bool)

Init initializes engine and sets window's properties

func LOG

func LOG(itm ...interface{})

LOG links to log.Println

func Launch

func Launch()

Launch launches main loop

func Limit

func Limit(val, min, max float64) float64

Limit constricts a value between a given range

func LinInt

func LinInt(val, target, rate float64) float64

LinInt provides linear interpolation (lerp)

func Line

func Line(x1, y1, x2, y2 float64)

Line draws a line

func Loop

func Loop()

Loop turns on automatic redrawing

func Mod

func Mod(n1, n2 float64) float64

Mod links to math.Mod

func MoveBackFor2D

func MoveBackFor2D()

MoveBackFor2D translates the matrix back from the point of origin, so that the entire 2d canvas can be seen

func MoveForwardFor3D

func MoveForwardFor3D()

MoveForwardFor3D translates the matrix forward, so that the camera is at the point of origin

func NoFill

func NoFill()

NoFill turns off fill

func NoLoop

func NoLoop()

NoLoop turns off automatic redrawing

func NoStroke

func NoStroke()

NoStroke turns off stroke

func PassFunctions

func PassFunctions(p ...func())

PassFunctions serves as a middleman between a user and the engine. It receives a number of funcs that are going to be called at certain engine's events

func Point

func Point(x, y float64)

Point draws a point

func Pop

func Pop()

Pop should be called after Push

It restores Your matrix to state saved by Push earlier

func Print

func Print(itm ...interface{})

Print links to fmt.Print

func Println

func Println(itm ...interface{})

Println links to fmt.Println

func Push

func Push()

Push stores current state of transformations

To restore it, see: Pop

func RadToDeg

func RadToDeg(radians float64) float64

RadToDeg converts radians to degrees

func Random

func Random(n float64) float64

Random returns a random float64 between 0 and n

func ReMap

func ReMap(val, start1, stop1, start2, stop2 float64) float64

ReMap converts a number that exists in one range to another

func Rect

func Rect(x, y, w, h float64)

Rect draws a rectangle

func Redraw

func Redraw()

Redraw executes the draw loop once

func RgbToGl

func RgbToGl(r, g, b, a byte) (float64, float64, float64, float64)

RgbToGl maps 0-255 range bytes to 0.0-1.0 range floats

func Rotate

func Rotate(degrees float64)

Rotate links to RotateZ

func RotateX

func RotateX(degrees float64)

RotateX rotates the matrix along X axis

func RotateY

func RotateY(degrees float64)

RotateY rotates the matrix along Y axis

func RotateZ

func RotateZ(degrees float64)

RotateZ rotates the matrix along Z axis

func Scale

func Scale(x, y, z float64)

Scale scales the matrix

func SetCursorPos

func SetCursorPos(x, y float64)

SetCursorPos sets cursor position

func SetPos

func SetPos(x, y int)

SetPos sets window position

func SetSize

func SetSize(x, y int)

SetSize sets window size

func ShowMouse

func ShowMouse()

ShowMouse shows mouse

func Sin

func Sin(n float64) float64

Sin links to math.Sin

func Sphere

func Sphere(size float64, fidelity uint32)

Sphere draws a sphere

func Sqrt

func Sqrt(n float64) float64

Sqrt links to math.Sqrt

func Stroke

func Stroke(r, g, b, a byte)

Stroke sets colour used to draw shapes' outline based on RGBA values (applies to Line and Point too)

func StrokeWidth

func StrokeWidth(width float64)

StrokeWidth sets width of the stroke

func Tan

func Tan(n float64) float64

Tan links to math.Tan

func Text

func Text(txt string, size int, center_align bool)

Text draws a string to the screen

func ToggleFullscreen

func ToggleFullscreen()

ToggleFullscreen toggles between fullscreen and windowed modes

func Translate

func Translate(x, y, z float64)

Translate translates the matrix

func TranslateGl

func TranslateGl(x, y, z float64)

TranslateGl translates the matrix using the OpenGL standard coordinates ((0,0) at the center)

func Vertex

func Vertex(x, y float64)

Vertex should be called between BeginShape and EndShape

Vertex lets you specify a vertex of the shape to be drawn using standard coordinates ((0,0) at upper left)

func Vertex3D

func Vertex3D(x, y, z float64)

Vertex3D should be called between BeginShape and EndShape

Vertex3D lets you specify a 3D vertex of the shape to be drawn using standard coordinates ((0,0) at upper left)

func VertexGl

func VertexGl(x, y, z float64)

VertexGl should be called between BeginShape and EndShape

VertexGl lets you specify a 3D vertex of the shape to be drawn using OpenGL standard coordinates ((0,0) at the center)

Types

type Color

type Color struct {
	R, G, B, A byte
}

Color is used to hold the 0-255 range RGBA values

func (Color) ToArray

func (c Color) ToArray() []byte

ToArray returns a byte array containing Color's values

type ColorGl

type ColorGl struct {
	R, G, B, A float64
}

ColorGl is used to hold the 0-1 range RGBA values

func (ColorGl) ToArray

func (c ColorGl) ToArray() []float64

ToArray returns a float64 array containing ColorGl's values

type Image

type Image struct {
	Pixels []byte // Pixels of a loaded image
	W, H   int    // Width and Height of an Image
}

Image is used to hold the loaded image's pixel and size data

Pixels are stored in a down to up && left to right arrangement

To manipulate them see (*Image) Replace

func CopyPixels

func CopyPixels() Image

CopyPixels returns an Image containing current frame buffer

func GenImage

func GenImage(c Color, w, h int) Image

GenImage generates an Image of specified dimensions filled with a given Color

func LoadImage

func LoadImage(path string) Image

LoadImage loads an image (.png or .jpg) from a given path

func (*Image) GetPixels

func (img *Image) GetPixels(x, y, w, h int) Image

GetPixels returns a section of received Image

func (*Image) PastePixels

func (img *Image) PastePixels(x, y, w, h float64)

PastePixels draws an Image to the screen

func (*Image) PixelAt

func (img *Image) PixelAt(x, y int) Color

PixelAt returns Color of a pixel at given coordinates

func (*Image) PushPixel

func (img *Image) PushPixel(c Color)

PushPixel appends a Color pixel to an Image

func (*Image) Replace

func (img *Image) Replace(x, y int, pixel Color)

Replace replaces a pixel in an Image with a Color

func (*Image) Save

func (img *Image) Save(path string)

Save saves an image to a file in a png format

Jump to

Keyboard shortcuts

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