infrastructure for caching LÖVE text objects

This commit is contained in:
Kartik K. Agaram 2022-08-24 13:27:04 -07:00
parent 89222f86a0
commit ce31b74b10
3 changed files with 13 additions and 6 deletions

View File

@ -467,6 +467,7 @@ function edit.update_font_settings(State, font_height)
love.graphics.setFont(love.graphics.newFont(Editor_state.font_height)) love.graphics.setFont(love.graphics.newFont(Editor_state.font_height))
State.line_height = math.floor(font_height*1.3) State.line_height = math.floor(font_height*1.3)
State.em = App.newText(love.graphics.getFont(), 'm') State.em = App.newText(love.graphics.getFont(), 'm')
Text_cache = {}
end end
--== some methods for tests --== some methods for tests

View File

@ -146,11 +146,6 @@ function current_shape(State, shape)
end end
end end
_bullet_indent = nil
function bullet_indent() function bullet_indent()
if _bullet_indent == nil then return App.width(to_text('* '))
local text = love.graphics.newText(love.graphics.getFont(), '* ')
_bullet_indent = text:getWidth()
end
return _bullet_indent
end end

View File

@ -16,6 +16,9 @@ Editor_state = {}
function App.initialize_globals() function App.initialize_globals()
-- tests currently mostly clear their own state -- tests currently mostly clear their own state
-- a few text objects we can avoid recomputing unless the font changes
Text_cache = {}
-- blinking cursor -- blinking cursor
Cursor_time = 0 Cursor_time = 0
@ -204,3 +207,11 @@ function App.keyreleased(key, scancode)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.key_released(Editor_state, key, scancode) return edit.key_released(Editor_state, key, scancode)
end end
-- use this sparingly
function to_text(s)
if Text_cache[s] == nil then
Text_cache[s] = App.newText(love.graphics.getFont(), s)
end
return Text_cache[s]
end