errhand.lua 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. local function error_printer(msg, layer)
  2. print((debug.traceback("Error: " .. tostring(msg), 1+(layer or 1)):gsub("\n[^\n]+$", "")))
  3. end
  4. function love.errhand(msg)
  5. msg = tostring(msg)
  6. error_printer(msg, 2)
  7. if not love.window or not love.graphics or not love.event then
  8. return
  9. end
  10. if not love.graphics.isCreated() or not love.window.isOpen() then
  11. local success, status = pcall(love.window.setMode, 192*3, 128*3)
  12. if not success or not status then
  13. return
  14. end
  15. end
  16. -- Reset state.
  17. if love.mouse then
  18. love.mouse.setVisible(true)
  19. love.mouse.setGrabbed(false)
  20. love.mouse.setRelativeMode(false)
  21. end
  22. if love.joystick then
  23. -- Stop all joystick vibrations.
  24. for i,v in ipairs(love.joystick.getJoysticks()) do
  25. v:setVibration()
  26. end
  27. end
  28. if love.audio then love.audio.stop() end
  29. love.graphics.reset()
  30. local font = love.graphics.setNewFont(math.floor(love.window.toPixels(14)))
  31. love.graphics.setBackgroundColor(89, 157, 220)
  32. love.graphics.setColor(255, 255, 255, 255)
  33. local trace = debug.traceback()
  34. love.graphics.clear(love.graphics.getBackgroundColor())
  35. love.graphics.origin()
  36. local err = {}
  37. table.insert(err, "Error\n")
  38. table.insert(err, msg.."\n\n")
  39. for l in string.gmatch(trace, "(.-)\n") do
  40. if not string.match(l, "boot.lua") then
  41. l = string.gsub(l, "stack traceback:", "Traceback\n")
  42. table.insert(err, l)
  43. end
  44. end
  45. local p = table.concat(err, "\n")
  46. p = string.gsub(p, "\t", "")
  47. p = string.gsub(p, "%[string \"(.-)\"%]", "%1")
  48. local function draw()
  49. local pos = love.window.toPixels(70)
  50. love.graphics.clear(love.graphics.getBackgroundColor())
  51. love.graphics.printf(p, pos, pos, love.graphics.getWidth() - pos)
  52. love.graphics.present()
  53. end
  54. while true do
  55. love.event.pump()
  56. for e, a, b, c in love.event.poll() do
  57. if e == "quit" then
  58. return
  59. elseif e == "keypressed" and a == "escape" then
  60. return
  61. elseif e == "touchpressed" then
  62. local name = love.window.getTitle()
  63. if #name == 0 or name == "Untitled" then name = "Game" end
  64. local buttons = {"OK", "Cancel"}
  65. local pressed = love.window.showMessageBox("Quit "..name.."?", "", buttons)
  66. if pressed == 1 then
  67. return
  68. end
  69. end
  70. end
  71. draw()
  72. if love.timer then
  73. love.timer.sleep(0.1)
  74. end
  75. end
  76. end