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 ¶
- func Run(model *model.Model)
- func Scale(win *opengl.Window, srcWidth, srcHeight float64) float64
- type Bounds
- type Drawer
- type Window
- func (w *Window) Finalize(world *ecs.World)
- func (w *Window) FinalizeUI(world *ecs.World)
- func (w *Window) Initialize(world *ecs.World)
- func (w *Window) InitializeUI(world *ecs.World)
- func (w *Window) PostUpdateUI(world *ecs.World)
- func (w *Window) Update(world *ecs.World)
- func (w *Window) UpdateUI(world *ecs.World)
- func (w *Window) With(drawers ...Drawer) *Window
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
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) FinalizeUI ¶
FinalizeUI the window system.
func (*Window) Initialize ¶ added in v0.0.3
Initialize the window system.
func (*Window) InitializeUI ¶
InitializeUI the window system.
func (*Window) PostUpdateUI ¶
PostUpdateUI updates the underlying GL window and input events.