Drawing image outside love.draw()

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
sevcik.daniel
Prole
Posts: 4
Joined: Fri Jul 15, 2011 10:45 pm

Drawing image outside love.draw()

Post 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"?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Drawing image outside love.draw()

Post 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.
Help us help you: attach a .love.
sevcik.daniel
Prole
Posts: 4
Joined: Fri Jul 15, 2011 10:45 pm

Re: Drawing image outside love.draw()

Post by sevcik.daniel »

I wanted to draw loading image in loop (while the game is loading), thanks anyway.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Drawing image outside love.draw()

Post by Robin »

If you really want that, you have two options:
  1. Load a bit of whatever you're loading, stop to draw things, resume loading, repeat.
  2. 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.
Help us help you: attach a .love.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Drawing image outside love.draw()

Post 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.
When I write def I mean function.
Post Reply

Who is online

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