Example #1
0
void Application::DrawText( const Reader& reader, Book& book )
{
	sf::Shape bg;
	bg = sf::Shape::Rectangle( 5, 5, m_textOutputWidth, m_screenHeight-5, sf::Color( 182, 220, 255 ) );
	m_app.Draw( bg );

	int yPos = 0;
	int xPos = 10;

	vector<string> lstLines = book.GetReadLines();

	for ( int i = 0; i < lstLines.size(); i++ )
	{
		yPos += m_fontSize;
		cout << i << ", " << lstLines[i] << endl;
		string currentLine = lstLines[i];
		int beginIndex = 0;
		int endIndex = 0;
		int pulledLines = 1;

		if ( i == reader.GetCurrentLine() )
		{
			cout << endl << endl;
			cout << "-------------------------------------------" << endl;
			cout << endl << "WRITE LINE" << endl << currentLine << endl;
			cout << endl << "SPLIT UP" << endl;
		}

		// Account for word-wrapping
		while ( currentLine.length() * (m_fontSize/2) > m_textOutputWidth )
		{
			endIndex = ( 2 * m_textOutputWidth / m_fontSize ) - 10;
			// Adjust endIndex to be where a ' ' is.
			while ( currentLine[ endIndex ] != ' ' )
			{
				endIndex++;
			}

			sf::String textLine( currentLine.substr( beginIndex, endIndex ),
								m_font, m_fontSize );
			textLine.SetPosition( xPos, yPos );
			textLine.SetColor( sf::Color( 0, 0, 0 ) );

			if ( i == reader.GetCurrentLine() )
			{
				cout << pulledLines << ": " << currentLine.substr( beginIndex, endIndex ) << endl;
			}

			m_app.Draw( textLine );
			yPos += m_fontSize;
			currentLine.erase( beginIndex, endIndex );
			pulledLines++;
		}

		// Output remaining line
		sf::String textLine( currentLine, m_font, m_fontSize );
		textLine.SetPosition( xPos, yPos );
		textLine.SetColor( sf::Color( 0, 0, 0 ) );

		if ( i == reader.GetCurrentLine() )
		{
			cout << pulledLines << ": " << currentLine << endl;
		}

		m_app.Draw( textLine );
		yPos += m_fontSize;
	}

	sf::Sprite character;
	character.SetImage( m_talker );
	character.SetSubRect( sf::IntRect( m_characterFrame * 112, 0, m_characterFrame * 112 + 112, 192 ) );
	character.SetPosition( 600, m_screenHeight - (m_talker.GetHeight()*2) );
	character.SetScale( 2, 2 );
	m_app.Draw( character );

	if ( ++m_characterFrame > 2 )
	{
		m_characterFrame = 0;
	}
}