main.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. io.stdout:setvbuf("no")
  2. local reboot, events = false
  3. --==Contribution Guide==--
  4. --[[
  5. I did create an events system for liko12, making my work more modular.
  6. Below there is a modified love.run function which implements 4 things:
  7. - Instead of calling love callbacks, it triggers the events with name "love:callback", for ex: "love:mousepressed".
  8. - It contains a small and a nice trick which reloads all the code files (expect main.lua & conf.lua) and reboots liko12 without haveing to restart love.
  9. - When the love.graphics is active (usable) it triggers "love:graphics" event.
  10. - If any "love:quit" event returned true, the quit will be canceled.
  11. About the soft restart:
  12. * To do a soft restart trigger the "love:reboot" event.
  13. * This works by clearing package.loaded expect bit library, then calling love.graphics.reset(), and reseting the events library, and finally restarts love.run from the top (there's an extra while loop you can see).
  14. * In case you changed something in main.lua or conf.lua then you can do a hard restart by calling love.event.quit("restart")
  15. * In DiskOS you can run 'reboot' command to do a soft reboot, or 'reboot hard' to do a hard one (by restarting love).
  16. I don't think anyone would want to edit anything in this file.
  17. ==Contributers to this file==
  18. (Add your name when contributing to this file)
  19. - Rami Sabbagh (RamiLego4Game)
  20. ]]
  21. love.setDeprecationOutput(false) --Disable desprecation output temporary (Until the 11.0 update is done)
  22. local package_exceptions = {
  23. "bit", "ffi", "ssl.core", "ssl.context", "ssl.x209", "ssl", "https", "socket.http", "ltn12", "mime", "socket.smtp", "socket", "socket.url"
  24. }
  25. for k,v in ipairs(package_exceptions) do package_exceptions[v] = k end
  26. love.filesystem.load("Engine/errhand.lua")() --Apply the custom error handler.
  27. --Internal Callbacks--
  28. function love.load(args)
  29. love.filesystem.load("BIOS/init.lua")() --Initialize the BIOS.
  30. events:trigger("love:load")
  31. end
  32. function love.run(arg)
  33. local function runReset()
  34. events = require("Engine.events")
  35. events:register("love:reboot",function(args) --Code can trigger this event to do a soft restart.
  36. reboot = args or {}
  37. end)
  38. if love.math then
  39. love.math.setRandomSeed(os.time())
  40. end
  41. if love.load then love.load(reboot or arg) end reboot = false
  42. -- We don't want the first frame's dt to include time taken by love.load.
  43. if love.timer then love.timer.step() end
  44. local dt = 0
  45. end
  46. runReset() --Reset for the first time
  47. return function()
  48. -- Process events.
  49. if love.event then
  50. local canvas = love.graphics.getCanvas()
  51. if canvas then love.graphics.setCanvas() end
  52. love.event.pump()
  53. if canvas then love.graphics.setCanvas{canvas,stencil=true} end
  54. for name, a,b,c,d,e,f in love.event.poll() do
  55. if name == "quit" then
  56. local r = events:trigger("love:quit")
  57. --If any event returns true the quit will be cancelled
  58. for k,v in pairs(r) do
  59. if v and v[1] then r = nil break end
  60. end
  61. if r then return a or 0 end
  62. else
  63. events:trigger("love:"..name,a,b,c,d,e,f)
  64. end
  65. end
  66. end
  67. -- Update dt, as we'll be passing it to update
  68. if love.timer then
  69. love.timer.step()
  70. dt = love.timer.getDelta()
  71. end
  72. -- Call update and draw
  73. events:trigger("love:update",dt) -- will pass 0 if love.timer is disabled
  74. if love.graphics and love.graphics.isActive() then
  75. events:trigger("love:graphics")
  76. end
  77. if love.timer then love.timer.sleep(0.001) end
  78. if reboot then
  79. for k,v in pairs(package.loaded) do
  80. if not package_exceptions[k] then package.loaded[k] = nil end
  81. end--Reset the required packages
  82. love.graphics.reset() --Reset the GPU
  83. events = nil --Must undefine this.
  84. runReset() --Reset
  85. end
  86. end
  87. end