main.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. io.stdout:setvbuf("no")
  2. love.graphics.setDefaultFilter("nearest")
  3. api = require("api") --I STILL WANT IT AS A GLOBAL !
  4. local utf8 = require("utf8")
  5. local giflib = require("gif")
  6. local debugrun = false
  7. function love.mousepressed(x,y,button,istouch)
  8. local x,y = _ScreenToLiko(x,y) if x < 0 or x > 192 or y < 0 or y > 128 then return end
  9. _auto_mpress(x,y,button,istouch)
  10. end
  11. function love.mousemoved(x,y,dx,dy,istouch)
  12. local x, y = _ScreenToLiko(x,y) if x < 0 or x > 192 or y < 0 or y > 128 then return end
  13. _auto_mmove(x,y,dx,dy,istouch)
  14. end
  15. function love.mousereleased(x,y,button,istouch)
  16. local x, y = _ScreenToLiko(x,y) if x < 0 or x > 192 or y < 0 or y > 128 then return end
  17. _auto_mrelease(x,y,button,istouch)
  18. end
  19. function love.wheelmoved(x,y)
  20. _auto_mmove(x,y,0,0,false,true) --Mouse button 0 is the wheel
  21. end
  22. function love.touchpressed(id,x,y,dx,dy,pressure)
  23. local x, y = _ScreenToLiko(x,y) if x < 0 or x > 192 or y < 0 or y > 128 then return end
  24. _auto_tpress(id,x,y,pressure)
  25. end
  26. function love.touchmoved(id,x,y,dx,dy,pressure)
  27. local x, y = _ScreenToLiko(x,y) if x < 0 or x > 192 or y < 0 or y > 128 then return end
  28. _auto_tmove(id,x,y,pressure)
  29. end
  30. function love.touchreleased(id,x,y,dx,dy,pressure)
  31. local x, y = _ScreenToLiko(x,y) if x < 0 or x > 192 or y < 0 or y > 128 then return end
  32. _auto_trelease(id,x,y,pressure)
  33. end
  34. function love.keypressed(key,scancode,isrepeat)
  35. if key == "f8" or key == "f3" then
  36. if not _GIFREC then
  37. local err
  38. _GIFREC, err = giflib.new("data/LIKO12-"..os.time()..".gif")
  39. if not _GIFREC then
  40. print("Failed to start recording: "..err)
  41. else
  42. print("Started recording ...") _GIFTIMER = _GIFINVERTAL --To flash the first frame
  43. end
  44. else
  45. print("Recording already in progress")
  46. end
  47. elseif key == "f4" or key == "f9" then
  48. if _GIFREC then
  49. _GIFREC:close()
  50. print("Saved gif recording to: ".._GIFREC.filename)
  51. _GIFREC = nil
  52. else
  53. print("No active gif recording !")
  54. end
  55. end
  56. _auto_kpress(key,scancode,isrepeat)
  57. end
  58. function love.keyreleased(key,scancode)
  59. _auto_krelease(key,scancode)
  60. end
  61. function love.textinput(text)
  62. local text_escaped = text:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
  63. if #text == 1 and _FontChars:find(text_escaped) then
  64. _auto_tinput(text)
  65. end
  66. end
  67. --Internal Callbacks--
  68. function love.load()
  69. --love.keyboard.setTextInput(true)
  70. love.graphics.setDefaultFilter("nearest","nearest")
  71. if not love.filesystem.exists("/data/") then love.filesystem.createDirectory("/data/") end
  72. if not love.filesystem.exists("/data/demos/") then
  73. love.filesystem.createDirectory("/data/demos/")
  74. for k, demo in ipairs(love.filesystem.getDirectoryItems("/demos/")) do
  75. api.fs.write("/demos/"..demo,love.filesystem.read("/demos/"..demo))
  76. end
  77. end
  78. api.loadDefaultCursors()
  79. _ScreenCanvas = love.graphics.newCanvas(192,128)
  80. _ScreenCanvas:setFilter("nearest")
  81. _GifCanvas = love.graphics.newCanvas(192*_GIFSCALE,128*_GIFSCALE)
  82. _GifCanvas:setFilter("nearest")
  83. love.graphics.clear(0,0,0,255)
  84. love.graphics.setCanvas(_ScreenCanvas)
  85. love.graphics.clear(0,0,0,255)
  86. love.graphics.translate(_ScreenTX,_ScreenTY)
  87. love.resize(love.graphics.getDimensions())
  88. love.graphics.setLineStyle("rough")
  89. love.graphics.setLineJoin("miter")
  90. love.graphics.setFont(_Font)
  91. api.clear() --Clear the canvas for the first time
  92. api.stroke(1)
  93. if debugrun then
  94. require("debugrun")
  95. else
  96. require("autorun")
  97. end
  98. _auto_init()
  99. end
  100. function love.resize(w,h)
  101. _ScreenWidth, _ScreenHeight = w, h
  102. local TSX, TSY = w/192, h/128 --TestScaleX, TestScaleY
  103. if TSX < TSY then
  104. _ScreenScaleX, _ScreenScaleY, _ScreenScale = w/192, w/192, w/192
  105. _ScreenX, _ScreenY = 0, (_ScreenHeight-128*_ScreenScaleY)/2
  106. else
  107. _ScreenScaleX, _ScreenScaleY, _ScreenScale = h/128, h/128, h/128
  108. _ScreenX, _ScreenY = (_ScreenWidth-192*_ScreenScaleX)/2, 0
  109. end
  110. api.clearCursorsCache()
  111. _ShouldDraw = true
  112. end
  113. function love.update(dt)
  114. local mx, my = _ScreenToLiko(love.mouse.getPosition())
  115. love.window.setTitle(_ScreenTitle.." FPS: "..love.timer.getFPS().." ShouldDraw: "..(_ForceDraw and "FORCE" or (_ShouldDraw and "Yes" or "No")).." MX, MY: "..mx..","..my)
  116. _auto_update(dt)
  117. end
  118. function love.visible(v)
  119. _ForceDraw = not v
  120. _ShouldDraw = v
  121. end
  122. function love.focus(f)
  123. _ForceDraw = not f
  124. _ShouldDraw = f
  125. end
  126. function love.run()
  127. if love.math then
  128. love.math.setRandomSeed(os.time())
  129. end
  130. if love.load then love.load(arg) end
  131. -- We don't want the first frame's dt to include time taken by love.load.
  132. if love.timer then love.timer.step() end
  133. local dt = 0
  134. -- Main loop time.
  135. while true do
  136. -- Process events.
  137. if love.event then
  138. love.event.pump()
  139. for name, a,b,c,d,e,f in love.event.poll() do
  140. if name == "quit" then
  141. if not love.quit or not love.quit() then
  142. return a
  143. end
  144. end
  145. love.handlers[name](a,b,c,d,e,f)
  146. end
  147. end
  148. -- Update dt, as we'll be passing it to update
  149. if love.timer then
  150. love.timer.step()
  151. dt = love.timer.getDelta()
  152. end
  153. if _REBOOT then break end
  154. -- Call update and draw
  155. if love.update then
  156. love.update(dt) -- will pass 0 if love.timer is disabled
  157. if _GIFREC then
  158. _GIFTIMER = _GIFTIMER + dt
  159. if _GIFTIMER >= _GIFINVERTAL then
  160. _GIFTIMER = _GIFTIMER - _GIFINVERTAL
  161. api.pushColor()
  162. love.graphics.setCanvas()
  163. love.graphics.origin()
  164. love.graphics.setColor(255,255,255,255)
  165. love.graphics.setCanvas(_GifCanvas)
  166. love.graphics.clear(0,0,0,255)
  167. love.graphics.draw(_ScreenCanvas, 0, 0, 0, _GIFSCALE, _GIFSCALE)
  168. local cur, curhx, curhy = api._Cursors[api._CurrentCursor].data:image().image,(api._Cursors[api._CurrentCursor].hotx-1)*_ScreenScale,(api._Cursors[api._CurrentCursor].hoty-1)*_ScreenScale
  169. local curx, cury = _ScreenToLiko(love.mouse.getPosition())
  170. love.graphics.draw(cur,(curx-curhx)*_GIFSCALE,(cury-curhy)*_GIFSCALE,0, _GIFSCALE, _GIFSCALE)
  171. _GIFREC:frame(_GifCanvas:newImageData())
  172. love.graphics.setCanvas()
  173. love.graphics.setCanvas(_ScreenCanvas)
  174. love.graphics.translate(_ScreenTX,_ScreenTY)
  175. api.popColor()
  176. end
  177. end
  178. end
  179. if love.graphics and love.graphics.isActive() and (_ShouldDraw or _ForceDraw) then
  180. love.graphics.setCanvas()
  181. love.graphics.origin()
  182. api.pushColor()
  183. love.graphics.setColor(255,255,255,255)
  184. love.graphics.clear(0,0,0,255)
  185. love.graphics.draw(_ScreenCanvas, _ScreenX,_ScreenY, 0, _ScreenScaleX,_ScreenScaleY)
  186. --love.graphics.api.points(1,1,_ScreenWidth,_ScreenHeight)
  187. love.graphics.present()
  188. love.graphics.setCanvas(_ScreenCanvas)
  189. love.graphics.translate(_ScreenTX,_ScreenTY)
  190. _ShouldDraw = false
  191. api.popColor()
  192. end
  193. if love.timer then love.timer.sleep(0.001) end
  194. end
  195. if _REBOOT then
  196. _REBOOT = false
  197. --[[if _GIFREC then
  198. _GIFREC:close()
  199. print("Saved gif recording to: ".._GIFREC.filename)
  200. _GIFREC = nil
  201. end]]
  202. for k,v in pairs(package.loaded) do
  203. package.loaded[k] = nil
  204. end
  205. love.filesystem.load("conf.lua")()
  206. love.filesystem.load("main.lua")()
  207. love.run()
  208. end
  209. end