glui

package module
v0.0.0-...-633dbc1 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

README

Simple Image Display Using Open GL in Go

This package wraps the Go port of the C implementation of GLFW in some boilerplate to make displaying an image on the screen almost trivial.

The version of OpenGL used is 4.1 and the boilerplate includes the necessary shaders to perform the rendering.

See glui/cmd/image.go for a simple example of reading an image and displaying it in a window.

$ go run image.go <your image here - most image types supported>

The glui/cmd/images.go version takes multiple images on the command line and rotates through them. This example also demonstrates attaching a call back for character processing - hitting ESC will close the window.

Functions are included to allow you to create new windows with a backing image, set that image to some other image and a loop function (which actually does the rendering). The loop function can take a zero argument function that will be called once per loop iteration. In addition to handling the window rendering, the loop function also handles any call backs registered on the windows.

Mouse Click, Scroll and Drag Listeners

These allow click, scroll and drag events to be generated and a list of observers to be called with the event location. See the mouse.go and drag.go examples in cmd/.

Key and Character Listeners

These provide glui style wrappers around GLFW's Key and Char window callbacks. Key provides access to press, release and repeat events, in addition to the key code and scan code of the actual button pressed. Character will only be notified for regular alphanumeric runes and any modifiers will have been applied.

Events.go

Finally, the events.c program provided in the C implementation of GLFW has been ported over to Go and is in glui/cmd/events.go.

$ go run events.go

This will dump out all the event types for which call backs can be registered, to the terminal. It doesn't utilize the glui package itself.

Documentation

Overview

Package glui provides a simple way to display an image or images on a screen, each in its own window. The package makes use of the GLFW library and OpenGL v4.1

Index

Constants

View Source
const (
	DefaultClickLimit = 2
)

Variables

View Source
var (
	ClickLimit float64 = DefaultClickLimit // Max allowed distance between press and release
)
View Source
var WinMap = make(map[*glfw.Window]*GLWin)

WinMap provides a map of all windows to their corresponding GLWin

Functions

func GetKeyName

func GetKeyName(key int) string

func Loop

func Loop(update func())

Loop is how window events get processed and the images rendered to their windows. It will run until there are no windows left to process. The update function provides a way to insert codeinto this loop - it should be non-blocking, otherwise window updates and callbacks will stall.

Types

type Action

type Action = glfw.Action
const (
	Press   Action = glfw.Press
	Repeat  Action = glfw.Repeat
	Release Action = glfw.Release
)

type CharacterListener

type CharacterListener struct {
	Window    *GLWin
	Observers []func(rune)
}

CharaterListener adds a wrapper around glfw's character callback.

func NewCharacterListener

func NewCharacterListener(win *GLWin, onChar func(rune)) *CharacterListener

NewCharacterListener adds a key listener to the supplied window and the function to be called when a character is detected.

type GLWin

type GLWin struct {
	Img *image.RGBA  // Image to load, nil otherwise
	Win *glfw.Window // Underlying GL window
	// contains filtered or unexported fields
}

GLWin ties an image to a window

func NewGLWin

func NewGLWin(w, h int, title string, img image.Image, decorated bool) *GLWin

NewGLWin is used to create a new window of width w and height h. If the window is decorated then the title will appear in the window frame. Note - the window and image sizes are independent of each other. The image is scaled (not cropped) to fit the window.

func (*GLWin) SetImage

func (w *GLWin) SetImage(img image.Image)

SetImage is used to change the current image in a window. It's thread safe.

type KeyListener

type KeyListener struct {
	Window    *GLWin
	Observers []func(int, int, Action, ModifierKey)
}

KeyListener adds a wrapper around glfw's key callback.

func NewKeyListener

func NewKeyListener(win *GLWin, onKey func(int, int, Action, ModifierKey)) *KeyListener

NewKeyListener adds a key listener to the supplied window and the function to be called when a key action is detected.

type ModifierKey

type ModifierKey = glfw.ModifierKey
const (
	ModShift    ModifierKey = glfw.ModShift
	ModControl  ModifierKey = glfw.ModControl
	ModAlt      ModifierKey = glfw.ModAlt
	ModSuper    ModifierKey = glfw.ModSuper
	ModCapsLock ModifierKey = glfw.ModCapsLock
	ModNumLock  ModifierKey = glfw.ModNumLock
)

type MouseButton

type MouseButton = glfw.MouseButton
const (
	MouseButton1      MouseButton = glfw.MouseButton1
	MouseButton2      MouseButton = glfw.MouseButton2
	MouseButton3      MouseButton = glfw.MouseButton3
	MouseButton4      MouseButton = glfw.MouseButton4
	MouseButton5      MouseButton = glfw.MouseButton5
	MouseButton6      MouseButton = glfw.MouseButton6
	MouseButton7      MouseButton = glfw.MouseButton7
	MouseButton8      MouseButton = glfw.MouseButton8
	MouseButtonLast   MouseButton = glfw.MouseButtonLast
	MouseButtonLeft   MouseButton = glfw.MouseButtonLeft
	MouseButtonRight  MouseButton = glfw.MouseButtonRight
	MouseButtonMiddle MouseButton = glfw.MouseButtonMiddle
)

type MouseClickListener

type MouseClickListener struct {
	Window    *GLWin
	Button    MouseButton
	Point     []float64
	Observers []func(point []float64)
}

MouseClickListener adds some processing to the mouse button press and release events generated by glfw to turn them into single click events at the point the button was first pressed.

func NewMouseClickListener

func NewMouseClickListener(win *GLWin, button MouseButton, onClick func([]float64)) *MouseClickListener

NewMouseClickListener adds a listener to the supplied window for the indicated button, and the function to be called when a click is detected.

type MouseDragListener

type MouseDragListener struct {
	Window    *GLWin
	Button    MouseButton
	Observers []func(point []float64, dx, dy float64, act Action)
	Point     []float64
	State     bool
}

MouseDragListener adds some processing to the mouse button press and release events generated by glfw to turn them into a sequence of drag events from the point the button was first pressed until it is released.

func NewMouseDragListener

func NewMouseDragListener(win *GLWin, button MouseButton, onDrag func([]float64, float64, float64, Action)) *MouseDragListener

NewMouseDragListener adds a listener to the supplied window for the indicated button, and the function to be called when a drag occurs.

type MouseMoveListener

type MouseMoveListener struct {
	Window    *GLWin
	Observers []func(point []float64)
}

MouseMoveListener is a wrapper around the cursor position callback.

func NewMouseMoveListener

func NewMouseMoveListener(win *GLWin, onMove func([]float64)) *MouseMoveListener

NewMouseMoveListener adds a listener to the supplied window, and the function to be called when mouse movement occurs.

type MouseScrollListener

type MouseScrollListener struct {
	Window    *GLWin
	Observers []func(dx, dy float64)
}

MouseScrollListener is a wrapper around the mouse scroll callback.

func NewMouseScrollListener

func NewMouseScrollListener(win *GLWin, onScroll func(float64, float64)) *MouseScrollListener

NewMouseScrollListener adds a listener to the supplied window, and the function to be called when mouse movement occurs.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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