Lag.

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.
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Lag.

Post by baconhawka7x »

I noticed if i leave my Love 2d game running for too long the frame rate drops insanely. i'm guessing that it is because of how i organize things? any and all tips will help!

This is my main.lua

Code: Select all

function love.load()
	love.graphics.setBackgroundColor(255, 255, 255)	
	
	character = love.graphics.newImage("character.png")
	
	x = 200
	y = 200

end
function love.update(dt)

end
function love.draw()
	love.graphics.setFont(54)
	love.graphics.setColor(0, 255, 255)
	love.graphics.print("Start!:D", 5, 240)
end
function love.mousepressed(x,y)
	if x > 5 and
	x < 400 and
	y > 240 and
	y < 400 then require "lvl1/data001.lua"
	end
end
This is my ".lua" for my first level

Code: Select all

function love.update(dt)
	if love.keyboard.isDown("w") then
		y = y - 5
	elseif love.keyboard.isDown("s") then
		y = y + 5
	end
	if love.keyboard.isDown("a") then
		x = x - 5
	elseif love.keyboard.isDown("d") then
		x = x + 5
	end
	if x < 0 then
		x = 799
	end
	if y > 600 then
		y = 0
	end
	if y < 0 then
		y = 599
	end
	if x > 800 then
		require "lvl1/data002.lua"
	end	
end
function love.draw()
	love.graphics.setBackgroundColor(255, 255, 255)
	
	love.graphics.setColor(255, 255, 255)
	love.graphics.draw(character, x, y)
	
	love.graphics.setFont(58)
	love.graphics.setColor(0, 255, 255)
	love.graphics.print("Use WASD To Move!:D", 10, 5)
	
end
it is a top-view shooter, very simple. im pretty new! please help!
Last edited by baconhawka7x on Sat Dec 03, 2011 12:43 am, edited 1 time in total.
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: Lag.

Post by Ellohir »

If you posted your .love we could take a look at it. Otherwise we can't know. Maybe you constantly create things and don't clean unused memory?
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Lag.

Post by TechnoCat »

using setFont(number) in the draw callback is going to instantiate a new font every frame. Memory leak!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Lag.

Post by bartbes »

baconhawka7x wrote:

Code: Select all

function love.draw()
-- stuff
	love.graphics.setFont(58)
NEVER DO THIS!
That can't be stressed enough, you're loading and creating fonts every single frame.
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Re: Lag.

Post by baconhawka7x »

Ok so how do I NOT load the font every frame?
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Lag.

Post by thelinx »

Code: Select all

function love.load()
  bigfont = love.graphics.newFont(58)
end

function love.draw()
  love.graphics.setFont(bigfont)
end
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Re: Lag.

Post by baconhawka7x »

thelinx wrote:

Code: Select all

function love.load()
  bigfont = love.graphics.newFont(58)
end

function love.draw()
  love.graphics.setFont(bigfont)
end
thank you so much!:D is that the only thing that is making it lag?
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Lag.

Post by Jasoco »

Probably. It takes a lot of processing to create the font. You don't notice it when you load because it does it once. But if you call it every frame, that time adds up and increases memory usage. It's a common newbie mistake actually so don't feel bad. Most of us have done it the first time too.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Lag.

Post by kikito »

I've just created an issue requesting the removal of love.graphics.setFont(number).
When I write def I mean function.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Lag.

Post by thelinx »

That's a stupid "solution".
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 207 guests