run.lua 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. return {
  2. tag = 'callbacks',
  3. summary = 'The main entry point.',
  4. description = [[
  5. This callback is the main entry point for a LÖVR program. It calls `lovr.load` and returns a
  6. function that will be called every frame.
  7. ]],
  8. arguments = {},
  9. returns = {
  10. {
  11. name = 'loop',
  12. type = 'function',
  13. arguments = {},
  14. returns = {
  15. {
  16. name = 'result',
  17. type = '*'
  18. }
  19. },
  20. description = 'The main loop function.'
  21. }
  22. },
  23. notes = [[
  24. The main loop function can return one of the following values:
  25. - Returning `nil` will keep the main loop running.
  26. - Returning the string 'restart' plus an optional value will restart LÖVR. The value can be
  27. accessed in the `restart` key of the `arg` global.
  28. - Returning a number will exit LÖVR using the number as the exit code (0 means success).
  29. Care should be taken when overriding this callback. For example, if the main loop does not call
  30. `lovr.system.pollEvents` then the OS will think LÖVR is unresponsive, or if the quit event is
  31. not handled then closing the window won't work.
  32. ]],
  33. example = {
  34. description = 'The default `lovr.run`:',
  35. code = [[
  36. function lovr.run()
  37. if lovr.timer then lovr.timer.step() end
  38. if lovr.load then lovr.load(arg) end
  39. return function()
  40. if lovr.system then lovr.system.pollEvents() end
  41. if lovr.event then
  42. for name, a, b, c, d in lovr.event.poll() do
  43. if name == 'restart' then
  44. local cookie = lovr.restart and lovr.restart()
  45. return 'restart', cookie
  46. elseif name == 'quit' and (not lovr.quit or not lovr.quit(a)) then
  47. return a or 0
  48. end
  49. if lovr.handlers[name] then lovr.handlers[name](a, b, c, d) end
  50. end
  51. end
  52. local dt = 0
  53. if lovr.timer then dt = lovr.timer.step() end
  54. if lovr.headset then dt = lovr.headset.update() end
  55. if lovr.update then lovr.update(dt) end
  56. if lovr.graphics then
  57. local headset = lovr.headset and lovr.headset.getPass()
  58. if headset and (not lovr.draw or lovr.draw(headset)) then headset = nil end
  59. local window = lovr.graphics.getWindowPass()
  60. if window and (not lovr.mirror or lovr.mirror(window)) then window = nil end
  61. lovr.graphics.submit(headset, window)
  62. lovr.graphics.present()
  63. end
  64. if lovr.headset then lovr.headset.submit() end
  65. if lovr.math then lovr.math.drain() end
  66. end
  67. end
  68. ]],
  69. },
  70. related = {
  71. 'lovr.load',
  72. 'lovr.quit'
  73. }
  74. }