-
Notifications
You must be signed in to change notification settings - Fork 0
/
obsi.lua
251 lines (234 loc) · 7.75 KB
/
obsi.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
local args = {...}
local gamePath = args[1]
if not gamePath then
printError("Obsi error: No path specified!")
printError("Usage: obsi <path>")
return
end
gamePath = fs.combine(shell.dir(), gamePath)
if not fs.exists(gamePath) then
printError("Obsi error: Path does not exist!")
printError(("Path being: %s"):format(gamePath))
printError("Please make sure that your path is correct!")
return
elseif not fs.isDir(gamePath) then
printError("Obsi error: Path is not a folder!")
printError(("Path being: %s"):format(gamePath))
printError("Please make sure that your path is correct!")
return
end
---@class obsi
obsi = {}
---@class obsi.Config
local config = {
maxfps = 20,
mintps = 60,
multiUpdate = true,
renderingAPI = "parea"
}
---@type fun(tab: obsi.Config)
obsi.config = nil
local function catch(err)
obsi.graphics.clearPalette()
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1, 1)
printError(debug.traceback(err, 2))
-- if obsi.debugger then
-- obsi.debugger.print(debug.traceback(err, 2))
-- end
end
local r = require "cc.require"
local env = setmetatable({obsi = obsi}, { __index = _G })
env.require, env.package = r.make(env, gamePath)
if fs.exists(fs.combine(gamePath, "config.lua")) then
local chunk, err = loadfile(fs.combine(gamePath, "config.lua"), "bt", env)
if not chunk then
printError(("obsi: config.lua file is found but can't be executed!"))
printError(err)
return
end
xpcall(chunk, catch)
if obsi.config then
obsi.config(config)
end
-- if config.maxfps > config.mintps then
-- config.maxfps = config.mintps
-- end
end
-- obsi.debugger = peripheral.find("debugger") or (periphemu and periphemu.create("right", "debugger") and peripheral.find("debugger")) or {print = function (...) end}
-- _G.debugger = _G.debugger or obsi.debugger
---@type obsi.Canvas|parea.Canvas
local canvas
---@type Window
local winh
---@type fun(), fun(), fun(), fun()
local soundLoop, mouseDown, mouseMove, mouseUp
local emptyFunc = function(...) end
---@type fun()
local fsInit
obsi.filesystem, fsInit = require("filesystem.init")(gamePath)
obsi.system = require("system")
---@type obsi.graphics, obsi.Canvas|parea.Canvas, Window
obsi.graphics, canvas, winh = require("graphics")(obsi.filesystem, config.renderingAPI)
obsi.time = require("time")
obsi.keyboard = require("keyboard")
obsi.mouse, mouseDown, mouseUp, mouseMove = require("mouse")()
obsi.audio, soundLoop = require("audio")(obsi.filesystem)
obsi.state = require("state")
obsi.debug = false
obsi.version = "1.5.1"
local chunk, err = loadfile(fs.combine(gamePath, "main.lua"), "bt", env)
if not chunk then
printError(("obsi: %s not found!"):format(fs.combine(gamePath, "main.lua")))
printError(err)
return
end
obsi.load = emptyFunc
---@type fun(dt: number)
obsi.update = emptyFunc
---@type fun(dt: number)
obsi.draw = emptyFunc
---@type fun(x: number, y: number, button: integer)
obsi.mousePressed = emptyFunc
---@type fun(x: number, y: number, button: integer)
obsi.mouseReleased = emptyFunc
---@type fun(x: number, y: number)
obsi.mouseMoved = emptyFunc
---@type fun(key: integer)
obsi.keyPressed = emptyFunc
---@type fun(key: integer)
obsi.keyReleased = emptyFunc
---@type fun(wind: Window)
obsi.windowFlush = emptyFunc -- sends a window object as a first argument, which you can mutate if you wish.
---@type fun(w: integer, h: integer)
obsi.resize = emptyFunc -- sends width and height of the window in characters, not pixels.
---@type fun(eventData: table)
obsi.onEvent = emptyFunc -- for any events that aren't caught! Runs last so that you won't mutate it.
---@type fun()
obsi.quit = emptyFunc -- called when Obsi recieves "terminate" event.
local function clock()
return periphemu and os.epoch(("nano")--[[@as "local"]])/10^9 or os.clock()
end
---@param time number
local function sleepRaw(time)
local timerID = os.startTimer(time)
while true do
local _, tID = os.pullEventRaw("timer")
if tID == timerID then
break
end
end
end
local t = clock()
local dt = 1/config.maxfps
local drawTime = t
local updateTime = t
local frameTime = t
fsInit() -- use game's path
local function gameLoop()
obsi.load()
while true do
local startTime = clock()
if config.multiUpdate then
local updated = false
for _ = 1, dt/(1/config.mintps) do
obsi.update(1/config.mintps)
updated = true
end
if not updated then
obsi.update(dt)
end
else
obsi.update(dt)
end
updateTime = clock() - startTime
startTime = clock()
obsi.draw(dt)
drawTime = clock() - startTime
obsi.graphics.setCanvas()
soundLoop(dt)
if obsi.debug then
local bg, fg = obsi.graphics.bgColor, obsi.graphics.fgColor
obsi.graphics.bgColor, obsi.graphics.fgColor = colors.black, colors.white
obsi.graphics.write("Obsi "..obsi.version, 1, 1)
obsi.graphics.write(obsi.system.getHost(), 1, 2)
obsi.graphics.write(("rendering: %s [%sx%s]"):format(obsi.graphics.getRenderer(), obsi.graphics.getPixelSize()), 1, 3)
obsi.graphics.write(("%.2f fps"):format(1/dt), 1, 4)
obsi.graphics.write(("%0.2fms update"):format(updateTime*1000), 1, 5)
obsi.graphics.write(("%0.2fms draw"):format(drawTime*1000), 1, 6)
obsi.graphics.write(("%0.2fms frame"):format(frameTime*1000), 1, 7)
obsi.graphics.bgColor, obsi.graphics.fgColor = bg, fg
end
-- obsi.debugger.print(("%0.2fms frame [%sx%s]"):format(frameTime*1000, obsi.graphics.getPixelSize()))
obsi.graphics.flushAll()
obsi.windowFlush(winh)
obsi.graphics.show()
frameTime = clock() - startTime
repeat
sleepRaw((1/config.maxfps)/20)
until (clock()-t >= 1/config.maxfps)
obsi.graphics.clear()
obsi.graphics.bgColor, obsi.graphics.fgColor = colors.black, colors.white
obsi.graphics.resetOrigin()
dt = clock()-t
t = clock()
end
end
local function eventLoop()
while true do
local eventData = {os.pullEventRaw()}
if eventData[1] == "mouse_click" then
mouseDown(eventData[3], eventData[4], eventData[2])
obsi.mousePressed(eventData[3], eventData[4], eventData[2])
elseif eventData[1] == "mouse_up" then
mouseUp(eventData[3], eventData[4], eventData[2])
obsi.mouseReleased(eventData[3], eventData[4], eventData[2])
elseif eventData[1] == "mouse_move" then -- apparently the second index is only there for compatibility? Alright.
mouseMove(eventData[3], eventData[4])
obsi.mouseMoved(eventData[3], eventData[4])
elseif eventData[1] == "mouse_drag" then
mouseMove(eventData[3], eventData[4])
obsi.mouseMoved(eventData[3], eventData[4])
elseif eventData[1] == "term_resize" then
local w, h = term.getSize()
winh.reposition(1, 1, w, h)
canvas:resize(w, h)
obsi.graphics.pixelWidth, obsi.graphics.pixelHeight = canvas.width, canvas.height
obsi.graphics.width, obsi.graphics.height = w, h
obsi.resize(w, h)
elseif eventData[1] == "key" and not eventData[3] then
obsi.keyboard.keys[keys.getName(eventData[2])] = true
obsi.keyboard.scancodes[eventData[2]] = true
obsi.keyPressed(eventData[2])
-- --the code below is only for testing!
-- if eventData[2] == keys.l then
-- local rentab = {
-- ["pixelbox"] = "parea",
-- ["parea"] = "hmon",
-- ["hmon"] = "pixelbox",
-- }
-- obsi.graphics.setRenderer(rentab[obsi.graphics.getRenderer()] or "parea")
-- elseif eventData[2] == keys.p then
-- obsi.debug = not obsi.debug
-- end
elseif eventData[1] == "key_up" then
obsi.keyboard.keys[keys.getName(eventData[2])] = false
obsi.keyboard.scancodes[eventData[2]] = false
obsi.keyReleased(eventData[2])
elseif eventData[1] == "terminate" then
obsi.quit()
obsi.graphics.clearPalette()
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1, 1)
return
end
obsi.onEvent(eventData)
end
end
local s = xpcall(chunk, catch) -- execute the main.lua
if not s then
return
end
parallel.waitForAny(function() xpcall(gameLoop, catch) end, function() xpcall(eventLoop, catch) end)