Page 1 of 1

[Solved] love.draw and RAM overuse

Posted: Wed Jan 11, 2012 8:27 pm
by Crimson
Hello! I'm new to this community, LÖVE, and Lua and am only somewhat acquainted with programming. I've read through Kikito's Love-Tile-Tutorial on GitHub as well as the Hamster Ball tutorial and use most of the code demonstrated there in the program I'm building now. I've also been through the 78 pages of this Support and Development forum reading anything with a title that might provide some clues as to why I'm experiencing the problem I'm having but didn't find anything directly related. (I did find tons of other useful things, though! :) )

What the program does:

Draws a tilemap to the screen using Kikito's string method
Draws a controllable character (Up, Down, Left, Right) to the screen from another tileset

The Problem: The program runs fully functioning for about 10 seconds, though with each passing second it uses 8% more RAM until the program crashes.

After some troubleshooting I believe I've pinpointed the issue to my function for drawing the character -

Code: Select all

function drawPlayer()
	playerImage = love.graphics.newImage('/tilesets/characters.png')
	fancyGhoul = love.graphics.newQuad(0, 0, 64, 64, 640, 640)
	love.graphics.drawq(playerImage, fancyGhoul, player.act_x, player.act_y)
end
This function is stored in a separate piece of code (Player-Functions.lua) and is called in Main.Lua at the love.draw function:

Code: Select all

function love.draw()
	drawMap()
	drawPlayer() 
end
When I delete the drawPlayer() function and just allow the map to be drawn, the program runs as expected, using a normal amount of RAM.

My first intuition was that redrawing the character every frame was using exponential amounts of RAM, if it saves a new character rather than overwrite the current one. However, my intuition isn't programming minded yet, so after trying some different things and failing I have come to you!

Re: love.draw and RAM overuse

Posted: Wed Jan 11, 2012 8:48 pm
by Ellohir

Code: Select all

   playerImage = love.graphics.newImage('/tilesets/characters.png')
   fancyGhoul = love.graphics.newQuad(0, 0, 64, 64, 640, 640)
This variables do not have to be refreshed. Store them and keep only the drawq one on the drawPlayer function. That should do it (you are reading the tileset every frame, that's a heavy process).

Re: love.draw and RAM overuse

Posted: Wed Jan 11, 2012 8:53 pm
by Robin
Yeah, you don't need to create an image each frame to use it.

Keep those two assignments in love.load() instead.

Re: love.draw and RAM overuse

Posted: Wed Jan 11, 2012 9:02 pm
by Crimson
That did it!

One of the things I tried was moving "fancyGhoul = love.graphics.newQuad(0, 0, 64, 64, 640, 640)" out as a global local variable, after reading about the problems people were having with constantly drawing newfonts, but that didn't solve it.

However, moving both of the variables out worked perfectly - thanks a bunch! It didn't occur to me that it would be checking the image every frame, I just kind of unconsciously assumed it was saved inside the code, which was a silly assumption in hindsight :P