Page 2 of 2

Re: Question about scaling sprites

Posted: October 11th, 2019, 9:09 am
by albinopapa
Here's the corrected version:

Code: Select all

	template<typename E>
	void DrawSprite( int x, int y, RectI srcRect, const RectI& clipRect, const Surface& s, E effect, Graphics& _graphics, float scale = 1.f )noexcept
	{
		auto const srcScale = 1.f / scale;
		auto const scaledWidth = int( srcRect.GetWidth() * scale );
		auto const scaledHeight = int( srcRect.GetHeight() * scale );

		auto const dst = RectI{
			std::max( -x, clipRect.left ),
			std::min( clipRect.GetWidth() - x, scaledWidth ),
			std::max( -y, clipRect.top ),
			std::min( clipRect.GetHeight() - y, scaledHeight )
		};

		for( int dy = dst.top; dy < dst.bottom; ++dy )
		{
			auto const sy = srcRect.top + int( ( float( dy ) * srcScale ) );
			for( int dx = dst.left; dx < dst.right; ++dx )
			{
				auto const sx = srcRect.left + int( float( dx ) * srcScale );
				effect( s.GetPixel( sx, sy ), x + dx, y + dy, *this );
			}
		}
	}

Re: Question about scaling sprites

Posted: October 16th, 2019, 6:54 am
by WilsonHuang
albinopapa wrote:
October 11th, 2019, 9:09 am
Here's the corrected version:

Code: Select all

	template<typename E>
	void DrawSprite( int x, int y, RectI srcRect, const RectI& clipRect, const Surface& s, E effect, Graphics& _graphics, float scale = 1.f )noexcept
	{
		auto const srcScale = 1.f / scale;
		auto const scaledWidth = int( srcRect.GetWidth() * scale );
		auto const scaledHeight = int( srcRect.GetHeight() * scale );

		auto const dst = RectI{
			std::max( -x, clipRect.left ),
			std::min( clipRect.GetWidth() - x, scaledWidth ),
			std::max( -y, clipRect.top ),
			std::min( clipRect.GetHeight() - y, scaledHeight )
		};

		for( int dy = dst.top; dy < dst.bottom; ++dy )
		{
			auto const sy = srcRect.top + int( ( float( dy ) * srcScale ) );
			for( int dx = dst.left; dx < dst.right; ++dx )
			{
				auto const sx = srcRect.left + int( float( dx ) * srcScale );
				effect( s.GetPixel( sx, sy ), x + dx, y + dy, *this );
			}
		}
	}
Well, look like we need to modify DrawSprite function, and thanks for the implementation. To make sure i know how to do it, I'll try to make my own version, and take yours as the answer.
BTW, I'm now at Intermediate Tutorial 18.1 [Virtual Funcs. / Polymorphism]. Will I miss some important concepts if I jump right into 3D fundamental series?

Re: Question about scaling sprites

Posted: October 16th, 2019, 4:17 pm
by AleksiyCODE
I tried going through 3Dfundamentals while watching Intermediate. At about 3Dfund Ep7 I stopped and decided to finish Intermediate and advanced first. It seems that it is the way tutorials are ment to be watched

Re: Question about scaling sprites

Posted: October 17th, 2019, 1:18 am
by WilsonHuang
AleksiyCODE wrote:
October 16th, 2019, 4:17 pm
I tried going through 3Dfundamentals while watching Intermediate. At about 3Dfund Ep7 I stopped and decided to finish Intermediate and advanced first. It seems that it is the way tutorials are ment to be watched
Thanks for your advice. I think I'll finish Intermediate and Advanced lessons first!

Re: Question about scaling sprites

Posted: October 17th, 2019, 7:10 am
by albinopapa
Glad someone chimed in here that has gone through it all. I went through the original series years before chili made the 3D fundamentals tuts, so I couldn't say either way. You will benefit from the Intermediate and Advance because they introduce you to some concepts that are in the 3D fundamentals tuts. The Advanced series has some math that helps for sure.

Re: Question about scaling sprites

Posted: November 15th, 2019, 2:35 am
by WilsonHuang
I just finish my version few weeks ago. Thanks albinopapa for the tip.
I'll share my version here.

Code: Select all

	template<typename E>
	void DrawSprite(int x, int y, RectI source, const RectI& clip, const Surface& surface, E effect, float scale = 1.0f)
	{
		assert(source.left >= 0);
		assert(source.right <= surface.GetWidth());
		assert(source.top >= 0);
		assert(source.bottom <= surface.GetHeight());
		assert(scale > 0.0f);

		const RectI scaleRect =
			RectI(
			x, 
			x + int(source.GetWidth() * scale),
			y,
			y + int(source.GetHeight() * scale)
			);

		for (int scaleY = scaleRect.top; scaleY < scaleRect.bottom; scaleY++)
		{
			for (int scaleX = scaleRect.left; scaleX < scaleRect.right; scaleX++)
			{
				const int sourceX = int((scaleX - scaleRect.left) / scale) + source.left;
				const int sourceY = int((scaleY - scaleRect.top) / scale) + source.top;

				if (clip.Contains({ scaleX, scaleY }))
				{
					effect(
						surface.GetPixel(sourceX, sourceY),
						scaleX,
						scaleY,
						*this
					);
				}
			}
		}

	}