Page 1 of 1

Images covering other images

Posted: Mon Nov 19, 2012 5:45 am
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?

Re: Images covering other images

Posted: Mon Nov 19, 2012 6:13 am
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?

Re: Images covering other images

Posted: Mon Nov 19, 2012 7:20 am
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

Re: Images covering other images

Posted: Mon Nov 19, 2012 7:25 am
by lolsasory
okay im really stupid and i just got backwards

nevermind

Re: Images covering other images

Posted: Mon Nov 19, 2012 7:32 am
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)

Re: Images covering other images

Posted: Mon Nov 19, 2012 4:41 pm
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).