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

Author Topic: Problem to draw 2 drawable on a renderWindow [Solved]  (Read 1725 times)

0 Members and 1 Guest are viewing this topic.

edd

  • Newbie
  • *
  • Posts: 3
    • View Profile
Problem to draw 2 drawable on a renderWindow [Solved]
« on: May 20, 2013, 01:43:15 pm »
hello,

i have a little project with two class inherit drawable and transformable, and a screen class for event loop and draw instructions.

  • a class map implement a tilemap with vertex array.
  • a class info try to implement an information box.
  • a screen class contain event loop and draw call.

I want display a box on same screen than my map with some information updated when user click on map.

theses two class implement a virtual function draw like explain in tutorial


class Map : public sf::Transformable, public sf::Drawable
{
 
public:
  Map();
  void setFilename(std::string filename);
  void load(sf::Vector2u tileSize);
 
protected :
  void initVertex();
   
private :
  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
 
  sf::Vector2u tileSize;
  sf::VertexArray m_secondary_vertices;
  sf::VertexArray m_vertices;
  std::string filename;
};


void Map::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
  states.transform *= getTransform();
 
  states.texture = tilesetCollection.get("tiles"); // give tileset texture
 
  target.draw(m_vertices, states);
  target.draw(m_secondary_vertices, states); // for square need a second texture (like coast i.e)
}
 


class Info  : public sf::Transformable, public sf::Drawable
{
  public :
    Info();
    int init();
   
  private :
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
    sf::Text test;
};

int Info::init()
{
  sf::Font font;
 
  if ( !font.loadFromFile("../Resources/Dustismo.ttf"))
  {
     std::cout << "Error loading font" << std::endl;
     return -1;
  }
 
  test.setFont(font);
  test.setCharacterSize(30);
  test.setPosition(0,0);
  test.setString("hello");
  test.setColor(sf::Color::Red);
}

void Info::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
  states.transform *= getTransform();
  target.draw(test, states); // Crash on this instruction.
}
 

The screen class is build in this way :


int ScreenGame::Run(sf::RenderWindow &window)
{
  map.setFilename("../Maps/default.map");
  map.load(tilesize); // init all vertices
  info.init(); // init sf::Text object

  viewMap.setSize(800,600);
  viewMap.setCenter(400,300);

 bool Running = true;
 
  while ( Running )
  {
    sf::Event event;
    while (App.pollEvent(event))
    {
      [...]
    }
    window.setView(viewMap);
    window.clear(sf::Color::Black);
    window.draw(map);
    window.draw(info); // without this call, all work perfect.
    window.display();
  }
}
 

When a run this project with only map draw instruction, work well, my map is correctly draw on my screen. But when i add info draw instruction, project crash with return code 0.

Someone could help me ?
« Last Edit: May 20, 2013, 02:16:32 pm by edd »

edd

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Problem to draw 2 drawable on a renderWindow
« Reply #1 on: May 20, 2013, 02:15:49 pm »
ok, i found !

in SFML, i think you can't load two time a font file.

In a previous screen (screen menu) i load a font for display menu entries, and in game screen a load again same file for display test text in info box.

So i just implement a font collection class, for use sames font in all my project and perform uniq load for my fonts.

edd

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Problem to draw 2 drawable on a renderWindow [Solved]
« Reply #2 on: May 20, 2013, 02:32:43 pm »
more info :

I don't understand why crash in first exemple. When i test sf::Font / sf::Text in simple game loop for reproduct my problem, all is fine with two load operation on same ttf file.

[...]
sf::Font font1, font2;

font1.loadFromFile("file.ttf")
font2.loadFromFile("file.ttf")

sf::Text text1, text2;

text1.setFont(font1);
text2.setFont(font2);

window.clear();
window.draw(text1);
window.draw(text2);
window.display();
 


zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Problem to draw 2 drawable on a renderWindow [Solved]
« Reply #3 on: May 20, 2013, 05:33:31 pm »
int Info::init()
{
  sf::Font font;
  .............
}
 

Your problem in your first code example is that the font was being freed from memory as soon as your init() function exited. You must keep the sf::Font object alive as long as you are using it.

If you read the documentation your see that setFont(xxx) only takes a reference.

void setFont(const Font& font);
« Last Edit: May 20, 2013, 05:53:15 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

 

anything