Question about scaling sprites

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

Re: Question about scaling sprites

Post by albinopapa » 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 );
			}
		}
	}
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

WilsonHuang
Posts: 44
Joined: February 13th, 2019, 3:23 am

Re: Question about scaling sprites

Post by WilsonHuang » October 16th, 2019, 6:54 am

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?

User avatar
AleksiyCODE
Posts: 32
Joined: September 21st, 2019, 8:47 pm

Re: Question about scaling sprites

Post by AleksiyCODE » 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
I like ass

WilsonHuang
Posts: 44
Joined: February 13th, 2019, 3:23 am

Re: Question about scaling sprites

Post by WilsonHuang » October 17th, 2019, 1:18 am

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!
Last edited by WilsonHuang on November 15th, 2019, 2:43 am, edited 1 time in total.

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

Re: Question about scaling sprites

Post by albinopapa » October 17th, 2019, 7:10 am

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.
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

WilsonHuang
Posts: 44
Joined: February 13th, 2019, 3:23 am

Re: Question about scaling sprites

Post by WilsonHuang » November 15th, 2019, 2:35 am

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
					);
				}
			}
		}

	}

Post Reply