Page 1 of 1

(Inherited) Alpha Mode Problem

Posted: Wed May 04, 2011 6:29 pm
by Cylog
Hey guys,
So, I'm new here and I started off with Loeves Framework by making a "Presentation"-like intro for my (future) game.
The code should do following:
1. Draw a black rectangle over the whole window.
2. Fade the rectangle with the (r, g, b, a) -Value
3.Don't draw the rectangle if the a-Value == 0

My main.lua file is just a skeleton, it serves as modular structure.
The problem is, all other Text (The FPS-Counter), Pictures etc. which are behind this particular black rectangle are being faded too and don't regain their original a-Value when the rectangle isn't being drawn anymore. So, maybe you guys know how to fix the Alpha-Issue there?

Thanks in advance.

main.lua:

Code: Select all

	require 'routines.lua'
	require 'variables.lua'
	
function love.load()
	extload()
end

function love.update(dt)
	extupdate(dt)
end

function love.draw()
	extdraw()
end
routines.lua:

Code: Select all

function extload()
	--load images
	image = love.graphics.newImage("1.jpg")
	-- set Caption
	love.graphics.setCaption("Hi there! Test!")
	--set Background Color
	love.graphics.setBackgroundColor(255,0,0)

-//init alpha ch	
	alphach = 255;

-//set picture coordinates
	x = 20
	y = 30

--//layeractive bool
    layeractive = true

--//alpha decrease speed
    decrease = 50

--//get length and width of window
    maxx = love.graphics.getWidth()
    maxy = love.graphics.getHeight()

end
--======================[UPDATE AND DRAW-FUNCTIONS]======================================================
function layershades(dt)
alphach = alphach - (decrease * dt)
if alphach <= 0 then 
layeractive = false end
end


function extdraw()
	-- draws text
if layeractive == true then 
	drawlayer()
end
	fps = love.timer.getFPS( )
    love.graphics.print(fps, 400, 300)
    love.graphics.draw(image, x, y)
end

function extupdate(dt)   
if layeractive == true then 
    layershades(dt)
else
end
end

function drawlayer()
love.graphics.setColor(0, 0, 0, alphach)
love.graphics.rectangle("fill", 0, 0, maxx, maxy)
end


Re: (Inherited) Alpha Mode Problem

Posted: Wed May 04, 2011 7:13 pm
by Ghuntar
The setColor function apply on all you draw. You have to set it one time to draw your rectangle and revert it back to ff.ff.ff to draw the rest.

Re: (Inherited) Alpha Mode Problem

Posted: Wed May 04, 2011 7:14 pm
by Cylog
Oh, I see. Thanks a lot, I guess that was a noobish one.

Re: (Inherited) Alpha Mode Problem

Posted: Wed May 04, 2011 7:17 pm
by Ghuntar
Do not worry, I fell in the same trap ^^.

Re: (Inherited) Alpha Mode Problem

Posted: Thu May 05, 2011 2:19 am
by Lafolie
Whenever I use a drawing operation I always use setColor first. You can't really go wrong then. Of course, if you're iterating through a table or something then it's better to just use the one setColor call, provided you want all the images to have the same colour offset.

Re: (Inherited) Alpha Mode Problem

Posted: Thu May 05, 2011 12:35 pm
by Cylog
Lafolie wrote:Whenever I use a drawing operation I always use setColor first. You can't really go wrong then. Of course, if you're iterating through a table or something then it's better to just use the one setColor call, provided you want all the images to have the same colour offset.
Seems a good (and of course the most logic) idea... does setColor have a big impact on performance if it is called repeatedly? The FPS-Counter ironically does.
I wonder if it would be a good idea to draw all (seperate) elements with the same Color once, then switch to another color and do the same, so you don't have to switch colors all the time...

@Ghuntar:
Ghuntar wrote:Do not worry, I fell in the same trap ^^.
It takes a while getting used to LUA and the Loeve Framework I guess ;)

Re: (Inherited) Alpha Mode Problem

Posted: Thu May 05, 2011 2:48 pm
by Lafolie
Cylog wrote: Seems a good (and of course the most logic) idea... does setColor have a big impact on performance if it is called repeatedly? The FPS-Counter ironically does.
I wonder if it would be a good idea to draw all (seperate) elements with the same Color once, then switch to another color and do the same, so you don't have to switch colors all the time...
Not quite sure what you mean by that last bit. Can you give us a code example? :)

And, it doesn't have a big impact on performance by itself. But say you have an enemies table that contains 100 enemies all drawn with no colour offset (255, 255, 255, 255) you can save 99 love.graphics.setColor() calls but calling it before you iterate. That would certainly save a bit of time in the long run.

Re: (Inherited) Alpha Mode Problem

Posted: Thu May 05, 2011 4:18 pm
by Cylog
@Lafolie:
I meant, something like a Optimization Algorithm, which looks through all the Enemies/Elements/Objects/Text which have to be drawn on Screen and sorts them, in order to save a few setColor() Calls...
That idea was foolish, I simply wasn't keeping in mind the importance of the order in which Objects are drawn. So, forget that idea, just ramblings of a noob ^^