API 레퍼런스는 소스 코드의 Go 문서 주석에서 추출되며 영어로 작성되어 있습니다.
package game
import "github.com/nsgo-dev/game"개요
Package game is a game layer for NSGo apps: a fixed-step loop, input actions, sprites, tilemaps, scenes and sound, built on NSGo's public API only, so an app that does not import it links none of it.
This package holds Loop, which the others share. Each domain has a package of its own: math2d for 2D vectors, rectangles, circles and affine transforms, sprite for atlases, sprites, animations and batches, and sound for the mixer, its effects and music; input, tilemap (with the camera) and scene follow, and examples holds sample games.
색인
- type Loop
- func (l *Loop) EffectiveTimeScale() float64
- func (l *Loop) Elapsed() time.Duration
- func (l *Loop) ElapsedSeconds() float64
- func (l *Loop) MarkDirty()
- func (l *Loop) Run(cx *core.Scope) (stop func())
- func (l *Loop) SetPaused(paused bool)
- func (l *Loop) SetTimeScale(scale float64)
- func (l *Loop) Step(n int)
- func (l *Loop) StepDuration() time.Duration
- func (l *Loop) StepNow(n int) int
- func (l *Loop) StepOnce()
- func (l *Loop) StepRate() int
- func (l *Loop) Steps() uint64
타입
type Loop struct {
// Rate is how many updates run a second, 60 when 0.
Rate int
// MaxSteps is the most updates one frame runs, Rate/10 (a tenth of a second's updates) and at least 1 when 0. A frame that would run more drops the whole steps past it, so an Update slower than its step, or a long hitch, slows the game rather than making each frame run more updates than the last.
MaxSteps int
// Paused stops Update from advancing with display time while Draw keeps running. Unpausing resumes from the current frame without a catch-up backlog.
Paused bool
// TimeScale scales simulated time, 1 when 0 or less (or not finite). Update still receives the fixed 1/Rate step, so 0.5 runs half as many updates a second and 2 runs twice as many.
TimeScale float64
// DrawRate caps Draw to at most DrawRate calls a second, uncapped when 0 or less. Update still runs at Rate.
DrawRate int
// OnDemand skips Draw on frames where no Update ran, unless MarkDirty has been called since the last Draw.
OnDemand bool
// Immersive hides a phone's system bars (mobile.Immersive) while the game plays, that is while the loop runs and is not Paused, and brings them back while it is Paused, as under a pause menu, and once it stops or its scope is disposed. The loop hides them as it starts or resumes in the foreground and applies a change to Immersive or Paused on its next frame, and leaves the bars as they are while the app is in the background. Desktop and web targets ignore it.
Immersive bool
// Update moves the game on by dt, which is always 1/Rate seconds.
Update func(dt time.Duration)
// Draw draws the frame; alpha, from 0 up to but not including 1, is how far the frame falls between the last update and the next.
Draw func(alpha float64)
// FrameTime, when set, receives each frame's length, the time it adds (0 for a frame stamped before the run started or resumed), ahead of the frame's updates. It is what frame-time statistics such as quality.Selector's read; a frame that ends a pause in the background measures from the time the window showed again, not the time away. It fires while Paused is set too, so a caller measuring only frames that simulate skips those itself.
FrameTime func(dt time.Duration)
// OnPause, when set, runs whenever Paused changes, so subsystems such as sound.Mixer (sound.Mixer.SetPaused) pause and resume with the loop. SetPaused calls it at once when the state changes; changing Paused directly calls it on the next frame, or in the same frame when Update changes it. If Paused is already true when Run starts, Run calls OnPause(true). Backgrounding does not call OnPause; both Loop and sound.Mixer follow core.Env.Lifecycle directly.
OnPause func(paused bool)
// contains filtered or unexported fields
}Loop runs a game's updates at a fixed rate on a scope's display frames, and draws once a frame in between.
Each frame adds the time since the last one and runs Update once for each whole step of 1/Rate seconds it holds, zero or more times, then Draw once with how far the time left over reaches into the next step. A game moves its state in Update by the fixed dt, so it plays alike at any display rate, and Draw places what moves between the last two states by alpha, so it moves smoothly at any display rate.
Run reads the fields on each frame, so a game may change them while the loop runs.
func (l *Loop) EffectiveTimeScale() float64EffectiveTimeScale returns the simulation time scale actually applied by Run (TimeScale when finite and > 0, else 1).
func (l *Loop) Elapsed() time.DurationElapsed returns the simulated game time advanced by fixed Update steps, rounded to the nearest nanosecond from ElapsedSeconds (so 60 steps at 60 Hz is exactly time.Second).
func (l *Loop) ElapsedSeconds() float64ElapsedSeconds returns the simulated game time advanced by fixed Update steps in seconds. Each contiguous run at a fixed rate contributes steps/rate seconds without integer-nanosecond drift, so 60 steps at 60 Hz is exactly 1.0.
func (l *Loop) MarkDirty()MarkDirty requests a Draw on the next frame when OnDemand is true.
func (l *Loop) Run(cx *core.Scope) (stop func())Run starts the loop on cx's display frames until cx is disposed or stop is called.
The loop pauses while cx's window is in the background (core.Env.Lifecycle), that is hidden, minimized or covered on a desktop and the app in the background on a phone, and resumes from the time it shows again, so the time away runs no updates. While running in the foreground it keeps the display awake (core.Scope.KeepAwake), releasing the hold when paused, stopped or disposed.
A frame adds the time between its display time and the last frame's, as core.Scope.RequestAnimationFrame gives it: the display's own frame time where the platform stamps one, so the frame length is the display's rather than when the engine got to the frame, and the graph clock's otherwise. The first frame after the loop starts or resumes adds the time from then to its display time, which may lie a frame ahead where the display stamps a frame with when it will show, so the loop's time is the shown frame's. A frame stamped before the time the loop started or resumed adds nothing, and the next frame adds the time from then.
With Immersive set, the run hides the system bars while the game plays and shows them again when it is paused, stopped or disposed. Runs in the same window share the bars: they stay hidden while any of them plays with Immersive set, and come back once the last of those pauses or ends, so a scene that starts its own loop before the last scene's loop is disposed keeps them hidden.
func (l *Loop) SetPaused(paused bool)SetPaused pauses or resumes the loop's updates without accumulating a catch-up backlog, calling OnPause if the paused state changed.
func (l *Loop) SetTimeScale(scale float64)SetTimeScale sets the simulation time scale; values at or below 0 run at 1.
func (l *Loop) Step(n int)Step queues n fixed updates to run on the next display frame while Paused is true, redrawing afterward. Values at or below 0 do nothing.
func (l *Loop) StepDuration() time.DurationStepDuration returns the fixed duration passed to Update (time.Second / StepRate()).
func (l *Loop) StepNow(n int) intStepNow synchronously executes n fixed Update steps right away (whether Paused is true or false), followed by a Draw when Draw is set, and returns how many steps ran. Values at or below 0 do nothing and return 0. If Update stops an active Run, StepNow stops immediately without calling Draw.
func (l *Loop) StepOnce()StepOnce queues one fixed update to run on the next display frame while Paused is true.
func (l *Loop) StepRate() intStepRate returns the effective fixed update rate in Hz (Rate when > 0, else 60).
func (l *Loop) Steps() uint64Steps returns the total number of fixed Update steps executed by l.