Hi, I've just started to learn SFML, and as I was experimenting with the views, I came up with a problem that I do not know how to solve, (I did this in SDL2 but things here are different), anyway, what I'm trying to do is keep the view inside of the map limits while scrolling. Like it would scroll in a super mario game, the view is not completely centered in the player but rather it moves when the player is at half of the window.
How can I keep the view inside of the map limits?
Here is what I have so far, it works well for the top and left corners but the right and bottom ones dont.
please see the gif to see my current behavior.
Here is my code:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow app(sf::VideoMode(480, 320), "view demo");
// create player, at the screen center
sf::RectangleShape player(sf::Vector2f(20, 20));
player.setFillColor(sf::Color::Red);
player.setOrigin(10, 10);
player.setPosition(240, 160);
const float player_speed = 100;
sf::Texture backgroundImg;
backgroundImg.loadFromFile("./resources/true.jpg");
sf::Sprite background(backgroundImg);
// we create our custom view
sf::View player_view(sf::FloatRect(0, 0, app.getSize().x, app.getSize().y));
player_view.setViewport(sf::FloatRect(0, 0, 1, 1));
sf::Clock clock;
bool running = true;
sf::Event event;
app.setView(player_view);
while (running)
{
while (app.pollEvent(event))
{
if (event.type == sf::Event::Closed)
running = false;
}
// moving player
float frametime = clock.getElapsedTime().asSeconds();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
player.move(0, -player_speed * frametime);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
player.move(0, player_speed * frametime);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
player.move(-player_speed * frametime, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
player.move(player_speed * frametime, 0);
clock.restart();
// we keep our view centered on the player
sf::Vector2f position;
auto sizeX(app.getSize().x);
auto sizeY(app.getSize().y);
player.getPosition().x >= sizeX / 2 ? position.x = player.getPosition().x : position.x = sizeX / 2;
player.getPosition().y >= sizeY / 2 ? position.y = player.getPosition().y : position.y = sizeY / 2;
player_view.setCenter(position);
app.setView(player_view);
// rendering entities
app.draw(background);
app.draw(player);
app.display();
app.clear();
}
app.close();
return 0;
}