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

Pages: [1]
1
Graphics / bug whith Text class
« on: April 09, 2018, 09:58:40 pm »
Hi!
First of all I want to say thanks for such a wonderful and simple graphicks libruary. I had a trouble whith Text class while coding. Hear two simplyfied code fragments:

this works properly:
#include <SFML/Graphics.hpp>
using namespace sf;

class A
{

public:
    void draw_on(RenderTarget & target)
    {
        Text text;
        Font font;
        font.loadFromFile("arial.ttf");
        text.setCharacterSize(100);
        text.setColor(Color::Red);
        text.setFont(font);
        text.setString("Experiment");
        target.draw(text);
    }
};


int main()
{
    sf::RenderWindow app(sf::VideoMode(800, 600), "SFML window");
    A a;
    while (app.isOpen())
    {
        sf::Event event;
        while (app.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                app.close();
        }
        app.clear();
        a.draw_on(app);
        app.display();
    }
    return 0;
}
 

and shows red word "Experiment"

this one shows nothing:
#include <SFML/Graphics.hpp>
using namespace sf;

class A
{
    Text text;
public:
    void draw_on(RenderTarget & target)
    {
        Font font;
        font.loadFromFile("arial.ttf");
        text.setCharacterSize(100);
        text.setColor(Color::Red);
        text.setFont(font);
        text.setString("Experiment");
        target.draw(text);
    }
};


int main()
{
    sf::RenderWindow app(sf::VideoMode(800, 600), "SFML window");
    A a;
    while (app.isOpen())
    {
        sf::Event event;
        while (app.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                app.close();
        }
        app.clear();
        a.draw_on(app);
        app.display();
    }
    return 0;
}
 

the difference is in place of declaration of Text text

Please, answer if you know why this happens.

2
Thank you very much!

3
Hellow!
I'm using SFML 2.4.2 with Code Blocks for some weeks. I've herd that RenderTexture is quite a useful thing so I tryed to use it.
When I use it in the main function then it works properly, but when i try to write the same comands in another function or class method it doesn't work correctly. I paste two pices of code that must give the same output (because the comands are almost the same) but they don't. If someone knows how to fix it or what mistake have I made, write please.
Code that works properly
#include <SFML/Graphics.hpp>
using namespace sf;

int main()
{
    //Block of the same functions
    Sprite sprite1;
    RenderTexture rendertexture;
    rendertexture.create(32, 32);
    Image im1;
    im1.create(32, 32, Color::Red);
    Texture tex1;
    tex1.loadFromImage(im1);
    sprite1.setTexture(tex1);
    sprite1.setPosition(0, 0);
    rendertexture.draw(sprite1);
    //Block of the same functions/
    RenderWindow app(VideoMode(100, 100), "SFML window");
    Sprite sprite2;
    while (app.isOpen())
    {
        Event event;
        while (app.pollEvent(event))
        {
            if (event.type == Event::Closed)
                app.close();
        }
        app.clear();
        sprite2.setTexture(rendertexture.getTexture());
        sprite2.setTextureRect(IntRect(0, 0, 64, 64));
        app.draw(sprite2);
        app.display();
    }
    return EXIT_SUCCESS;
}
 

result: red square


#include <SFML/Graphics.hpp>
using namespace sf;

Texture makeTexture()
    {
    //Block of the same functions
    Sprite sprite1;
    RenderTexture rendertexture;
    rendertexture.create(32, 32);
    Image im1;
    im1.create(32, 32, Color::Red);
    Texture tex1;
    tex1.loadFromImage(im1);
    sprite1.setTexture(tex1);
    sprite1.setPosition(0, 0);
    rendertexture.draw(sprite1);
    //Block of the same functions
    return rendertexture.getTexture();
    }

int main()
{
    RenderWindow app(VideoMode(100, 100), "SFML window");
    Sprite sprite2;
    while (app.isOpen())
    {
        Event event;
        while (app.pollEvent(event))
        {
            if (event.type == Event::Closed)
                app.close();
        }
        app.clear();
        sprite2.setTexture(makeTexture());
        sprite2.setTextureRect(IntRect(0, 0, 64, 64));
        app.draw(sprite2);
        app.display();
    }
    return EXIT_SUCCESS;
}
 

result: white square!!!
(it's not a joke. Оn my computer 1st code gives red squer, the second gives white)
Why is it so?

Pages: [1]
anything