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

Author Topic: trying to override Drawable::draw() but no effect  (Read 196 times)

0 Members and 1 Guest are viewing this topic.

koyuki

  • Guest
trying to override Drawable::draw() but no effect
« on: November 25, 2019, 07:38:25 pm »
I'm having problems.

here is the class that extend Drawable:
class TileMenu : public sf::Drawable{
private:
    sf::Sprite monitor;
    sf::Text residents;
    sf::Text workers;
    sf::Text energy;
    sf::Text money;
    sf::Text pollution;
    sf::Text happiness;
    sf::Text food;
    sf::Text goods;
    sf::Font font;
    sf::FloatRect close;
public:
    TileMenu(Tile& tile, TextureManager& txm);
    void draw(sf::RenderTarget& target, sf::RenderStates states) const override;

};
 


and here is the implementation of draw, which i honestly don't know if it is right:
void TileMenu::draw(sf::RenderTarget &target, sf::RenderStates states) const {
    target.draw(monitor, states);
    target.draw(residents, states);
    target.draw(workers, states);
    target.draw(energy, states);
    target.draw(pollution, states);
    target.draw(money, states);
    target.draw(happiness, states);
    target.draw(goods, states);
    target.draw(food, states);

}
 

in the gameloop class i create a pointer TileMenu* tileMenu and i initialise it as nullptr.
Then i handle the vent of click in a tile and i create in that moment a new TileMenu passing the parameters it needs.
If i call:
renderWindow.draw(*tileMenu);
 

the program run, but the TileMenu is not shown.

where is the problem?

« Last Edit: November 25, 2019, 08:26:45 pm by koyuki »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9388
    • View Profile
    • development blog
    • Email
Re: trying to override Drawable::draw() but no effect
« Reply #1 on: November 26, 2019, 12:33:32 am »
To understand what your code is doing you should learn how to use a debugger.
That way you can add a break point where ever your want your code to psss and then step through things step by step.

Are you setting the texture for the monitor sprite?
Are you setting the font on the text objects?
Does the class instance exist until display() is called on the window?
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

koyuki

  • Guest
Re: trying to override Drawable::draw() but no effect
« Reply #2 on: November 26, 2019, 09:23:12 am »
this is the constructor:
TileMenu::TileMenu(Tile &tile, TextureManager& txm, sf::Font font) {

    monitor.setOrigin(tile.getCenter());
    monitor.setTexture(txm.getRefTex("Monitor"));

    renderTexture.create(80,51);
    renderTexture.clear(sf::Color(0,0,0,0));

    residents.setString((char)tile.getResidents());
    residents.setFont(font);
    residents.setCharacterSize(8);
    residents.setOrigin(19, 8);
    renderTexture.draw(residents);

    workers.setString((char)tile.getWorkers());
    workers.setFont(font);
    workers.setCharacterSize(8);
    workers.setOrigin(53, 8);
    renderTexture.draw(workers);

    energy.setString((char)tile.getEnergy());
    energy.setFont(font);
    energy.setCharacterSize(8);
    energy.setOrigin(19, 19);
    renderTexture.draw(energy);

    food.setString((char)tile.getFood());
    food.setFont(font);
    food.setCharacterSize(8);
    food.setOrigin(53, 19);
    renderTexture.draw(food);

    happiness.setString((char)tile.getHappiness());
    happiness.setFont(font);
    happiness.setCharacterSize(8);
    happiness.setOrigin(19, 30);
    renderTexture.draw(happiness);

    money.setString((char)tile.getMoney());
    money.setFont(font);
    money.setCharacterSize(8);
    money.setOrigin(53, 30);
    renderTexture.draw(money);

    goods.setString((char)tile.getGoods());
    goods.setFont(font);
    goods.setCharacterSize(8);
    goods.setOrigin(19, 41);
    renderTexture.draw(goods);

    pollution.setString((char)tile.getPollution());
    pollution.setFont(font);
    pollution.setCharacterSize(8);
    pollution.setOrigin(53, 41);
    renderTexture.draw(pollution);

    monitor.setTexture(renderTexture.getTexture());

    std::cout << "menu created" << std::endl;
}
 

by the way i modified the class, to see if it was a bug or something, now it doesn't inherit from drawable, it doesn't have a draw method, it just have a getter for monitor which is supposed to have a texture created via the RenderTexture and in the game loop i just draw monitor.
« Last Edit: November 26, 2019, 09:26:28 am by koyuki »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9388
    • View Profile
    • development blog
    • Email
Re: trying to override Drawable::draw() but no effect
« Reply #3 on: November 26, 2019, 09:24:33 am »
You're never calling display on the render texture.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

koyuki

  • Guest
Re: trying to override Drawable::draw() but no effect
« Reply #4 on: November 26, 2019, 09:30:52 am »
but i don't want to display the RenderTexture, i want to pass its texture to monitor.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9388
    • View Profile
    • development blog
    • Email
Re: trying to override Drawable::draw() but no effect
« Reply #5 on: November 26, 2019, 08:24:54 pm »
You aren't "displaying" it on the window, but you're making sure the rendered object on the render texture are actually drawn properly. Before you use the texture of a render texture you have to call display().
See also: https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1RenderTexture.php#details
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/