bool CreditsMenu::isInRect(sf::Vector2i position, sf::RectangleShape menuItemRect) { return(menuItemRect.getPosition().y < position.y && menuItemRect.getPosition().y + menuItemRect.getSize().y > position.y && menuItemRect.getPosition().x < position.x && menuItemRect.getPosition().x + menuItemRect.getSize().x > position.x); }
//Ugly collision detection! bool Game::checkColl(sf::RectangleShape first, sf::RectangleShape second) const { double firstMaxX = first.getPosition().x + first.getSize().x/2.0; double firstMinX = first.getPosition().x - first.getSize().x/2.0; double firstMaxY = first.getPosition().y + first.getSize().y/2.0; double firstMinY = first.getPosition().y - first.getSize().y/2.0; double secondMaxX = second.getPosition().x + second.getSize().x/2.0; double secondMinX = second.getPosition().x - second.getSize().x/2.0; double secondMaxY = second.getPosition().y + second.getSize().y/2.0; double secondMinY = second.getPosition().y - second.getSize().y/2.0; if (firstMaxX < secondMinX || firstMaxY < secondMinY || firstMinX > secondMaxX || firstMinY > secondMaxY) { return false; } else { return true; } }
bool CollisionChecker::IsPossibleCollisionWithTopOrBottomSide(sf::RectangleShape &r1, sf::RectangleShape &r2) { if(r1.getPosition().x+r1.getGlobalBounds().width > r2.getPosition().x && r1.getPosition().x < r2.getPosition().x+r2.getGlobalBounds().width){ return true; } return false; }
bool CollisionChecker::IsPossibleCollisionWithLeftOrRightSide(sf::RectangleShape &r1, sf::RectangleShape &r2) { if(r1.getPosition().y+r1.getGlobalBounds().height > r2.getPosition().y && r1.getPosition().y < r2.getPosition().y+r2.getGlobalBounds().height){ return true; } return false; }
void Physic(float time) { Control(time); if (recShape->getPosition().x <= 50) recShape->setPosition(sf::Vector2f(50, recShape->getPosition().y)); else if (recShape->getPosition().x >= 974) recShape->setPosition(sf::Vector2f(974, recShape->getPosition().y)); }
void WindowCreateThing::init(sf::RenderWindow &window) { rectTitle.setSize(sf::Vector2f(window.getSize().x*0.45, window.getSize().y*0.07)); rectTitle.setPosition(sf::Vector2f(window.getSize().x/2-(rectTitle.getSize().x/2), window.getSize().x/4-(rectTitle.getSize().y/2))); rectTitle.setFillColor(sf::Color(158, 158, 158)); rectTitle.setOutlineColor(sf::Color::Black); rectTitle.setOutlineThickness(1.f); rectMain.setSize(sf::Vector2f(window.getSize().x*0.45,window.getSize().y*0.45)); rectMain.setPosition(sf::Vector2f(rectTitle.getPosition().x, rectTitle.getPosition().y+rectTitle.getSize().y)); rectMain.setFillColor(sf::Color::White); rectMain.setOutlineColor(sf::Color::Black); rectMain.setOutlineThickness(1.f); load(); starting_position = sf::Mouse::getPosition(window); textTitle.setFont(font); textTitle.setString("CreateThings.txt"); textTitle.setCharacterSize(24); textTitle.setColor(sf::Color::White); textTitle.setPosition(sf::Vector2f(rectTitle.getPosition().x+rectTitle.getSize().x*0.3, rectTitle.getPosition().y+rectTitle.getSize().y*0.1)); //textTitle.setPosition(sf::Vector2f(400,10)); textClose.setFont(font); textClose.setString("X"); textClose.setStyle(sf::Text::Bold); textClose.setCharacterSize(35); textClose.setColor(sf::Color::White); textClose.setPosition(sf::Vector2f(rectTitle.getPosition().x+rectTitle.getSize().x-30, rectTitle.getPosition().y+rectTitle.getSize().y*0.05)); ///// FOLDER ICONE textureFolder.setSmooth(true); spriteFodler.setTexture(textureFolder); sf::Vector2f targetSize(25.0f, 25.0f); spriteFodler.setScale( targetSize.x / spriteFodler.getLocalBounds().width, targetSize.y / spriteFodler.getLocalBounds().height); spriteFodler.setPosition(sf::Vector2f(textTitle.getPosition().x-targetSize.x, textTitle.getPosition().y)); ///// CLOSE ICONE /*textureClose.setSmooth(true); spriteClose.setTexture(textureClose); sf::Vector2f targetSize2(window.getSize().y*0.07, window.getSize().y*0.07); spriteClose.setScale( targetSize2.x / spriteClose.getLocalBounds().width, targetSize2.y / spriteClose.getLocalBounds().height); spriteClose.setPosition(rectTitle.getPosition().x+rectTitle.getSize().x-targetSize2.x, rectTitle.getPosition().y);*/ }
//Creates a sf::rectangleshape representing a button with given color Button::Button(sf::RectangleShape &rect, sf::Color col) : mRect(rect), mMoveToPosition(rect.getPosition().x, rect.getPosition().y), mDirection(0, 0), mIsOnPosition(true), mSpeed(100), mMode(RectangleShape) { mRect.setPosition(rect.getPosition().x, rect.getPosition().y); mRect.setFillColor(col); }
bool CollisionChecker::IsCollidingWithRightSide(sf::RectangleShape &r1, sf::RectangleShape &r2) { if(r1.getPosition().x <= r2.getPosition().x+r2.getGlobalBounds().width && r1.getPosition().x >= r2.getPosition().x && r1.getPosition().y+r1.getGlobalBounds().height > r2.getPosition().y && r1.getPosition().y < r2.getPosition().y+r2.getGlobalBounds().height){ return true; } return false; }
Button::Button(ResourceHandler &handler, sf::RectangleShape &rect, std::string textureName, std::string hoverName): mTextureName(textureName), mRect(rect), mHoverRect(rect), mMoveToPosition(rect.getPosition().x, rect.getPosition().y), mDirection(0, 0), mIsOnPosition(true), mSpeed(100), mMode(RectangleShape) { mRect.setTexture(handler.getTexture(textureName)); mHoverRect.setTexture(handler.getTexture(hoverName)); }
bool ifCollide(const sf::RectangleShape& A, const sf::RectangleShape& B) { float aRad = A.getGlobalBounds().height; float bRad = B.getGlobalBounds().height; float length = (aRad + bRad)/2.f; sf::Vector2f aPos = A.getPosition(); sf::Vector2f bPos = B.getPosition(); sf::Vector2f diff = aPos - bPos; float magn = sqrt(diff.x*diff.x + diff.y*diff.y); return magn <= length; }
void Control(float time) { /*if (event.type == sf::Event::KeyPressed) { if (event.key.code == sf::Keyboard::A || event.key.code == sf::Keyboard::Left) recShape->setPosition(sf::Vector2f(recShape->getPosition().x - 10. * time, recShape->getPosition().y)); else if (event.key.code == sf::Keyboard::D || event.key.code == sf::Keyboard::Right) recShape->setPosition(sf::Vector2f(recShape->getPosition().x + 10. * time, recShape->getPosition().y)); std::cout << "press"; }*/ if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) recShape->setPosition(sf::Vector2f(recShape->getPosition().x - 0.5 * time, recShape->getPosition().y)); else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) recShape->setPosition(sf::Vector2f(recShape->getPosition().x + 0.5 * time, recShape->getPosition().y)); }
void click(sf::RectangleShape rectangle,sf::Sprite &sprite, bool &clicked, bool &player, sf::Texture &crossTexture, sf::Texture &circleTexture, sf::Event &event) { if (((rectangle.getPosition().x <= event.mouseButton.x && event.mouseButton.x <= rectangle.getLocalBounds().width + rectangle.getPosition().x) && (rectangle.getPosition().y <= event.mouseButton.y && event.mouseButton.y <= rectangle.getLocalBounds().height + rectangle.getPosition().y)) && !(clicked)) { std::cout << "You clicked on a rectangle" << std::endl; clicked = true; if (player) { std::cout << "cross" << std::endl; sprite.setTexture(crossTexture); } else { std::cout << "circle" << std::endl; sprite.setTexture(circleTexture); } player = !player; } }
void game_update(float elapsedTime) { static float moveSpeed = 100.0f; color.a += 1; r.setFillColor(color); if(r.getPosition().x + r.getSize().x > g_pEngine->getScreenWidth() || r.getPosition().x < 0) { moveDir *= -1; moveSpeed += 1; } r.move(moveSpeed * elapsedTime * moveDir, 0); }
bool Ray::ClipVector(int axis, sf::RectangleShape& boxIn) { float tempClipFractionLow = 0; float tempClipFractionHigh = 1;//temp Fractions //gets fraction of vector that fits inside of bounding box if(axis == 0)//x axis { tempClipFractionLow = (((boxIn.getPosition().x - boxIn.getGlobalBounds().width/2) - startPos.x)/(endPos.x - startPos.x)); tempClipFractionHigh = (((boxIn.getPosition().x + boxIn.getGlobalBounds().width/2) - startPos.x)/(endPos.x - startPos.x)); } else if(axis == 1)//y axis { tempClipFractionLow = (((boxIn.getPosition().y + boxIn.getGlobalBounds().height/2) - startPos.y)/(endPos.y - startPos.y)); tempClipFractionHigh = (((boxIn.getPosition().y - boxIn.getGlobalBounds().height/2) - startPos.y)/(endPos.y - startPos.y)); } else { return false; } if(tempClipFractionHigh < tempClipFractionLow) { swap(tempClipFractionHigh,tempClipFractionLow); } if(tempClipFractionHigh < clipFractionLow) { return false; } else if(tempClipFractionLow > clipFractionHigh) { return false; } clipFractionLow = max(tempClipFractionLow,clipFractionLow); clipFractionHigh = min(tempClipFractionHigh,clipFractionHigh); if(clipFractionLow > clipFractionHigh) { return false; } return true; }
massCenter getMassCenter(){ massCenter mc; mc.x = playerSprite.getPosition().x + (playerSprite.getSize().x/2); mc.y = playerSprite.getPosition().y + (playerSprite.getSize().y/2); return mc; }
//set square (user) position void set_square_position(){ sf::Vector2f v = square.getPosition(); if(v.x < 0){ square.setPosition(0, window_y - shape_size);} else if(v.x > window_x){ square.setPosition(window_x - shape_size, window_y - shape_size);} }
void PongGame::updatePositionOfPlayer(sf::RectangleShape &player) { sf::FloatRect topRectangleGlobalBounds = topRectangle.getGlobalBounds(); sf::FloatRect bottomRectangleGlobalBounds = bottomRectangle.getGlobalBounds(); sf::Vector2f playerPosition = player.getPosition(); float topConstraint = topRectangleGlobalBounds.top + topRectangleGlobalBounds.height + 5; float bottomConstraint = bottomRectangleGlobalBounds.top - player.getSize().y - 5; playerPosition.y = getYCoordinate((int const) playerPosition.y, (int const) topConstraint, (int const) bottomConstraint); player.setPosition(playerPosition); }
void Game::loadLevel() { m_walls.clear(); m_start = m_end = sf::RectangleShape(); std::istringstream level(levels[m_level]); int width = 0, height = 0; level >> width >> height; const int dx = m_videoMode.width / width, dy = m_videoMode.height / height; m_window.setSize(sf::Vector2u(dx * width, dy * height)); m_pointer.setSize(sf::Vector2f(1, 1) * (std::min(dx, dy) * .6f)); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { char c; level >> c; sf::RectangleShape wall(sf::Vector2f(dx, dy)); wall.setPosition(x * dx, y * dy); if (c == '+') m_walls.push_back(wall); else if (c == 'S') { wall.setFillColor(sf::Color::Green); m_start = wall; } else if (c == 'E') { wall.setFillColor(sf::Color::Red); m_end = wall; } } } sf::Mouse::setPosition(sf::Vector2i(m_start.getPosition().x + dx / 2.f, m_start.getPosition().y + dy / 2.f), m_window); }
void Game::update() { const sf::Vector2i mouse(sf::Mouse::getPosition(m_window)); m_pointer.setPosition(mouse.x - (m_pointer.getSize().x / 2), mouse.y - (m_pointer.getSize().y / 2)); if (not sf::FloatRect(0, 0, m_window.getSize().x, m_window.getSize().y).intersects(sf::FloatRect(m_pointer.getPosition(), m_pointer.getSize()))) loadLevel(); { int count = 0; for (int i = 0; i < m_pointer.getPointCount(); ++i) count += sf::FloatRect(m_end.getPosition(), m_end.getSize()).contains (m_pointer.getPosition() + m_pointer.getPoint(i)); if (count == m_pointer.getPointCount()) ++m_level, loadLevel(); } for (const auto& wall: m_walls) for (int i = 0; i < m_pointer.getPointCount(); ++i) if (sf::FloatRect(wall.getPosition(), wall.getSize()).contains (m_pointer.getPosition() + m_pointer.getPoint(i))) loadLevel(); }
bool Snake::move(Direction newDirection, Game& game) { if (newDirection == direction) { return move(game); } const sf::RectangleShape firstShape = blocks[blocks.size() - 1].getShape(); int x; int y; if (direction == Direction::Up && newDirection == Direction::Left) { x = firstShape.getPosition().x; y = firstShape.getPosition().y; } else if (direction == Direction::Up && newDirection == Direction::Right) { x = firstShape.getPosition().x + thickness; y = firstShape.getPosition().y; } else if (direction == Direction::Left && newDirection == Direction::Up) { x = firstShape.getPosition().x; y = firstShape.getPosition().y; } else if (direction == Direction::Left && newDirection == Direction::Down) { x = firstShape.getPosition().x; y = firstShape.getPosition().y + thickness; } else if (direction == Direction::Down && newDirection == Direction::Left) { x = firstShape.getPosition().x; y = firstShape.getPosition().y + firstShape.getSize().y - thickness; } else if (direction == Direction::Down && newDirection == Direction::Right) { x = firstShape.getPosition().x + thickness; y = firstShape.getPosition().y + firstShape.getSize().y - thickness; } else if (direction == Direction::Right && newDirection == Direction::Up) { x = firstShape.getPosition().x + firstShape.getSize().x - thickness; y = firstShape.getPosition().y; } else if (direction == Direction::Right && newDirection == Direction::Down) { x = firstShape.getPosition().x + firstShape.getSize().x - thickness; y = firstShape.getPosition().y + thickness; } else { return move(game); } Block block = Block(x, y, thickness, newDirection, color); blocks.push_back(block); direction = newDirection; return move(game); }
void WindowCreateThing::update(sf::RenderWindow &window) { sf::FloatRect test(rectTitle.getPosition().x, rectTitle.getPosition().y, rectTitle.getSize().x, rectTitle.getSize().y); if(test.contains(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y)){ if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && boo){ starting_position.x = sf::Mouse::getPosition(window).x-rectTitle.getPosition().x; starting_position.y = sf::Mouse::getPosition(window).y-rectTitle.getPosition().y; boo = false; } if( sf::Event::MouseMoved && sf::Mouse::isButtonPressed(sf::Mouse::Left) ){ rectTitle.setPosition( sf::Mouse::getPosition(window).x-starting_position.x, sf::Mouse::getPosition(window).y-starting_position.y ); rectMain.setPosition(sf::Vector2f(rectTitle.getPosition().x, rectTitle.getPosition().y+rectTitle.getSize().y)); textTitle.setPosition(sf::Vector2f(rectTitle.getPosition().x+rectTitle.getSize().x*0.3, rectTitle.getPosition().y+rectTitle.getSize().y*0.1)); spriteFodler.setPosition(sf::Vector2f(textTitle.getPosition().x-25.0f, textTitle.getPosition().y)); } if(!sf::Mouse::isButtonPressed(sf::Mouse::Left)){ boo = true; } } if(!sf::Mouse::isButtonPressed(sf::Mouse::Left)){ boo = true; } }
float x() const noexcept { return shape.getPosition().x; }
float CollisionChecker::GetDistanceToCollisionWithRightSide(sf::RectangleShape &r1, sf::RectangleShape &r2) { return r1.getPosition().x - (r2.getPosition().x+r2.getGlobalBounds().width); }
float x() { return shape.getPosition().x; }
void IsSelected(Selection s, sf::RenderWindow &Window) { if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) { if(rect.getGlobalBounds().intersects(s.rect.getGlobalBounds()) || (sf::Mouse::getPosition(Window).x > rect.getPosition().x && sf::Mouse::getPosition(Window).x < rect.getPosition().x + rect.getSize().x && sf::Mouse::getPosition(Window).y > rect.getPosition().y && sf::Mouse::getPosition(Window).y < rect.getPosition().y + rect.getSize().y)) { selected = true; rect.setFillColor(sf::Color::Magenta); } else { selected = false; rect.setFillColor(sf::Color::Green); } } }
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 ) ); }
void fixRect( sf::RenderWindow& window, sf::RectangleShape& rect ) { sf::Vector2i mousePos = sf::Mouse::getPosition( window ); sf::Vector2f rectPos = rect.getPosition(); rect.setSize( sf::Vector2f( mousePos.x - rectPos.x, mousePos.y - rectPos.y ) ); }
int main(){ logger.logNoEndl("Setting up window..."); window.create(sf::VideoMode(430,430), "GL"); logger.continueln(" done!"); logger.logNoEndl("Setting up variables..."); rect1.setFillColor(sf::Color(200,200,200)); rect1.setOutlineColor(sf::Color(0,0,0)); rect1.setOutlineThickness(-2); rect1.setPosition(10,10); rect2.setFillColor(sf::Color(200,200,200)); rect2.setOutlineColor(sf::Color(0,0,0)); rect2.setOutlineThickness(-2); rect2.setPosition(220,10); rect3.setFillColor(sf::Color(200,200,200)); rect3.setOutlineColor(sf::Color(0,0,0)); rect3.setOutlineThickness(-2); rect3.setPosition(10,220); rect4.setFillColor(sf::Color(200,200,200)); rect4.setOutlineColor(sf::Color(0,0,0)); rect4.setOutlineThickness(-2); rect4.setPosition(220,220); sf::Font font; if (!font.loadFromFile("arial.ttf")){ logger.log(Logger::LogType::Error, "Arial font not found. Cannot continue..."); return 1; } sf::Text label1("Solid White", font); label1.setColor(sf::Color::Black); label1.setPosition(rect1.getPosition().x + rect1.getSize().x / 2 - label1.getLocalBounds().width / 2, rect1.getPosition().y + rect1.getSize().y / 2 - label1.getLocalBounds().height / 2 - 10); sf::Text label2("Marquee", font); label2.setColor(sf::Color::Black); label2.setPosition(rect2.getPosition().x + rect2.getSize().x / 2 - label2.getLocalBounds().width / 2, rect2.getPosition().y + rect2.getSize().y / 2 - label2.getLocalBounds().height / 2 - 10); sf::Text label3("Color Cycle", font); label3.setColor(sf::Color::Black); label3.setPosition(rect3.getPosition().x + rect3.getSize().x / 2 - label3.getLocalBounds().width / 2, rect3.getPosition().y + rect3.getSize().y / 2 - label3.getLocalBounds().height / 2 - 10); sf::Text label4("Pew", font); label4.setColor(sf::Color::Black); label4.setPosition(rect4.getPosition().x + rect4.getSize().x / 2 - label4.getLocalBounds().width / 2, rect4.getPosition().y + rect4.getSize().y / 2 - label4.getLocalBounds().height / 2 - 10); unsigned short r = 0, g = 0, b = 0; logger.continueln(" done!"); logger.log("Running until told to stop."); bool running = true; while(running){ sf::Event event; while (window.pollEvent(event)){ if (event.type == sf::Event::Closed){ running = false; } else if (event.type == sf::Event::MouseMoved){ doMouseMove(); } else if (event.type == sf::Event::MouseButtonPressed || event.type == sf::Event::MouseButtonReleased){ doMouseButton(event.mouseButton.button); } } window.clear(sf::Color::White); window.draw(rect1); window.draw(rect2); window.draw(rect3); window.draw(rect4); window.draw(label1); window.draw(label2); window.draw(label3); window.draw(label4); window.display(); } logger.log("Closing..."); }
sf::Vector2f collide(sf::CircleShape ball, sf::RectangleShape block) { sf::Vector2f ball_center = ball.getPosition() + sf::Vector2f(ball.getRadius(), ball.getRadius()); sf::Vector2f block_center = block.getPosition() + sf::Vector2f(block.getSize().x/2, block.getSize().y/2); sf::Vector2f diff = ball_center - block_center; // Not intersecting if(abs(diff.x) > block.getSize().x/2 + ball.getRadius() || abs(diff.y) > block.getSize().y/2 + ball.getRadius()) { return sf::Vector2f(0,0); } std::cout << diff.x << ", " << diff.y << std::endl; // hit edge of block (top/bottom) if(abs(diff.x) <= PADDLE_WIDTH/2) { if(diff.y <= 0) { return sf::Vector2f(0,-1); } else if(diff.y > 0) { return sf::Vector2f(0,1); } } // hit edge of block (left/right) else if(abs(diff.y) <= PADDLE_HEIGHT/2) { if(diff.x <= 0) { return sf::Vector2f(-1,0); } else if(diff.x > 0) { return sf::Vector2f(1,0); } } float distance = getDistance(ball_center, block.getPosition()); if(distance <= ball.getRadius()) { return (ball_center - block.getPosition()) / distance; } sf::Vector2f block_corner = block.getPosition() + sf::Vector2f(block.getSize().x,0); distance = getDistance(ball_center, block_corner); if(distance <= ball.getRadius()) { return (ball_center - block_corner) / distance; } block_corner = block.getPosition() + sf::Vector2f(0, block.getSize().y); distance = getDistance(ball_center, block_corner); if(distance <= ball.getRadius()) { return (ball_center - block_corner) / distance; } block_corner = block.getPosition() + block.getSize(); distance = getDistance(ball_center, block_corner); if(distance <= ball.getRadius()) { return (ball_center - block_corner) / distance; } // Not intersecting, even in the corners return sf::Vector2f(0,0); }
void Reset_Ball(sf::CircleShape *ball, sf::RectangleShape paddle) { ball->setPosition(paddle.getPosition().x + PADDLE_WIDTH / 2 - BALL_RADIUS, paddle.getPosition().y - BALL_RADIUS * 2 - 1); }