main.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. if not love.filesystem.exists("/data/") then love.filesystem.createDirectory("/data/") end
  71. if not love.filesystem.exists("/data/demos/") then
  72. love.filesystem.createDirectory("/data/demos/")
  73. for k, demo in ipairs(love.filesystem.getDirectoryItems("/demos/")) do
  74. api.fs.write("/demos/"..demo,love.filesystem.read("/demos/"..demo))
  75. end
  76. end
  77. api.loadDefaultCursors()
  78. _ScreenCanvas = love.graphics.newCanvas(192,128)
  79. _ScreenCanvas:setFilter("nearest")
  80. _GifCanvas = love.graphics.newCanvas(192*_GIFSCALE,128*_GIFSCALE)
  81. _GifCanvas:setFilter("nearest")
  82. love.graphics.clear(0,0,0,255)
  83. love.graphics.setCanvas(_ScreenCanvas)
  84. love.graphics.clear(0,0,0,255)
  85. love.graphics.translate(_ScreenTX,_ScreenTY)
  86. love.resize(love.graphics.getDimensions())
  87. love.graphics.setLineStyle("rough")
  88. love.graphics.setLineJoin("miter")
  89. love.graphics.setFont(_Font)
  90. api.clear() --Clear the canvas for the first time
  91. api.stroke(1)
  92. if debugrun then
  93. require("debugrun")
  94. else
  95. require("autorun")
  96. end
  97. _auto_init()
  98. end
  99. function love.resize(w,h)
  100. _ScreenWidth, _ScreenHeight = w, h
  101. local TSX, TSY = w/192, h/128 --TestScaleX, TestScaleY
  102. if TSX < TSY then
  103. _ScreenScaleX, _ScreenScaleY, _ScreenScale = w/192, w/192, w/192
  104. _ScreenX, _ScreenY = 0, (_ScreenHeight-128*_ScreenScaleY)/2
  105. else
  106. _ScreenScaleX, _ScreenScaleY, _ScreenScale = h/128, h/128, h/128
  107. _ScreenX, _ScreenY = (_ScreenWidth-192*_ScreenScaleX)/2, 0
  108. end
  109. api.clearCursorsCache()
  110. _ShouldDraw = true
  111. end
  112. function love.update(dt)
  113. local mx, my = _ScreenToLiko(love.mouse.getPosition())
  114. love.window.setTitle(_ScreenTitle.." FPS: "..love.timer.getFPS().." ShouldDraw: "..(_ForceDraw and "FORCE" or (_ShouldDraw and "Yes" or "No")).." MX, MY: "..mx..","..my)
  115. _auto_update(dt)
  116. end
  117. function love.visible(v)
  118. _ForceDraw = not v
  119. _ShouldDraw = v
  120. end
  121. function love.focus(f)
  122. _ForceDraw = not f
  123. _ShouldDraw = f
  124. end
  125. function love.run()
  126. if love.math then
  127. love.math.setRandomSeed(os.time())
  128. end
  129. if love.load then love.load(arg) end
  130. -- We don't want the first frame's dt to include time taken by love.load.
  131. if love.timer then love.timer.step() end
  132. local dt = 0
  133. -- Main loop time.
  134. while true do
  135. -- Process events.
  136. if love.event then
  137. love.event.pump()
  138. for name, a,b,c,d,e,f in love.event.poll() do
  139. if name == "quit" then
  140. if not love.quit or not love.quit() then
  141. return a
  142. end
  143. end
  144. love.handlers[name](a,b,c,d,e,f)
  145. end
  146. end
  147. -- Update dt, as we'll be passing it to update
  148. if love.timer then
  149. love.timer.step()
  150. dt = love.timer.getDelta()
  151. end
  152. if _REBOOT then break end
  153. -- Call update and draw
  154. if love.update then
  155. love.update(dt) -- will pass 0 if love.timer is disabled
  156. if _GIFREC then
  157. _GIFTIMER = _GIFTIMER + dt
  158. if _GIFTIMER >= _GIFINVERTAL then
  159. _GIFTIMER = _GIFTIMER - _GIFINVERTAL
  160. api.pushColor()
  161. love.graphics.setCanvas()
  162. love.graphics.origin()
  163. love.graphics.setColor(255,255,255,255)
  164. love.graphics.setCanvas(_GifCanvas)
  165. love.graphics.clear(0,0,0,255)
  166. love.graphics.draw(_ScreenCanvas, 0, 0, 0, _GIFSCALE, _GIFSCALE)
  167. 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
  168. local curx, cury = _ScreenToLiko(love.mouse.getPosition())
  169. love.graphics.draw(cur,(curx-curhx)*_GIFSCALE,(cury-curhy)*_GIFSCALE,0, _GIFSCALE, _GIFSCALE)
  170. _GIFREC:frame(_GifCanvas:newImageData())
  171. love.graphics.setCanvas()
  172. love.graphics.setCanvas(_ScreenCanvas)
  173. love.graphics.translate(_ScreenTX,_ScreenTY)
  174. api.popColor()
  175. end
  176. end
  177. end
  178. if love.graphics and love.graphics.isActive() and (_ShouldDraw or _ForceDraw) then
  179. love.graphics.setCanvas()
  180. love.graphics.origin()
  181. api.pushColor()
  182. love.graphics.setColor(255,255,255,255)
  183. love.graphics.clear(0,0,0,255)
  184. love.graphics.draw(_ScreenCanvas, _ScreenX,_ScreenY, 0, _ScreenScaleX,_ScreenScaleY)
  185. --love.graphics.api.points(1,1,_ScreenWidth,_ScreenHeight)
  186. love.graphics.present()
  187. love.graphics.setCanvas(_ScreenCanvas)
  188. love.graphics.translate(_ScreenTX,_ScreenTY)
  189. _ShouldDraw = false
  190. api.popColor()
  191. end
  192. if love.timer then love.timer.sleep(0.001) end
  193. end
  194. if _REBOOT then
  195. _REBOOT = false
  196. --[[if _GIFREC then
  197. _GIFREC:close()
  198. print("Saved gif recording to: ".._GIFREC.filename)
  199. _GIFREC = nil
  200. end]]
  201. for k,v in pairs(package.loaded) do
  202. package.loaded[k] = nil
  203. end
  204. love.filesystem.load("conf.lua")()
  205. love.filesystem.load("main.lua")()
  206. love.run()
  207. end
  208. end