include header file
include header file
How do you include a header filer where the compiler doesn't tell me cannot find.
Re: include header file
LoL Nevermind I was using a beginner project and just had to drag and drop from the Advanced files.
Re: include header file
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
(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
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: include header file
Unfortunately, you cannot do what you are trying to do:
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
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:
Could also be written as
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.0f, 100.0f }, (Vec2)wnd.mouse.GetPos(), Colors::Magenta);
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 ) )
{}
Now the original line works:
Code: Select all
gfx.DrawLine({ 100.0f, 100.0f }, (Vec2)wnd.mouse.GetPos(), Colors::Magenta);
Code: Select all
gfx.DrawLine( { 100.f, 100.f }, Vec2{ wnd.mouse.GetPos() }, Colors::Magenta );
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
Re: include header file
Now I'm getting an assertion failed on line 312. I'm wondering if their a problem with the math somewhere.
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: include header file
You can always compare what chili has with what you have:
Your version:
Chili's version:
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);
}
}
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