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

Author Topic: Zoom problem  (Read 2696 times)

0 Members and 1 Guest are viewing this topic.

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Zoom problem
« on: March 19, 2014, 01:43:24 pm »
#ifdef SFML_STATIC
#pragma comment(lib, "glew.lib")
#pragma comment(lib, "freetype.lib")
#pragma comment(lib, "jpeg.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "gdi32.lib")  
#endif // SFML_STATIC


#include <SFML/Graphics.hpp>

int posx, posy;
sf::View view1;

int main()
{
        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
       

        sf::CircleShape shape(100.f);
        shape.setFillColor(sf::Color::Green);
        sf::View view1(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                       

                        if (event.type == sf::Event::MouseMoved)
                        {

                                posx = event.mouseMove.x ;
                                posy = event.mouseMove.y ;
                        }

                        if (event.type == sf::Event::MouseWheelMoved)
                        {

                                if (event.mouseWheel.delta >= 1)
                                {
                                        sf::View view1(sf::FloatRect(0.3f, 0.3f, window.getSize().x, window.getSize().y));
                                        view1.zoom(0.3f);
                                        view1.setCenter(posx, posy);
                                        window.setView(view1);
                                }
                                if (event.mouseWheel.delta <= -1)
                                {
                                        sf::View view1(sf::FloatRect(3.f, 3.f, window.getSize().x, window.getSize().y));
                                        view1.zoom(3.f);
                                        view1.setCenter(posx, posy);
                                        window.setView(view1);
                                }


                        }
                        if (event.type == sf::Event::Resized)
                        {
                                sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
                                window.setView(sf::View(visibleArea));
                        }
                }

                window.clear();
                window.draw(shape);
                window.display();
               
        }

        return 0;
}

My problem is, It not zoom to area that cursor point.
I have move center of view into right position but It still have problem.

And when I resize window, It will reset zoom and position. How can I still zoom and position when resize window?
Thanks
« Last Edit: March 19, 2014, 01:45:31 pm by delio »

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Zoom problem
« Reply #1 on: March 21, 2014, 10:17:22 am »
You will have to map your Mouse-Coordinates (which are in Pixel on your Monitor) to you World-Coordinates, to get the Mouse-Position in your world.

Something like this :
sf::View view = window.getView();
//If not in fullscreen you have to substract the window-position, since the mouse-position is the pixel on your monitor
sf::Vector2f mousepos = window.mapPixelToCoords(sf::Mouse::getPosition()-window.getPosition());
view.zoom(0.9f);
view.setCenter(mousepos);
window.setView(view);
//set the cursor to your new center
sf::Mouse::setPosition(window.mapCoordsToPixel(mousepos)+ window.getPosition());
« Last Edit: March 21, 2014, 10:37:45 am by Ghosa »

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: Zoom problem
« Reply #2 on: March 21, 2014, 01:38:18 pm »
You will have to map your Mouse-Coordinates (which are in Pixel on your Monitor) to you World-Coordinates, to get the Mouse-Position in your world.

Something like this :
sf::View view = window.getView();
//If not in fullscreen you have to substract the window-position, since the mouse-position is the pixel on your monitor
sf::Vector2f mousepos = window.mapPixelToCoords(sf::Mouse::getPosition()-window.getPosition());
view.zoom(0.9f);
view.setCenter(mousepos);
window.setView(view);
//set the cursor to your new center
sf::Mouse::setPosition(window.mapCoordsToPixel(mousepos)+ window.getPosition());
Thank you very much

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Zoom problem
« Reply #3 on: March 21, 2014, 04:48:32 pm »
Rather than subtracting, you can just:
sf::Mouse::getPosition(window)
That will return the mouse position relative to the window.