a lot of changes (sorry, shouldve made smaller commits)
This commit is contained in:
parent
0ef5a26f95
commit
350c656f71
|
@ -1,5 +1,3 @@
|
|||
print("love start", test)
|
||||
|
||||
function love.load()
|
||||
counter = 0
|
||||
end
|
||||
|
@ -9,7 +7,11 @@ function love.update(dt)
|
|||
end
|
||||
|
||||
function love.draw()
|
||||
love.graphics.print("hello!!\n"..counter, 20, 20)
|
||||
love.graphics.print("hello!!\n"..counter.."\n"..love.timer.getTime(), 20, 20)
|
||||
love.graphics.rectangle("fill", 20, 100, 30, 40)
|
||||
love.graphics.rectangle("stroke", 60, 150, 60, 50)
|
||||
end
|
||||
|
||||
print("love end")
|
||||
function love.handlers.keypressed(key, scan, repeated)
|
||||
print(key, scan, repeated)
|
||||
end
|
||||
|
|
296
main.lua
296
main.lua
|
@ -1,3 +1,299 @@
|
|||
local oldprint = print
|
||||
local function print(...)
|
||||
oldprint("[Infatuated] "..string.format(...))
|
||||
end
|
||||
|
||||
infatuated = {}
|
||||
infatuated.version = "11.3"
|
||||
infatuated.conf = {
|
||||
identity = nil,
|
||||
appendidentity = false,
|
||||
version = infatuated.version,
|
||||
console = false,
|
||||
accelerometerjoystick = true,
|
||||
externalstorage = false,
|
||||
gammacorrect = false,
|
||||
audio = {
|
||||
mic = false,
|
||||
mixwithsystem = true,
|
||||
},
|
||||
window = {
|
||||
title = "Untitled",
|
||||
icon = nil,
|
||||
width = 800,
|
||||
height = 600,
|
||||
borderless = false,
|
||||
resizable = false,
|
||||
minwidth = 1,
|
||||
minheight = 1,
|
||||
fullscreen = false,
|
||||
fullscreentype = "desktop",
|
||||
vsync = 1,
|
||||
msaa = 0,
|
||||
depth = nil,
|
||||
stencil = nil,
|
||||
display = 1,
|
||||
highdpi = false,
|
||||
usedpiscale = true,
|
||||
x = nil,
|
||||
y = nil
|
||||
},
|
||||
modules = {
|
||||
audio = true,
|
||||
data = true,
|
||||
event = true,
|
||||
font = true,
|
||||
graphics = true,
|
||||
image = true,
|
||||
joystick = true,
|
||||
keyboard = true,
|
||||
math = true,
|
||||
mouse = true,
|
||||
physics = true,
|
||||
sound = true,
|
||||
system = true,
|
||||
thread = true,
|
||||
timer = true,
|
||||
touch = true,
|
||||
video = true,
|
||||
window = true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
js = require "js"
|
||||
window = js.global
|
||||
document = window.document
|
||||
canvas = document:getElementById("infatuated-canvas")
|
||||
ctx = canvas:getContext("2d")
|
||||
--infatuated.ctx = ctx
|
||||
|
||||
local function errorprint(...)
|
||||
window.console:error("[Infatuated] "..string.format(...))
|
||||
end
|
||||
|
||||
local oldrequire = require
|
||||
infatuated.require = oldrequire
|
||||
function require(path)
|
||||
assert(type(path) == "string", "bad argument #1 to require")
|
||||
oldrequire("game."..path)
|
||||
end
|
||||
|
||||
function infatuated.stop(status)
|
||||
print("Stopping with code %s", status or "null")
|
||||
infatuated.stopDue = status
|
||||
end
|
||||
|
||||
function infatuated.crash(errmsg)
|
||||
errorprint("%s\n%s", errmsg, debug.traceback())
|
||||
if infatuated.crashed then
|
||||
print("Error handler crashed; stopping execution entirely")
|
||||
infatuated.loopGuest = function() end
|
||||
else
|
||||
infatuated.crashed = true
|
||||
infatuated.loopGuest = love.errorhandler(errmsg)
|
||||
end
|
||||
end
|
||||
|
||||
function infatuated.parseColor(r, g, b, a)
|
||||
if type(r) == "table" then
|
||||
return infatuated.parseColor(r[1], r[2], r[3], r[4])
|
||||
elseif type(r) == "number" then
|
||||
a = a or 1
|
||||
assert(type(g) == "number", "bad argument #2 to parseColor")
|
||||
assert(type(b) == "number", "bad argument #3 to parseColor")
|
||||
assert(type(a) == "number", "bad argument #4 to parseColor")
|
||||
return {r, g, b, a}, string.format("rgba(%f, %f, %f, %f)", r*255, g*255, b*255, a)
|
||||
else
|
||||
error("bad argument #1 to parseColor")
|
||||
end
|
||||
end
|
||||
|
||||
love = {}
|
||||
|
||||
function love.conf() end
|
||||
function love.errorhandler(msg)
|
||||
msg = tostring(msg)
|
||||
oldprint((debug.traceback("Error: "..msg, 1+(layer or 1)):gsub("\n[^\n]+$", "")))
|
||||
--if not love.window or not love.graphics or not love.event then return end -- SOMEHOW THIS IS AN "ATTEMPT TO INDEX A NIL VALUE". WTF????
|
||||
if love.mouse then
|
||||
love.mouse.setVisible(true)
|
||||
love.mouse.setGrabbed(false)
|
||||
love.mouse.setRelativeMode(false)
|
||||
love.mouse.setCursor()
|
||||
end
|
||||
if love.audio then love.audio.stop() end
|
||||
|
||||
love.graphics.reset()
|
||||
love.graphics.setColor(1, 1, 1, 1)
|
||||
love.graphics.setBackgroundColor(89/255, 157/255, 220/255)
|
||||
|
||||
return function()
|
||||
love.graphics.clear()
|
||||
love.graphics.print("An error was detected. Check console for details.", 70, 70)
|
||||
love.graphics.present()
|
||||
end
|
||||
end
|
||||
love.errhand = love.errorhandler
|
||||
function love.run()
|
||||
if love.load then love.load() end
|
||||
|
||||
love.graphics.setColor(1, 1, 1, 1)
|
||||
love.graphics.setBackgroundColor(0, 0, 0, 1)
|
||||
love.graphics.setLineWidth(1)
|
||||
|
||||
infatuated.startTime = window.performance:now()
|
||||
infatuated.dt1 = infatuated.startTime
|
||||
|
||||
return function()
|
||||
if infatuated.stopDue then return infatuated.stopDue end
|
||||
if love.update then love.update(infatuated.dt) end
|
||||
if love.graphics and love.graphics.isActive() then
|
||||
love.graphics.origin()
|
||||
love.graphics.clear()
|
||||
if love.draw then love.draw() end
|
||||
love.graphics.present()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
love.handlers = {}
|
||||
-- TODO: attach the shorthand variants
|
||||
function love.handlers.keypressed() end
|
||||
document.body:addEventListener("keydown", function(self, event)
|
||||
if love.handlers.keypressed and not infatuated.stopDue then
|
||||
-- TODO: properly implement scancodes
|
||||
love.handlers.keypressed(event.key, event.key, event["repeat"])
|
||||
--love.handlers.keypressed(event.key, event.code, event["repeat"])
|
||||
end
|
||||
end)
|
||||
|
||||
love.event = {}
|
||||
function love.event.quit(status)
|
||||
status = status or 0
|
||||
local type = type(status)
|
||||
if type == "number" then
|
||||
infatuated.stop(status)
|
||||
elseif status == "restart" then
|
||||
infatuated.stop()
|
||||
window.location:reload()
|
||||
else
|
||||
error("bad argument #1 to quit")
|
||||
end
|
||||
end
|
||||
|
||||
love.graphics = {}
|
||||
function love.graphics.reset()
|
||||
love.graphics.setColor(1, 1, 1, 1)
|
||||
love.graphics.setBackgroundColor(0, 0, 0, 1)
|
||||
love.graphics.origin()
|
||||
end
|
||||
function love.graphics.clear(r, g, b, a)
|
||||
if r then
|
||||
local a, b = infatuated.parseColor(r, g, b, a)
|
||||
ctx.fillStyle = b
|
||||
else
|
||||
ctx.fillStyle = infatuated.backgroundString
|
||||
end
|
||||
ctx:fillRect(0, 0, 800, 600)
|
||||
end
|
||||
function love.graphics.origin(x, y)
|
||||
x, y = x or 0, y or 0
|
||||
assert(type(x) == "number", "bad argument #1 to origin")
|
||||
assert(type(y) == "number", "bad argument #2 to origin")
|
||||
infatuated.dx, infatuated.dy = x, y
|
||||
ctx:translate(x, y)
|
||||
end
|
||||
function love.graphics.present() end
|
||||
function love.graphics.isActive()
|
||||
return true
|
||||
end
|
||||
function love.graphics.getColor()
|
||||
local c
|
||||
c = infatuated.color
|
||||
return c[1], c[2], c[3], c[4]
|
||||
end
|
||||
function love.graphics.setColor(r, g, b, a)
|
||||
infatuated.color, infatuated.colorString = infatuated.parseColor(r, g, b, a)
|
||||
end
|
||||
function love.graphics.getBackgroundColor()
|
||||
local c
|
||||
c = infatuated.background
|
||||
return c[1], c[2], c[3], c[4]
|
||||
end
|
||||
function love.graphics.setBackgroundColor(r, g, b, a)
|
||||
infatuated.background, infatuated.backgroundString = infatuated.parseColor(r, g, b, a)
|
||||
end
|
||||
function love.graphics.getLineWidth()
|
||||
return infatuated.lineWidth
|
||||
end
|
||||
function love.graphics.setLineWidth(width)
|
||||
assert(type(width) == "number", "bad argument #1 to setLineWidth")
|
||||
infatuated.lineWidth = width
|
||||
end
|
||||
function love.graphics.print(text, x, y, r, sx, sy, ox, oy, kx, ky)
|
||||
-- TODO: Add newline support and compensate for height of text
|
||||
ctx.fillStyle = infatuated.colorString
|
||||
ctx.font = "16px sans-serif"
|
||||
ctx:fillText(text, x, y)
|
||||
end
|
||||
function love.graphics.rectangle(mode, x, y, w, h)
|
||||
assert(type(x) == "number", "bad argument #2 to rectangle")
|
||||
assert(type(y) == "number", "bad argument #3 to rectangle")
|
||||
assert(type(w) == "number", "bad argument #4 to rectangle")
|
||||
assert(type(h) == "number", "bad argument #5 to rectangle")
|
||||
if mode == "fill" then
|
||||
ctx.fillStyle = infatuated.colorString
|
||||
ctx:fillRect(x, y, w, h)
|
||||
elseif mode == "stroke" then
|
||||
ctx.strokeStyle = infatuated.colorString
|
||||
ctx.lineWidth = infatuated.lineWidth
|
||||
ctx:strokeRect(x+0.5, y+0.5, w, h)
|
||||
else
|
||||
error("bad argument #1 to rectangle")
|
||||
end
|
||||
end
|
||||
|
||||
love.timer = {}
|
||||
function love.timer.getTime()
|
||||
return (window.performance:now()-infatuated.startTime)/1000
|
||||
end
|
||||
|
||||
love.run()
|
||||
|
||||
print("Importing conf")
|
||||
|
||||
local status, errmsg = pcall(oldrequire, "game.conf")
|
||||
if not status and not errmsg:find("^module 'game.conf' not found:") then
|
||||
infatuated.crash(errmsg)
|
||||
end
|
||||
local status, errmsg = pcall(love.conf, infatuated.conf)
|
||||
if not status then infatuated.crash(errmsg) end
|
||||
|
||||
print("Importing main")
|
||||
|
||||
local status, errmsg = pcall(oldrequire, "game.main")
|
||||
if not status then infatuated.crash(errmsg) end
|
||||
local status, errmsg = pcall(love.run)
|
||||
if not status then
|
||||
infatuated.crash(errmsg)
|
||||
else
|
||||
infatuated.loopGuest = errmsg
|
||||
end
|
||||
|
||||
print("Starting event loop")
|
||||
|
||||
function infatuated.loopMaster()
|
||||
local dt2 = window.performance:now()
|
||||
infatuated.dt = (dt2-infatuated.dt1)/1000
|
||||
infatuated.dt1 = dt2
|
||||
local status, errmsg = pcall(infatuated.loopGuest)
|
||||
if not status then
|
||||
infatuated.crash(errmsg)
|
||||
elseif errmsg then
|
||||
print("Exited with code %d", errmsg or -1)
|
||||
return
|
||||
end
|
||||
window:requestAnimationFrame(infatuated.loopMaster)
|
||||
end
|
||||
window:requestAnimationFrame(infatuated.loopMaster)
|
||||
|
|
Loading…
Reference in New Issue