application.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. package application
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "runtime"
  7. "runtime/pprof"
  8. "time"
  9. "github.com/g3n/engine/audio/al"
  10. "github.com/g3n/engine/audio/ov"
  11. "github.com/g3n/engine/audio/vorbis"
  12. "github.com/g3n/engine/camera"
  13. "github.com/g3n/engine/camera/control"
  14. "github.com/g3n/engine/core"
  15. "github.com/g3n/engine/gls"
  16. "github.com/g3n/engine/gui"
  17. "github.com/g3n/engine/math32"
  18. "github.com/g3n/engine/renderer"
  19. "github.com/g3n/engine/util/logger"
  20. "github.com/g3n/engine/window"
  21. )
  22. // Application is a basic standard application object which can be extended.
  23. // It creates a Window, OpenGL state, default cameras, default scene and Gui and
  24. // has a method to run the render loop.
  25. type Application struct {
  26. core.Dispatcher // Embedded event dispatcher
  27. core.TimerManager // Embedded timer manager
  28. win window.IWindow // Application window
  29. gl *gls.GLS // OpenGL state
  30. log *logger.Logger // Default application logger
  31. renderer *renderer.Renderer // Renderer object
  32. camPersp *camera.Perspective // Perspective camera
  33. camOrtho *camera.Orthographic // Orthographic camera
  34. camera camera.ICamera // Current camera
  35. orbit *control.OrbitControl // Camera orbit controller
  36. audio bool // Audio available
  37. vorbis bool // Vorbis decoder available
  38. audioEFX bool // Audio effect extension support available
  39. audioDev *al.Device // Audio player device
  40. captureDev *al.Device // Audio capture device
  41. frameRater *FrameRater // Render loop frame rater
  42. scene *core.Node // Node container for 3D tests
  43. guiroot *gui.Root // Gui root panel
  44. frameCount uint64 // Frame counter
  45. frameTime time.Time // Time at the start of the frame
  46. frameDelta time.Duration // Time delta from previous frame
  47. startTime time.Time // Time at the start of the render loop
  48. cpuProfile string // File to write cpu profile to
  49. swapInterval int // Swap interval option
  50. targetFPS uint // Target FPS option
  51. noglErrors bool // No OpenGL check errors options
  52. }
  53. // Options defines initial options passed to application creation function
  54. type Options struct {
  55. WinHeight int // Initial window height (default is screen width)
  56. WinWidth int // Initial window width (default is screen height)
  57. LogLevel int // Initial log level (default = DEBUG)
  58. EnableFlags bool // Enable command line flags (default = false)
  59. TargetFPS uint // Desired frames per second rate (default = 60)
  60. }
  61. // appInstance contains the pointer to the single Application instance
  62. var appInstance *Application
  63. const (
  64. OnBeforeRender = "util.application.OnBeforeRender"
  65. OnAfterRender = "util.application.OnAfterRender"
  66. )
  67. // Creates creates and returns the application object using the specified name for
  68. // the window title and log messages
  69. // This function must be called only once.
  70. func Create(name string, ops Options) (*Application, error) {
  71. if appInstance != nil {
  72. return nil, fmt.Errorf("Application already created")
  73. }
  74. app := new(Application)
  75. app.Dispatcher.Initialize()
  76. app.TimerManager.Initialize()
  77. appInstance = app
  78. // Initialize options default values
  79. app.cpuProfile = ""
  80. app.swapInterval = -1
  81. app.targetFPS = 60
  82. if ops.TargetFPS != 0 {
  83. app.targetFPS = ops.TargetFPS
  84. }
  85. app.noglErrors = false
  86. // Parse standard application command flags if requested
  87. if ops.EnableFlags {
  88. cpuProfile := flag.String("cpuprofile", "", "Activate cpu profiling writing profile to the specified file")
  89. swapInterval := flag.Int("swapinterval", -1, "Sets the swap buffers interval to this value")
  90. targetFPS := flag.Uint("targetfps", 60, "Sets the frame rate in frames per second")
  91. noglErrors := flag.Bool("noglerrors", false, "Do not check OpenGL errors at each call (may increase FPS)")
  92. flag.Parse()
  93. app.cpuProfile = *cpuProfile
  94. app.swapInterval = *swapInterval
  95. app.targetFPS = *targetFPS
  96. app.noglErrors = *noglErrors
  97. }
  98. // Creates application logger
  99. app.log = logger.New(name, nil)
  100. app.log.AddWriter(logger.NewConsole(false))
  101. app.log.SetFormat(logger.FTIME | logger.FMICROS)
  102. app.log.SetLevel(ops.LogLevel)
  103. // Window event handling must run on the main OS thread
  104. runtime.LockOSThread()
  105. // Creates window and sets it as the current context
  106. win, err := window.New("glfw", 10, 10, name, false)
  107. if err != nil {
  108. return nil, err
  109. }
  110. // Sets the window size
  111. swidth, sheight := win.GetScreenResolution(nil)
  112. if ops.WinWidth != 0 {
  113. swidth = ops.WinWidth
  114. }
  115. if ops.WinHeight != 0 {
  116. sheight = ops.WinHeight
  117. }
  118. win.SetSize(swidth, sheight)
  119. app.win = win
  120. // Create OpenGL state
  121. gl, err := gls.New()
  122. if err != nil {
  123. return nil, err
  124. }
  125. app.gl = gl
  126. app.gl.SetCheckErrors(!app.noglErrors)
  127. cc := math32.NewColor("gray")
  128. app.gl.ClearColor(cc.R, cc.G, cc.B, 1)
  129. app.gl.Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
  130. // Creates perspective camera
  131. width, height := app.win.GetSize()
  132. aspect := float32(width) / float32(height)
  133. app.camPersp = camera.NewPerspective(65, aspect, 0.01, 1000)
  134. // Creates orthographic camera
  135. app.camOrtho = camera.NewOrthographic(-2, 2, 2, -2, 0.01, 100)
  136. app.camOrtho.SetPosition(0, 0, 3)
  137. app.camOrtho.LookAt(&math32.Vector3{0, 0, 0})
  138. app.camOrtho.SetZoom(1.0)
  139. // Default camera is perspective
  140. app.camera = app.camPersp
  141. // Creates orbit camera control
  142. // It is important to do this after the root panel subscription
  143. // to avoid GUI events being propagated to the orbit control.
  144. app.orbit = control.NewOrbitControl(app.camera, app.win)
  145. // Creates scene for 3D objects
  146. app.scene = core.NewNode()
  147. // Creates gui root panel
  148. app.guiroot = gui.NewRoot(app.gl, app.win)
  149. // Creates renderer
  150. app.renderer = renderer.NewRenderer(gl)
  151. err = app.renderer.AddDefaultShaders()
  152. if err != nil {
  153. return nil, fmt.Errorf("Error from AddDefaulShaders:%v", err)
  154. }
  155. app.renderer.SetScene(app.scene)
  156. app.renderer.SetGui(app.guiroot)
  157. // Create frame rater
  158. app.frameRater = NewFrameRater(uint(app.targetFPS))
  159. return app, nil
  160. }
  161. // App returns the application single instance or nil
  162. // if the application was not created yet
  163. func Get() *Application {
  164. return appInstance
  165. }
  166. // Log returns the application logger
  167. func (app *Application) Log() *logger.Logger {
  168. return app.log
  169. }
  170. // Window returns the application window
  171. func (app *Application) Window() window.IWindow {
  172. return app.win
  173. }
  174. // Gl returns the OpenGL state associated
  175. func (app *Application) Gl() *gls.GLS {
  176. return app.gl
  177. }
  178. // Gui returns the current application Gui root panel
  179. func (app *Application) Gui() *gui.Root {
  180. return app.guiroot
  181. }
  182. // Scene returns the current application 3D scene
  183. func (app *Application) Scene() *core.Node {
  184. return app.scene
  185. }
  186. // SetScene sets the 3D scene to be rendered
  187. func (app *Application) SetScene(scene *core.Node) {
  188. app.renderer.SetScene(scene)
  189. }
  190. // SetGui sets the root panel of the gui to be rendered
  191. func (app *Application) SetGui(root *gui.Root) {
  192. app.guiroot = root
  193. app.renderer.SetGui(app.guiroot)
  194. }
  195. // SetPanel3D sets the gui panel inside which the 3D scene is shown.
  196. func (app *Application) SetPanel3D(panel3D gui.IPanel) {
  197. app.renderer.SetGuiPanel3D(panel3D)
  198. }
  199. // Panel3D returns the current gui panel where the 3D scene is shown.
  200. func (app *Application) Panel3D() gui.IPanel {
  201. return app.renderer.Panel3D()
  202. }
  203. // CameraPersp returns the application perspective camera
  204. func (app *Application) CameraPersp() *camera.Perspective {
  205. return app.camPersp
  206. }
  207. // CameraOrtho returns the application orthographic camera
  208. func (app *Application) CameraOrtho() *camera.Orthographic {
  209. return app.camOrtho
  210. }
  211. // Camera returns the current application camera
  212. func (app *Application) Camera() camera.ICamera {
  213. return app.camera
  214. }
  215. // SetCamera sets the current application camera
  216. func (app *Application) SetCamera(cam camera.ICamera) {
  217. app.camera = cam
  218. }
  219. // Orbit returns the current camera orbit control
  220. func (app *Application) Orbit() *control.OrbitControl {
  221. return app.orbit
  222. }
  223. // SetOrbit sets the camera orbit control
  224. func (app *Application) SetOrbit(oc *control.OrbitControl) {
  225. app.orbit = oc
  226. }
  227. // FrameRater returns the FrameRater object
  228. func (app *Application) FrameRater() *FrameRater {
  229. return app.frameRater
  230. }
  231. func (app *Application) FrameCount() uint64 {
  232. return app.frameCount
  233. }
  234. func (app *Application) FrameDelta() time.Duration {
  235. return app.frameDelta
  236. }
  237. func (app *Application) FrameDeltaSeconds() float32 {
  238. return float32(app.frameDelta.Seconds())
  239. }
  240. func (app *Application) RunTime() time.Duration {
  241. return time.Now().Sub(app.startTime)
  242. }
  243. func (app *Application) RunSeconds() float32 {
  244. return float32(time.Now().Sub(app.startTime).Seconds())
  245. }
  246. func (app *Application) Renderer() *renderer.Renderer {
  247. return app.renderer
  248. }
  249. func (app *Application) AudioSupport() bool {
  250. return app.audio
  251. }
  252. func (app *Application) VorbisSupport() bool {
  253. return app.vorbis
  254. }
  255. // SetCpuProfile must be called before Run() and sets the file name for cpu profiling.
  256. // If set the cpu profiling starts before running the render loop and continues
  257. // till the end of the application.
  258. func (app *Application) SetCpuProfile(fname string) {
  259. app.cpuProfile = fname
  260. }
  261. // Runs runs the application render loop
  262. func (app *Application) Run() error {
  263. // Set swap interval
  264. if app.swapInterval >= 0 {
  265. app.win.SwapInterval(app.swapInterval)
  266. app.log.Debug("Swap interval set to:%v", app.swapInterval)
  267. }
  268. // Start profiling if requested
  269. if app.cpuProfile != "" {
  270. f, err := os.Create(app.cpuProfile)
  271. if err != nil {
  272. return err
  273. }
  274. err = pprof.StartCPUProfile(f)
  275. if err != nil {
  276. return err
  277. }
  278. app.log.Info("Started writing CPU profile to:%s", app.cpuProfile)
  279. defer pprof.StopCPUProfile()
  280. }
  281. app.startTime = time.Now()
  282. app.frameTime = time.Now()
  283. // Render loop
  284. for !app.win.ShouldClose() {
  285. // Starts measuring this frame
  286. app.frameRater.Start()
  287. // Updates frame start and time delta in context
  288. now := time.Now()
  289. app.frameDelta = now.Sub(app.frameTime)
  290. app.frameTime = now
  291. // Process root panel timers
  292. if app.Gui() != nil {
  293. app.Gui().TimerManager.ProcessTimers()
  294. }
  295. // Process application timers
  296. app.ProcessTimers()
  297. // Dispatch before render event
  298. app.Dispatch(OnBeforeRender, nil)
  299. // Renders the current scene and/or gui
  300. rendered, err := app.renderer.Render(app.camera)
  301. if err != nil {
  302. return err
  303. }
  304. // Poll input events and process them
  305. app.win.PollEvents()
  306. if rendered {
  307. app.win.SwapBuffers()
  308. }
  309. // Dispatch after render event
  310. app.Dispatch(OnAfterRender, nil)
  311. // Controls the frame rate and updates the FPS for the user
  312. app.frameRater.Wait()
  313. app.frameCount++
  314. }
  315. return nil
  316. }
  317. // Quit ends the application
  318. func (app *Application) Quit() {
  319. app.win.SetShouldClose(true)
  320. }
  321. // OnWindowResize is default handler for window resize events.
  322. func (app *Application) OnWindowResize(evname string, ev interface{}) {
  323. // Get window size and sets the viewport to the same size
  324. width, height := app.win.GetSize()
  325. app.gl.Viewport(0, 0, int32(width), int32(height))
  326. // Sets perspective camera aspect ratio
  327. aspect := float32(width) / float32(height)
  328. app.camPersp.SetAspect(aspect)
  329. // Sets the size of GUI root panel size to the size of the screen
  330. if app.guiroot != nil {
  331. app.guiroot.SetSize(float32(width), float32(height))
  332. }
  333. }
  334. // LoadAudioLibs try to load audio libraries
  335. func (app *Application) LoadAudioLibs() error {
  336. // Try to load OpenAL
  337. err := al.Load()
  338. if err != nil {
  339. return err
  340. }
  341. // Opens default audio device
  342. app.audioDev, err = al.OpenDevice("")
  343. if app.audioDev == nil {
  344. return fmt.Errorf("Error: %s opening OpenAL default device", err)
  345. }
  346. // Checks for OpenAL effects extension support
  347. if al.IsExtensionPresent("ALC_EXT_EFX") {
  348. app.audioEFX = true
  349. }
  350. // Creates audio context with auxiliary sends
  351. var attribs []int
  352. if app.audioEFX {
  353. attribs = []int{al.MAX_AUXILIARY_SENDS, 4}
  354. }
  355. acx, err := al.CreateContext(app.audioDev, attribs)
  356. if err != nil {
  357. return fmt.Errorf("Error creating audio context:%s", err)
  358. }
  359. // Makes the context the current one
  360. err = al.MakeContextCurrent(acx)
  361. if err != nil {
  362. return fmt.Errorf("Error setting audio context current:%s", err)
  363. }
  364. app.audio = true
  365. app.log.Info("%s version: %s", al.GetString(al.Vendor), al.GetString(al.Version))
  366. // Ogg Vorbis support
  367. err = ov.Load()
  368. if err == nil {
  369. app.vorbis = true
  370. vorbis.Load()
  371. }
  372. return nil
  373. }