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

Author Topic: Resizing and setCenter on view  (Read 1215 times)

0 Members and 1 Guest are viewing this topic.

CutlassSoft

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Resizing and setCenter on view
« on: October 11, 2023, 12:45:56 am »
While building a traditional boardgame (like Life, Risk, etc.) I cannot get the zooming out of the board image to work properly. I want the ability to show a particular quadrant of the board, say the upper left for example, and display as much of that quadrant given the current window size. This works as designed when the view’s & window’s aspect ratio are equal at 1:1. At first I used the window size to calculate the setCenter() values after a call to zoom() function on the view object. Expectedly black areas appeared within the window. I soon realized my error in using window size and then switched to using the actual gameboard sprite image dimensions to calculate setPosition() values. It worked somewhat. The more I zoomed out the view the larger the black areas became. I have to believe I am missing something obvious to do the calculations correctly to get the results I desire.

void GrapeGameGuiWindow::DisplayGameboardQuardrant(int nQuadrantValue)
{
        if (m_pGameboardView != nullptr)
        {
                unsigned int DisplayW = getSize().x;
                unsigned int DisplayH = getSize().y;

                // m_ImageW = 4000
                // m_ImageH = 3000

                switch (nQuadrantValue)
                {
                        case GUI_QUADRANT_CENTERED:
                                m_pGameboardView->setCenter(sf::Vector2f((m_ImageW / 2), (m_ImageH / 2)));
                                break;

                        case GUI_QUADRANT_TOP_LEFT:
                                // tl: 0,0  br: .5,.5  <-- Values to calculate which portion of the image’s rectangle to base setCenter()
                                // m_pGameboardView->setCenter(sf::Vector2f((float)(DisplayW / 2), (float)(DisplayH / 2)));    
                                m_pGameboardView->setCenter(sf::Vector2f((m_ImageW * 0.25f), (m_ImageH * 0.25f)));  
                                break;

                        case GUI_QUADRANT_TOP_RIGHT:
                                // tl: .5,0  br: 1,.5   <-- Values to calculate which portion of the image&#39;s rectangle to base setCenter()
                                m_pGameboardView->setCenter(sf::Vector2f(((m_ImageW - DisplayW) + (float)(DisplayW / 2)), (float)(DisplayH / 2)));
                                break;

                        case GUI_QUADRANT_BOTTOM_LEFT:
                                // tl: 0,.5  br: .5,1    <-- Values to calculate which portion of the image&#39;s rectangle to base setCenter()
                                m_pGameboardView->setCenter(sf::Vector2f((float)(DisplayW / 2), ((m_ImageH - DisplayH) + (DisplayH / 2))));
                                break;

                        case GUI_QUADRANT_BOTTOM_RIGHT:
                                // tl: .5,.5  br: 1,1    <-- Values to calculate which portion of the image&#39;s rectangle to base setCenter()
                                m_pGameboardView->setCenter(sf::Vector2f(((m_ImageW - DisplayW) + (DisplayW / 2)), ((m_ImageH - DisplayH) + (DisplayH / 2))));
                                break;
                }
        }
}
 
« Last Edit: October 16, 2023, 11:17:02 pm by eXpl0it3r »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Resizing and setCenter on view
« Reply #1 on: October 13, 2023, 08:47:26 pm »
With views, you need to set its centre to be the co-ordinate that you want in the centre of the window*. This co-ordinate is the position that you are using to draw things. Basically, the location of the thing you draw that you want in the centre of the window* is what the view's centre should be.

On the SFML wiki, there are a few things that might help:

First, there is a full tutorial on how to use the view:
https://github.com/SFML/SFML/wiki/Tutorial%3A-Using-View

Second, there is a short function to zoom a view using a specific pixel of the window instead of its centre:

Zoom View At:
https://github.com/SFML/SFML/wiki/Source%3A-Zoom-View-At-%28specified-pixel%29

And in addition, there are another couple of other view manipulation functions you may enjoy:

Rotate View At:
https://github.com/SFML/SFML/wiki/Source%3A-Rotate-View-At-%28specified-co%E2%80%90ordinate%29

Convert Point Of View:
https://github.com/SFML/SFML/wiki/Source%3A-Convert-Point-Of-View

*"window" should be "viewport" but, since the viewport covers the entire window by default, I've used "window" for simplicity.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

CutlassSoft

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Resizing and setCenter on view
« Reply #2 on: October 20, 2023, 09:17:28 pm »
Thanks for the helpful advice.

 

anything