app-desktop.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !wasm
  5. package app
  6. import (
  7. "fmt"
  8. "github.com/g3n/engine/audio/al"
  9. "github.com/g3n/engine/audio/vorbis"
  10. "github.com/g3n/engine/renderer"
  11. "github.com/g3n/engine/window"
  12. "time"
  13. )
  14. // Application
  15. type Application struct {
  16. window.IWindow // Embedded GlfwWindow
  17. keyState *window.KeyState // Keep track of keyboard state
  18. renderer *renderer.Renderer // Renderer object
  19. audioDev *al.Device // Default audio device
  20. startTime time.Time // Application start time
  21. frameStart time.Time // Frame start time
  22. frameDelta time.Duration // Duration of last frame
  23. }
  24. // App returns the Application singleton, creating it the first time.
  25. func App(width, height int, title string) *Application {
  26. // Return singleton if already created
  27. if a != nil {
  28. return a
  29. }
  30. a = new(Application)
  31. // Initialize window
  32. err := window.Init(width, height, title)
  33. if err != nil {
  34. panic(err)
  35. }
  36. a.IWindow = window.Get()
  37. a.openDefaultAudioDevice() // Set up audio
  38. a.keyState = window.NewKeyState(a) // Create KeyState
  39. // Create renderer and add default shaders
  40. a.renderer = renderer.NewRenderer(a.Gls())
  41. err = a.renderer.AddDefaultShaders()
  42. if err != nil {
  43. panic(fmt.Errorf("AddDefaultShaders:%v", err))
  44. }
  45. return a
  46. }
  47. // Run starts the update loop.
  48. // It calls the user-provided update function every frame.
  49. func (a *Application) Run(update func(rend *renderer.Renderer, deltaTime time.Duration)) {
  50. // Initialize start and frame time
  51. a.startTime = time.Now()
  52. a.frameStart = time.Now()
  53. // Set up recurring calls to user's update function
  54. for true {
  55. // If Exit() was called or there was an attempt to close the window dispatch OnExit event for subscribers.
  56. // If no subscriber cancelled the event, terminate the application.
  57. if a.IWindow.(*window.GlfwWindow).ShouldClose() {
  58. a.Dispatch(OnExit, nil)
  59. // TODO allow for cancelling exit e.g. showing dialog asking the user if he/she wants to save changes
  60. // if exit was cancelled {
  61. // a.IWindow.(*window.GlfwWindow).SetShouldClose(false)
  62. // } else {
  63. break
  64. // }
  65. }
  66. // Update frame start and frame delta
  67. now := time.Now()
  68. a.frameDelta = now.Sub(a.frameStart)
  69. a.frameStart = now
  70. // Call user's update function
  71. update(a.renderer, a.frameDelta)
  72. // Swap buffers and poll events
  73. a.IWindow.(*window.GlfwWindow).SwapBuffers()
  74. a.IWindow.(*window.GlfwWindow).PollEvents()
  75. }
  76. // Close default audio device
  77. if a.audioDev != nil {
  78. al.CloseDevice(a.audioDev)
  79. }
  80. // Destroy window
  81. a.Destroy()
  82. }
  83. // Exit requests to terminate the application
  84. // Application will dispatch OnQuit events to registered subscribers which
  85. // can cancel the process by calling CancelDispatch().
  86. func (a *Application) Exit() {
  87. a.IWindow.(*window.GlfwWindow).SetShouldClose(true)
  88. }
  89. // Renderer returns the application's renderer.
  90. func (a *Application) Renderer() *renderer.Renderer {
  91. return a.renderer
  92. }
  93. // KeyState returns the application's KeyState.
  94. func (a *Application) KeyState() *window.KeyState {
  95. return a.keyState
  96. }
  97. // RunTime returns the elapsed duration since the call to Run().
  98. func (a *Application) RunTime() time.Duration {
  99. return time.Now().Sub(a.startTime)
  100. }
  101. // openDefaultAudioDevice opens the default audio device setting it to the current context
  102. func (a *Application) openDefaultAudioDevice() error {
  103. // Opens default audio device
  104. var err error
  105. a.audioDev, err = al.OpenDevice("")
  106. if err != nil {
  107. return fmt.Errorf("opening OpenAL default device: %s", err)
  108. }
  109. // Check for OpenAL effects extension support
  110. var attribs []int
  111. if al.IsExtensionPresent("ALC_EXT_EFX") {
  112. attribs = []int{al.MAX_AUXILIARY_SENDS, 4}
  113. }
  114. // Create audio context
  115. acx, err := al.CreateContext(a.audioDev, attribs)
  116. if err != nil {
  117. return fmt.Errorf("creating OpenAL context: %s", err)
  118. }
  119. // Makes the context the current one
  120. err = al.MakeContextCurrent(acx)
  121. if err != nil {
  122. return fmt.Errorf("setting OpenAL context current: %s", err)
  123. }
  124. // Logs audio library versions
  125. log.Info("%s version: %s", al.GetString(al.Vendor), al.GetString(al.Version))
  126. log.Info("%s", vorbis.VersionString())
  127. return nil
  128. }