screen

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 4 Imported by: 8 modules, 7 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package screen maps a game's virtual canvas onto any window size, converts pointer input between view and virtual coordinates, keeps HUD layers inside the platform's safe area, and applies a player's video settings (full screen, display and frame cap) with Video.

Index

Examples

Constants

View Source
const DefaultLowPowerCap = 30

DefaultLowPowerCap is the frame cap Video drops to while the system is constrained, when Video.LowPowerCap is 0.

Variables

This section is empty.

Functions

func Constrained

func Constrained(lowPower core.LowPowerMode, thermal core.ThermalState) bool

Constrained reports whether a system in low-power mode lowPower with thermal pressure thermal should draw fewer frames: low-power mode on, or thermal pressure serious or critical.

func DisplayID

func DisplayID(s save.DisplaySettings, displays []core.DisplayInfo) string

DisplayID returns the ID of the display s chooses among displays: DisplayID when it is connected, else the display at DisplayIndex, else "" for the window's current display.

func SafeRect

func SafeRect(window core.Size, insets core.Edges) core.Bounds

SafeRect returns the window rectangle inset by insets in view coordinates, clamped so its origin stays in the window and its size is never negative.

Example

SafeRect is the part of the window clear of notches and rounded corners, where a HUD should go.

package main

import (
	"fmt"

	"github.com/nsgo-dev/game/pkg/screen"
	"github.com/nsgo-dev/nsgo/core"
)

func main() {
	r := screen.SafeRect(core.Size{Width: 844, Height: 390}, core.Edges{Left: 47, Right: 47, Bottom: 21})
	fmt.Println(r)
}
Output:
{{47 0} {750 369}}

func SafeRectForEnv

func SafeRectForEnv(env *core.Env) core.Bounds

SafeRectForEnv returns SafeRect for env's current window size and safe-area insets. Called inside a computation or Canvas.Draw, it subscribes to window and safe-area changes.

func Snap

func Snap(x, y float32) (float32, float32)

Snap rounds x and y to whole virtual pixels.

Example

Snap rounds a position to whole points so pixel art does not shimmer as it moves.

package main

import (
	"fmt"

	"github.com/nsgo-dev/game/pkg/screen"
)

func main() {
	fmt.Println(screen.Snap(10.4, 20.6))
}
Output:
10 21

func SnapPoint

func SnapPoint(p core.Point) core.Point

SnapPoint rounds p's coordinates to whole virtual pixels.

Types

type Mode

type Mode uint8

Mode chooses how a Viewport fits its virtual canvas into a window of another aspect ratio.

const (
	// ModeFit scales uniformly so the virtual canvas fits inside the window, centered, with bars filling any leftover margin.
	ModeFit Mode = iota
	// ModeFill scales uniformly so the virtual canvas covers the window, centered, clipping what overflows.
	ModeFill
	// ModeStretch scales X and Y independently so the virtual canvas fills the window.
	ModeStretch
	// ModeExpand scales uniformly to match the virtual canvas on the tighter axis and expands the visible virtual world along the other so the window fills with no bars or cropping.
	ModeExpand
)

func (Mode) String

func (m Mode) String() string

String returns the mode's name.

type Placement

type Placement struct {
	// Rect is the virtual canvas's rectangle in view coordinates. In ModeFill it may extend past the window's edges.
	Rect core.Bounds
	// VirtualSize is the visible virtual canvas size passed to Draw's callback: (Width, Height) in ModeFit, ModeFill and ModeStretch, and expanded along the longer axis in ModeExpand.
	VirtualSize core.Size
	// ScaleX and ScaleY convert virtual coordinates to view coordinates.
	ScaleX, ScaleY float32
	// Bars are the non-overlapping letterbox or pillarbox rectangles in view coordinates, empty when the virtual canvas covers the window.
	Bars []core.Bounds
}

Placement is where a Viewport places its virtual canvas in a window.

type Video

type Video struct {
	// Loop receives the frame cap in effect as its DrawRate; nil caps nothing.
	Loop *game.Loop
	// LowPowerCap is the frame cap while the system is constrained and AutoDropLowPower is on, DefaultLowPowerCap when 0 or less. A lower FrameCap stays in effect.
	LowPowerCap int
	// contains filtered or unexported fields
}

Video applies a player's video settings (save.DisplaySettings) to a scope's window and a Loop: windowed or full screen on the chosen display, a cap on how many frames Loop draws a second, and, with AutoDropLowPower, a lower cap while the system is in low-power mode or under serious thermal pressure. It ships no UI: a settings screen edits save.Settings, saves it, and applies it, and the game applies the saved settings at start with save.DefaultSettingsStore.LoadAndApply and Video.Apply as save.SettingsTargets.ApplyDisplay.

Video asks the window to change full screen or display only when the settings change them, or on the first Apply when they differ from the window, so applying other settings does not undo a player leaving full screen with the system's own controls.

Example
package main

import (
	"fmt"

	"github.com/nsgo-dev/game"
	"github.com/nsgo-dev/game/pkg/save"
	"github.com/nsgo-dev/game/pkg/screen"
)

func main() {
	l := &game.Loop{}
	v := &screen.Video{Loop: l}
	s := save.DefaultSettings().Display
	s.FrameCap = 30
	_ = v.Apply(s)
	fmt.Println(l.DrawRate, v.FrameCap())
}
Output:
30 30

func (*Video) Apply

func (v *Video) Apply(s save.DisplaySettings) error

Apply validates s and applies it: it moves the window into or out of full screen on the chosen display when that changed, and sets Loop's DrawRate to the frame cap in effect. Its signature matches save.SettingsTargets.ApplyDisplay; it always returns nil.

func (*Video) Dropped

func (v *Video) Dropped() bool

Dropped reports whether the frame cap is lowered because the system is constrained and AutoDropLowPower is on. Read inside a computation while Run is active, it subscribes to the system's power state; settings changes arrive through Apply, which the caller makes itself.

func (*Video) FrameCap

func (v *Video) FrameCap() int

FrameCap returns the frame cap in effect, 0 for uncapped: the settings' FrameCap, lowered to LowPowerCap while Dropped. It subscribes as Dropped does.

func (*Video) Run

func (v *Video) Run(cx *core.Scope) (stop func())

Run starts applying v's settings on cx's window and watching the system's power state, until the current owner is disposed or stop is called; either one releases the lowered cap and stops sending window requests. Settings applied before Run take effect when it starts. Calling Run again stops the earlier run first.

func (*Video) Settings

func (v *Video) Settings() save.DisplaySettings

Settings returns the settings last applied.

type Viewport

type Viewport struct {
	// Width and Height are the virtual canvas size in game points (for example 320 by 180).
	Width, Height float32
	// Mode chooses how the virtual canvas adapts to the window's aspect ratio. Its zero value is ModeFit.
	Mode Mode
	// IntegerScale scales by the largest whole integer >= 1 that fits the virtual canvas inside the window, falling back to fractional ModeFit when the window is smaller than 1x, centered with bars.
	IntegerScale bool
	// PixelSnap aligns viewport offsets to whole points and causes the Viewport's Snap and SnapPoint methods to round coordinates to whole virtual pixels.
	PixelSnap bool
	// BarColor is the color filled into letterbox and pillarbox bars. Zero defaults to opaque black; core.Transparent draws no bars.
	BarColor core.Color
}

Viewport maps a virtual resolution onto any window: computing its placement, recording its bars, clip and transform into a core.Drawing, converting pointer input, and deriving safe-area bounds for HUD layers.

Example

A Viewport fits a fixed game resolution into the window; with IntegerScale a pixel-art game scales by whole numbers only and the rest becomes bars.

package main

import (
	"fmt"

	"github.com/nsgo-dev/game/pkg/screen"
	"github.com/nsgo-dev/nsgo/core"
)

func main() {
	vp := screen.Viewport{Width: 320, Height: 180, Mode: screen.ModeFit, IntegerScale: true}
	window := core.Size{Width: 1366, Height: 768}
	p := vp.Layout(window)
	fmt.Println(p.ScaleX, p.Rect, len(p.Bars))

	game, inside := vp.ViewToGame(window, core.Point{X: 683, Y: 384})
	fmt.Println(game, inside)
}
Output:
4 {{43 24} {1280 720}} 4
{160 90} true

func (Viewport) Draw

func (v Viewport) Draw(d *core.Drawing, window core.Size, fn func(d *core.Drawing, size core.Size))

Draw records the viewport's bars, clip (when bars or overflow are present), translation and scale into d, calls fn with the visible virtual size, and restores d's state to what it was before.

func (Viewport) GameToView

func (v Viewport) GameToView(window core.Size, p core.Point) core.Point

GameToView converts a point in virtual game coordinates to view coordinates.

func (Viewport) Layout

func (v Viewport) Layout(window core.Size) Placement

Layout returns where v places its virtual canvas in a window of the given size.

It returns a zero Placement if the window or virtual resolution has a non-positive dimension.

func (Viewport) SafeGameRect

func (v Viewport) SafeGameRect(window core.Size, insets core.Edges) core.Bounds

SafeGameRect returns the safe area clamped to the viewport's visible game rectangle and converted into virtual game coordinates, so screen-space HUD layers stay clear of notches, status bars and home indicators.

func (Viewport) SafeGameRectForEnv

func (v Viewport) SafeGameRectForEnv(env *core.Env) core.Bounds

SafeGameRectForEnv returns SafeGameRect for env's current window size and safe-area insets. Called inside a computation or Canvas.Draw, it subscribes to window and safe-area changes.

func (Viewport) Snap

func (v Viewport) Snap(x, y float32) (float32, float32)

Snap rounds x and y to whole virtual pixels when v.PixelSnap is true, and returns them unchanged otherwise.

func (Viewport) SnapPoint

func (v Viewport) SnapPoint(p core.Point) core.Point

SnapPoint rounds p to whole virtual pixels when v.PixelSnap is true, and returns it unchanged otherwise.

func (Viewport) ViewToGame

func (v Viewport) ViewToGame(window core.Size, p core.Point) (core.Point, bool)

ViewToGame converts a point in view coordinates (such as core.PressEvent.Position) to virtual game coordinates.

It returns ok == false when p falls on a letterbox or pillarbox bar, outside the window, or when the viewport has a non-positive layout.

Jump to

Keyboard shortcuts

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