std::vector<CL_ClanImageStretch::ImagePart> CL_ClanImageStretch::calc_stretch_image_parts(const CL_Rect &dest, CL_Image &sprite, int sizing_left, int sizing_top, int sizing_right, int sizing_bottom)
{
	int src_width = sprite.get_width();
	int src_height = sprite.get_height();
	int dest_width = dest.get_width();
	int dest_height = dest.get_height();

	int src_grid_x[4];
	int src_grid_y[4];
	int dest_grid_x[4];
	int dest_grid_y[4];

	int rows = 0;
	int cols = 0;

	src_grid_x[cols++] = 0;
	src_grid_x[cols++] = sizing_left;
	src_grid_x[cols++] = src_width - sizing_right;
	src_grid_x[cols++] = src_width;

	src_grid_y[rows++] = 0;
	src_grid_y[rows++] = sizing_top;
	src_grid_y[rows++] = src_height - sizing_bottom;
	src_grid_y[rows++] = src_height;

	rows = 0;
	cols = 0;

	dest_grid_x[cols++] = dest.left;
	dest_grid_x[cols++] = dest.left + sizing_left;
	dest_grid_x[cols++] = dest.right - sizing_right;
	dest_grid_x[cols++] = dest.right;

	dest_grid_y[rows++] = dest.top;
	dest_grid_y[rows++] = dest.top + sizing_top;
	dest_grid_y[rows++] = dest.bottom - sizing_bottom;
	dest_grid_y[rows++] = dest.bottom;

	std::vector<ImagePart> image_parts;
	for (int y = 0; y < rows-1; y++)
	{
		for (int x = 0; x < cols-1; x++)
		{
			ImagePart part;
			part.source_rect = CL_Rect(src_grid_x[x], src_grid_y[y], src_grid_x[x+1], src_grid_y[y+1]);
			part.dest_rect = CL_Rect(dest_grid_x[x], dest_grid_y[y], dest_grid_x[x+1], dest_grid_y[y+1]);
			if (part.source_rect.get_width() > 0 && part.source_rect.get_height() > 0 &&
				part.dest_rect.get_width() > 0 && part.dest_rect.get_height() > 0)
			{
				image_parts.push_back(part);
			}
		}
	}

	return image_parts;
}
void CL_ClanImageStretch::draw_image(CL_GraphicContext &gc, const CL_Rect &rect, CL_Image &image, int sizing_left, int sizing_top, int sizing_right, int sizing_bottom)
{
	std::vector<ImagePart> image_parts = calc_stretch_image_parts(rect, image, sizing_left, sizing_top, sizing_right, sizing_bottom);
	size_t size = image_parts.size();
	for (size_t index = 0; index < size; index++)
		image.draw(gc, image_parts[index].source_rect, image_parts[index].dest_rect);
}
/** Builds the screen.
*/
void
CAChampionshipScreen::buildScreen() 
{
    // Background:
    //
    m_background.draw ( *CA_APP->graphicContext, CL_Rect(0, 0, CA_APP->width, CA_APP->height) );
    displayTitle();

    CL_Draw::fill( *CA_APP->graphicContext, CL_Rectf(left, top, right, bottom), CL_Colorf(0, 0, 0, 64) );
    
    const int rankWidth = 30;
    const int rankLeft = 0;

    const int nameWidth = 200;
    const int nameLeft = 50;

    const int addPtWidth = 30;
    const int addPtLeft = 260;

    const int totalPtWidth = 50;
    const int totalPtLeft = 300;

    const int caWidth = (totalPtLeft + totalPtWidth)*2;
    const int middleMargin = 30;
   

    const int caHeight = (barHeight * m_player.size())/2;
    const int marginTop = (top+bottom)/2 - caHeight/2;

    for (unsigned int rank=0; rank < m_player.size(); rank++)
    {
        std::ostringstream ossRankStr, ossTotalPoints, ossRacePoints;
        ossRankStr << rank + 1 << ".";
        ossTotalPoints << m_player[rank]->getTotalPoints();
        int marginLeft = (right+left)/2 + middleMargin/2;
        int upPos = rank - m_player.size()/2; 
        if (rank < m_player.size()/2 )
        {
            marginLeft -= caWidth/2 + middleMargin;
            upPos = rank;
        }
        // Buttons:
        //
        m_button.draw ( *CA_APP->graphicContext, CL_Rect(marginLeft+rankLeft, marginTop+barHeight*upPos, marginLeft+rankLeft+rankWidth, marginTop+(barHeight*(upPos+1))) );
        m_button.draw ( *CA_APP->graphicContext, CL_Rect(marginLeft+nameLeft, marginTop+barHeight*upPos, marginLeft+nameLeft+nameWidth, marginTop+(barHeight*(upPos+1))) );
        if (m_displayMode != DISPLAY_CHAMPIONSHIP)
        {
            for (int i=0; i<= int(m_displayMode); i++)
            {
                std::vector<Player*>::const_iterator it = std::find(m_runningPlayers[i].begin(), m_runningPlayers[i].end(), m_player[rank]);
                if (it != m_runningPlayers[i].end() && m_player[rank]->getRacePoints() != 0)
                {
                    CL_Image buttonToUse = (i==0 ? m_buttonEasy : (i==1)? m_buttonMedium : m_buttonHard); // Choose the button color
                    buttonToUse.draw ( *CA_APP->graphicContext, CL_Rect(marginLeft+addPtLeft, marginTop+barHeight*upPos, marginLeft+addPtLeft+addPtWidth, marginTop+(barHeight*(upPos+1))) );
                    ossRacePoints << "+" << m_player[rank]->getRacePoints();
                }
            }
        }
        m_button.draw ( *CA_APP->graphicContext, CL_Rect(marginLeft+totalPtLeft, marginTop+barHeight*upPos, marginLeft+totalPtLeft+totalPtWidth,  marginTop+(barHeight*(upPos+1))) );

        // Texts:
        //
        const int textPosX = marginLeft;
        const int textPosY = marginTop + 4 + barHeight*upPos;
        m_font.draw_text( *CA_APP->graphicContext, textPosX+rankLeft+rankWidth/2, textPosY,  ossRankStr.str());
        m_font.draw_text( *CA_APP->graphicContext, textPosX+nameLeft+nameWidth/2, textPosY, m_player[rank]->getName() );
        m_font.draw_text( *CA_APP->graphicContext, textPosX+addPtLeft+addPtWidth/2, textPosY, ossRacePoints.str() );
        m_font.draw_text( *CA_APP->graphicContext, textPosX+totalPtLeft+totalPtWidth/2, textPosY, ossTotalPoints.str() );
    }
}