Sorry, but I can't figure out the solution.
So I try posting the code just for clarity... (use an image as you like)
#include <SFML/Graphics.hpp>
sf::Sprite g_sprite;
sf::Texture g_texture;
sf::FloatRect getViewBounds(const sf::View &view)
{
sf::FloatRect rt;
rt.left = view.getCenter().x - view.getSize().x/2.f;
rt.top = view.getCenter().y - view.getSize().y/2.f;
rt.width = view.getSize().x;
rt.height = view.getSize().y;
return rt;
}
float posx,posy;
float g_zoom[] = {2.0f, 1.5f, 1.0f, 0.8f, 0.6f, 0.4f, 0.2f, 0.1f};
int currentZoom = 2;
int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(1024, 768), "Moving Map");
window.setVerticalSyncEnabled(true);
sf::View view;
view.setSize(1024, 768);
// load map
g_texture.loadFromFile("map.png");
g_sprite.setTexture(g_texture);
g_sprite.setPosition(0, 0);
posx = 0;
posy = 0;
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
else
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
else
if (event.type == sf::Event::MouseWheelMoved)
{
currentZoom += event.mouseWheel.delta;
if (currentZoom < 0) {currentZoom = 0;}
else if (currentZoom > 7) {currentZoom = 7;}
sf::Vector2i localPos = sf::Mouse::getPosition(window);
sf::Vector2f realPos = window.mapPixelToCoords(localPos);
view.setSize(1024*g_zoom[currentZoom],768*g_zoom[currentZoom]);
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
posx -= 10;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
posx += 10;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
posy -= 10;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
posy += 10;
view.setCenter(posx, posy);
// clear the window with black color
window.clear(sf::Color::Black);
// set view
window.setView(view);
// draw everything here...
window.draw(g_sprite);
// end the current frame
window.display();
}
return 0;
}