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

Author Topic: Crash when trying to draw sf::Text?  (Read 5085 times)

0 Members and 1 Guest are viewing this topic.

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Crash when trying to draw sf::Text?
« on: October 23, 2014, 05:21:27 am »
My code was working fine when I was drawing RectangleShapes from a vector, but then I tried doing the same thing with sf::Text and got the error "Unhandled exception at 0x5E7FBA72 (sfml-graphics-d-2.dll) in Dickinson Project.exe: 0xC0000005: Access violation reading location 0x00000004." I'm not sure what's wrong, as the dll's can't be linked incorrectly since the other sfml objects were drawing fine. Here is the problem code:
void Engine::Render(){
        window.clear(sf::Color::Black);
        window.draw(board.GetWalls()[0].GetWallShape());
//comment the next line out, and program runs fine
    window.draw(board.GetTexts()[0].GetText());
        window.display();
}

Here is the Text class:

#include "stdafx.h"
#include "SFML\Graphics.hpp"

class Text{
public:
        Text(std::string str, float posx, float posy, float rotation, int size, sf::Color color);
        ~Text();

        sf::Text GetText();
private:
        sf::Text text;
        sf::Font font;
};


#include "stdafx.h"
#include "Text.h"

Text::Text(std::string str, float posx, float posy, float rotation, int size, sf::Color color){
        font.loadFromFile("fonts/arial.ttf");
        text.setFont(font);
        text.setString(str);
        text.setPosition(posx, posy);
        text.setCharacterSize(size);
        text.setRotation(rotation);
        text.setColor(color);
}

Text::~Text(){

}

sf::Text Text::GetText(){
        return text;
}

In another program I have, similar code displays sf::Texts just fine, so what could be the issue?


wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Crash when trying to draw sf::Text?
« Reply #1 on: October 23, 2014, 06:43:15 am »
Hey look, I solved it myself. So apparently, sf::Text and sf::Font can't be created in the same class declaration. I had to declare my sf::Font in a base class and pass it as a reference to my Text class. What a peculiar requirement.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Crash when trying to draw sf::Text?
« Reply #2 on: October 25, 2014, 12:18:46 am »
sf::Text and sf::Font can't be created in the same class declaration.
Do you mean this part:
private:
    sf::Text text;
    sf::Font font;
because a class can have members of both types.

It looks like you might be trying to draw it before it's been created i.e. board.GetTexts() has a size of zero (doesn't have an element zero). Try outputting the size of that vector instead of drawing it to see if it is indeed zero.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Crash when trying to draw sf::Text?
« Reply #3 on: October 25, 2014, 12:53:19 am »
Before anyone else comes up with more conspiracy theories...

This is almost the same problem as this one. Copying is your friend, and enemy.

What a peculiar requirement.
The only requirement is that you understand what is happening in your own code and how that affects your SFML objects. Anybody proficient in C++ (and STL) and who understands that sf::Text keeps a reference to sf::Font can add the picture up for themselves and see what is wrong. Although you didn't show us how you populate your board.GetTexts() container, based on your code returning a copy of text, I can tell that you probably made the same mistake there.

Don't pass copies around as much as you are doing now. SFML classes, even when built in C++11, are not enabled to be moved yet. Your code, if it runs without crashing, probably runs horribly slow because of all the excessive copying that is happening.

If you want to fix this crash you were having before you resorted to shotgun debugging, you should read up on what the container does when you add new elements to it.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).