Run starts the update loop. It calls the user-provided update function every frame.
(update func(rend *renderer.Renderer, deltaTime time.Duration))
| 57 | // Run starts the update loop. |
| 58 | // It calls the user-provided update function every frame. |
| 59 | func (a *Application) Run(update func(rend *renderer.Renderer, deltaTime time.Duration)) { |
| 60 | |
| 61 | // Create channel so later we can prevent application from finishing while we wait for callbacks |
| 62 | done := make(chan bool) |
| 63 | |
| 64 | // Initialize start and frame time |
| 65 | a.startTime = time.Now() |
| 66 | a.frameStart = time.Now() |
| 67 | |
| 68 | // Set up recurring calls to user's update function |
| 69 | var tick js.Func |
| 70 | tick = js.FuncOf(func(this js.Value, args []js.Value) interface{} { |
| 71 | // Update frame start and frame delta |
| 72 | now := time.Now() |
| 73 | a.frameDelta = now.Sub(a.frameStart) |
| 74 | a.frameStart = now |
| 75 | // Call user's update function |
| 76 | update(a.renderer, a.frameDelta) |
| 77 | // Set up new callback if not exiting |
| 78 | if !a.exit { |
| 79 | a.cbid = js.Global().Call("requestAnimationFrame", tick) |
| 80 | } else { |
| 81 | a.Dispatch(OnExit, nil) |
| 82 | done <- true // Write to done channel to exit the app |
| 83 | } |
| 84 | return nil |
| 85 | }) |
| 86 | defer tick.Release() |
| 87 | |
| 88 | a.cbid = js.Global().Call("requestAnimationFrame", tick) |
| 89 | |
| 90 | // Read from done channel |
| 91 | // This channel will be empty (except when we want to exit the app) |
| 92 | // It keeps the app from finishing while we wait for the next call to tick() |
| 93 | <-done |
| 94 | |
| 95 | // Destroy the window |
| 96 | a.IWindow.Destroy() |
| 97 | } |
| 98 | |
| 99 | // Exit exits the app. |
| 100 | func (a *Application) Exit() { |