window

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package window provides an OpenGL window system for plotting and free drawing.

For the actual visualization/plotting part, see package github.com/mlange-42/arche-pixel/plot.

Example
// Create a new model.
m := model.New()

// Limit the the simulation speed.
m.TPS = 30

// Create a window system with a single drawer.
win := (&window.Window{}).
	With(&RectDrawer{})
// Add the window as UI system.
m.AddUISystem(win)

// Add a termination system that ends the simulation.
m.AddSystem(&system.FixedTermination{
	Steps: 100,
})

m.Run()

// Run the simulation.
// Due to the use of the OpenGL UI system, the model must be run via [window.Run].
// Comment out the code line above, and uncomment the next line to run this example stand-alone.

// window.Run(m)
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run added in v0.6.0

func Run(model *model.Model)

Run is essential to run simulations that feature arche-pixel UI components. Call this function from the main function of your application, with a Model as argument. This is necessary, so that Model.Run runs on the main thread.

func Scale added in v0.0.3

func Scale(win *opengl.Window, srcWidth, srcHeight float64) float64

Scale calculates the drawing scale for fitting a source region into a window's canvas.

Types

type Bounds

type Bounds struct {
	X int // X position
	Y int // Y position
	W int // Width
	H int // Height
}

Bounds define a bounding box for a window.

func B

func B(x, y, w, h int) Bounds

B created a new Bounds object.

type Drawer

type Drawer interface {
	// Initialize is called before any other method.
	// Use it to initialize the Drawer.
	Initialize(w *ecs.World, win *opengl.Window)

	// Update is called with normal system updates.
	// Can be used to update observers.
	Update(w *ecs.World)

	// UpdateInputs is called on every UI update, i.e. with the frequency of FPS.
	// Can be used to handle user input of the previous frame update.
	UpdateInputs(w *ecs.World, win *opengl.Window)

	// Draw is called on UI updates, every [Model.DrawInterval] steps.
	// Draw is not called when the host window is minimized.
	// Do all OpenGL drawing on the window here.
	Draw(w *ecs.World, win *opengl.Window)
}

Drawer interface. Drawers are used by the Window to render information from an Arche model.

Example
package main

import (
	"image/color"

	pixel "github.com/gopxl/pixel/v2"
	"github.com/gopxl/pixel/v2/backends/opengl"
	"github.com/gopxl/pixel/v2/ext/imdraw"
	"github.com/mlange-42/arche-model/resource"
	"github.com/mlange-42/arche-pixel/window"
	"github.com/mlange-42/arche/ecs"
)

func main() {
	var dr window.Drawer = &RectDrawer{}
	_ = dr
}

// RectDrawer is an example drawer.
type RectDrawer struct {
	dr imdraw.IMDraw
}

// Initialize the RectDrawer.
func (d *RectDrawer) Initialize(w *ecs.World, win *opengl.Window) {
	// Create a drawer from the Pixel engine.
	d.dr = *imdraw.New(nil)
}

// Update the RectDrawer (does nothing).
func (d *RectDrawer) Update(w *ecs.World) {}

// UpdateInputs handles input events of the previous frame update.
func (d *RectDrawer) UpdateInputs(w *ecs.World, win *opengl.Window) {}

// Draw the RectDrawer's stuff.
func (d *RectDrawer) Draw(w *ecs.World, win *opengl.Window) {
	// Get a resource from the world.
	tick := ecs.GetResource[resource.Tick](w)
	offset := float64(tick.Tick)

	// Create a white rectangle that moves with progressing model tick.
	d.dr.Color = color.White
	d.dr.Push(pixel.V(50+offset, 50+offset), pixel.V(250+offset, 200+offset))
	d.dr.Rectangle(0)

	// Draw everything on the window.
	d.dr.Draw(win)

	// Reset the drawer
	d.dr.Reset()
	d.dr.Clear()
}
Output:

type Window

type Window struct {
	Title        string   // Window title. Optional.
	Bounds       Bounds   // Window bounds (position and size). Optional.
	Drawers      []Drawer // Drawers in increasing z order.
	DrawInterval int      // Interval for re-drawing, in UI frames. Optional.
	// contains filtered or unexported fields
}

Window provides an OpenGL window for drawing. Drawing is done by one or more Drawer instances. Further, window bounds and update and draw intervals can be configured.

If the world contains a resource of type github.com/mlange-42/arche-model/resource/Termination, the model is terminated when the window is closed.

Example
m := model.New()

// Create a Window with at least one Drawer.
window := (&window.Window{Bounds: window.B(100, 100, 800, 600)}).
	With(&RectDrawer{})

// Add is to the model as UI system.
m.AddUISystem(window)
Output:

func (*Window) Finalize added in v0.0.3

func (w *Window) Finalize(world *ecs.World)

Finalize the window system.

func (*Window) FinalizeUI

func (w *Window) FinalizeUI(world *ecs.World)

FinalizeUI the window system.

func (*Window) Initialize added in v0.0.3

func (w *Window) Initialize(world *ecs.World)

Initialize the window system.

func (*Window) InitializeUI

func (w *Window) InitializeUI(world *ecs.World)

InitializeUI the window system.

func (*Window) PostUpdateUI

func (w *Window) PostUpdateUI(world *ecs.World)

PostUpdateUI updates the underlying GL window and input events.

func (*Window) Update added in v0.0.3

func (w *Window) Update(world *ecs.World)

Update the window system.

func (*Window) UpdateUI

func (w *Window) UpdateUI(world *ecs.World)

UpdateUI the window system.

func (*Window) With added in v0.0.3

func (w *Window) With(drawers ...Drawer) *Window

With adds one or more Drawer instances to the window.

Jump to

Keyboard shortcuts

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