Images covering other images

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
lolsasory
Prole
Posts: 21
Joined: Mon Nov 19, 2012 5:27 am

Images covering other images

Post by lolsasory »

How do you make some images be drawn on top of others?
For example

Code: Select all

player_draw
background_draw
player is on top.
but

Code: Select all

 if blaa blaa then
player_draw
end
background_draw
background is on top so how can I get player on top again?
User avatar
Dattorz
Citizen
Posts: 66
Joined: Sat Oct 27, 2012 12:32 am
Contact:

Re: Images covering other images

Post by Dattorz »

LOVE follows the Painter's Algorithm, which means you must make the draw calls in the order that you would like the graphics to appear, the first call being the graphic that appears furthest in the back. You'll need to design your algorithm and data structures accordingly. For example you'll probably draw your background tiles, and after that you'll iterate through some sort of list structure to draw each of your sprites.
lolsasory wrote:

Code: Select all

player_draw
background_draw
player is on top.
I have no idea how this is causing the player to be drawn on top. Can you post the code for player_draw and background_draw?
lolsasory
Prole
Posts: 21
Joined: Mon Nov 19, 2012 5:27 am

Re: Images covering other images

Post by lolsasory »

Code: Select all

function background_draw()
	love.graphics.draw(bg, background.x, background.y)
end

Code: Select all

function player_draw()
	love.graphics.draw(player.image, player.x, player.y)
		if love.keyboard.isDown("w")then
		player.image = love.graphics.newImage("images/ship02.png") else
		player.image = love.graphics.newImage("images/ship01.png")
		end
end

Code: Select all

function love.draw()
		if gamestate == "menu" then
		menu_draw()
		end
		if gamestate == "playing" then
		player_draw()
		end
	background_draw()
end
lolsasory
Prole
Posts: 21
Joined: Mon Nov 19, 2012 5:27 am

Re: Images covering other images

Post by lolsasory »

okay im really stupid and i just got backwards

nevermind
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Images covering other images

Post by Roland_Yonaba »

Just draw the background first. And everythinbg else after.

Code: Select all

function love.draw()
  background_draw()
  -- ..etc etc
end
And , just noticing, do not create new images each frame. That will quickly consume more and more memory.
Instead, load them once, then alternate the picture to be drawn inside love.draw.

Code: Select all

function love.load()
  ship02 =  love.graphics.newImage("images/ship02.png") 
  ship01 )  love.graphics.newImage("images/ship01.png") 
  -- etc etc
end

Code: Select all

function player_draw()
  if love.keyboard.isDown("w")then
    player.image = ship02 
  else
    player.image = ship01
  end
end
love.graphics.draw(player.image, player.x, player.y)
User avatar
Dattorz
Citizen
Posts: 66
Joined: Sat Oct 27, 2012 12:32 am
Contact:

Re: Images covering other images

Post by Dattorz »

Roland_Yonaba wrote:And , just noticing, do not create new images each frame. That will quickly consume more and more memory.
Instead, load them once, then alternate the picture to be drawn inside love.draw.
The memory use will be a bit inefficient, but Lua _is_ garbage-collected and it's not like his code would leave dangling references. I'm more concerned about the performance overhead incurred by constantly reloading the same image from disk (or from the RAM cache, depending on the OS, but it still has to be processed by the CPU).
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 7 guests