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 ¶
- Constants
- func CanFullScreen(env *core.Env) bool
- func Constrained(lowPower core.LowPowerMode, thermal core.ThermalState) bool
- func DisplayID(s save.DisplaySettings, displays []core.DisplayInfo) string
- func SafeRect(window core.Size, insets core.Edges) core.Bounds
- func SafeRectForEnv(env *core.Env) core.Bounds
- func Snap(x, y float32) (float32, float32)
- func SnapPoint(p core.Point) core.Point
- func SupportsWindowResolution(env *core.Env) bool
- type Mode
- type Placement
- type Video
- func (v *Video) Apply(s save.DisplaySettings) error
- func (v *Video) ApplyPending()
- func (v *Video) ApplyResolution(width, height core.Points)
- func (v *Video) Attach(view *core.View)
- func (v *Video) Dropped() bool
- func (v *Video) FrameCap() int
- func (v *Video) PendingFullScreen() bool
- func (v *Video) Resolution() core.Size
- func (v *Video) Run(cx *core.Scope) (stop func())
- func (v *Video) Settings() save.DisplaySettings
- type Viewport
- func (v Viewport) Draw(d *core.Drawing, window core.Size, fn func(d *core.Drawing, size core.Size))
- func (v Viewport) GameToView(window core.Size, p core.Point) core.Point
- func (v Viewport) Layout(window core.Size) Placement
- func (v Viewport) SafeGameRect(window core.Size, insets core.Edges) core.Bounds
- func (v Viewport) SafeGameRectForEnv(env *core.Env) core.Bounds
- func (v Viewport) Snap(x, y float32) (float32, float32)
- func (v Viewport) SnapPoint(p core.Point) core.Point
- func (v Viewport) ViewToGame(window core.Size, p core.Point) (core.Point, bool)
Examples ¶
Constants ¶
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 CanFullScreen ¶
CanFullScreen reports whether the platform in env can enter full screen: macOS, Windows, Linux, and non-phone web browsers return true; iOS, Android, and phone web browsers (such as iPhone Safari, until NSGo exposes a capability in nsgo-dev/nsgo#2690) return false. A nil env or an unset OS returns true.
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 ¶
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 ¶
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 ¶
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 SupportsWindowResolution ¶
SupportsWindowResolution reports whether env runs in a resizable desktop window (macOS, Windows, or Linux, not simulated; or, in headless tests with an empty OS, IdiomDesktop or IdiomMac). Phones, tablets, and the web return false.
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 )
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
// NativeWindowSize is the windowed content size restored when ApplyResolution is called with a zero width or height ("native"), such as the project's window size from game.yaml. When zero, Video falls back to the window's initial size captured on Run.
NativeWindowSize core.Size
// OnWindowChanged, when set, is called when the window enters or leaves full screen outside a Video request (such as the player pressing Escape or using browser or system controls). Video updates the settings returned by Settings before calling it.
OnWindowChanged func(save.DisplaySettings)
// contains filtered or unexported fields
}
Video applies a player's video settings (save.DisplaySettings) and windowed resolution to a scope's window and a Loop: windowed or full screen on the chosen display, desktop windowed resolution centered on the current 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, display, or windowed size only when the settings change them, or on the first Apply/ApplyResolution when they differ from the window, so applying other settings does not undo a player leaving full screen or resizing the window with the system's own controls. On the web, a saved full-screen setting applied at start is deferred until the next user press (via Attach or ApplyPending) so the browser does not reject it outside a user gesture. While Run is active, Video also watches Env.FullScreen and updates its held settings (and calls OnWindowChanged) when the window enters or leaves full screen outside a Video request. save.DisplaySettings.VSync is currently ignored because NSGo v0.3.4 exposes no swap-interval control.
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, applies any pending windowed resolution when windowed on desktop, and sets Loop's DrawRate to the frame cap in effect. On the web, entering full screen on the initial Apply is deferred to the next user press (Attach or ApplyPending). Its signature matches save.SettingsTargets.ApplyDisplay; it always returns nil.
func (*Video) ApplyPending ¶
func (v *Video) ApplyPending()
ApplyPending sends a web full-screen request deferred from startup Apply. Call it inside a press, touch, or key handler (or wire a view with Attach) so the browser receives the request during a user gesture. It does nothing when no request is pending or Run is not active.
func (*Video) ApplyResolution ¶
ApplyResolution sets the target windowed content size in points and, while windowed on desktop (SupportsWindowResolution), resizes the window centered on the current display. Passing a non-positive width or height selects NativeWindowSize (falling back to the window's initial size captured on Run). In full screen the choice is remembered and applied if the window returns to windowed mode; render-size scaling in full screen awaits canvas resolution scaling.
func (*Video) Attach ¶
Attach wires v.ApplyPending onto view's press, touch, and key-down handlers, chaining any callbacks already set on view. On the web, attaching the game's input view sends a deferred startup full-screen request on the player's first click, tap, or key press.
func (*Video) Dropped ¶
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 ¶
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) PendingFullScreen ¶
PendingFullScreen reports whether a web full-screen request deferred from startup Apply is waiting for a user press.
func (*Video) Resolution ¶
Resolution returns the resolution last passed to ApplyResolution, or the zero Size for native/unset.
func (*Video) Run ¶
Run starts applying v's settings on cx's window and watching the system's power and full-screen state, until the current owner is disposed or stop is called; either one releases the lowered cap and stops sending window requests. Settings and resolutions 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, updated when the window enters or leaves full screen outside a Video request.
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 ¶
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 ¶
GameToView converts a point in virtual game coordinates to view coordinates.
func (Viewport) Layout ¶
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 ¶
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 ¶
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 ¶
Snap rounds x and y to whole virtual pixels when v.PixelSnap is true, and returns them unchanged otherwise.
func (Viewport) SnapPoint ¶
SnapPoint rounds p to whole virtual pixels when v.PixelSnap is true, and returns it unchanged otherwise.
func (Viewport) ViewToGame ¶
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.