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

Pages: [1]
1
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 ;)

2
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.

3
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?

Pages: [1]