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

Author Topic: Font::loadFromFile(), Text::setFont() - problem with pointers; how use them?  (Read 4078 times)

0 Members and 1 Guest are viewing this topic.

jrt

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Hi, I've problem with the use of pointers of Font and Text classes.

[SOLVED] *click*

I searched forum and found this similar topic, but I'm not sure what I'm doing wrong in my code.
#include <iostream>
#include <cstdlib>

#include "SFML/Graphics.hpp"
#include "SFML/Audio.hpp"
#include "SFML/Network.hpp"

using namespace std;

int main()
{
     sf::RenderWindow window(sf::VideoMode(640, 480), "Hello world");
     cout << "Window has been created" << endl;

     sf::Font* font;

     if (!font->loadFromFile("/home/jaffa/workspace/hello-world/fonts/ubuntu-font-family/Ubuntu-R.ttf")) {
        cout << "Couldn't load font!" << endl;
     } else {
        cout << "Font has been loaded!" << endl;
     }

     sf::Text* text;
     text->setFont(*font);
     cout << "Text font has been set" << endl;
     text->setString("Hello world");
     cout << "Text string has been set" << endl;
     text->setColor(sf::Color::Blue);
     cout << "Text color has been set" << endl;

     while (window.isOpen()) {
         sf::Event event;
         while (window.pollEvent(event)) {
             cout << "pollEvent" << endl;

             if (event.type == sf::Event::Closed) {
                 window.close();
             }
         }

         cout << "Trying to clear window" << endl;
         window.clear(sf::Color::Black);

         cout << "Trying to draw text" << endl;
         window.draw(*text);

         cout << "Trying to display" << endl;
         window.display();
         cout << "End of the current frame" << endl;
     }

    return EXIT_SUCCESS;
}
When I run this code app's, window appeared and immediately closed. In my console I saw:
Quote
Window has been created
The program has unexpectedly finished.
Can someone show me how I should use these classes with pointers?
« Last Edit: August 19, 2013, 03:39:04 am by jrt »

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Well a pointer always has to point to something, your pointers point to anything random or nothing and not on what you want :D

Use new and delete or let them point to something ;)
Failing to succeed does not mean failing to progress!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Use new and delete or let them point to something ;)
No. There's no need at all to use a pointer here!

Just remove the * for "font" and "text" and you should be fine.
Also if an application crashes, then you should first run it in the debugger and it will tell you exactly where it happened and very often why.

If you ever want to use pointers, I strongly advice against the use of manual memory management (new/delete) pairs. Instead you should make use of (const) references where possible and otherwise use smart pointers with RAII. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jrt

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
I am an idiot. Thank you for making me aware of this.
I rarely forgot to initialize objects in more complicated project - but this... so small example hello world app. I feel embarassed.

This works fine:
#include <iostream>
#include <cstdlib>
#include <memory>

#include "SFML/Graphics.hpp"
#include "SFML/Audio.hpp"
#include "SFML/Network.hpp"

using namespace std;

int main()
{
    // create the window
     sf::RenderWindow window(sf::VideoMode(640, 480), "Hello world");
     cout << "Window has been created" << endl;

     sf::Font* font = new sf::Font();

     if (!font->loadFromFile("/home/jaffa/workspace/hello-world/fonts/ubuntu-font-family/Ubuntu-R.ttf")) {
        cout << "Couldn't load font!" << endl;
     } else {
        cout << "Font has been loaded!" << endl;
     }

     sf::Text* text = new sf::Text();
     text->setFont(*font);
     cout << "Text font has been set" << endl;
     text->setString("Hello world");
     cout << "Text string has been set" << endl;
     text->setColor(sf::Color::Blue);
     cout << "Text color has been set" << endl;

     while (window.isOpen()) {
         sf::Event event;
         while (window.pollEvent(event)) {
             cout << "pollEvent" << endl;

             if (event.type == sf::Event::Closed) {
                 window.close();
             }
         }

         cout << "Trying to clear window" << endl;
         window.clear(sf::Color::Black);

         cout << "Trying to draw text" << endl;
         window.draw(*text);

         cout << "Trying to display" << endl;
         window.display();
         cout << "End of the current frame" << endl;
     }

    return EXIT_SUCCESS;
}

This should works fine and is better:
#include <iostream>
#include <cstdlib>
#include <memory>

#include "SFML/Graphics.hpp"
#include "SFML/Audio.hpp"
#include "SFML/Network.hpp"

using namespace std;

int main()
{
    // create the window
     sf::RenderWindow window(sf::VideoMode(640, 480), "Hello world");
     cout << "Window has been created" << endl;

     shared_ptr<sf::Font> font(new sf::Font());

     if (!font->loadFromFile("/home/jaffa/workspace/hello-world/fonts/ubuntu-font-family/Ubuntu-R.ttf")) {
        cout << "Couldn't load font!" << endl;
     } else {
        cout << "Font has been loaded!" << endl;
     }

     shared_ptr<sf::Text> text(new sf::Text);
     text->setFont(*font);
     cout << "Text font has been set" << endl;
     text->setString("Hello world");
     cout << "Text string has been set" << endl;
     text->setColor(sf::Color::Blue);
     cout << "Text color has been set" << endl;

     while (window.isOpen()) {
         sf::Event event;
         while (window.pollEvent(event)) {
             cout << "pollEvent" << endl;

             if (event.type == sf::Event::Closed) {
                 window.close();
             }
         }

         cout << "Trying to clear window" << endl;
         window.clear(sf::Color::Black);

         cout << "Trying to draw text" << endl;
         window.draw(*text);

         cout << "Trying to display" << endl;
         window.display();
         cout << "End of the current frame" << endl;
     }

    return EXIT_SUCCESS;
}

Thank you both for your answers. Topic can be closed.
« Last Edit: August 19, 2013, 04:17:17 am by jrt »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
I still don't see, why you would work with pointers at all... Just use stack allocated object and save you a lot of writing and possible issues.

Also don't using namespace std, it will make you and anyone else reading your code more trouble than what it will save you from typing. ;)

You should also add SFML's header to the linking path, so you can use <SFML/Name.hpp>. :)

Topic can be closed.
Topics get never closed here, but if you want you can add something in front of your first post, to signal that it was solved. ;)
« Last Edit: August 19, 2013, 01:33:32 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jrt

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
I still don't see, why you would work with pointers at all... Just use stack allocated object and save you a lot of writing and possible issues.
This was just my first SFML 2.1 hello world example code. I often use my ResourceManager class with instance variable like this:
std::map<std::string, sf::Sprite*>
or this:
std::map<std::string, std::shared_ptr<sf::Sprite>>

Also don't using namespace std, it will make you and anyone else reading your code more trouble than what it will save you from typing. ;)
It's just example code. I'm trying to avoid using "using namespace *" in every project.

You should also add SFML's header to the linking path, so you can use <SFML/Name.hpp>. :)
I did. I just forgot to change double quotation marks into angle brackets.

Topic can be closed.
Topics get never closed here, but if you want you can add something in front of your first post, to signal that it was solved. ;)
Done. Thank you for your answer ;)

 

anything