123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- --Games runtime API
- local rt = {}
- function rt.loadResources()
- local scripts = fs.getDirectoryItems("C:/Runtime/Resources/")
-
- for id, name in ipairs(scripts) do
- scripts[id] = fs.load("C:/Runtime/Resources/"..name)
- end
-
- return scripts
- end
- function rt.loadGlobals()
- local scripts = fs.getDirectoryItems("C:/Runtime/Globals/")
-
- for id, name in ipairs(scripts) do
- scripts[id] = fs.load("C:/Runtime/Globals/"..name)
- end
-
- return scripts
- end
- function rt.loadGame()
-
- local glob = _FreshGlobals()
- glob._G = glob --Magic ;)
-
- local resources = rt.loadResources()
- local globals = rt.loadGlobals()
-
- --Execute the resources
- for i=1, #resources do
- resources[i](glob)
- end
-
- --Load the lua code
- local chunk, cerr = loadstring(glob._GameCode,"")
-
- if not chunk then
- cerr = tostring(cerr)
-
- if cerr:sub(1,12) == '[string ""]:' then
- cerr = cerr:sub(13,-1)
- end
-
- return false, "Compile ERR :"..cerr
- end
-
- --Set the sandbox
- setfenv(chunk, glob)
-
- --Create the coroutine
- local co = coroutine.create(chunk)
-
- --Execute the globals constructors
- for i=1, #globals do
- globals[i](glob,co)
- end
-
- return glob, co, chunk
- end
- function rt.resetEnvironment()
- pal() palt() cam() clip()
-
- clearEStack()
- clearMatrixStack()
- colorPalette() --Reset the color palette.
- patternFill()
-
- TC.setInput(false)
- Audio.stop()
- end
- function rt.runGame(glob,co,...)
-
- --Enable the touch controls on mobile
- if isMobile() then TC.setInput(true) end
- textinput(not isMobile()) --And disable the touch keyboard
-
- --The event loop coroutine, created in the 01_Lua.lua script
- local evco = glob.__evco; glob.__evco = nil
-
- local pause = glob.pause --Backup the pause function
-
- local lastArgs = {...}
- while true do
- if coroutine.status(co) == "dead" then
- if evco then
- co = evco; evco = false --So it doesn't get placed again.
- else
- break
- end
- end
-
- local args = {coroutine.resume(co,unpack(lastArgs))}
-
- --Program crashed
- if not args[1] then
- local err = tostring(args[2])
- if err:sub(1,12) == '[string ""]:' then err = err:sub(13,-1) end
- rt.resetEnvironment(); print("")
- return false, "ERR :"..err
- end
-
- --Nope, it's alive :-)
- if args[2] then
-
- --Special command for exiting the game
- if args[2] == "RUN:exit" then
- rt.resetEnvironment(); print("")
- if args[3] then
- return false, tostring(args[3])
- else
- return true
- end
-
- --Capture the keypressed event
- elseif args[2] == "CPU:pullEvent" or args[2] == "CPU:rawPullEvent" then
- lastArgs = {coroutine.yield(select(2,unpack(args)))}
-
- local event, a,b,c,d,e,f = select(2,unpack(lastArgs))
-
- --Check for the escape key
- if event == "keypressed" and a == "escape" then
- rt.resetEnvironment(); print(""); return true
- elseif event == "keypressed" and a == "return" then
- pause()
- end
-
- --Hack the sleep command
- elseif args[2] == "CPU:sleep" then
- local timer = args[3] --The sleep timer
-
- if type(timer) ~= "number" or timer < 0 then
- lastArgs = {coroutine.yield(select(2,unpack(args)))} --Let the original sleep blame the programmer.
- else
- while timer > 0 do
- local event, a,b,c,d,e,f = rawPullEvent()
-
- if event == "update" then
- timer = timer - a
- elseif event == "keypressed" and a == "escape" then
- rt.resetEnvironment(); print(""); return true
- elseif event == "keypressed" and a == "return" then
- pause()
- else
- triggerEvent(event, a,b,c,d,e,f)
- end
- end
-
- lastArgs = {true} --Sleep ran successfully
- end
-
- --Hack the flip command
- elseif args[2] == "CPU:flip" then
- _hasFlipped() --Clear the flip flag
- while true do
- local event, a,b,c,d,e,f = rawPullEvent()
-
- if event == "keypressed" and a == "escape" then
- rt.resetEnvironment(); print(""); return true
- elseif event == "keypressed" and a == "return" then
- pause()
- end
-
- triggerEvent(event, a,b,c,d,e,f)
-
- if _hasFlipped() then break end
- end
-
- lastArgs = {true} --Sleep ran successfully
-
- --Run the rest of the commands normally
- else
- lastArgs = {coroutine.yield(select(2,unpack(args)))}
- end
-
- end
- end
-
- rt.resetEnvironment()
-
- print("")
-
- return true
-
- end
- return rt
|