Page 1 of 1
Drawing image outside love.draw()
Posted: Sun Oct 30, 2011 10:28 am
by sevcik.daniel
Hi there,
i have small problem, i haven't found any solution for it so i am asking you.
Is there way to draw image outside love.draw() or any command to call love.draw() inside any other "event"?
Re: Drawing image outside love.draw()
Posted: Sun Oct 30, 2011 11:04 am
by Robin
The way this is done is by setting variables in love.update() and have love.draw() draw certain images and other graphics, based on those variables.
Re: Drawing image outside love.draw()
Posted: Sun Oct 30, 2011 11:07 am
by sevcik.daniel
I wanted to draw loading image in loop (while the game is loading), thanks anyway.
Re: Drawing image outside love.draw()
Posted: Sun Oct 30, 2011 11:20 am
by Robin
If you really want that, you have two options:
- Load a bit of whatever you're loading, stop to draw things, resume loading, repeat.
- Do the same, but load things in a separate thread.
Usually, though, it is not necessary to show a loading screen, as images and sounds load quite fast.
Re: Drawing image outside love.draw()
Posted: Sun Oct 30, 2011 4:00 pm
by kikito
sevcik.daniel wrote:I wanted to draw loading image in loop (while the game is loading), thanks anyway.
I've been wanting to do a "resource loader" that allowed for automating that for a while now. But it's on my TODO list.
In the meantime, the first thing you need to know when you are programming in LÖVE is that you'll be better of if your code works "with" the love.callbacks, instead of "against" or "around" them.
The easiest way of doing what you want working with LÖVE is:
- Loading the actual images inside love.update instead of love.load and
- Using a variable inside love.draw and love.update to know when you are "loading the game images" and when you are "done loading images, and just showing the real game"
Here's a short example. The variable I'm using to distinguish between both states is called "gameState".
Code: Select all
function love.load()
loadingImage = love.graphics.load("loading.png")
imageNames = { 'player', 'enemy' }
images = {}
loadedImagesCount = 0
gameState = "loading"
player = {x=100, y=100}
enemy = {x=300, y=300}
end
function love.draw()
if gameState == "loading" then
love.graphics.draw(loadingImage, 200, 200)
else -- game is in "normal" state
love.graphics.draw(images.player, player.x, player.y)
love.graphics.draw(images.enemy, enemy.x, enemy.y)
end
end
function love.update(dt)
if gameState == "loading" then
local imageName = imageNames[loadedImagesCount + 1]
images[imageName] = love.graphics.newImage( imageName .. ".png" )
loadedImagesCount = loadedImagesCount + 1
if loadedImagesCount >= #imageNames then loading = false end
else
-- move the player and enemy coordinates around using dt
end
end
This "separation into states" is very common; appart from "loading" and "playing", there are other common values like "showing the dialog menu", "showing the pause menu" or "closing".
If your love.update and love.draw functions start getting too big, divide them in smaller ones (i.e. drawLoading() ).
Also, a small self-plug: I've developed a couple libs that might interest you to handle this complexity; but you will likely need to learn more stuff before being able to use them properly. For now, just know there are libs out there that might help.