Page 1 of 1

Example: Color Changing Functions

Posted: 17 Sep 2008, 07:41
by REVENGE
While writing the ctf gadget, I made some color changing functions, which I'm posting here as example code free for use (probably by newbies).

In order to use them, you must first setup a timer during initialization. For these, I've used

Code: Select all

local startTimer = Spring.GetTimer()
I also localized

Code: Select all

Spring.DiffTimers()
as

Code: Select all

DiffTimers()
Once that is done, here come the functions:

Code: Select all

--Cycles from 'value1' to 'value2' at designated 'rate'
--There are 3 options that control switching: 0 or nil is a square wave, 1 is a sawtooth wave, 2 is a sine wave
function ColorCycle(r1, g1, b1, a1, r2, g2, b2, a2, rate, option)
	local diffTime = DiffTimers(GetTimer(), startTimer)
	local factor	
	if (option == 1) then
		factor = math.abs(1.0 - math.mod(diffTime * rate, 2.0))
	elseif (option == 2) then
		factor = math.abs(math.sin(math.mod(diffTime * rate, 2.0) * math.pi))
	else
		if (math.mod(math.floor(diffTime * rate), 2.0) == 0) then
			return r1, g1, b1, a1
		else
			return r2, g2, b2, a2
		end
	end
	
	local r = r1 + ((r2 - r1) * factor)
	local g = g1 + ((g2 - g1) * factor)
	local b = b1 + ((b2 - b1) * factor)	
	local a = a1 + ((a2 - a1) * factor)
	return r, g, b, a	
end
		
--Fades from 'value1' to 'value2' at designated 'rate'
--Direction is binary: 1 fades in, 0 or nil fades out, default is fade out
function ColorFade(r1, g1, b1, a1, r2, g2, b2, a2, rate, direction)
	local diffTime	= DiffTimers(GetTimer(), startTimer)
	local factor
	if (direction == 1) then
		factor = math.abs(1.0 - math.mod(diffTime * rate, 1.0))
	else
		factor = math.abs(math.mod(diffTime * rate, 1.0))
	end
	
	local r = r1 + ((r2 - r1) * factor)
	local g = g1 + ((g2 - g1) * factor)
	local b = b1 + ((b2 - b1) * factor)	
	local a = a1 + ((a2 - a1) * factor)
	return r, g, b, a
end
Questions, comments, suggestions are welcome. I'm posting these here, with the hope that maybe in the future, we could get a better practical tutorial to lua for Spring going.

Re: Example: Color Changing Functions

Posted: 19 Sep 2008, 21:13
by Evil4Zerggin
Question: To measure local time, assuming accuracy is not too important and only differences in time matter (e.g., widget gfx), is it better to use DiffTimers() or keep track of time through widget:Update(dt)?

Re: Example: Color Changing Functions

Posted: 20 Sep 2008, 04:05
by REVENGE
I don't know about efficiency, but one important difference to keep in mind is that timers don't pause when you pause the game, but it looks like update(dt) will.

Re: Example: Color Changing Functions

Posted: 20 Sep 2008, 06:05
by Argh
Er... a lot of stuff like special effects needs to alter over gameframe-time, not absolute time. It's pretty important that it stays locked with sync to some extent. I really like the gamecode example though, that looks like it'd be useful if I ever have time to mess around with that end of things again.

Re: Example: Color Changing Functions

Posted: 20 Sep 2008, 11:42
by Evil4Zerggin
REVENGE wrote:I don't know about efficiency, but one important difference to keep in mind is that timers don't pause when you pause the game, but it looks like update(dt) will.
Update(dt) doesn't pause either.

Re: Example: Color Changing Functions

Posted: 20 Sep 2008, 21:28
by REVENGE
Evil4Zerggin wrote:
REVENGE wrote:I don't know about efficiency, but one important difference to keep in mind is that timers don't pause when you pause the game, but it looks like update(dt) will.
Update(dt) doesn't pause either.
Hmm...then the only synced methods of timing I can think of are manually getting gameframe diffs or gamesecond diffs. Any other way I don't know about?