oscillating function

May 8, 2011 at 10:16pm
closed account (zwA4jE8b)
I am exercising a DX9 tutorial and one practice is to make the screen oscillate from black to blue (255) and back to black.

This function works, but I am just wondering if any of you more experienced people would call it efficient.

I thought about maybe using a sin() function because it is oscillating, but I have not yet worked it out.

the following function is called right before the frame is rendered. either r,g,or b can be sent to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void switchback(int& _c)
{
	static bool _swp = true;
	if (_swp)
	{
		_c++;
		if (_c == 255)
			_swp = false;
	}
	else
	{
		_c--;
		if (_c == 0)
			_swp = true;
	}
}
May 8, 2011 at 10:28pm
I don't think one can refer to this approach as inefficient. Clearly this produces a different output as sin(): The sin() function will produce a curve while this function produces "triangles". So it is really up to you: Do you want your colors to transition "triangly", or "sinely"? hehe

A note on this, however: if I call switchback() with the blue component, and that blue reaches 255, you invert the direction, but what if the next call comes with the red component?? Even worse: What if the red component == 0? You'll leave it as -1 and you will swap again. Just something to have in mind.
May 8, 2011 at 11:18pm
closed account (zwA4jE8b)
well triangular is smooth enough for me. I just want to make sure I am starting out on the right track.

In this particular case blue is the only color being changed. I am just learning the DX IDE.
May 9, 2011 at 12:27am
I see. FYI, I am a DX neophite, so I just commented on the function mathematically speaking and without any DX background.
May 9, 2011 at 12:40am
I'd use a proper triangle wave implementation with no internal state. Maybe something like:
1
2
3
4
5
6
7
8
double triangle_wave(unsigned t){
	t%=1000;
	if (t<250)
		return double(t)/250;
	if (t<750)
		return -double(t)/250+2;
	return double(t)/250-4;
}
Topic archived. No new replies allowed.