app-desktop.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // Application singleton
  30. var app *Application
  31. // App returns the Application singleton, creating it the first time.
  32. func App() *Application {
  33. // Return singleton if already created
  34. if app != nil {
  35. return app
  36. }
  37. app = new(Application)
  38. // Initialize window
  39. err := window.Init(width, height, title)
  40. if err != nil {
  41. panic(err)
  42. }
  43. app.IWindow = window.Get()
  44. app.openDefaultAudioDevice() // Set up audio
  45. app.keyState = window.NewKeyState(app) // Create KeyState
  46. // Create renderer and add default shaders
  47. app.renderer = renderer.NewRenderer(app.Gls())
  48. err = app.renderer.AddDefaultShaders()
  49. if err != nil {
  50. panic(fmt.Errorf("AddDefaultShaders:%v", err))
  51. }
  52. return app
  53. }
  54. // Run starts the update loop.
  55. // It calls the user-provided update function every frame.
  56. func (app *Application) Run(update func(renderer *renderer.Renderer, deltaTime time.Duration)) {
  57. // Initialize frame time
  58. app.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 app.IWindow.(*window.GlfwWindow).ShouldClose() {
  64. app.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. // app.IWindow.(*window.GlfwWindow).SetShouldClose(false)
  68. // } else {
  69. break
  70. // }
  71. }
  72. // Update frame start and frame delta
  73. now := time.Now()
  74. app.frameDelta = now.Sub(app.frameStart)
  75. app.frameStart = now
  76. // Call user's update function
  77. update(app.renderer, app.frameDelta)
  78. // Swap buffers and poll events
  79. app.IWindow.(*window.GlfwWindow).SwapBuffers()
  80. app.IWindow.(*window.GlfwWindow).PollEvents()
  81. }
  82. // Close default audio device
  83. if app.audioDev != nil {
  84. al.CloseDevice(app.audioDev)
  85. }
  86. // Destroy window
  87. app.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 (app *Application) Exit() {
  93. app.IWindow.(*window.GlfwWindow).SetShouldClose(true)
  94. }
  95. // Renderer returns the application's renderer.
  96. func (app *Application) Renderer() *renderer.Renderer {
  97. return app.renderer
  98. }
  99. // KeyState returns the application's KeyState.
  100. func (app *Application) KeyState() *window.KeyState {
  101. return app.keyState
  102. }
  103. // openDefaultAudioDevice opens the default audio device setting it to the current context
  104. func (app *Application) openDefaultAudioDevice() error {
  105. // Opens default audio device
  106. var err error
  107. app.audioDev, err = al.OpenDevice("")
  108. if err != nil {
  109. return fmt.Errorf("opening OpenAL default device: %s", err)
  110. }
  111. // Check for OpenAL effects extension support
  112. var attribs []int
  113. if al.IsExtensionPresent("ALC_EXT_EFX") {
  114. attribs = []int{al.MAX_AUXILIARY_SENDS, 4}
  115. }
  116. // Create audio context
  117. acx, err := al.CreateContext(app.audioDev, attribs)
  118. if err != nil {
  119. return fmt.Errorf("creating OpenAL context: %s", err)
  120. }
  121. // Makes the context the current one
  122. err = al.MakeContextCurrent(acx)
  123. if err != nil {
  124. return fmt.Errorf("setting OpenAL context current: %s", err)
  125. }
  126. // Logs audio library versions
  127. fmt.Println("LOGGING")
  128. log.Info("%s version: %s", al.GetString(al.Vendor), al.GetString(al.Version))
  129. log.Info("%s", vorbis.VersionString())
  130. return nil
  131. }