app-desktop.go 4.3 KB

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