La référence API provient des commentaires de documentation Go du code source et est rédigée en anglais.
package screen
import "github.com/nsgo-dev/game/pkg/screen"Vue d’ensemble
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
- Constantes
- 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)
Constantes
const DefaultLowPowerCap = 30DefaultLowPowerCap is the frame cap Video drops to while the system is constrained, when Video.LowPowerCap is 0.
Fonctions
func CanFullScreen(env *core.Env) boolCanFullScreen 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(lowPower core.LowPowerMode, thermal core.ThermalState) boolConstrained 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(s save.DisplaySettings, displays []core.DisplayInfo) stringDisplayID 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(window core.Size, insets core.Edges) core.BoundsSafeRect returns the window rectangle inset by insets in view coordinates, clamped so its origin stays in the window and its size is never negative.
func SafeRectForEnv(env *core.Env) core.BoundsSafeRectForEnv 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(x, y float32) (float32, float32)Snap rounds x and y to whole virtual pixels.
func SnapPoint(p core.Point) core.PointSnapPoint rounds p's coordinates to whole virtual pixels.
func SupportsWindowResolution(env *core.Env) boolSupportsWindowResolution 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 uint8Mode 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 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 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.
func (v *Video) Apply(s save.DisplaySettings) errorApply 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 (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 (v *Video) ApplyResolution(width, height core.Points)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 (v *Video) Attach(view *core.View)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 (v *Video) Dropped() boolDropped 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 (v *Video) FrameCap() intFrameCap returns the frame cap in effect, 0 for uncapped: the settings' FrameCap, lowered to LowPowerCap while Dropped. It subscribes as Dropped does.
func (v *Video) PendingFullScreen() boolPendingFullScreen reports whether a web full-screen request deferred from startup Apply is waiting for a user press.
func (v *Video) Resolution() core.SizeResolution returns the resolution last passed to ApplyResolution, or the zero Size for native/unset.
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 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 (v *Video) Settings() save.DisplaySettingsSettings returns the settings last applied, updated when the window enters or leaves full screen outside a Video request.
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.
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 (v Viewport) GameToView(window core.Size, p core.Point) core.PointGameToView converts a point in virtual game coordinates to view coordinates.
func (v Viewport) Layout(window core.Size) PlacementLayout 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 (v Viewport) SafeGameRect(window core.Size, insets core.Edges) core.BoundsSafeGameRect 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 (v Viewport) SafeGameRectForEnv(env *core.Env) core.BoundsSafeGameRectForEnv 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 (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 (v Viewport) SnapPoint(p core.Point) core.PointSnapPoint rounds p to whole virtual pixels when v.PixelSnap is true, and returns it unchanged otherwise.
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.