app-desktop.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. frameStart time.Time // Frame start time
  27. frameDelta time.Duration // Duration of last frame
  28. }
  29. // App returns the Application singleton, creating it the first time.
  30. func App() *Application {
  31. // Return singleton if already created
  32. if app != nil {
  33. return app
  34. }
  35. app = new(Application)
  36. // Initialize window
  37. err := window.Init(width, height, title)
  38. if err != nil {
  39. panic(err)
  40. }
  41. app.IWindow = window.Get()
  42. app.openDefaultAudioDevice() // Set up audio
  43. app.keyState = window.NewKeyState(app) // Create KeyState
  44. // Create renderer and add default shaders
  45. app.renderer = renderer.NewRenderer(app.Gls())
  46. err = app.renderer.AddDefaultShaders()
  47. if err != nil {
  48. panic(fmt.Errorf("AddDefaultShaders:%v", err))
  49. }
  50. return app
  51. }
  52. // Run starts the update loop.
  53. // It calls the user-provided update function every frame.
  54. func (app *Application) Run(update func(renderer *renderer.Renderer, deltaTime time.Duration)) {
  55. // Initialize frame time
  56. app.frameStart = time.Now()
  57. // Set up recurring calls to user's update function
  58. for true {
  59. // If Exit() was called or there was an attempt to close the window dispatch OnExit event for subscribers.
  60. // If no subscriber cancelled the event, terminate the application.
  61. if app.IWindow.(*window.GlfwWindow).ShouldClose() {
  62. app.Dispatch(OnExit, nil)
  63. // TODO allow for cancelling exit e.g. showing dialog asking the user if he/she wants to save changes
  64. // if exit was cancelled {
  65. // app.IWindow.(*window.GlfwWindow).SetShouldClose(false)
  66. // } else {
  67. break
  68. // }
  69. }
  70. // Update frame start and frame delta
  71. now := time.Now()
  72. app.frameDelta = now.Sub(app.frameStart)
  73. app.frameStart = now
  74. // Call user's update function
  75. update(app.renderer, app.frameDelta)
  76. // Swap buffers and poll events
  77. app.IWindow.(*window.GlfwWindow).SwapBuffers()
  78. app.IWindow.(*window.GlfwWindow).PollEvents()
  79. }
  80. // Close default audio device
  81. if app.audioDev != nil {
  82. al.CloseDevice(app.audioDev)
  83. }
  84. // Destroy window
  85. app.Destroy()
  86. }
  87. // Exit requests to terminate the application
  88. // Application will dispatch OnQuit events to registered subscribers which
  89. // can cancel the process by calling CancelDispatch().
  90. func (app *Application) Exit() {
  91. app.IWindow.(*window.GlfwWindow).SetShouldClose(true)
  92. }
  93. // Renderer returns the application's renderer.
  94. func (app *Application) Renderer() *renderer.Renderer {
  95. return app.renderer
  96. }
  97. // KeyState returns the application's KeyState.
  98. func (app *Application) KeyState() *window.KeyState {
  99. return app.keyState
  100. }
  101. // openDefaultAudioDevice opens the default audio device setting it to the current context
  102. func (app *Application) openDefaultAudioDevice() error {
  103. // Opens default audio device
  104. var err error
  105. app.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(app.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. fmt.Println("LOGGING")
  126. log.Info("%s version: %s", al.GetString(al.Vendor), al.GetString(al.Version))
  127. log.Info("%s", vorbis.VersionString())
  128. return nil
  129. }