(Inherited) Alpha Mode Problem

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
Cylog
Prole
Posts: 12
Joined: Wed May 04, 2011 5:28 pm

(Inherited) Alpha Mode Problem

Post 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

User avatar
Ghuntar
Prole
Posts: 29
Joined: Wed Nov 25, 2009 8:56 am

Re: (Inherited) Alpha Mode Problem

Post 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.
Crazy Little Thing Called LÖVE.
Cylog
Prole
Posts: 12
Joined: Wed May 04, 2011 5:28 pm

Re: (Inherited) Alpha Mode Problem

Post by Cylog »

Oh, I see. Thanks a lot, I guess that was a noobish one.
User avatar
Ghuntar
Prole
Posts: 29
Joined: Wed Nov 25, 2009 8:56 am

Re: (Inherited) Alpha Mode Problem

Post by Ghuntar »

Do not worry, I fell in the same trap ^^.
Crazy Little Thing Called LÖVE.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: (Inherited) Alpha Mode Problem

Post 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.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Cylog
Prole
Posts: 12
Joined: Wed May 04, 2011 5:28 pm

Re: (Inherited) Alpha Mode Problem

Post 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 ;)
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: (Inherited) Alpha Mode Problem

Post 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.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Cylog
Prole
Posts: 12
Joined: Wed May 04, 2011 5:28 pm

Re: (Inherited) Alpha Mode Problem

Post 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 ^^
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 0 guests