float getStrWidth(sf::Text const &ref) { float ret = static_cast<std::string const&>(ref.getString()) .length(); if (ref.getCharacterSize() <= 12) return ((ref.getCharacterSize() - 4)) * ret; if (ref.getCharacterSize() >= 20) return ((ref.getCharacterSize() - 8) * ret); return ((ref.getCharacterSize() - 5) * ret); }
void pauseMenu() { pause.setFont(myFont); pause.setCharacterSize(WIDTH / 16); pause.setStyle(sf::Text::Bold); pause.setColor(sf::Color::Black); pause.setString("PAUSE"); pause.setOrigin(pause.getLocalBounds().width / 2, pause.getLocalBounds().height / 2); pause.setPosition(WIDTH / 2, 3 * HEIGHT / 16); sf::Vector2f menuRectSize(3 * WIDTH / 16, HEIGHT / 8); for (int i = 0; i < pauseMenuRect.size(); i++) { pauseMenuRect[i].setSize(menuRectSize); pauseMenuRect[i].setFillColor(grey); pauseMenuRect[i].setOrigin(menuRectSize.x / 2, menuRectSize.y / 2); pauseMenuRect[i].setPosition(WIDTH / 2, 6 * HEIGHT / 16 + i * (5 * HEIGHT / 32)); pauseMenuText[i].setFont(myFont); pauseMenuText[i].setCharacterSize(2 * pause.getCharacterSize() / 5); pauseMenuText[i].setStyle(sf::Text::Bold); pauseMenuText[i].setColor(sf::Color::Black); pauseMenuText[i].setString(pauseMenuCommands[i]); pauseMenuText[i].setOrigin(pauseMenuText[i].getLocalBounds().width / 2, pauseMenuText[i].getLocalBounds().height / 2); pauseMenuText[i].setPosition(pauseMenuRect[i].getPosition().x, pauseMenuRect[i].getPosition().y); } BILLIARDS.draw(pause); for (int i = 0; i < pauseMenuRect.size(); i++) { BILLIARDS.draw(pauseMenuRect[i]); BILLIARDS.draw(pauseMenuText[i]); } }
float getStrHeight(sf::Text const &ref) { if (ref.getCharacterSize() <= 15) return (12.5); return (25); }
//Enemy sprite render. void update(float deltaTime, char** levelMap, struct config* config) { if(mDirection == 0) {mMovement.x = 0; mMovement.y = -mSpeed;} if(mDirection == 1) {mMovement.x = mSpeed; mMovement.y = 0;} if(mDirection == 2) {mMovement.x = 0; mMovement.y = mSpeed;} if(mDirection == 3) {mMovement.x = mSpeed; mMovement.y = 0;} mRect.left += mMovement.x * deltaTime; collision(levelMap, config); mRect.top += mMovement.y * deltaTime; collision(levelMap, config); //Enemy animation. mCurrentFrame += mAnimationSpeed * deltaTime; if(mCurrentFrame > mFrameCount) mCurrentFrame -= mFrameCount; if(mMovement.x > 0) mSprite.setTextureRect(sf::IntRect(mRect.width * int(mCurrentFrame), 925, mRect.width, mRect.height)); if(mMovement.x < 0) mSprite.setTextureRect(sf::IntRect(mRect.width * int(mCurrentFrame), 667, mRect.width, mRect.height)); if(mMovement.y > 0) mSprite.setTextureRect(sf::IntRect(mRect.width * int(mCurrentFrame), 535, mRect.width, mRect.height)); if(mMovement.y < 0) mSprite.setTextureRect(sf::IntRect(mRect.width * int(mCurrentFrame), 787, mRect.width, mRect.height)); mSprite.setPosition(mRect.left - offsetX, mRect.top - offsetY); mTextName.setPosition(mRect.left - offsetX, mRect.top - mTextName.getCharacterSize() - offsetY); }
void endGame() { endTitle.setFont(myFont); endTitle.setCharacterSize(WIDTH / 16); endTitle.setStyle(sf::Text::Bold); endTitle.setColor(sf::Color::Black); string str; if (end1P) { stringstream message; message << "Final score: " << mover; str = message.str(); } else if (end2P) { stringstream message; message << "Player " << mover << " wins!"; str = message.str(); } endTitle.setString(str); endTitle.setOrigin(endTitle.getLocalBounds().width / 2, endTitle.getLocalBounds().height / 2); endTitle.setPosition(WIDTH / 2, 2 * HEIGHT / 5); sf::Vector2f menuRectSize(3 * WIDTH / 16, HEIGHT / 8); for (int i = 0; i < endMenuRect.size(); i++) { endMenuRect[i].setSize(menuRectSize); endMenuRect[i].setFillColor(grey); endMenuRect[i].setOrigin(menuRectSize.x / 2, menuRectSize.y / 2); endMenuRect[i].setPosition(WIDTH / 2, 4 * HEIGHT / 5); endMenuText[i].setFont(myFont); endMenuText[i].setCharacterSize(2 * pause.getCharacterSize() / 5); endMenuText[i].setStyle(sf::Text::Bold); endMenuText[i].setColor(sf::Color::Black); endMenuText[i].setString(endMenuCommands[i]); endMenuText[i].setOrigin(endMenuText[i].getLocalBounds().width / 2, endMenuText[i].getLocalBounds().height / 2); endMenuText[i].setPosition(endMenuRect[i].getPosition().x, endMenuRect[i].getPosition().y); } BILLIARDS.draw(endTitle); for (int i = 0; i < endMenuRect.size(); i++) { BILLIARDS.draw(endMenuRect[i]); BILLIARDS.draw(endMenuText[i]); } }
//Player sprite render. void update(float deltaTime, char** levelMap, struct config* config) { //Player controls (testing). if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) mMovement.y -= mSpeed; if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) mMovement.y += mSpeed; if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) mMovement.x -= mSpeed; if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) mMovement.x += mSpeed; mRect.left += mMovement.x * deltaTime; collision(0, levelMap, config); mRect.top += mMovement.y * deltaTime; collision(1, levelMap, config); //Player animation. mCurrentFrame += mAnimationSpeed * deltaTime; if(mCurrentFrame > mFrameCount) mCurrentFrame -= mFrameCount; if(mMovement.x > 0) mSprite.setTextureRect(sf::IntRect(mRect.width * int(mCurrentFrame), 925, mRect.width, mRect.height)); if(mMovement.x < 0) mSprite.setTextureRect(sf::IntRect(mRect.width * int(mCurrentFrame), 667, mRect.width, mRect.height)); if(mMovement.y > 0) mSprite.setTextureRect(sf::IntRect(mRect.width * int(mCurrentFrame), 535, mRect.width, mRect.height)); if(mMovement.y < 0) mSprite.setTextureRect(sf::IntRect(mRect.width * int(mCurrentFrame), 787, mRect.width, mRect.height)); mSprite.setPosition(mRect.left - offsetX, mRect.top - offsetY); //HP. if(mHP / mMaxHp >= 0.6) mHpSprite.setColor(sf::Color::Green); else if((mHP / mMaxHp >= 0.35) && (mHP / mMaxHp < 0.6)) mHpSprite.setColor(sf::Color::Yellow); else if(mHP / mMaxHp < 0.35) mHpSprite.setColor(sf::Color::Red); mHpSprite.setTextureRect(sf::IntRect(100 * (1 - mHP / mMaxHp), 0, 100, 10)); mHpSprite.setPosition(mRect.left - offsetX, mRect.top + mRect.height - offsetY); mTextName.setPosition(mRect.left - offsetX, mRect.top - mTextName.getCharacterSize() - offsetY); //- //Camera scrolling. if(mRect.left > config->screenWidth / 2 - mRect.width / 2) offsetX = mRect.left - (config->screenWidth / 2 - mRect.width / 2); if(mRect.top > config->screenHeight / 2 - mRect.height / 2) offsetY = mRect.top - (config->screenHeight / 2 - mRect.height / 2); //Stopping the player. mMovement.x = 0; mMovement.y = 0; }
Primitive::Ptr Renderer::CreateText( const sf::Text& text, sf::Color background_color_hint ) { Primitive::Ptr primitive( new Primitive ); const sf::Font& font = text.getFont(); unsigned int character_size = text.getCharacterSize(); sf::Color color = text.getColor(); if( m_preblend ) { color = sf::Color::White; } sf::Vector2f atlas_offset = LoadFont( font, character_size, background_color_hint, text.getColor() ); const sf::String& str = text.getString(); std::size_t length = str.getSize(); float horizontal_spacing = static_cast<float>( font.getGlyph( L' ', character_size, false ).advance ); float vertical_spacing = static_cast<float>( font.getLineSpacing( character_size ) ); sf::Vector2f position( std::floor( text.getPosition().x + .5f ), std::floor( text.getPosition().y + static_cast<float>( character_size ) + .5f ) ); const static float tab_spaces = 2.f; sf::Uint32 previous_character = 0; for( std::size_t index = 0; index < length; ++index ) { sf::Uint32 current_character = str[index]; position.x += static_cast<float>( font.getKerning( previous_character, current_character, character_size ) ); switch( current_character ) { case L' ': position.x += horizontal_spacing; continue; case L'\t': position.x += horizontal_spacing * tab_spaces; continue; case L'\n': position.y += vertical_spacing; position.x = 0.f; continue; case L'\v': position.y += vertical_spacing * tab_spaces; continue; default: break; } const sf::Glyph& glyph = font.getGlyph( current_character, character_size, false ); Primitive::Vertex vertex0; Primitive::Vertex vertex1; Primitive::Vertex vertex2; Primitive::Vertex vertex3; vertex0.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left ), static_cast<float>( glyph.bounds.top ) ); vertex1.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left ), static_cast<float>( glyph.bounds.top + glyph.bounds.height ) ); vertex2.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left + glyph.bounds.width ), static_cast<float>( glyph.bounds.top ) ); vertex3.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left + glyph.bounds.width ), static_cast<float>( glyph.bounds.top + glyph.bounds.height ) ); vertex0.color = color; vertex1.color = color; vertex2.color = color; vertex3.color = color; // Let SFML cast the Rect for us. sf::FloatRect texture_rect( glyph.textureRect ); vertex0.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left, texture_rect.top ); vertex1.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left, texture_rect.top + texture_rect.height ); vertex2.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left + texture_rect.width, texture_rect.top ); vertex3.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left + texture_rect.width, texture_rect.top + texture_rect.height ); primitive->AddVertex( vertex0 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex2 ); primitive->AddVertex( vertex2 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex3 ); position.x += static_cast<float>( glyph.advance ); previous_character = current_character; } AddPrimitive( primitive ); return primitive; }
void centerTextPosition( const sf::RectangleShape& rect, sf::Text& text ) { text.setPosition( rect.getPosition().x + ( rect.getLocalBounds().width / 2 ), rect.getPosition().y + ( rect.getLocalBounds().height / 2 ) - ( text.getCharacterSize() / 2 ) ); }