goga

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

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

Go to latest
Published: Aug 18, 2016 License: MIT Imports: 18 Imported by: 0

README

go-game (package "goga")

Game engine written in Go using OpenGL and GLFW. Mostly for 2D rendering, but also capable of rendering 3D, providing everything to get you started.

Install

go-game requires OpenGL and GLFW. The following three steps install everything you need:

go get github.com/go-gl/gl/v3.2-core/gl
go get github.com/go-gl/glfw/v3.2/glfw
go get github.com/DeKugelschieber/go-game

You also need a cgo compiler (typically gcc) and GL/GLFW development libraries and headers. You can find further instructions on the GitHub pages below (see dependencies).

Usage

Examples can be found within the demo folder. For full reference visit: https://godoc.org/github.com/DeKugelschieber/go-game

Dependencies

To use a different GL version, you need to replace the GL imports in package goga.

Contribute

To contribute, please create pull requests. The code must be formatted by gofmt and fit into the architecture.

License

MIT

Documentation

Index

Constants

View Source
const (
	// constants for default 2D shader
	Default_shader_2D_vertex_attrib   = "vertex"
	Default_shader_2D_texcoord_attrib = "texCoord"
	Default_shader_2D_ortho           = "o"
	Default_shader_2D_model           = "m"
	Default_shader_2D_tex             = "tex"

	// constants for default 3D shader
	Default_shader_3D_vertex_attrib   = "vertex"
	Default_shader_3D_texcoord_attrib = "texCoord"
	Default_shader_3D_pv              = "pv"
	Default_shader_3D_model           = "m"
	Default_shader_3D_tex             = "tex"

	// constants for default text shader
	Default_shader_text_vertex_attrib   = "vertex"
	Default_shader_text_texcoord_attrib = "texCoord"
	Default_shader_text_ortho           = "o"
	Default_shader_text_model           = "m"
	Default_shader_text_tex             = "tex"
	Default_shader_text_color           = "color"
)
View Source
const NullTerminator = "\x00"

Variables

View Source
var (

	// Default resources
	DefaultCamera     *Camera
	Default2DShader   *Shader
	Default3DShader   *Shader
	DefaultTextShader *Shader
)

Functions

func AddKeyboardListener

func AddKeyboardListener(listener KeyboardListener)

Adds a new keyboard listener.

func AddLoader

func AddLoader(loader ResLoader) bool

Adds a loader. If a loader with the same file extension exists already, false will be returned.

func AddMouseListener

func AddMouseListener(listener MouseListener)

Adds a new mouse listener.

func AddScene

func AddScene(scene Scene) bool

Adds a scene to game. Returns false if the scene exists already. The first scene added will be set active.

func AddSystem

func AddSystem(system System) bool

Adds a system to the game. Returns false if the system exists already.

func CheckGLError

func CheckGLError()

Checks for GL errors and prints to log if one occured.

func ClearColorBuffer

func ClearColorBuffer(do bool)

Adds color buffer to list of buffers to be cleared. If parameter is false, it will be removed.

func ClearDepthBuffer

func ClearDepthBuffer(do bool)

Adds depth buffer to list of buffers to be cleared. If parameter is false, it will be removed.

func CreateCubeMesh

func CreateCubeMesh() (*VBO, *VBO, *VBO)

Creates three VBOs for a 3D cube mesh. Texture coordinates won't map properly. This function is supposed to be used for 3D testing.

func CreateRectMesh

func CreateRectMesh(flip bool) (*VBO, *VBO, *VBO)

Creates three VBOs for a 2D rectangle.

func DistanceVec2

func DistanceVec2(a, b Vec2) float64

Returns the distance between two 2D vectors.

func DistanceVec3

func DistanceVec3(a, b Vec3) float64

Returns the distance between two 3D vectors.

func DistanceVec4

func DistanceVec4(a, b Vec4) float64

Returns the distance between two 4D vectors.

func Drop

func Drop(objects []Dropable)

Drops given GL objects. Objects must implement the Dropable interface.

func EnableAlphaBlending

func EnableAlphaBlending(enable bool)

Enables/Disables alpha blending by source alpha channel. BLEND = SRC_ALPHA | ONE_MINUS_SRC_ALPHA

func EnableDepthTest

func EnableDepthTest(enable bool)

Enables/Disables depth test.

func GetHeight

func GetHeight() int

Returns height of viewport.

func GetWidth

func GetWidth() int

Returns width of viewport.

func LoadResFromFolder

func LoadResFromFolder(path string) error

Loads all files from given folder path. If a loader is missing or fails to load the resource, an error will be returned. All resources will be kept until an error occures.

func NewFBOWithTex2D

func NewFBOWithTex2D(width, height, filter int32) (*FBO, *Tex)

Creates a new FBO with given target and 2D texture. Used to render to texture.

func RemoveActor

func RemoveActor(actor *Actor) bool

Removes an actor from all systems. This maybe not as performant as directly removing it from the right system. Returns true if it could be removed from at least one system, else false.

func RemoveActorById

func RemoveActorById(id ActorId) bool

Removes an actor from all systems by ID. This maybe not as performant as directly removing it from the right system. Returns true if it could be removed from at least one system, else false.

func RemoveAllKeyboardListener

func RemoveAllKeyboardListener()

Removes all registered keyboard listeners.

func RemoveAllLoaders

func RemoveAllLoaders()

Removes all loaders.

func RemoveAllMouseListener

func RemoveAllMouseListener()

Removes all registered mouse listeners.

func RemoveAllRes

func RemoveAllRes()

Removes all resources.

func RemoveAllScenes

func RemoveAllScenes()

Removes all scenes.

func RemoveAllSystems

func RemoveAllSystems()

Removes all systems.

func RemoveKeyboardListener

func RemoveKeyboardListener(listener KeyboardListener)

Removes given keyboard listener if found.

func RemoveLoader

func RemoveLoader(loader ResLoader) bool

Removes a loader. Returns false if loader could not be found.

func RemoveLoaderByExt

func RemoveLoaderByExt(ext string) bool

Removes a loader by file extension. Returns false if loader could not be found.

func RemoveMouseListener

func RemoveMouseListener(listener MouseListener)

Removes given mouse listener if found.

func RemoveResByName

func RemoveResByName(name string) bool

Removes a resource by name. Returns false if resource could not be found.

func RemoveResByPath

func RemoveResByPath(path string) bool

Removes a resource by path. Returns false if resource could not be found.

func RemoveScene

func RemoveScene(scene Scene) bool

Removes a given scene. Returns false if it could not be found.

func RemoveSystem

func RemoveSystem(system System) bool

Removes the given system. Returns false if it could not be found.

func Run

func Run(game Game, options *RunOptions)

Creates a new window with given options and starts the game. The game struct must implement the Game interface. If options is nil, the default options will be used. This function will panic on error.

func SetClearColor

func SetClearColor(r, g, b, a float64)

Sets GL clear color.

func SetViewport

func SetViewport(x, y, width, height int32)

Sets GL viewport and updates default resources and systems.

func Stop

func Stop()

Stops the game and closes the window.

func SwitchScene

func SwitchScene(scene Scene)

Switches to given scene. This will pause the currently active scene.

func SwitchSceneByName

func SwitchSceneByName(name string) bool

Switches to given existing scene by name. Returns false if the scene does not exist.

Types

type Actor

type Actor struct {
	// contains filtered or unexported fields
}

A basic actor, having a unique ID. Use NewActor() to create new actors.

func NewActor

func NewActor() *Actor

Creates a new basic actor with unique ID.

func (*Actor) GetId

func (a *Actor) GetId() ActorId

Returns the ID of actor.

type ActorId

type ActorId uint64

An actor ID is a unique integer, which can be used to reference an actor.

type AnimatedSprite

type AnimatedSprite struct {
	*Actor
	*Pos2D
	*Tex
	*KeyframeSet
	*KeyframeAnimation
}

An animated sprite is a sprite with keyframe animation information. It will be updated and rendered by the KeyframeRenderer.

func NewAnimatedSprite

func NewAnimatedSprite(tex *Tex, set *KeyframeSet, width, height int) *AnimatedSprite

Creates a new animated sprite.

type Camera

type Camera struct {
	Viewport                  Vec4
	Position, LookAt, Up      Vec3
	Fov, Ratio, Znear, Zfar   float64
	Projection, Ortho3D, View Mat4
	Ortho                     Mat3
}

func NewCamera

func NewCamera(x, y, width, height int) *Camera

Creates a new 2D/3D camera. Pass the viewport as arguments.

func (*Camera) CalcOrtho

func (c *Camera) CalcOrtho() *Mat3

Calculates orthogonal projection matrix and returns it.

func (*Camera) CalcOrtho3D

func (c *Camera) CalcOrtho3D() *Mat4

Calculates 3D orthogonal projection matrix and returns it.

func (*Camera) CalcProjection

func (c *Camera) CalcProjection() *Mat4

Calculates projection matrix and returns it.

func (*Camera) CalcRatio

func (c *Camera) CalcRatio()

Calculates viewport ratio (width/height).

func (*Camera) CalcView

func (c *Camera) CalcView() *Mat4

Calculates view matrix and returns it.

func (*Camera) SetViewport

func (c *Camera) SetViewport(x, y, width, height int)

Updates viewport.

type Cullable

type Cullable struct {
	*Actor
	*Pos2D
}

type Culling2D

type Culling2D struct {
	// contains filtered or unexported fields
}

func GetCulling2DSystem

func GetCulling2DSystem() *Culling2D

func NewCulling2D

func NewCulling2D(x, y, width, height int) *Culling2D

Creates a new sprite culling system. To update the viewport, call SetViewport().

func (*Culling2D) Add

func (c *Culling2D) Add(actor *Actor, pos *Pos2D) bool

Adds actor with Pos2D to the system.

func (*Culling2D) Cleanup

func (c *Culling2D) Cleanup()

func (*Culling2D) GetName

func (c *Culling2D) GetName() string

func (*Culling2D) Len

func (c *Culling2D) Len() int

Returns number of cullable objects.

func (*Culling2D) Remove

func (c *Culling2D) Remove(actor *Actor) bool

Removes actor with Pos2D from system.

func (*Culling2D) RemoveAll

func (c *Culling2D) RemoveAll()

Removes all cullable objects.

func (*Culling2D) RemoveById

func (c *Culling2D) RemoveById(id ActorId) bool

Removes actor with Pos2D from system by ID.

func (*Culling2D) SetViewport

func (c *Culling2D) SetViewport(x, y, width, height int)

Sets the culling outer bounds. Actors outside of this box won't be rendered.

func (*Culling2D) Update

func (c *Culling2D) Update(delta float64)

Updates visibility of all contained sprites.

type Dropable

type Dropable interface {
	Drop()
}

The dropable interface is used to clean up GL objects. Use the Drop() function to drop a range of objects.

type FBO

type FBO struct {
	// contains filtered or unexported fields
}

Frame Buffer Object.

func NewFBO

func NewFBO(target uint32) *FBO

Creates a new FBO with given target.

func (*FBO) Bind

func (f *FBO) Bind()

Binds the FBO for usage (rendered on).

func (*FBO) ClearAttachments

func (f *FBO) ClearAttachments()

Removes all attached textures from FBO.

func (*FBO) Complete

func (f *FBO) Complete() bool

Returns true if FBO is complete, else false.

func (*FBO) DrawBuffer

func (f *FBO) DrawBuffer(mode uint32)

Sets draw buffer.

func (*FBO) DrawBuffers

func (f *FBO) DrawBuffers(mode uint32)

func (*FBO) Drop

func (f *FBO) Drop()

Drops the FBO.

func (*FBO) GetId

func (f *FBO) GetId() uint32

Returns the GL ID.

func (*FBO) GetStatus

func (f *FBO) GetStatus() uint32

Returns the status of the FBO.

func (*FBO) GetTarget

func (f *FBO) GetTarget() uint32

Returns the target.

func (*FBO) ReadBuffer

func (f *FBO) ReadBuffer(mode uint32)

Sets read buffer.

func (*FBO) Texture

func (f *FBO) Texture(attachment, texId uint32, level int32)

Attaches a texture.

func (*FBO) Texture1D

func (f *FBO) Texture1D(attachment, texId uint32, level int32)

Attaches a 1D texture.

func (*FBO) Texture2D

func (f *FBO) Texture2D(attachment, texId uint32, level int32)

Attaches a 2D texture.

func (*FBO) Texture3D

func (f *FBO) Texture3D(attachment, texId uint32, level, layer int32)

Attaches a 3D texture.

func (*FBO) Unbind

func (f *FBO) Unbind()

Unbinds.

type Font

type Font struct {
	Tex *Tex

	CharPadding      Vec2
	Space, Tab, Line float64
	// contains filtered or unexported fields
}

Font represents a texture mapped font. It can be loaded from JSON together with a texture.

func NewFont

func NewFont(tex *Tex, tileSize float64) *Font

Creates a new font for given texture. The tile size specifies the size of one character tile on texture. Characters must be added afterwards.

func (*Font) FromJson

func (f *Font) FromJson(path string, cut bool) error

Loads characters from JSON file. Format:

[

    {
        "char": "a",
        "x": 0,
        "y": 0,
		   "offset": 0
    },
    ...

]

Where x and y start in the upper left corner of the texture, both of type int. Offset is optional and can be used to move a character up or down (relative to others). If cut is set to true, the characters will be true typed.

type Game

type Game interface {
	Setup()
	Update(float64)
}

Main game object. Setup will be called before the main loop and after GL context has been created. Update will be called each frame. This can be used to switch scenes or end game on win state. For game logic, System should be used.

type KeyboardListener

type KeyboardListener interface {
	OnKeyEvent(glfw.Key, int, glfw.Action, glfw.ModifierKey)
}

Interface for keyboard input events. Implement and register to receive keyboard input.

type Keyframe

type Keyframe struct {
	Min, Max Vec2
	// contains filtered or unexported fields
}

A single keyframe within a keyframe set.

func NewKeyframe

func NewKeyframe(min, max Vec2) *Keyframe

Creates a new single keyframe with texture VBO.

type KeyframeAnimation

type KeyframeAnimation struct {
	Start, End    int
	Loop          bool
	Speed         float64
	Current       int
	Interpolation float64
}

Keyframe animation component. It has a start and an end frame, a play speed and option to loop.

func NewKeyframeAnimation

func NewKeyframeAnimation(start, end int, loop bool, speed float64) *KeyframeAnimation

Creates a new keyframe animation with given start, end and loop.

type KeyframeRenderer

type KeyframeRenderer struct {
	Pos2D

	Shader *Shader
	Camera *Camera
	// contains filtered or unexported fields
}

The keyframe renderer renders animated sprites. It has a 2D position component, to move all sprites at once.

func GetKeyframeRenderer

func GetKeyframeRenderer() *KeyframeRenderer

func NewKeyframeRenderer

func NewKeyframeRenderer(shader *Shader, camera *Camera) *KeyframeRenderer

Creates a new keyframe renderer using given shader and camera. If shader and/or camera are nil, the default one will be used.

func (*KeyframeRenderer) Add

func (s *KeyframeRenderer) Add(actor *Actor, pos *Pos2D, tex *Tex, set *KeyframeSet, animation *KeyframeAnimation) bool

Adds animated sprite to the renderer.

func (*KeyframeRenderer) Cleanup

func (s *KeyframeRenderer) Cleanup()

Frees recources created by keyframe renderer. This is called automatically when system gets removed.

func (*KeyframeRenderer) GetName

func (s *KeyframeRenderer) GetName() string

func (*KeyframeRenderer) Len

func (s *KeyframeRenderer) Len() int

Returns number of sprites.

func (*KeyframeRenderer) Remove

func (s *KeyframeRenderer) Remove(actor *Actor) bool

Removes animated sprite from renderer.

func (*KeyframeRenderer) RemoveAll

func (s *KeyframeRenderer) RemoveAll()

Removes all animated sprites.

func (*KeyframeRenderer) RemoveById

func (s *KeyframeRenderer) RemoveById(id ActorId) bool

Removes sprite from renderer by ID.

func (*KeyframeRenderer) Update

func (s *KeyframeRenderer) Update(delta float64)

Updates animation state and renders sprites.

type KeyframeSet

type KeyframeSet struct {
	Keyframes []Keyframe
}

A set of keyframes making up an animation.

func NewKeyframeSet

func NewKeyframeSet() *KeyframeSet

Creates a new empty keyframe set with given size.

func (*KeyframeSet) Add

func (s *KeyframeSet) Add(frame *Keyframe) int

Adds a new keyframe to set and returns new length.

type Mat3

type Mat3 struct {
	Values [9]float64
}

3x3 column major matrix.

func MultMat3

func MultMat3(a, b *Mat3) *Mat3

Multiplies both matrices and returns a new Mat3.

func NewMat3

func NewMat3(m00, m10, m20, m01, m11, m21, m02, m12, m22 float64) *Mat3

Creates a new 3x3 matrix with initial values.

func (*Mat3) Clear

func (m *Mat3) Clear()

Sets the matrix to zeros.

func (*Mat3) Copy

func (m *Mat3) Copy() *Mat3

Creates a copy of actual matrix.

func (*Mat3) Determinate

func (m *Mat3) Determinate() float64

Returns the determinate of actual matrix.

func (*Mat3) Identity

func (m *Mat3) Identity()

Sets the matrix to identity matrix.

func (*Mat3) Inverse

func (m *Mat3) Inverse()

Sets the inverse of actual matrix.

func (*Mat3) Mult

func (m *Mat3) Mult(mat *Mat3)

Multiplies actual matrix with given matrix and saves result.

func (*Mat3) MultVec

func (m *Mat3) MultVec(v Vec3) Vec3

Multiplies given vector with actual matrix and returns result.

func (*Mat3) Ortho

func (m *Mat3) Ortho(viewport Vec4)

Sets actual matrix to orthogonal projection with given viewport.

func (*Mat3) Rotate

func (m *Mat3) Rotate(angle float64)

Rotates and saves actual matrix by given vector.

func (*Mat3) Scale

func (m *Mat3) Scale(v Vec2)

Scales and saves actual matrix by given vector.

func (*Mat3) Translate

func (m *Mat3) Translate(v Vec2)

Translates and saves actual matrix by given vector.

func (*Mat3) Transpose

func (m *Mat3) Transpose()

Calculates and saves the transpose of actual matrix.

type Mat4

type Mat4 struct {
	Values [16]float64
}

4x4 column major matrix.

func MultMat4

func MultMat4(a, b *Mat4) *Mat4

Multiplies both matrices and returns a new Mat4.

func NewMat4

func NewMat4(m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32, m03, m13, m23, m33 float64) *Mat4

Creates a new 4x4 matrix with initial values.

func (*Mat4) Clear

func (m *Mat4) Clear()

Sets the matrix to zeros.

func (*Mat4) Copy

func (m *Mat4) Copy() *Mat4

Creates a copy of actual matrix.

func (*Mat4) Determinate

func (m *Mat4) Determinate() float64

Returns the determinate of actual matrix.

func (*Mat4) Identity

func (m *Mat4) Identity()

Sets the matrix to identity matrix.

func (*Mat4) Inverse

func (m *Mat4) Inverse()

Sets the inverse of actual matrix.

func (*Mat4) LookAt

func (m *Mat4) LookAt(pos, lookAt, up Vec3)

Sets actual matrix to project to specified locations with given upper axis.

func (*Mat4) Mult

func (m *Mat4) Mult(mat *Mat4)

Multiplies actual matrix with given matrix and saves result.

func (*Mat4) MultVec

func (m *Mat4) MultVec(v Vec3) Vec3

Multiplies given vector with actual matrix and returns result.

func (*Mat4) Ortho

func (m *Mat4) Ortho(viewport Vec4, znear, zfar float64)

Sets actual matrix to orthogonal projection with given viewport.

func (*Mat4) Perspective

func (m *Mat4) Perspective(fov, ratio, znear, zfar float64)

Sets actual matrix to perspective projection with given viewport.

func (*Mat4) Rotate

func (m *Mat4) Rotate(angle float64, axis Vec3)

Rotates and saves actual matrix by given vector.

func (*Mat4) Scale

func (m *Mat4) Scale(v Vec3)

Scales and saves actual matrix by given vector.

func (*Mat4) Translate

func (m *Mat4) Translate(v Vec3)

Translates and saves actual matrix by given vector.

func (*Mat4) Transpose

func (m *Mat4) Transpose()

Calculates and saves the transpose of actual matrix.

type Mesh

type Mesh struct {
	Index, Vertex, TexCoord *VBO
	Vao                     *VAO
}

Component representing a 3D mesh.

func NewMesh

func NewMesh(index, vertex, texcoord *VBO) *Mesh

Creates a new mesh with given GL buffers. The VAO must be prepared by ModelRenderer.

func (*Mesh) Drop

func (m *Mesh) Drop()

Drops the VBOs and VAO contained in mesh. This must not be done, if mesh was filled from outer source (like a ply file).

type Model

type Model struct {
	*Actor
	*Pos3D
	*Tex
	*Mesh
}

Model is an actor having a 3D position, a texture and a 3D mesh.

func NewModel

func NewModel(mesh *Mesh, tex *Tex) *Model

Creates a new model with given mesh and texture.

type ModelRenderer

type ModelRenderer struct {
	Pos3D

	Shader *Shader
	Camera *Camera
	// contains filtered or unexported fields
}

The model renderer is a system rendering models. It has a 3D position component, to move all models at once.

func GetModelRenderer

func GetModelRenderer() *ModelRenderer

func NewModelRenderer

func NewModelRenderer(shader *Shader, camera *Camera, ortho bool) *ModelRenderer

Creates a new model renderer using given shader and camera. If shader and/or camera are nil, the default one will be used. Orth can be set to true, to use orthogonal projection.

func (*ModelRenderer) Add

func (s *ModelRenderer) Add(actor *Actor, pos *Pos3D, tex *Tex, mesh *Mesh) bool

Adds model to the renderer. Perpare it first!

func (*ModelRenderer) Cleanup

func (s *ModelRenderer) Cleanup()

func (*ModelRenderer) GetName

func (s *ModelRenderer) GetName() string

func (*ModelRenderer) Len

func (s *ModelRenderer) Len() int

Returns number of sprites.

func (*ModelRenderer) Prepare

func (s *ModelRenderer) Prepare(model *Model)

Prepares a model to be rendered by setting up its VAO.

func (*ModelRenderer) Remove

func (s *ModelRenderer) Remove(actor *Actor) bool

Removes model from renderer.

func (*ModelRenderer) RemoveAll

func (s *ModelRenderer) RemoveAll()

Removes all sprites.

func (*ModelRenderer) RemoveById

func (s *ModelRenderer) RemoveById(id ActorId) bool

Removes model from renderer by ID.

func (*ModelRenderer) Update

func (s *ModelRenderer) Update(delta float64)

Render models.

type MouseListener

type MouseListener interface {
	OnMouseButton(glfw.MouseButton, glfw.Action, glfw.ModifierKey)
	OnMouseMove(float64, float64)
	OnMouseScroll(float64, float64)
}

Interface for mouse input events. Implement and register to receive mouse input.

type Ply

type Ply struct {
	IndexBuffer, VertexBuffer, TexCoordBuffer, NormalBuffer *VBO
	// contains filtered or unexported fields
}

Standford ply file resource.

func GetPly

func GetPly(name string) (*Ply, error)

Finds and returns a Ply resource. If not found or when the resource is of wrong type, an error will be returned.

func (*Ply) Drop

func (p *Ply) Drop()

Drops contained GL buffers.

func (*Ply) GetExt

func (p *Ply) GetExt() string

Returns the file extension of this resource.

func (*Ply) GetName

func (p *Ply) GetName() string

Returns the name of this resource.

func (*Ply) GetPath

func (p *Ply) GetPath() string

Returns the path of this resource.

func (*Ply) SetExt

func (p *Ply) SetExt(ext string)

Sets the file extension of this resource.

func (*Ply) SetName

func (p *Ply) SetName(name string)

Sets the name of this resource.

func (*Ply) SetPath

func (p *Ply) SetPath(path string)

Sets the path of this resource.

type PlyLoader

type PlyLoader struct {
	VboUsage uint32
}

Loads ply files and creates VBOs within the Ply resource. The indices must be present as triangles. Expected type is float32. If it fails to parse, it will panic.

func (*PlyLoader) Ext

func (p *PlyLoader) Ext() string

func (*PlyLoader) Load

func (p *PlyLoader) Load(file string) (Res, error)

type PngLoader

type PngLoader struct {
	Filter   int32
	KeepData bool
}

Loads textures from png files. If keepData is set to true, pixel data will be stored inside the texture (additionally to VRAM).

func (*PngLoader) Ext

func (p *PngLoader) Ext() string

func (*PngLoader) Load

func (p *PngLoader) Load(file string) (Res, error)

type Pos2D

type Pos2D struct {
	Pos, Size, Scale, RotPoint Vec2
	Rot                        float64
	Visible                    bool
	M                          Mat3
}

Position component for 2D objects.

func NewPos2D

func NewPos2D() *Pos2D

Creates a default initialized Pos2D.

func (*Pos2D) CalcModel

func (p *Pos2D) CalcModel() *Mat3

Calculates model matrix for 2D positioning.

func (*Pos2D) GetCenter

func (p *Pos2D) GetCenter() Vec2

Returns the center of object. Assumes y = 0 is bottom left corner, if not you have to subtract height of object.

func (*Pos2D) PointInRect

func (p *Pos2D) PointInRect(point Vec2) bool

Returns true when given point is within rectangle of this object.

type Pos3D

type Pos3D struct {
	Pos, Size, Scale, RotPoint, Rot Vec3
	Visible                         bool
	M                               Mat4
}

Position component for 3D objects

func NewPos3D

func NewPos3D() *Pos3D

Creates a default initialized Pos3D.

func (*Pos3D) CalcModel

func (p *Pos3D) CalcModel() *Mat4

Calculates model matrix for 3D positioning.

func (*Pos3D) GetCenter

func (p *Pos3D) GetCenter() Vec3

Returns the center of object. Assumes y = 0 is bottom left corner, if not you have to subtract height of object.

type Res

type Res interface {
	GetName() string
	SetName(string)
	GetPath() string
	SetPath(string)
	GetExt() string
	SetExt(string)
}

A generic resource. Must be cast to appropriate type. The name is the file name and must be unique.

func GetResByName

func GetResByName(name string) Res

Returns a resource by name or nil, if not found.

func GetResByPath

func GetResByPath(path string) Res

Returns a resource by path or nil, if not found.

func LoadRes

func LoadRes(path string) (Res, error)

Loads a resource by file path. If no loader is present for given file, an error will be returned. If the loader fails to load the resource, an error will be returned. If the resource name exists already, an error AND the resource will be returned. This is allows to cleanup on failure.

type ResLoader

type ResLoader interface {
	Load(string) (Res, error)
	Ext() string
}

Resource loader interface. The loader accepts files by file extension. and loads them if accepted.

func GetLoaderByExt

func GetLoaderByExt(ext string) ResLoader

Returns a loader by file extension. If not found, nil will be returned.

type ResizeCallback

type ResizeCallback func(width, height int)

If set in RunOptions, the function will be called on window resize.

type RunOptions

type RunOptions struct {
	Title               string
	Width               uint32
	Height              uint32
	ClearColor          Vec4
	Resizable           bool
	SetViewportOnResize bool
	ResizeCallbackFunc  ResizeCallback
	ExitOnClose         bool
	RefreshRate         int
	Fullscreen          bool
	MonitorId           uint // index
}

Run options allow to set some parameters on startup.

type Scene

type Scene interface {
	Pause()
	Resume()
	Cleanup()
	Resize(int, int)
	GetName() string
}

A scene used to switch between game states. The Cleanup() method is called when a scene is removed or the program is stopped. It can be used to cleanup open resources (like GL objects). On switch, Pause() and Resume() are called. The name returned by GetName() must be unique. A scene must only be registered once.

func GetActiveScene

func GetActiveScene() Scene

Returns the currently active scene. Can be nil if no scene was set.

func GetSceneByName

func GetSceneByName(name string) Scene

Finds and returns a scene by name, or nil if not found.

type Shader

type Shader struct {
	// contains filtered or unexported fields
}

Combination of shaders and shader program.

func NewShader

func NewShader(vertexShader, fragmentShader string) (*Shader, error)

Creates a new shader program by given vertex and fragment shader source code. The shaders itself will be deleted when compiled, only the program ID will be kept.

func (*Shader) Bind

func (s *Shader) Bind()

Binds the shader for usage.

func (*Shader) BindAttrib

func (s *Shader) BindAttrib(name string)

Binds an attribute to this shader (with default index), name must be present in shader source.

func (*Shader) BindAttribIndex

func (s *Shader) BindAttribIndex(name string, index uint32)

Binds an attribute to this shader, name must be present in shader source.

func (*Shader) DisableVertexAttribArrays

func (s *Shader) DisableVertexAttribArrays()

Disables all bound attributes.

func (*Shader) Drop

func (s *Shader) Drop()

Deletes the GL program object.

func (*Shader) EnableVertexAttribArrays

func (s *Shader) EnableVertexAttribArrays()

Enables all bound attributes.

func (*Shader) GetAttribLocation

func (s *Shader) GetAttribLocation(name string) int32

Returns the location of an attribute. The name must be present in the shader source.

func (*Shader) GetFragmentId

func (s *Shader) GetFragmentId() uint32

Returns the fragment shader GL ID.

func (*Shader) GetProgramId

func (s *Shader) GetProgramId() uint32

Retuns the program GL ID.

func (*Shader) GetUniformLocation

func (s *Shader) GetUniformLocation(name string) int32

Returns the uniform location of a variable. The name must be present in the shader source.

func (*Shader) GetVertexId

func (s *Shader) GetVertexId() uint32

Returns the vertex shader GL ID.

func (*Shader) SendMat3

func (s *Shader) SendMat3(name string, m Mat3)

func (*Shader) SendMat4

func (s *Shader) SendMat4(name string, m Mat4)

func (*Shader) SendUniform1f

func (s *Shader) SendUniform1f(name string, v float32)

func (*Shader) SendUniform1fv

func (s *Shader) SendUniform1fv(name string, count int32, data *float32)

func (*Shader) SendUniform1i

func (s *Shader) SendUniform1i(name string, v int32)

func (*Shader) SendUniform1iv

func (s *Shader) SendUniform1iv(name string, count int32, data *int32)

func (*Shader) SendUniform2f

func (s *Shader) SendUniform2f(name string, v0, v1 float32)

func (*Shader) SendUniform2fv

func (s *Shader) SendUniform2fv(name string, count int32, data *float32)

func (*Shader) SendUniform2i

func (s *Shader) SendUniform2i(name string, v0, v1 int32)

func (*Shader) SendUniform2iv

func (s *Shader) SendUniform2iv(name string, count int32, data *int32)

func (*Shader) SendUniform2x2

func (s *Shader) SendUniform2x2(name string, data *float32, count int32, transpose bool)

func (*Shader) SendUniform2x3

func (s *Shader) SendUniform2x3(name string, data *float32, count int32, transpose bool)

func (*Shader) SendUniform2x4

func (s *Shader) SendUniform2x4(name string, data *float32, count int32, transpose bool)

func (*Shader) SendUniform3f

func (s *Shader) SendUniform3f(name string, v0, v1, v2 float32)

func (*Shader) SendUniform3fv

func (s *Shader) SendUniform3fv(name string, count int32, data *float32)

func (*Shader) SendUniform3i

func (s *Shader) SendUniform3i(name string, v0, v1, v2 int32)

func (*Shader) SendUniform3iv

func (s *Shader) SendUniform3iv(name string, count int32, data *int32)

func (*Shader) SendUniform3x2

func (s *Shader) SendUniform3x2(name string, data *float32, count int32, transpose bool)

func (*Shader) SendUniform3x3

func (s *Shader) SendUniform3x3(name string, data *float32, count int32, transpose bool)

func (*Shader) SendUniform3x4

func (s *Shader) SendUniform3x4(name string, data *float32, count int32, transpose bool)

func (*Shader) SendUniform4f

func (s *Shader) SendUniform4f(name string, v0, v1, v2, v3 float32)

func (*Shader) SendUniform4fv

func (s *Shader) SendUniform4fv(name string, count int32, data *float32)

func (*Shader) SendUniform4i

func (s *Shader) SendUniform4i(name string, v0, v1, v2, v3 int32)

func (*Shader) SendUniform4iv

func (s *Shader) SendUniform4iv(name string, count int32, data *int32)

func (*Shader) SendUniform4x2

func (s *Shader) SendUniform4x2(name string, data *float32, count int32, transpose bool)

func (*Shader) SendUniform4x3

func (s *Shader) SendUniform4x3(name string, data *float32, count int32, transpose bool)

func (*Shader) SendUniform4x4

func (s *Shader) SendUniform4x4(name string, data *float32, count int32, transpose bool)

func (*Shader) Unbind

func (s *Shader) Unbind()

Unbinds shader.

type Sprite

type Sprite struct {
	*Actor
	*Pos2D
	*Tex
}

Sprite is an actor having a 2D position and a texture.

func NewSprite

func NewSprite(tex *Tex) *Sprite

Creates a new sprite with given texture.

type SpriteRenderer

type SpriteRenderer struct {
	Pos2D

	Shader *Shader
	Camera *Camera
	// contains filtered or unexported fields
}

The sprite renderer is a system rendering sprites. It has a 2D position component, to move all sprites at once.

func GetSpriteRenderer

func GetSpriteRenderer() *SpriteRenderer

func NewSpriteRenderer

func NewSpriteRenderer(shader *Shader, camera *Camera, flip bool) *SpriteRenderer

Creates a new sprite renderer using given shader and camera. If shader and/or camera are nil, the default one will be used.

func (*SpriteRenderer) Add

func (s *SpriteRenderer) Add(actor *Actor, pos *Pos2D, tex *Tex) bool

Adds sprite to the renderer.

func (*SpriteRenderer) Cleanup

func (s *SpriteRenderer) Cleanup()

Frees recources created by sprite renderer. This is called automatically when system gets removed.

func (*SpriteRenderer) GetName

func (s *SpriteRenderer) GetName() string

func (*SpriteRenderer) Len

func (s *SpriteRenderer) Len() int

Returns number of sprites.

func (*SpriteRenderer) Remove

func (s *SpriteRenderer) Remove(actor *Actor) bool

Removes sprite from renderer.

func (*SpriteRenderer) RemoveAll

func (s *SpriteRenderer) RemoveAll()

Removes all sprites from renderer.

func (*SpriteRenderer) RemoveById

func (s *SpriteRenderer) RemoveById(id ActorId) bool

Removes sprite from renderer by ID.

func (*SpriteRenderer) Update

func (s *SpriteRenderer) Update(delta float64)

Render sprites.

type System

type System interface {
	Update(float64)
	Cleanup()
	Remove(*Actor) bool
	RemoveById(ActorId) bool
	RemoveAll()
	Len() int
	GetName() string
}

A system provides logic for actors satisfying required components. They are automatically updated on each frame. When a system is removed from systems, the Cleanup() method will be called. This will also happen on program stop. It can be used to cleanup open resources (like GL objects).

func GetSystemByName

func GetSystemByName(name string) System

Finds and returns a system by name, or nil if not found.

type Tex

type Tex struct {
	// contains filtered or unexported fields
}

Texture object.

func GetTex

func GetTex(name string) (*Tex, error)

Finds and returns a Tex resource. If not found or when the resource is of wrong type, an error will be returned.

func NewTex

func NewTex(target uint32) *Tex

Creates a new texture for given target (e.g. GL_TEXTURE_2D).

func (*Tex) Bind

func (t *Tex) Bind()

Binds the texture for rendering.

func (*Tex) Drop

func (t *Tex) Drop()

Drops the texture.

func (*Tex) GetExt

func (t *Tex) GetExt() string

Returns the file extension of this resource.

func (*Tex) GetId

func (t *Tex) GetId() uint32

Returns the GL ID.

func (*Tex) GetName

func (t *Tex) GetName() string

Returns the name of this resource.

func (*Tex) GetPath

func (t *Tex) GetPath() string

Returns the path of this resource.

func (*Tex) GetRGBA

func (t *Tex) GetRGBA() *image.RGBA

Returns the pixel data.

func (*Tex) GetSize

func (t *Tex) GetSize() Vec3

Returns the size of this texture.

func (*Tex) GetTarget

func (t *Tex) GetTarget() uint32

Returns the texture target

func (*Tex) Parameterf

func (t *Tex) Parameterf(name uint32, param float32)

Sets float parameter.

func (*Tex) Parameteri

func (t *Tex) Parameteri(name uint32, param int32)

Sets integer parameter.

func (*Tex) SetActiveTexture

func (t *Tex) SetActiveTexture(activeTexture uint32)

Sets which texture boundary is used when bound for rendering. Can be GL_TEXTURE0, GL_TEXTURE1, ... GL_TEXTUREn.

func (*Tex) SetDefaultParams

func (t *Tex) SetDefaultParams(filter int32)

Sets the default parameters, which are passed filter and CLAMP_TO_EDGE.

func (*Tex) SetExt

func (t *Tex) SetExt(ext string)

Sets the file extension of this resource.

func (*Tex) SetName

func (t *Tex) SetName(name string)

Sets the name of this resource.

func (*Tex) SetPath

func (t *Tex) SetPath(path string)

Sets the path of this resource.

func (*Tex) SetRGBA

func (t *Tex) SetRGBA(rgba *image.RGBA)

Sets pixel data.

func (*Tex) Texture1D

func (t *Tex) Texture1D(level, internalFormat, width int32, format, ttype uint32, data []uint8)

Creates a new 1D texture.

func (*Tex) Texture2D

func (t *Tex) Texture2D(level, internalFormat, width, height int32, format, ttype uint32, data []uint8)

Creates a new 2D texture.

func (*Tex) Texture3D

func (t *Tex) Texture3D(level, internalFormat, width, height, depth int32, format, ttype uint32, data []uint8)

Creates a new 3D texture.

func (*Tex) Unbind

func (t *Tex) Unbind()

Unbinds.

type Text

type Text struct {
	*Actor
	*Pos2D
	*TextComponent
}

Text is an actor representing text rendered as texture mapped font. Each Text has a position and its own buffers.

func NewText

func NewText(font *Font, textStr string) *Text

Returns a new renderable text object.

func (*Text) GetBounds

func (t *Text) GetBounds() Vec2

Returns bounds of text, which is the size of characters.

func (*Text) GetText

func (t *Text) GetText() string

Returns the text as string.

func (*Text) SetText

func (t *Text) SetText(font *Font, text string)

Sets the given string as text and (re)creates buffers.

type TextComponent

type TextComponent struct {
	Color Vec4
	// contains filtered or unexported fields
}

Renderable text component. Use together with Text and create using NewText().

func (*TextComponent) Drop

func (t *TextComponent) Drop()

Deletes GL buffers bound to this text component.

type TextRenderer

type TextRenderer struct {
	Pos2D

	Shader *Shader
	Camera *Camera
	Font   *Font
	// contains filtered or unexported fields
}

The text renderer is a system rendering 2D texture mapped font. It has a 2D position component, to move all texts at once.

func GetTextRenderer

func GetTextRenderer() *TextRenderer

func NewTextRenderer

func NewTextRenderer(shader *Shader, camera *Camera, font *Font) *TextRenderer

Creates a new text renderer using given shader, camera and font. If shader and/or camera are nil, the default one will be used.

func (*TextRenderer) Add

func (r *TextRenderer) Add(actor *Actor, pos *Pos2D, text *TextComponent) bool

Adds text to the renderer.

func (*TextRenderer) Cleanup

func (r *TextRenderer) Cleanup()

Frees recources created by text component. This is called automatically when system gets removed.

func (*TextRenderer) GetName

func (r *TextRenderer) GetName() string

func (*TextRenderer) Len

func (r *TextRenderer) Len() int

Returns number of texts.

func (*TextRenderer) Prepare

func (r *TextRenderer) Prepare(text *Text)

Prepares given text for rendering.

func (*TextRenderer) Remove

func (r *TextRenderer) Remove(actor *Actor) bool

Removes text from renderer.

func (*TextRenderer) RemoveAll

func (r *TextRenderer) RemoveAll()

Removes all texts.

func (*TextRenderer) RemoveById

func (r *TextRenderer) RemoveById(id ActorId) bool

Removes text from renderer by ID.

func (*TextRenderer) Update

func (r *TextRenderer) Update(delta float64)

Renders texts.

type VAO

type VAO struct {
	// contains filtered or unexported fields
}

Vertex Array Object.

func NewVAO

func NewVAO() *VAO

Creates a new VAO. Bind and use VBOs for rendering later.

func (*VAO) Bind

func (v *VAO) Bind()

Binds VAO for rendering.

func (*VAO) Drop

func (v *VAO) Drop()

Drops this VAO.

func (*VAO) GetId

func (v *VAO) GetId() uint32

Returns the GL ID.

func (*VAO) Unbind

func (v *VAO) Unbind()

Unbinds.

type VBO

type VBO struct {
	// contains filtered or unexported fields
}

Vertex Buffer Object.

func NewVBO

func NewVBO(target uint32) *VBO

Creates a new VBO with given target.

func (*VBO) AttribPointer

func (v *VBO) AttribPointer(attribLocation int32, size int32, btype uint32, normalized bool, stride int32)

Sets the attribute pointer for rendering. Used together with shader.

func (*VBO) Bind

func (v *VBO) Bind()

Binds VBO for rendering.

func (*VBO) Drop

func (v *VBO) Drop()

Drops this VBO.

func (*VBO) Fill

func (v *VBO) Fill(data unsafe.Pointer, elements, size int, use uint32)

Fills VBO with data. An unsafe pointer must be used out of Gos unsafe package.

func (*VBO) GetId

func (v *VBO) GetId() uint32

Returns the GL ID.

func (*VBO) GetTarget

func (v *VBO) GetTarget() uint32

Returns the target.

func (*VBO) Size

func (v *VBO) Size() int32

Returns the number of elements within this VBO.

func (*VBO) Unbind

func (v *VBO) Unbind()

Unbinds.

func (*VBO) Update

func (v *VBO) Update(data unsafe.Pointer, elements, offset, size int)

Updates data or part of data. An unsafe pointer must be used out of Gos unsafe package.

type Vec2

type Vec2 struct {
	X, Y float64
}

A 2D vector.

func (*Vec2) Add

func (v *Vec2) Add(vec Vec2)

Adds and saves the given vector to actual vector.

func (*Vec2) Copy

func (v *Vec2) Copy() Vec2

Creates a copy of actual vector.

func (*Vec2) Div

func (v *Vec2) Div(vec Vec2)

Divides and saves the given vector to actual vector.

func (*Vec2) Dot

func (v *Vec2) Dot() float64

Calculates and returns the dot product of actual vector.

func (*Vec2) DotVec

func (v *Vec2) DotVec(vec Vec2) float64

Calculates and returns the dot product of combination of given vector and actual vector.

func (*Vec2) Length

func (v *Vec2) Length() float64

Returns the length of actual vector.

func (*Vec2) Mult

func (v *Vec2) Mult(vec Vec2)

Multiplies and saves the given vector to actual vector.

func (*Vec2) Normalize

func (v *Vec2) Normalize()

Normalizes actual vector to length 1.

func (*Vec2) Sub

func (v *Vec2) Sub(vec Vec2)

Subtracts and saves the given vector to actual vector.

type Vec3

type Vec3 struct {
	X, Y, Z float64
}

A 3D vector.

func CrossVec3

func CrossVec3(a, b Vec3) Vec3

Calulates the cross product of given vectors and returns result as a new vector.

func (*Vec3) Add

func (v *Vec3) Add(vec Vec3)

Adds and saves the given vector to actual vector.

func (*Vec3) Copy

func (v *Vec3) Copy() Vec3

Creates a copy of actual vector.

func (*Vec3) Cross

func (v *Vec3) Cross(vec Vec3)

Calculates and saves cross product of given and actual vector.

func (*Vec3) Div

func (v *Vec3) Div(vec Vec3)

Divides and saves the given vector to actual vector.

func (*Vec3) Dot

func (v *Vec3) Dot() float64

Calculates and returns the dot product of actual vector.

func (*Vec3) DotVec

func (v *Vec3) DotVec(vec Vec3) float64

Calculates and returns the dot product of combination of given vector and actual vector.

func (*Vec3) Length

func (v *Vec3) Length() float64

Returns the length of actual vector.

func (*Vec3) Mult

func (v *Vec3) Mult(vec Vec3)

Multiplies and saves the given vector to actual vector.

func (*Vec3) Normalize

func (v *Vec3) Normalize()

Normalizes actual vector to length 1.

func (*Vec3) Sub

func (v *Vec3) Sub(vec Vec3)

Subtracts and saves the given vector to actual vector.

type Vec4

type Vec4 struct {
	X, Y, Z, W float64
}

A 4D vector.

func (*Vec4) Add

func (v *Vec4) Add(vec Vec4)

Adds and saves the given vector to actual vector.

func (*Vec4) Copy

func (v *Vec4) Copy() Vec4

Creates a copy of actual vector.

func (*Vec4) Div

func (v *Vec4) Div(vec Vec4)

Divides and saves the given vector to actual vector.

func (*Vec4) Dot

func (v *Vec4) Dot() float64

Calculates and returns the dot product of actual vector.

func (*Vec4) DotVec

func (v *Vec4) DotVec(vec Vec4) float64

Calculates and returns the dot product of combination of given vector and actual vector.

func (*Vec4) Length

func (v *Vec4) Length() float64

Returns the length of actual vector.

func (*Vec4) Mult

func (v *Vec4) Mult(vec Vec4)

Multiplies and saves the given vector to actual vector.

func (*Vec4) Normalize

func (v *Vec4) Normalize()

Normalizes actual vector to length 1.

func (*Vec4) Sub

func (v *Vec4) Sub(vec Vec4)

Subracts and saves the given vector to actual vector.

Directories

Path Synopsis
demo

Jump to

Keyboard shortcuts

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