Performance fix, inject into standard love.run loop, fix fuzzy search
* draw_text was generating _way_ too much garbage * love.run loop requires a small change to core
This commit is contained in:
parent
240a1a7d16
commit
74b2f1970f
|
@ -416,7 +416,7 @@ function core.step()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local run_threads = coroutine.wrap(function()
|
core.run_threads = coroutine.wrap(function()
|
||||||
while true do
|
while true do
|
||||||
local max_time = 1 / config.fps - 0.004
|
local max_time = 1 / config.fps - 0.004
|
||||||
local ran_any_threads = false
|
local ran_any_threads = false
|
||||||
|
@ -452,7 +452,7 @@ function core.run()
|
||||||
while true do
|
while true do
|
||||||
core.frame_start = system.get_time()
|
core.frame_start = system.get_time()
|
||||||
local did_redraw = core.step()
|
local did_redraw = core.step()
|
||||||
run_threads()
|
core.run_threads()
|
||||||
if not did_redraw and not system.window_has_focus() then
|
if not did_redraw and not system.window_has_focus() then
|
||||||
system.wait_event(0.25)
|
system.wait_event(0.25)
|
||||||
end
|
end
|
||||||
|
|
114
main.lua
114
main.lua
|
@ -9,12 +9,11 @@ function renderer.get_size()
|
||||||
end
|
end
|
||||||
|
|
||||||
function renderer.begin_frame()
|
function renderer.begin_frame()
|
||||||
love.graphics.origin()
|
-- handled in love.run
|
||||||
love.graphics.clear(love.graphics.getBackgroundColor())
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function renderer.end_frame()
|
function renderer.end_frame()
|
||||||
love.graphics.present()
|
-- handled in love.run
|
||||||
end
|
end
|
||||||
|
|
||||||
function renderer.set_clip_rect(x, y, w, h)
|
function renderer.set_clip_rect(x, y, w, h)
|
||||||
|
@ -22,12 +21,10 @@ function renderer.set_clip_rect(x, y, w, h)
|
||||||
end
|
end
|
||||||
|
|
||||||
function renderer.set_litecolor(color)
|
function renderer.set_litecolor(color)
|
||||||
if not color or #color < 3 then color = { 255, 255, 255, 255 } end
|
local r, g, b, a = 255, 255, 255, 255
|
||||||
local alpha = 1
|
if color and #color >= 3 then r, g, b = unpack(color, 1, 3) end
|
||||||
if #color == 4 then
|
if #color >= 4 then a = color[4] end
|
||||||
alpha = color[4] / 255
|
love.graphics.setColor(r / 255, g / 255, b / 255, a / 255)
|
||||||
end
|
|
||||||
love.graphics.setColor(color[1] / 255, color[2] / 255, color[3] / 255, alpha)
|
|
||||||
end
|
end
|
||||||
function renderer.draw_rect(x, y, w, h, color)
|
function renderer.draw_rect(x, y, w, h, color)
|
||||||
renderer.set_litecolor(color)
|
renderer.set_litecolor(color)
|
||||||
|
@ -36,9 +33,9 @@ end
|
||||||
|
|
||||||
function renderer.draw_text(font, text, x, y, color)
|
function renderer.draw_text(font, text, x, y, color)
|
||||||
renderer.set_litecolor(color)
|
renderer.set_litecolor(color)
|
||||||
local textobj = love.graphics.newText(font.font, text)
|
love.graphics.setFont(font.font)
|
||||||
love.graphics.draw(textobj, x, y)
|
love.graphics.print(text, x, y)
|
||||||
return x + textobj:getWidth()
|
return x + font.font:getWidth(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
renderer.font = {}
|
renderer.font = {}
|
||||||
|
@ -59,7 +56,8 @@ function renderer.font.load(filename, size)
|
||||||
end
|
end
|
||||||
|
|
||||||
system = {}
|
system = {}
|
||||||
function system.poll_event()
|
system.event_queue = {}
|
||||||
|
function system.enqueue_love_event(ev, a, b, c, d, e, f)
|
||||||
local function button_name(button)
|
local function button_name(button)
|
||||||
if button == 1 then return 'left' end
|
if button == 1 then return 'left' end
|
||||||
if button == 2 then return 'right' end
|
if button == 2 then return 'right' end
|
||||||
|
@ -76,30 +74,40 @@ function system.poll_event()
|
||||||
end
|
end
|
||||||
return key
|
return key
|
||||||
end
|
end
|
||||||
love.event.pump()
|
local function convert_love_event()
|
||||||
for ev, a, b, c, d, e, f in love.event.poll() do
|
|
||||||
if ev == 'quit' then
|
if ev == 'quit' then
|
||||||
return 'quit'
|
return {'quit'}
|
||||||
elseif ev == 'resize' then
|
elseif ev == 'resize' then
|
||||||
return 'resized', a, b
|
return {'resized', a, b}
|
||||||
elseif ev == 'filedropped' then
|
elseif ev == 'filedropped' then
|
||||||
return 'filedropped', a:getFilename(), love.mouse.getX(), love.mouse.getY()
|
return {'filedropped', a:getFilename(), love.mouse.getX(), love.mouse.getY()}
|
||||||
elseif ev == 'keypressed' then
|
elseif ev == 'keypressed' then
|
||||||
return 'keypressed', key_name(a or b)
|
return {'keypressed', key_name(a or b)}
|
||||||
elseif ev == 'keyreleased' then
|
elseif ev == 'keyreleased' then
|
||||||
return 'keyreleased', key_name(a or b)
|
return {'keyreleased', key_name(a or b)}
|
||||||
elseif ev == 'textinput' then
|
elseif ev == 'textinput' then
|
||||||
return 'textinput', a
|
return {'textinput', a}
|
||||||
elseif ev == 'mousepressed' then
|
elseif ev == 'mousepressed' then
|
||||||
return 'mousepressed', button_name(c), a, b, e
|
return {'mousepressed', button_name(c), a, b, e}
|
||||||
elseif ev == 'mousereleased' then
|
elseif ev == 'mousereleased' then
|
||||||
return 'mousereleased', button_name(c), a, b
|
return {'mousereleased', button_name(c), a, b}
|
||||||
elseif ev == 'mousemoved' then
|
elseif ev == 'mousemoved' then
|
||||||
return 'mousemoved', a, b, c, d
|
return {'mousemoved', a, b, c, d}
|
||||||
elseif ev == 'wheelmoved' then
|
elseif ev == 'wheelmoved' then
|
||||||
return 'mousewheel', b
|
return {'mousewheel', b}
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
function system.wait_event(n)
|
function system.wait_event(n)
|
||||||
|
@ -129,7 +137,7 @@ function system.window_has_focus()
|
||||||
end
|
end
|
||||||
|
|
||||||
function system.show_confirm_dialog(title, msg)
|
function system.show_confirm_dialog(title, msg)
|
||||||
return love.window.showMessageBox(title, message, { 'Yes', 'No', escapebutton = 2 }, 'warning') == 1
|
return love.window.showMessageBox(title, msg, { 'Yes', 'No', escapebutton = 2 }, 'warning') == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
function system.chdir(dir)
|
function system.chdir(dir)
|
||||||
|
@ -183,7 +191,6 @@ end
|
||||||
function system.sleep(s)
|
function system.sleep(s)
|
||||||
love.timer.sleep(s)
|
love.timer.sleep(s)
|
||||||
end
|
end
|
||||||
|
|
||||||
function system.exec(cmd)
|
function system.exec(cmd)
|
||||||
-- ehhhh todo I guess
|
-- ehhhh todo I guess
|
||||||
end
|
end
|
||||||
|
@ -201,7 +208,7 @@ function system.fuzzy_match(str, ptn)
|
||||||
if cstr:lower() == cptn:lower() then
|
if cstr:lower() == cptn:lower() then
|
||||||
score = score + (run * 10)
|
score = score + (run * 10)
|
||||||
if cstr ~= cptn then score = score - 1 end
|
if cstr ~= cptn then score = score - 1 end
|
||||||
istr = istr + 1
|
run = run + 1
|
||||||
iptn = iptn + 1
|
iptn = iptn + 1
|
||||||
else
|
else
|
||||||
score = score - 10
|
score = score - 10
|
||||||
|
@ -210,10 +217,9 @@ function system.fuzzy_match(str, ptn)
|
||||||
istr = istr + 1
|
istr = istr + 1
|
||||||
end
|
end
|
||||||
if iptn > ptn:len() then
|
if iptn > ptn:len() then
|
||||||
return score - str:len() + istr - 1
|
return score - str:len() - istr + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
table.unpack = unpack
|
table.unpack = unpack
|
||||||
|
|
||||||
core = ""
|
core = ""
|
||||||
|
@ -230,5 +236,51 @@ function love.run()
|
||||||
local style = require('core.style')
|
local style = require('core.style')
|
||||||
style.code_font = renderer.font.load(EXEDIR .. "/data/fonts/monospace.ttf", 15 * SCALE)
|
style.code_font = renderer.font.load(EXEDIR .. "/data/fonts/monospace.ttf", 15 * SCALE)
|
||||||
core.init()
|
core.init()
|
||||||
return core.run
|
|
||||||
|
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
|
||||||
|
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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue