gfx

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package gfx boots a 2D window on the GoGPU stack.

Layout:

gogpu  — window, input, lifecycle
gg     — 2D drawing (via ggcanvas → surface)
wgpu   — pulled in transitively (GPU API)

g3d is intentionally not wired here; add it when a 3D game needs it.

Build with CGO disabled (Pure Go backend):

CGO_ENABLED=0 go run ./examples/hello2d

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

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

App is a thin gogpu + ggcanvas host for 2D games and tools.

func New

func New(cfg Config) *App

New creates an App. Call OnDraw, then Run.

func (*App) EventSource

func (a *App) EventSource() gpucontext.EventSource

EventSource returns the gogpu event source (keys, pointer, …).

func (*App) Native

func (a *App) Native() *gogpu.App

Native returns the underlying gogpu.App for advanced use (input polling, animation tokens, multi-window, etc.).

func (*App) OnDraw

func (a *App) OnDraw(fn DrawFunc) *App

OnDraw sets the per-frame 2D draw callback.

func (*App) RequestRedraw

func (a *App) RequestRedraw()

RequestRedraw schedules another frame when Continuous is false.

func (*App) Run

func (a *App) Run() error

Run opens the window and blocks until it closes.

type Config

type Config struct {
	Title  string
	Width  int
	Height int
	// Continuous keeps redrawing every frame (game loops).
	// When false, redraw only on RequestRedraw / input (power-friendly).
	Continuous bool
}

Config configures the window. Zero values get sensible defaults.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a 1280×720 continuous game window titled "Gomag".

type DrawFunc

type DrawFunc func(dc *gg.Context, width, height int)

DrawFunc is called each frame with a gg drawing context and logical size.

type Letterbox added in v0.0.5

type Letterbox struct {
	Scale            float64
	OffsetX, OffsetY float64
	VirtW, VirtH     float64
}

Letterbox maps a fixed virtual (playfield) size into a window while preserving aspect ratio, with black bars on the unused sides.

func Fit added in v0.0.5

func Fit(winW, winH, virtW, virtH float64) Letterbox

Fit computes a letterbox for virtual size (virtW×virtH) inside winW×winH. Degenerate sizes yield Scale=0 and zero offsets.

func (Letterbox) Apply added in v0.0.5

func (l Letterbox) Apply(dc *gg.Context)

Apply pushes a transform so subsequent drawing uses virtual coordinates. Caller must dc.Pop() when finished (typically via defer).

func (Letterbox) ToScreen added in v0.0.5

func (l Letterbox) ToScreen(vx, vy float64) (sx, sy float64)

ToScreen converts virtual playfield coordinates into window/screen pixels.

func (Letterbox) ToVirtual added in v0.0.5

func (l Letterbox) ToVirtual(sx, sy float64) (vx, vy float64)

ToVirtual converts window/screen pixels into virtual playfield coordinates.

Jump to

Keyboard shortcuts

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