Don't know why I didn't think of this sooner...

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Don't know why I didn't think of this sooner...

Post by albinopapa » December 16th, 2017, 10:42 pm

Code: Select all

template<class T, template<class> class RectType, class Func>
void for_each_XY( const RectType<T>& Rect, Func&& Fn )
{
	constexpr auto one = static_cast<T>( 1 );
	for( T y = Rect.top; y < Rect.bottom; y += one )
	{
		for( T x = Rect.left; x < Rect.right; x += one )
		{
			Fn( x, y );
		}
	}
}

// Usage

	template<typename E>
	void DrawSprite( int x,int y,RectI srcRect,const RectI& clip,const Surface& s,E effect )
	{
		assert( srcRect.left >= 0 );
		assert( srcRect.right <= s.GetWidth() );
		assert( srcRect.top >= 0 );
		assert( srcRect.bottom <= s.GetHeight() );
		if( x < clip.left )
		{
			srcRect.left += clip.left - x;
			x = clip.left;
		}
		if( y < clip.top )
		{
			srcRect.top += clip.top - y;
			y = clip.top;
		}
		if( x + srcRect.GetWidth() > clip.right )
		{
			srcRect.right -= x + srcRect.GetWidth() - clip.right;
		}
		if( y + srcRect.GetHeight() > clip.bottom )
		{
			srcRect.bottom -= y + srcRect.GetHeight() - clip.bottom;
		}

		auto DrawUsingEffect = [&effect, &s, this]( int X, int Y)
		{
			effect(
				s.GetPixel( sx,sy ),
				X + sx - srcRect.left,
				Y + sy - srcRect.top,
				*this
				);
		};
		for_each_XY(srcRect, DrawUsingEffect);
	}

So, after working in 2D for so long, I find myself making these nested for loops all over the place. So why dear God have I not thought about this until now. I could even put in the clipping and translation code in this function for drawing algorithms and not have to keep rewriting this crap everywhere. Chili, I think subliminally you gave me this idea with your functors video on sprite effects and the shaders from the 3D Fundamentals.
I could take this even further I suppose to make it take in multiple dimensions ( 1D, 2D, 3D ), but the STL already has a std::for_each that handles 1D arrays and I never iterate over 3D arrays, so meh.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Don't know why I didn't think of this sooner...

Post by chili » December 17th, 2017, 11:43 am

I like it.

I've been doing something similar with a Grid class that i created.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Don't know why I didn't think of this sooner...

Post by albinopapa » December 17th, 2017, 1:21 pm

I need to run some more tests, I think there is some overhead, but it could be the something I was doing. Initial results brought my fps down from 340 to 290 ( 2.941 milliseconds to 3.448 milliseconds ). Again, this could just be something I wasn't comparing correctly ( apples to oranges ). If everything inline, there shouldn't be this much overhead.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Don't know why I didn't think of this sooner...

Post by chili » December 18th, 2017, 12:00 pm

Hmm, i would reconsider a drop of > than 10%... technically it *should* be as fast, in a perfect world. Unfortunately the optimizing compiler is not always able to deliver the goods.
Chili

Post Reply