Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to resize background  (Read 1213 times)

0 Members and 1 Guest are viewing this topic.

davejc

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
How to resize background
« on: August 09, 2018, 10:27:01 am »
I'm trying to figure out how to resize the contents of a window accurately when the window is resized. My goal is to have the view zoomed in depending on how much bigger the window is. I also do not want stretching. The background (for now) is a simple side-scrolling map with a fixed height of 798 pixels. I've programmed it so that there is always a margin above and below the graphic. I've figured it out for the most part, but the part that I'm not sure how to deal with is the zoom. I move the view based on if the "player" is moving towards the left side of the screen and is a certain distance from the center of the view. That gets all messed up after I zoom in. How to I accurately determine the change in distance from before/after I zoom the view?

I know I probably am not making much sense. Views have been a confusing topic for me but I am determined to understand them and implement them correctly and efficiently. I've been following this tutorial (https://github.com/SFML/SFML/wiki/Tutorial:-Using-View) but some of it has been confusing to me. Also, for reference, here is my updateView code:

void Game::_updateViewPos() {
    sf::Vector2f size = static_cast<sf::Vector2f>(_window.getSize());
   
    // Minimum size
    if(size.x < 1080)
        size.x = 1080;
    if(size.y < 958)
        size.y = 958;
   
    // Apply possible size changes
    _window.setSize(static_cast<sf::Vector2u>(size));
    _view = sf::View(sf::FloatRect(0.f, 0.f, size.x, size.y));
   
    // Move view down to have the bg centered vertically
    _view.move(0.f, -(float)((_window.getSize().y - _background.getTextureRect().height) / 2));
   
    // Zoom in to account for window size
    float zoomAmount = (float)HEIGHT / (float)_window.getSize().y;
    _view.zoom(zoomAmount);
   
    // Center up on player pos
    _view.setCenter(_player.getPosition().x, _view.getCenter().y);
   
    // Adjust so the view doesn't spill over if the player is on the far right or left side
    if (_view.getCenter().x < _window.getSize().x / 2) {
        _view.setCenter(_window.getSize().x / 2, _view.getCenter().y);
                _view.setCenter(_window.getSize().x / 2, _view.getCenter().y);
    }
        if (_view.getCenter().x > (_background.getTextureRect().width) - (_window.getSize().x / 2)) {
                _view.setCenter((_background.getTextureRect().width) - (_window.getSize().x / 2), _view.getCenter().y);
        }
   
    // Update the text positions
    _updateTextPos();
}