app-desktop.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // Initialize frame time
  60. app.frameStart = time.Now()
  61. // Set up recurring calls to user's update function
  62. for true {
  63. // If Exit() was called or there was an attempt to close the window dispatch OnExit event for subscribers.
  64. // If no subscriber cancelled the event, terminate the application.
  65. if a.IWindow.(*window.GlfwWindow).ShouldClose() {
  66. a.Dispatch(OnExit, nil)
  67. // TODO allow for cancelling exit e.g. showing dialog asking the user if he/she wants to save changes
  68. // if exit was cancelled {
  69. // a.IWindow.(*window.GlfwWindow).SetShouldClose(false)
  70. // } else {
  71. break
  72. // }
  73. }
  74. // Update frame start and frame delta
  75. now := time.Now()
  76. a.frameDelta = now.Sub(a.frameStart)
  77. a.frameStart = now
  78. // Call user's update function
  79. update(a.renderer, a.frameDelta)
  80. // Swap buffers and poll events
  81. a.IWindow.(*window.GlfwWindow).SwapBuffers()
  82. a.IWindow.(*window.GlfwWindow).PollEvents()
  83. }
  84. // Close default audio device
  85. if a.audioDev != nil {
  86. al.CloseDevice(a.audioDev)
  87. }
  88. // Destroy window
  89. a.Destroy()
  90. }
  91. // Exit requests to terminate the application
  92. // Application will dispatch OnQuit events to registered subscribers which
  93. // can cancel the process by calling CancelDispatch().
  94. func (a *Application) Exit() {
  95. a.IWindow.(*window.GlfwWindow).SetShouldClose(true)
  96. }
  97. // Renderer returns the application's renderer.
  98. func (a *Application) Renderer() *renderer.Renderer {
  99. return a.renderer
  100. }
  101. // KeyState returns the application's KeyState.
  102. func (a *Application) KeyState() *window.KeyState {
  103. return a.keyState
  104. }
  105. // RunTime returns the elapsed duration since the call to Run().
  106. func (a *Application) RunTime() time.Duration {
  107. return time.Now().Sub(a.startTime)
  108. }
  109. // openDefaultAudioDevice opens the default audio device setting it to the current context
  110. func (a *Application) openDefaultAudioDevice() error {
  111. // Opens default audio device
  112. var err error
  113. a.audioDev, err = al.OpenDevice("")
  114. if err != nil {
  115. return fmt.Errorf("opening OpenAL default device: %s", err)
  116. }
  117. // Check for OpenAL effects extension support
  118. var attribs []int
  119. if al.IsExtensionPresent("ALC_EXT_EFX") {
  120. attribs = []int{al.MAX_AUXILIARY_SENDS, 4}
  121. }
  122. // Create audio context
  123. acx, err := al.CreateContext(a.audioDev, attribs)
  124. if err != nil {
  125. return fmt.Errorf("creating OpenAL context: %s", err)
  126. }
  127. // Makes the context the current one
  128. err = al.MakeContextCurrent(acx)
  129. if err != nil {
  130. return fmt.Errorf("setting OpenAL context current: %s", err)
  131. }
  132. // Logs audio library versions
  133. fmt.Println("LOGGING")
  134. log.Info("%s version: %s", al.GetString(al.Vendor), al.GetString(al.Version))
  135. log.Info("%s", vorbis.VersionString())
  136. return nil
  137. }