Snek function and the brd object

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
CKatt
Posts: 15
Joined: March 2nd, 2018, 8:37 pm

Snek function and the brd object

Post by CKatt » April 21st, 2018, 5:20 pm

So, I know that the answer to this question is probably large in scope but I'm not sure exactly what to be asking. So here it is.

Code: Select all

void Snake::Draw(Board & brd)
{
	for (int i = 0; i < nSegments; ++i)
	{
		segments[i].Draw(brd);
	}
}
In the line " segments.Draw(brd); " what exactly is brd doing. What I'm trying to understand is where the locations are coming from.

What I do know is that the sneks initial position it defined in the constructor for snek and then the delta position is added to it to move it. I understand that the board class is handling all the drawing but I don’t fully see why we are passing the brd object into the Draw function and not a location.


Thanks for the help guys.

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Snek function and the brd object

Post by Yumtard » April 21st, 2018, 6:05 pm

The segments know their locations.

Snake::Draw basically just calls Segment::Draw which calls Board::DrawSegment. To be able to call Board::DrawSegment from segment, the segments draw function need to get a reference to the board class.
When calling Board::DrawSegment you're passing the segments location and color

step 1

Code: Select all

void Snake::Draw( Board& brd ) const
{
	for( const auto& s : segments )
	{
		s.Draw( brd );
	}
}
step 2

Code: Select all

void Snake::Segment::Draw( Board& brd ) const
{
	brd.DrawCell( loc,c );
}
step 3

Code: Select all

void Board::DrawCell( const Location & loc,Color c )
{
	assert( loc.x >= 0 );
	assert( loc.x < width );
	assert( loc.y >= 0 );
	assert( loc.y < height );

	const int off_x = x + borderWidth + borderPadding;
	const int off_y = y + borderWidth + borderPadding;

	gfx.DrawRectDim( loc.x * dimension + off_x + cellPadding,loc.y * dimension + off_y + cellPadding,dimension - cellPadding * 2,dimension - cellPadding * 2,c );
}

Post Reply