include header file

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
emily
Posts: 22
Joined: November 9th, 2019, 9:04 pm
Location: Kentucky

include header file

Post by emily » May 9th, 2020, 7:23 pm

How do you include a header filer where the compiler doesn't tell me cannot find.
Advanced 1 Lines.zip
(89.88 KiB) Downloaded 211 times

User avatar
emily
Posts: 22
Joined: November 9th, 2019, 9:04 pm
Location: Kentucky

Re: include header file

Post by emily » May 9th, 2020, 7:40 pm

LoL Nevermind I was using a beginner project and just had to drag and drop :geek: from the Advanced files.

User avatar
emily
Posts: 22
Joined: November 9th, 2019, 9:04 pm
Location: Kentucky

Re: include header file

Post by emily » May 9th, 2020, 10:08 pm

So now I get the following errors:

(29,60): error C2440: 'type cast': cannot convert from 'std::pair<int,int>' to 'Vec2'
(29,60): message : No constructor could take the source type, or constructor overload resolution was ambiguous
(29,77): error C2660: 'Graphics::DrawLine': function does not take 2 arguments
Advanced 1 Lines (2).zip
(101.61 KiB) Downloaded 151 times

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

Re: include header file

Post by albinopapa » May 10th, 2020, 5:31 am

Unfortunately, you cannot do what you are trying to do:

Code: Select all

		gfx.DrawLine({ 100.0f, 100.0f }, (Vec2)wnd.mouse.GetPos(), Colors::Magenta);
You cannot cast a std::pair<int, int> to a Vec2. You can create a constructor in Vec2 that takes an std::pair<int, int> then you could pass the mouse position and sort of cast to a Vec2.

In class _Vec2, create the following constructor

Code: Select all

	_Vec2( std::pair<int, int> const& p )
		:
		x( T( p.first ) ),
		y( T( p.second ) )
	{}
This takes in an std::pair<int, int> that you get when you call wnd.mouse.GetPos(), converts first and second members of std::pair to whatever the template parameter T is for the _Vec2, for instance you are using Vec2 which is an alias for _Vec2<float>, so this would convert your mouse position from int, int to float, float.

Now the original line works:

Code: Select all

		gfx.DrawLine({ 100.0f, 100.0f }, (Vec2)wnd.mouse.GetPos(), Colors::Magenta);
Could also be written as

Code: Select all

gfx.DrawLine( { 100.f, 100.f }, Vec2{ wnd.mouse.GetPos() }, Colors::Magenta );
You could also modify the Mouse class to return a Vec2 instead of an std::pair<int, int>.
You could also modify the DrawLine function to take in an std::pair<int, int>
You could have a free function that converts from std::pair<int, int> and returns a Vec2
You could also do:

Code: Select all

gfx.DrawLine( { 100.f, 100.f }, Vec2{ float( wnd.mouse.GetMouseX() ), float( wnd.mouse.GetMouseY() }, Colors::Magenta );
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
emily
Posts: 22
Joined: November 9th, 2019, 9:04 pm
Location: Kentucky

Re: include header file

Post by emily » May 10th, 2020, 7:07 pm

Now I'm getting an assertion failed on line 312. I'm wondering if their a problem with the math somewhere.
Advanced 1 Line.zip
(89.15 KiB) Downloaded 197 times

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

Re: include header file

Post by albinopapa » May 10th, 2020, 9:34 pm

You can always compare what chili has with what you have:

Your version:

Code: Select all

	float m = 0.0f;
	if (p1.x > p0.x)
	{
		m = (p1.y - p0.y) / (p1.x - p0.x);
	}

	if (p0.x > p1.x && m <= 1.0f)
	{
		if (p0.x > p1.x)
		{
			std::swap(p0, p1);
		}


		const float b = p0.y - m * p0.x;

		for (int x = (int)p0.x; x < (int)p1.x; x++)
		{
			const float y = m * (float)x + b;
			PutPixel(x, (int)y, c);
		}
	}
	else
	{
		if (p0.y > p1.y);
		{
			std::swap(p0, p1);
		}

		const float w = (p1.x - p0.x) / (p1.y - p0.y);
		const float p = p0.x - m * p0.y;

		for (int y = (int)p0.y; y < (int)p1.y; y++)
		{
			const float x = w * (float)y + p;
			PutPixel((int)x, y, c);
		}
	}
Chili's version:

Code: Select all

	float m = 0.0f;
	if( p1.x != p0.x )
	{
		m = (p1.y - p0.y) / (p1.x - p0.x);
	}

	if( p1.x != p0.x && std::abs( m ) <= 1.0f )
	{
		if( p0.x > p1.x )
		{
			std::swap( p0,p1 );
		}

		const float b = p0.y - m * p0.x;

		for( int x = (int)p0.x; x <= (int)p1.x; x++ )
		{
			const float y = m * (float)x + b;

			const int yi = (int)y;
			if( x >= 0 && x < ScreenWidth && yi >= 0 && yi < ScreenHeight )
			{
				PutPixel( x,yi,c );
			}
		}
	}
	else
	{
		if( p0.y > p1.y )
		{
			std::swap( p0,p1 );
		}

		const float w = (p1.x - p0.x) / (p1.y - p0.y);
		const float p = p0.x - w * p0.y;

		for( int y = (int)p0.y; y <= (int)p1.y; y++ )
		{
			const float x = w * (float)y + p;

			const int xi = (int)x;
			if( xi >= 0 && xi < ScreenWidth && y >= 0 && y < ScreenHeight )
			{
				PutPixel( xi,y,c );
			}
		}
	}
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

Post Reply