How to change font size dynamically?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Green_Hell
Citizen
Posts: 94
Joined: Thu Feb 21, 2008 1:11 am

How to change font size dynamically?

Post 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 :'-(
>>I love LÖVE.<<
Mr. Strange
Party member
Posts: 101
Joined: Mon Aug 11, 2008 5:19 am

Re: How to change font size dynamically?

Post 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
Green_Hell
Citizen
Posts: 94
Joined: Thu Feb 21, 2008 1:11 am

Re: How to change font size dynamically?

Post by Green_Hell »

Mr. Strange wrote:that will return true 60 times a second, and overflow your stack (apparently)
That's why I use

Code: Select all

love.timer.sleep(50)
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.
>>I love LÖVE.<<
emonk
Prole
Posts: 24
Joined: Tue Aug 12, 2008 11:43 am

Re: How to change font size dynamically?

Post 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

Code: Select all

love.timer.sleep(50)
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.
Sarcasm - just one of the services I offer.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests