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

Pages: [1]
1
Graphics / Re: Image with out of objects sf::Texture and sf::Sprite
« on: August 20, 2021, 02:24:29 am »
you don't need a lot of textures and sprites, you just need one of each for every image.
the way you are trying to do is simply wrong, you'd be creating and destroying the images every frame. thats too expensive to the cpu.

and sf::Image can't be drawn to the screen. you use it only when you need to change something in the image (as changing some pixel color).
Okay, so, I'll try to minimize number of textures and sprite. Thanks you.

2
Graphics / Image with out of objects sf::Texture and sf::Sprite
« on: August 20, 2021, 12:35:38 am »
Hello! I want to draw an image on user's screen, but I don't want to create objects sf::Texture and sf::Sprite for it because with these method I need a lot of textures and sprites. I want to do it like:
window.draw(sf::Image(x, y, way));

3
Graphics / Re: Text at center of rect
« on: August 14, 2021, 12:04:21 pm »
I used:
text1.setPosition(rect1.getPosition().x + rect1.getSize().x / 2 - text1.getLocalBounds().width / 2, rect1.getPosition().y + rect1.getSize().y / 2 - text1.getLocalBounds().height / 2);
 
Now text is not on center of rect, but it's better, then what I had. So, I think it's normal result and I don't want to do something else with this text.

4
Graphics / Re: Text at center of rect
« on: August 13, 2021, 11:41:39 pm »
To center text you also need to consider the local bounds' offset.

    auto text = sf::Text{ "Test 1234", font };
    text.setOrigin(text.getGlobalBounds().getSize() / 2.f + text.getLocalBounds().getPosition());
    text.setPosition(rectangle.getPosition() + (rectangle.getSize() / 2.f));
 

And as G. pointed you, you of course have to first set the wanted font size and string, otherwise the calculation is off

I'm creating a sf::Text object with:
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text text1("", font, 40);
 
I tried to use your code, but compiler gives error:
Quote
main.cpp:92:43: error: «sf::FloatRect» {aka «class sf::Rect<float>»} doesn't contain element with name «getSize»
   92 |     text.setOrigin(text.getGlobalBounds().getSize() / 2.f + text.getLocalBounds().getPosition());
      |                                           ^~~~~~~
main.cpp:92:83: error: «sf::FloatRect» {aka «class sf::Rect<float>»} doesn't contain element with name «getPosition»
   92 | t.getGlobalBounds().getSize() / 2.f + text.getLocalBounds().getPosition());
      |                                                             ^~~~~~~~~~~

5
Graphics / Re: Text at center of rect
« on: August 13, 2021, 11:36:25 pm »
Not sure that's what's happening because you didn't post a picture of the problem
Yes, you are right.

6
Graphics / Text at center of rect
« on: August 13, 2021, 12:40:28 am »
I have a rect and a text. So, I need to put my text at center of rect. Like there:

Now I'm using this code:
And it's drawing text from center of rect, but not on center.
Quote
sf::Vector2f centerPos = sf::Vector2f(rect1.getPosition().x + rect1.getSize().x / 2, rect1.getPosition().y + rect1.getSize().y / 2);
text1.setPosition(centerPos.x - text1.getGlobalBounds().width / 2, centerPos.y - text1.getGlobalBounds().height / 2);

7
Graphics / Re: Unique names for rectangles in class
« on: August 13, 2021, 12:33:06 am »
Thanks you very much and sory for my stupidity.  ;D

8
Graphics / Re: Unique names for rectangles in class
« on: August 08, 2021, 08:03:03 pm »
But what I will set like a type of array?

9
Graphics / Re: Unique names for rectangles in class
« on: August 08, 2021, 07:28:43 pm »
Thanks you! But if I need only 16, can I use another method?

10
Graphics / Unique names for rectangles in class
« on: August 08, 2021, 01:20:31 pm »
Hello! I'm new to SFML and I try to create a game 2048. So, I create a class, that have a function to create and draw rectangle with:
sf::RectangleShape rect(sf::Vector2f(130.f, 130.f));
                rect.move(20.f, 20.f);
                rect.setFillColor(sf::Color(255, 160, 122));
                window.draw(rect);
But if I need to create two or more objects of this class, it will not work correctly because it create and draw one rectangle with one name. So, can I create a class, that will give an unique name for all rectangles and how?
And sorry for my english.

Pages: [1]