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

Author Topic: Trouble with font/text  (Read 1094 times)

0 Members and 1 Guest are viewing this topic.

boonstyle_

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Trouble with font/text
« on: December 30, 2017, 11:53:28 pm »
Hi there,

im boonstyle and im new to SFML. Before going into my problem i would lik to say that this library is pretty damn neat so keep up the good work. Its perfect for someone like me who always wanted to try out programing a game. As for me im working as programmer in the financial system working with properitary languages (sadly) so i got some kind of experience i guess :D

Now for the problem i went into:

I created a while loop which starts the functions from different classes according to an enum. Im using sf::Font and sf::Text to display some texts (obviously). The problem i did ran into is that as soon i start the function a second time i wont get the text rendered (drawing other objects works). I tried tinker around a little bit but i couldnt find any solution myself nor with the documentation.

the code looks something like this:

int game::start() {
       
        title_screen title_screen; //the classes troubling
        game_menu game_menu; //the classes troubling

//some code here
       
        while (!game_exiting) {        

                switch (_game_state) {

                case game_states::initializing:
                        cout << "initializing\n";
                        player.name = "unnamed"; //just tinkering around for placeholding
                        _game_state = game_states::title_screen;
                        break;

                case game_states::title_screen:
                        cout << "entering title screen\n";
                        title_window.start(&_game_window, _game_state, game_setting.screen_res_x, game_setting.screen_res_y);
                        break;

                case game_states::main_menu:
                        cout << "entering main menu\n";
                        game_menu.start(&_game_window, _game_state, game_setting.screen_res_x, game_setting.screen_res_y);
                        break;

                case game_states::exiting:
                        game_exiting = true;
                        break;

                default:
                        break;

                }
        }

//some code here

        return 0;
}


void title_window::start(sf::RenderWindow * game_window, game::game_states& game_state, int size_x, int size_y) {
       
        //some code here

        //preload font

        if (!screen_font.loadFromFile("arial.ttf"))
        {
        }
        else {
                std::cout << "Successfully loaded\n";
        }
                               
        //some other code here

        while (!exit_title_screen) {

        //some event coding

                if ( /* some fixed time steps */){

                       //more fixed time steps stuff here

                        game_window->clear();

                        sf::CircleShape shape(100.f);
                        shape.setFillColor(sf::Color::White);
                        game_window->draw(shape); //darwing the shapes works perfect every time

                        screen_text.setFont(screen_font);
                        screen_text.setString("The Text");
                        screen_text.setPosition(sf::Vector2f(((size_x - 144) / 2) + 1, ((size_y - 36) / 2) + 1));
                        screen_text.setCharacterSize(30);
                        screen_text.setFillColor(sf::Color::White);

                        game_window->draw(screen_text); // this guy does not as i please :D

                        game_window->display();

                };
        };
}

 

I replaced all code which does not do affect the drawing to make this thing smaller here. The class does draw the text and the shape properly on first load. As soon i return to that loop i dont get the text anymore. The size_x and size_y arent touched anywhere so its still in the right place.

Btw. sorry for probably bad coding style (just started c++ and oop 3 days ago :()
« Last Edit: December 31, 2017, 12:01:27 am by boonstyle_ »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10822
    • View Profile
    • development blog
    • Email
Re: Trouble with font/text
« Reply #1 on: December 31, 2017, 12:33:01 am »
Who owns screen_font? The font needs to exist for as long as you're trying to draw stuff via sf::Text.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

boonstyle_

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Trouble with font/text
« Reply #2 on: December 31, 2017, 12:36:36 am »
screen_font is owned by the classes title_screen and game_menu. Its private in both of them:

class game_menu {

        public:
                void start(sf::RenderWindow *game_window, game::game_states& game_state, int size_x, int size_y);

        private:
                bool exit_game_menu;
                sf::Event event;
                sf::Text screen_text;
                sf::Font screen_font;

                sf::Clock game_timer;
                sf::Clock step_timer;
                sf::Int64 delta_time = 0;
                sf::Int64 delta_time_second = 0;

};
« Last Edit: December 31, 2017, 12:53:41 am by boonstyle_ »

boonstyle_

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Trouble with font/text
« Reply #3 on: December 31, 2017, 09:54:19 am »
I did add a sprite the same way i did with the font. It is loading perfectly like the shapes i create. Its pretty weird the font does not appear :/

Ok i found an odd solution. As soon as i have more then one text drawn all texts do reappear. So by adding another text with other coords and text like:



                screen_text.setString("asdsad");
                screen_text.setPosition(sf::Vector2f(500, 400));

The original text does appear again. But that feels somehow weird :D
« Last Edit: December 31, 2017, 12:18:29 pm by boonstyle_ »