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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Zaper127

Pages: [1]
1
Graphics / Making scalable text or preventing views from stretching it.
« on: September 11, 2021, 05:17:42 pm »
So now I’ve moved into my title screen text. And got it figured out. Font, message, color, and size. I can resize the window if it’s in the main view and the text doesn’t change size but moves and stays sharp, and getGlobalBoundingbox() works perfectly regardless of the move. But when I put it inside any view that is not exactly the size of the window, even with the size set to the size of the text or larger, it becomes horribly blurry and the bounding box is moved to the top left of the window, even if outside the view, and becomes like 1X1 pixel big. So my question is how do I stop this? Text that scales with the view yet stays crips is Ideal, but even if I can stop the view from distorting it and destroying the bounding box I could work with it. I will need the views for positioning, scrolling threw longer lists, and so one later so I have to have a solution to put it in a view.

Does anyone have a link to to a good tutorial or know the answer for this? Thank you.

2
Alright. I figured it out, the issue is I was using the view incorrectly. I can’t explain exactly what i was doing but prt of it was setting the scale of the sprite within the view.

3
So I'm new to c++ and SMFL. I'm trying to set a background image and have it fill the window as much as possible to, filling either top to bottom or left to right depending on the image and window dimensions. I have tried that letter boxing wiki that shows up in all the searches, and serval other things.

With the View/letter boxing the image centers but doesn't scale. with my attempts I can get it to fill the width but won't maintain aspect ratio. Here are to two codes I've tried:

//Letterbox code always referred to on these forms
                sf::View getLetterboxView(sf::View view, sf::Vector2u windowSize) {
               
                // Compares the aspect ratio of the window to the aspect ratio of the view,
                // and sets the view's viewport accordingly in order to archieve a letterbox effect.
                // A new view (with a new viewport set) is returned.

                float windowRatio = (float)windowSize.x / windowSize.y;
                float viewRatio = (float)view.getSize().x / view.getSize().y;
                float sizeX = 1;
                float sizeY = 1;
                float posX = 0;
                float posY = 0;

                bool horizontalSpacing = true;
                if (windowRatio < viewRatio) horizontalSpacing = false;

                // If horizontalSpacing is true, the black bars will appear on the left and right side.
                // Otherwise, the black bars will appear on the top and bottom.

                if (horizontalSpacing) {
                        sizeX = viewRatio / windowRatio;
                        posX = (1 - sizeX) / 2.f;
                }
                else {
                        sizeY = windowRatio / viewRatio;
                        posY = (1 - sizeY) / 2.f;
                }

                view.setViewport(sf::FloatRect(posX, posY, sizeX, sizeY));

                return view;
        }
//My own attempts
        sf::Sprite setBackground(sf::Vector2u windowSize, sf::Texture& background) //preps image background
        {

                sf::Sprite backgroundSprite;
                backgroundSprite.setTexture(background);
                float scale;

                //I have tried several formula attempts, this is just my last attempt
                if (windowSize.x >= windowSize.y) scale= (float)windowSize.x / background.getSize().x;
                else  scale = (float)windowSize.y / background.getSize().y;
                backgroundSprite.setScale(scale, scale);

                return backgroundSprite;
        }


And this is how I call the functions:
   
bool mouseleftpressed;
                sf::Vector2u windowSize(window.getSize());
                //Background
                        sf::Texture background = setTexture("images/backgrounds/Grassy_Mountains_preview_fullcolor.png");
                        sf::Sprite backgroundImage = setBackground(windowSize, background);
                        sf::View backgroundView;

                        backgroundView.setSize(windowSize.x, windowSize.y);
                        backgroundView.setCenter(backgroundView.getSize().x / 2, backgroundView.getSize().y / 2);
                        backgroundView = getLetterboxView(backgroundView, windowSize);
                //
                                while (window.pollEvent(event))
                                {
                                        if (event.type == sf::Event::Closed) { mode = 3; };

                                        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                                        {
                                                mouseleftpressed = 1;
                                        }
                                        if (sf::Event::MouseButtonReleased && (mouseleftpressed == 1))
                                        {
                                                sf::Vector2i clickedco = sf::Mouse::getPosition(window);
                                                mouseleftpressed = 0;
                                        }
                                        if (event.type == sf::Event::Resized)
                                        {
                                                windowSize.x = event.size.width;
                                                windowSize.y = event.size.height;
                                                sf::Sprite backgroundImage = setBackground(windowSize, background);
                                        }
                                }

You guys have any ideas or a good tutorial link? Please remember I am a beginner in C++ and SFML.

Pages: [1]
anything