forked from rxi/lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
311 lines (270 loc) · 8.56 KB
/
main.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
renderer = {}
function renderer.show_debug(show)
-- useful?
end
function renderer.get_size()
return love.graphics.getWidth(), love.graphics.getHeight()
end
function renderer.begin_frame()
-- handled in love.run
end
function renderer.end_frame()
-- handled in love.run
end
function renderer.set_clip_rect(x, y, w, h)
love.graphics.setScissor(x, y, w, h)
end
function renderer.set_litecolor(color)
local r, g, b, a = 255, 255, 255, 255
if color and #color >= 3 then r, g, b = unpack(color, 1, 3) end
if #color >= 4 then a = color[4] end
love.graphics.setColor(r / 255, g / 255, b / 255, a / 255)
end
function renderer.draw_rect(x, y, w, h, color)
renderer.set_litecolor(color)
love.graphics.rectangle("fill", x, y, w, h)
end
function renderer.draw_text(font, text, x, y, color)
renderer.set_litecolor(color)
love.graphics.setFont(font.font)
if pcall(function() love.graphics.print(text, x, y) end) then
return x + font.font:getWidth(text)
else
return x
end
end
renderer.font = {}
function renderer.font.load(filename, size)
local font = love.graphics.newFont(filename, size)
return {
font = font,
set_tab_width = function(self, n)
-- todo? Ignore?
end,
get_width = function(self, text)
return self.font:getWidth(text)
end,
get_height = function(self)
return self.font:getHeight()
end
}
end
system = {}
system.event_queue = {}
function system.enqueue_love_event(ev, a, b, c, d, e, f)
local function button_name(button)
if button == 1 then return 'left' end
if button == 2 then return 'right' end
if button == 3 then return 'middle' end
return '?'
end
local function key_name(key)
if key:sub(1, 2) == "kp" then
return "keypad " .. key:sub(3)
end
if key:sub(2) == "ctrl" or key:sub(2) == "shift" or key:sub(2) == "alt" or key:sub(2) == "gui" then
if key:sub(1, 1) == "l" then return "left " .. key:sub(2) end
return "right " .. key:sub(2)
end
return key
end
local function convert_love_event()
if ev == 'quit' then
return {'quit'}
elseif ev == 'resize' then
return {'resized', a, b}
elseif ev == 'filedropped' then
return {'filedropped', a:getFilename(), love.mouse.getX(), love.mouse.getY()}
elseif ev == 'keypressed' then
return {'keypressed', key_name(a or b)}
elseif ev == 'keyreleased' then
return {'keyreleased', key_name(a or b)}
elseif ev == 'textinput' then
return {'textinput', a}
elseif ev == 'mousepressed' then
return {'mousepressed', button_name(c), a, b, e}
elseif ev == 'mousereleased' then
return {'mousereleased', button_name(c), a, b}
elseif ev == 'mousemoved' then
return {'mousemoved', a, b, c, d}
elseif ev == 'wheelmoved' then
return {'mousewheel', b}
end
end
local liteev = convert_love_event()
if liteev then
table.insert(system.event_queue, liteev)
end
end
function system.poll_event()
local liteev = table.remove(system.event_queue, 1)
if liteev then
return unpack(liteev)
end
end
function system.wait_event(n)
-- no love2d equivalent
return false
end
function system.set_cursor(cursor)
if cursor == nil then cursor = 'arrow' end
if cursor == 'sizeh' then cursor = 'sizewe' end
if cursor == 'sizev' then cursor = 'sizens' end
love.mouse.setCursor(love.mouse.getSystemCursor(cursor))
end
function system.set_window_title(title)
love.window.setTitle(title)
end
function system.set_window_mode(mode)
love.window.setFullscreen(mode == 'fullscreen')
if mode == nil or mode == 'normal' then love.window.restore() end
if mode == 'maximized' then love.window.maximize() end
end
function system.window_has_focus()
return love.window.hasFocus()
end
function system.show_confirm_dialog(title, msg)
return love.window.showMessageBox(title, msg, { 'Yes', 'No', escapebutton = 2 }, 'warning') == 1
end
function system.chdir(dir)
-- not possible with love2d
end
function system.list_dir(path)
if path == '.' then path = '' end
local info = love.filesystem.getInfo(path)
if info and info.type == 'directory' then
return love.filesystem.getDirectoryItems(path)
elseif info and info.type == 'symlink' then
return love.filesystem.getDirectoryItems(path .. "/.")
end
return nil, "Not a directory"
end
function system.absolute_path(path)
return path -- love.filesystem.getRealDirectory(path)
end
function system.get_file_info(path)
local info = love.filesystem.getInfo(path)
if info then
local type = nil
if info.type == 'file' then
type = 'file'
elseif info.type == 'directory' then
type = 'dir'
elseif info.type == 'symlink' then
if love.filesystem.read(path, 1) then
type = 'file'
else
type = 'dir'
end
end
return {
modified = info.modtime,
size = info.size,
type = type
}
else
return nil, "Doesn't exist"
end
end
function system.get_clipboard()
return love.system.getClipboardText()
end
function system.set_clipboard(text)
love.system.setClipboardText(text)
end
function system.get_time()
return love.timer.getTime()
end
function system.sleep(s)
love.timer.sleep(s)
end
function system.exec(cmd)
-- ehhhh todo I guess
end
function system.fuzzy_match(str, ptn)
local istr = 1
local iptn = 1
local score = 0
local run = 0
while istr <= str:len() and iptn <= ptn:len() do
while str:sub(istr,istr) == ' ' do istr = istr + 1 end
while ptn:sub(iptn,iptn) == ' ' do iptn = iptn + 1 end
local cstr = str:sub(istr,istr)
local cptn = ptn:sub(iptn,iptn)
if cstr:lower() == cptn:lower() then
score = score + (run * 10)
if cstr ~= cptn then score = score - 1 end
run = run + 1
iptn = iptn + 1
else
score = score - 10
run = 0
end
istr = istr + 1
end
if iptn > ptn:len() then
return score - str:len() - istr + 1
end
end
table.unpack = unpack
PATHSEP = package.config:sub(1, 1)
ARGS = love.arg.parseGameArguments(arg)
VERSION = "1.11"
PLATFORM = "love2d"
SCALE = love.graphics.getDPIScale()
local modname = ...
local ilastdot = modname:reverse():find("%.")
EXEDIR = modname:gsub("%.", PATHSEP)
if ilastdot then
EXEDIR = EXEDIR:sub(1, (-1 - ilastdot))
else
EXEDIR = ""
end
local literoot = love.filesystem.getWorkingDirectory()
if #EXEDIR > 0 then
literoot = literoot .. PATHSEP .. EXEDIR
end
package.path = literoot .. '/data/?/init.lua;' .. package.path
package.path = literoot .. '/data/?.lua;' .. package.path
function love.run()
local core = require('core')
local style = require('core.style')
style.code_font = renderer.font.load(EXEDIR .. "/data/fonts/monospace.ttf", 15 * SCALE)
core.init()
if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
-- We don't want the first frame's dt to include time taken by love.load.
if love.timer then love.timer.step() end
local dt = 0
-- Main loop time.
return function()
-- Process events.
if love.event then
love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do
system.enqueue_love_event(name, a, b, c, d, e, f)
if name == "quit" then
if not love.quit or not love.quit() then
return a or 0
end
end
love.handlers[name](a,b,c,d,e,f)
end
end
-- Update dt, as we'll be passing it to update
if love.timer then dt = love.timer.step() end
-- Call update and draw
if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
if love.graphics and love.graphics.isActive() then
love.graphics.origin()
love.graphics.clear(love.graphics.getBackgroundColor())
-- update lite & draw
core.redraw = true
core.frame_start = system.get_time()
core.step()
core.run_threads()
if love.draw then love.draw() end
love.graphics.present()
end
if love.timer then love.timer.sleep(0.001) end
end
end