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

Author Topic: Text rendered from in a class is displayed as a dot.  (Read 2647 times)

0 Members and 1 Guest are viewing this topic.

CrosisBH

  • Newbie
  • *
  • Posts: 2
    • View Profile
Text rendered from in a class is displayed as a dot.
« on: March 09, 2019, 08:26:31 am »
For a small project of mine, I wanted to make a game without any graphics objects and just text. I went to SFML because it seemed the best for me (plus I wanted to brush up my C++). I followed the guide up to creating text and started to branch away. I got to this point, and the text loads just fine.

My main file.

#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
using namespace std;

int main()
{
    //Boring window init
    cout<<"Creating window..."<<endl;
    sf::RenderWindow window(sf::VideoMode(800,600),"Space");
    window.setVerticalSyncEnabled(true);

    //Font Creation
    cout<<"Creating Font..."<<endl;

    sf::Font font;

    if(!font.loadFromFile("res/font.ttf"))
    {
        cout<<"Font could not load. Perhaps it doesn't exist?"<<endl;
    }

    sf::Text text;
    text.setFont(font);
    text.setPosition(300,300);
    text.setString("Hello World");

    cout<<"Done."<<endl;

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

        window.clear(sf::Color::Black);

        window.draw(text);

        window.display();
    }

    cout<<"Finished!"<<endl;
    return 0;
}
 



I went on to create a class so I could create a character of any color at any position. I called it Sprite.

Sprite header
#ifndef SPRITE_H
#define SPRITE_H
#include <SFML/Graphics.hpp>

class Sprite
{
    public:
        Sprite(char Character, sf::Font font, float r, float g, float b, float x, float y);
        void draw(sf::RenderWindow& win);
        void updatePosition(float x, float y);
        void updateColor(float r, float g, float b);
    protected:
        char Character;
        float r,g,b;
        sf::Text text;
};

#endif // SPRITE_H


and the Implementation
#include "Sprite.h"
#include <SFML/Graphics.hpp>

Sprite::Sprite(char Character, sf::Font font, float r, float g, float b, float x, float y)
{
    this->Character=Character;
    this->r = r;
    this->g = g;
    this->b = b;
    text.setFont(font);
    text.setPosition(x,y);
    text.setString(Character);
    text.setFillColor(sf::Color(r,g,b));
}

void Sprite::draw(sf::RenderWindow& win)
{
    win.draw(text);
}

void Sprite::updatePosition(float x, float y)
{
    text.setPosition(x,y);
}

void Sprite::updateColor(float r, float g, float b)
{
    text.setFillColor(sf::Color(r,g,b));
}
 

I go to try and create a sprite and see how it works.

Updated main

#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <Sprite.h>
using namespace std;

int main()
{
     //Boring window init
    cout<<"Creating window..."<<endl;
    sf::RenderWindow window(sf::VideoMode(800,600),"Space");
    window.setVerticalSyncEnabled(true);


    //Font creation
    cout<<"Creating Font..."<<endl;

    sf::Font font;

    if(!font.loadFromFile("res/font.ttf"))
    {
        cout<<"Font could not load. Perhaps it doesn't exist?"<<endl;
    }

    Sprite Star('*', font, 0, 255, 255, 5, 5);

    cout<<"Done."<<endl;

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

        window.clear(sf::Color::Black);

        Star.draw(window);

        window.display();
    }

    cout<<"Finished!"<<endl;
    return 0;
}
 

This is what I get



I tried some things including changing the symbol, changing the char to a string, increasing the font size, and even rewrote the class (this is my second draft of the class). I just can't figure out what's happening. I assume it's the Sprite::draw(sf::RenderWindow& win) that is causing trouble. If so, what's the best way I could draw something from another class onto the window? Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text rendered from in a class is displayed as a dot.
« Reply #1 on: March 09, 2019, 08:36:02 am »
You pass your sf::Font argument by value, so your text points to a local copy of it which is destroyed immediately. Your sf::Font argument should be passed by reference.
Laurent Gomila - SFML developer

CrosisBH

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Text rendered from in a class is displayed as a dot.
« Reply #2 on: March 09, 2019, 08:47:08 am »
Thanks for the quick response. I changed my method to pass by reference instead and it's working as intended. Thanks a bunch!

 

anything