main.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. local package_exceptions = {
  22. "bit", "ffi", "ssl.core", "ssl.context", "ssl.x209", "ssl", "https", "socket.http", "ltn12", "mime", "socket.smtp", "socket", "socket.url"
  23. }
  24. for k,v in ipairs(package_exceptions) do package_exceptions[v] = k end
  25. love.filesystem.load("Engine/errhand.lua")() --Apply the custom error handler.
  26. --Internal Callbacks--
  27. function love.load(args)
  28. love.filesystem.load("BIOS/init.lua")() --Initialize the BIOS.
  29. events:trigger("love:load")
  30. end
  31. function love.run(arg)
  32. while true do
  33. events = require("Engine.events")
  34. events:register("love:reboot",function(args) --Code can trigger this event to do a soft restart.
  35. reboot = args or {}
  36. end)
  37. if love.math then
  38. love.math.setRandomSeed(os.time())
  39. end
  40. if love.load then love.load(reboot or arg) end reboot = false
  41. -- We don't want the first frame's dt to include time taken by love.load.
  42. if love.timer then love.timer.step() end
  43. local dt = 0
  44. -- Main loop time.
  45. while true do
  46. -- Process events.
  47. if love.event then
  48. love.event.pump()
  49. for name, a,b,c,d,e,f in love.event.poll() do
  50. if name == "quit" then
  51. local r = events:trigger("love:quit") --If any event returns true the quit will be cancelled
  52. for k,v in pairs(r) do
  53. if v and v[1] then r = nil break end
  54. end
  55. if r then return a end
  56. else
  57. events:trigger("love:"..name,a,b,c,d,e,f)
  58. end
  59. end
  60. end
  61. -- Update dt, as we'll be passing it to update
  62. if love.timer then
  63. love.timer.step()
  64. dt = love.timer.getDelta()
  65. end
  66. -- Call update and draw
  67. events:trigger("love:update",dt) -- will pass 0 if love.timer is disabled
  68. if love.graphics and love.graphics.isActive() then
  69. events:trigger("love:graphics")
  70. end
  71. if love.timer then love.timer.sleep(0.001) end
  72. if reboot then
  73. for k,v in pairs(package.loaded) do
  74. if not package_exceptions[k] then package.loaded[k] = nil end
  75. end--Reset the required packages
  76. love.graphics.reset() --Reset the GPU
  77. events = nil --Must undefine this.
  78. break --Break our game loop
  79. end
  80. end
  81. end
  82. end