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 - nktsrbrkv

Pages: [1]
1
Graphics / Re: Align the text perfectly centered inside any shape
« on: May 15, 2024, 07:14:24 pm »
Many thanks for all of your replies!

2
Graphics / Re: Align the text perfectly centered inside any shape
« on: May 12, 2024, 10:01:28 am »
Thank you for bringing that to my attention. I fixed this. Now I return a reference to the sf::Text object instead. However, the problem in my algorithm is still present. Although this alignment looks good, it is not ideal. Maybe I should attach a screenshot?

3
Graphics / Align the text perfectly centered inside any shape
« on: May 11, 2024, 10:18:06 pm »
Hello everyone!

I have a set of different shapes like rectangles, circles, etc., and I need to align the text associated with each shape perfectly in the center of the shape. I wrote the following algorithm to determine offsets for the x and y axes respectively:
sf::Text text = shape.getText();
float textOffsetX = (shape.getShape()->getGlobalBounds().width - text.getGlobalBounds().width) / 2;
float textOffsetY = (shape.getShape()->getGlobalBounds().height - text.getGlobalBounds().height) / 2;
text.setPosition(shapeOffsetX + textOffsetX, shapeOffsetY + textOffsetY);
As a result, the text inside each shape is perfectly aligned horizontally but wrong vertically. Please find the screenshot in attachments. I do not know how I can put it here :-[

My shapes are moving, so in addition to text offsets, there are also shape offsets. It does not have to matter. I have also tried two more ways, but the result is the same:
1.
sf::Text text = shape.getText();
float textOffsetX = (shape.getShape()->getGlobalBounds().width - text.getGlobalBounds().width) / 2;
auto glyph = text.getFont()->getGlyph(text.getString()[0], text.getCharacterSize(), false);
float textOffsetY = (shape.getShape()->getGlobalBounds().height - glyph.bounds.height) / 2;
text.setPosition(shapeOffsetX + textOffsetX, shapeOffsetY + textOffsetY);
2.
sf::Text text = shape.getText();
float textOffsetX = (shape.getShape()->getGlobalBounds().width - text.getGlobalBounds().width) / 2;
float textOffsetY = (shape.getShape()->getGlobalBounds().height - text.getCharacterSize()) / 2;
text.setPosition(shapeOffsetX + textOffsetX, shapeOffsetY + textOffsetY);

Could you please help me with this question?

4
Many thanks! I adjusted my code according to your advice, and it works as I want:

5
Graphics / Shapes are moving simultaneously instead of separately
« on: May 10, 2024, 03:16:44 pm »
Hello everyone!

I need my shapes to move completely independently and include collision detection. When I had only one shape, everything was okay, but when I added one more, it began behaving strangely. For some unknown reason to me, my shapes move together; it seems like they are sharing some data. I was trying to figure out what is wrong on my own, but it seems like I am stuck. If it is possible, try to give me a hint instead of a solution. Many thanks in advance.

main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath> // fabs
#include <vector>

int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Works!");

        sf::CircleShape* circle = new sf::CircleShape(100.f);
        circle->setFillColor(sf::Color::Green);
        circle->move(400.f, 0.f);

        sf::RectangleShape* rect = new sf::RectangleShape(sf::Vector2f(100, 200));
        rect->setFillColor(sf::Color::Yellow);
        rect->move(200.f, 150.f);

        std::vector<sf::Shape*> shapes;
        shapes.push_back(circle);
        shapes.push_back(rect);

        float xOffset = 0.5f;
        float yOffset = 0.5f;

        float topRightX = 0.f;
        float bottomRightY = 0.f;
        float topLeftX = 0.f;
        float topLeftY = 0.f;

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear();

                for (auto& shape : shapes)
                {
                        if (shape)
                        {
                                if (dynamic_cast<sf::CircleShape*>(shape))
                                {
                                        sf::FloatRect boundRect = shape->getGlobalBounds();

                                        topLeftX = boundRect.getPosition().x;
                                        topLeftY = boundRect.getPosition().y;

                                        auto circle = dynamic_cast<sf::CircleShape*>(shape);
                                        topRightX = boundRect.getPosition().x + circle->getRadius() * 2.f;
                                        bottomRightY = boundRect.getPosition().y + circle->getRadius() * 2.f;
                                }
                                else if (dynamic_cast<sf::RectangleShape*>(shape))
                                {
                                        sf::FloatRect boundRect = shape->getGlobalBounds();

                                        topLeftX = boundRect.getPosition().x;
                                        topLeftY = boundRect.getPosition().y;

                                        auto rectangle = dynamic_cast<sf::RectangleShape*>(shape);
                                        topRightX = boundRect.getPosition().x + rect->getSize().x;
                                        bottomRightY = boundRect.getPosition().y + rect->getSize().y;
                                }

                                if (topRightX >= window.getSize().x - 1.f)
                                        xOffset *= -1.f;
                                else if (bottomRightY >= window.getSize().y - 1.f)
                                        yOffset *= -1.f;
                                else if (topLeftX <= 0.f)
                                        xOffset = fabs(xOffset);
                                else if (topLeftY <= 0.f)
                                        yOffset = fabs(yOffset);

                                sf::Vector2f curPos = shape->getPosition();
                                shape->setPosition(curPos.x + xOffset, curPos.y + yOffset);

                                window.draw(*shape);
                        }
                }

                window.display();
        }

        return 0;
}

Please find in attachments how it works now.

Pages: [1]
anything