Page 1 of 1
How to change font size dynamically?
Posted: Wed Aug 27, 2008 1:43 pm
by Green_Hell
I tried this but only result is memory leak(not a right expression).
Code: Select all
-- change font size dynamically, debug program
function load()
-- load font for the first time
my_font_size = 12
my_font = love.graphics.newFont("DejaVuSans.ttf", my_font_size)
end
function update(dt)
if love.keyboard.isDown(love.key_space) then
-- Space to incrase font size
my_font_size = my_font_size + 1
my_font = love.graphics.newFont("DejaVuSans.ttf", my_font_size)
love.timer.sleep(50)
elseif love.keyboard.isDown(love.key_escape) then
-- Escape to exit
love.system.exit()
end
end
function draw()
-- draw message
love.graphics.setFont(my_font)
love.graphics.draw("Font size is " .. my_font_size .. ".", 100, 100)
end
- debug.love
- The same with the font.
- (326.41 KiB) Downloaded 259 times
Request: Crying Smile :'-(
Re: How to change font size dynamically?
Posted: Wed Aug 27, 2008 5:14 pm
by Mr. Strange
You should put this in the "keypressed" function, rather than checking "isDown" - that will return true 60 times a second, and overflow your stack (apparently)
Code: Select all
function keypressed(key)
if(key == love.key_space) then
-- Space to incrase font size
my_font_size = my_font_size + 1
my_font = love.graphics.newFont("DejaVuSans.ttf", my_font_size)
love.timer.sleep(50)
end
end
Your exit key could go in the keypressed function as well, or remain as you had it, since it can only return true once.
--Mr. Strange
Re: How to change font size dynamically?
Posted: Wed Aug 27, 2008 8:16 pm
by Green_Hell
Mr. Strange wrote:that will return true 60 times a second, and overflow your stack (apparently)
That's why I use
Problem is that I need to increase font size by holding the key and also be able to increase precisely by one click.
The program above is debug program not my real program.
Anyway.
Code: Select all
function keypressed(key)
if key == love.key_space then
-- Space to incrase font size
my_font_size = my_font_size + 1
my_font = love.graphics.newFont("DejaVuSans.ttf", my_font_size)
-- love.timer.sleep(50)
elseif key == love.key_escape then
-- Escape to exit
love.system.exit()
end
end
Works the same => eats memory.
Re: How to change font size dynamically?
Posted: Thu Aug 28, 2008 10:02 am
by emonk
Mr. Strange wrote:You should put this in the "keypressed" function, rather than checking "isDown" - that will return true 60 times a second, and overflow your stack (apparently)
Uhhh... no. Code gets called 60 times per second (assuming vsync, etc) but returns each time. No stack problem.
Green_Hell wrote:That's why I use
That's just for debugging purposes, right?
Green_Hell wrote:
Problem is that I need to increase font size by holding the key and also be able to increase precisely by one click.
The program above is debug program not my real program.
Anyway.
Code: Select all
function keypressed(key)
if key == love.key_space then
-- Space to incrase font size
my_font_size = my_font_size + 1
my_font = love.graphics.newFont("DejaVuSans.ttf", my_font_size)
-- love.timer.sleep(50)
elseif key == love.key_escape then
-- Escape to exit
love.system.exit()
end
end
Works the same => eats memory.
Yeah, it does. I can get it to suck up a couple hundred MB of memory in under a second. So either there's a memory leak or garbage collection isn't happening. Nothing we can do about the first, but let's test the second...
Code: Select all
function load()
my_font_size = 12;
end
function update(dt)
collectgarbage("step");
if love.keyboard.isDown(love.key_lshift) or love.keyboard.isDown(love.key_rshift) then
if love.keyboard.isDown(love.key_period) and my_font_size < 128 then
my_font = nil;
my_font_size = my_font_size + 1;
elseif love.keyboard.isDown(love.key_comma) and my_font_size > 5 then
my_font = nil;
my_font_size = my_font_size -1;
end;
end;
if not my_font then
my_font = love.graphics.newFont(love.default_font, my_font_size);
end;
end;
function keypressed(key)
if key == love.key_escape then
love.system.exit()
elseif key == love.key_g then
collectgarbage("collect")
elseif not (love.keyboard.isDown(love.key_lshift) or love.keyboard.isDown(love.key_rshift)) then
if key == love.key_period and my_font_size < 128 then
my_font = nil;
my_font_size = my_font_size + 1;
elseif key == love.key_comma and my_font_size > 5 then
my_font = nil;
my_font_size = my_font_size - 1;
end;
end;
end;
function draw()
-- draw message
love.graphics.setFont(my_font);
love.graphics.draw("Font size is " .. my_font_size .. ".", 100, 100);
end
Sorry about changing the keys on you.
This runs a single step of the garbage collector each update, and that seems to fix the problem. Memory allocation still balloons when you're reallocating fonts 60 times per second, but it drops as the garbage collector works its way through the released objects.