Die API-Referenz stammt aus den Go-Doc-Kommentaren im Quellcode und ist auf Englisch verfasst.
package screen
import "github.com/nsgo-dev/game/pkg/screen"Übersicht
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
- Konstanten
- 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
- type Mode
- type Placement
- type Video
- 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)
Konstanten
const DefaultLowPowerCap = 30DefaultLowPowerCap is the frame cap Video drops to while the system is constrained, when Video.LowPowerCap is 0.
Funktionen
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.
Typen
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
// 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.
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, and sets Loop's DrawRate to the frame cap in effect. Its signature matches save.SettingsTargets.ApplyDisplay; it always returns nil.
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) 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 (v *Video) Settings() save.DisplaySettingsSettings returns the settings last applied.
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.