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

Author Topic: Font loading problem (Crash)  (Read 7158 times)

0 Members and 1 Guest are viewing this topic.

xorioz

  • Newbie
  • *
  • Posts: 9
    • View Profile
Font loading problem (Crash)
« on: March 19, 2014, 03:10:23 pm »
So. I'm fairly new to SFML and still learning c++.
And i'm curently in the process of making a simple Frame counter "FPS".
But i have run into a problem with Font loading.
When i do an ultra simple code like this.

    // some text stuff
    sf::Font font;
    if (!font.loadFromFile("consola.ttf"))
    {
        // error...
    }
    sf::Text text;
    text.setString("FPS: 0");
    text.setFont(font);
    text.setCharacterSize(12);
    text.setColor(sf::Color::Black);
    text.setStyle(sf::Text::Bold);
 

the program functions just fine.
But when i start to work with classes the program crashes at startup

Game.h
class Game
{
public:
    Game();
    void run();
private:
    ...
private:
    ...
    sf::Font mFont;
    sf::Text mText;
};
 

Game.cpp
Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application"), mFont(), mText()
{
    if (!mFont.loadFromFile("consola.ttf"))
    {
       /* Handle loading error */
    };

    mText.setString("FPS: 0");
    mText.setFont(mFont);
    mText.setCharacterSize(12);
    mText.setColor(sf::Color::Black);
    mText.setStyle(sf::Text::Bold);
}
 

Now i think it has something to do with the font as the problem persists in all cases except when i comment out mText.setFont(mFont);
I have tried other fonts including Times New Roman.

I'm currently reading up on debugging in code::blocks but i'm not getting anywhere.
If anyone can give a helping hand i would appreciate it a lot.

Thinks XorioZ

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Font loading problem (Crash)
« Reply #1 on: March 19, 2014, 03:12:56 pm »
How/where do you create the Game instance? In which scope is it declared?
Laurent Gomila - SFML developer

xorioz

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Font loading problem (Crash)
« Reply #2 on: March 19, 2014, 03:34:57 pm »
well the class Game is declared in Game.h witch in turn is included in main.cpp where i initialize the class as the object game and execute the run function of the class
#include "Game.h"

int main()
{
    Game game;
    game.run();
}
 

Vary simple.

I can do a full code paste here. might help.

main.cpp
#include "Game.h"

int main()
{
    Game game;
    game.run();
}
 

Game.h
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>

class Game
{
public:
    Game();
    void run();
private:
    void calculateFPS(sf::Time deltaTime);
    void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
    void processEvents();
    void update(sf::Time deltaTime);
    void render();
private:
    bool mIsMovingUp = false, mIsMovingDown = false, mIsMovingLeft = false, mIsMovingRight = false;
    float mPlayerSpeed = 100.0f;
    sf::Time mTimePerFrame = sf::seconds(1.0f / 60.0f), returnTime = sf::seconds(0.0f), FPSTime = sf::seconds(1.0f);
    sf::RenderWindow mWindow;
    sf::Texture mTexture;
    sf::Sprite mPlayer;
    sf::Font mFont;
    sf::Text mText;
    int FPS = 0;
    std::string baseStr = "FPS: ", showStr;
};

#endif // GAME_H_INCLUDED
 

Game.cpp
#include "Game.h"

Game::Game()
    : mWindow(sf::VideoMode(640, 480), "SFML Application")
    , mPlayer()
    , mTexture()
    , mFont()
    , mText()
{
    if (!mFont.loadFromFile("consola.ttf"))
    {
       /* Handle loading error */
    };

    if (!mTexture.loadFromFile("dot.png"))
    {
        /* Handle loading error */
    }

    mPlayer.setTexture(mTexture);
    mPlayer.setPosition(100.f, 100.f);
    mText.setString("FPS: 0");
    mText.setFont(mFont);
    mText.setCharacterSize(12);
    mText.setColor(sf::Color::Black);
    mText.setStyle(sf::Text::Bold);
}
void Game::run()
{
    sf::Clock clock;
    sf::Clock frameTimer;
    sf::Time timeSinceLastUpdate = sf::Time::Zero;
    while (mWindow.isOpen())
    {
        returnTime += frameTimer.restart();
        calculateFPS(returnTime);
        processEvents();
        timeSinceLastUpdate += clock.restart();
        while (timeSinceLastUpdate > mTimePerFrame)
        {
            timeSinceLastUpdate -= mTimePerFrame;
            processEvents();
            update(mTimePerFrame);
        }
        render();
    }
}

void Game::calculateFPS(sf::Time deltaTime)
{
    FPS++;
    if(deltaTime >= FPSTime)
    {
        std::stringstream ss;
        ss << FPS;
        showStr = baseStr + ss.str();
        mText.setString(showStr);
        FPS = 0;
        deltaTime = sf::seconds(0.0f);
    }
}

void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{
    if (key == sf::Keyboard::W)
        mIsMovingUp = isPressed;
    else if (key == sf::Keyboard::S)
        mIsMovingDown = isPressed;
    else if (key == sf::Keyboard::A)
        mIsMovingLeft = isPressed;
    else if (key == sf::Keyboard::D)
        mIsMovingRight = isPressed;
}

void Game::processEvents()
{
    sf::Event event;
    while (mWindow.pollEvent(event))
    {
        switch (event.type)
        {
        case sf::Event::KeyPressed:
            handlePlayerInput(event.key.code, true);
            break;
        case sf::Event::KeyReleased:
            handlePlayerInput(event.key.code, false);
            break;
        case sf::Event::Closed:
            mWindow.close();
            break;
        }
    }
}

void Game::update(sf::Time deltaTime)
{
    sf::Vector2f movement(0.f, 0.f);
    if (mIsMovingUp)
        movement.y -= mPlayerSpeed;
    if (mIsMovingDown)
        movement.y += mPlayerSpeed;
    if (mIsMovingLeft)
        movement.x -= mPlayerSpeed;
    if (mIsMovingRight)
        movement.x += mPlayerSpeed;
    mPlayer.move(movement * deltaTime.asSeconds());
}

void Game::render()
{
    mWindow.clear(sf::Color::White);
    mWindow.draw(mPlayer);
    mWindow.display();
}
 

Well i know the code is not perfect. but i'm still learning proper coding practices so bare with me :)
PS. using SFML 2.1
« Last Edit: March 19, 2014, 03:39:41 pm by xorioz »

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Font loading problem (Crash)
« Reply #3 on: March 19, 2014, 04:57:06 pm »
First: You should NOT initialize your class members in the header file.

Second: Did some testing after some modification for making it working with visual c++. So it making me wonder, does your code compile at all? After I did get your the code compiled, I got no crash. hmmm...

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Font loading problem (Crash)
« Reply #4 on: March 19, 2014, 04:59:57 pm »
First: You should NOT initialize your class members in the header file.
http://www.stroustrup.com/C++11FAQ.html#member-init
Back to C++ gamedev with SFML in May 2023

xorioz

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Font loading problem (Crash)
« Reply #5 on: March 19, 2014, 06:01:55 pm »
W8 now i'm confused.
I'm compiling just fine with the code as is. using Dynamic linked libraries.
Are you sure you are compiling with the newest mingw/gcc compiler with c++ 1.1 enabled?
And according to the link FRex wrote i can initialize my members in the header just fine "c++ 1.1".
If it's good coding practice is a whole other matter.

I'm getting some stuff out of the debugger in C::B and it looks like the error might have something to do with another library that SFML depends upon.

Oh well i'm about to go to bed atm so I will post some debugging info tomorrow.

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: Font loading problem (Crash)
« Reply #6 on: March 19, 2014, 11:36:42 pm »
A quick note about your code (I know you are learning c++, but the best way to learn is to correct small errors ),

You must not put ; after an if statement, very bad!

Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application"), mFont(), mText()
{
    if (!mFont.loadFromFile("consola.ttf"))
    {
       /* Handle loading error */
    };    //NO ; here -----------------------------------------------

    mText.setString("FPS: 0");
    mText.setFont(mFont);
    mText.setCharacterSize(12);
    mText.setColor(sf::Color::Black);
    mText.setStyle(sf::Text::Bold);
}

 

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Font loading problem (Crash)
« Reply #7 on: March 20, 2014, 12:01:13 am »

You must not put ; after an if statement, very bad!

If you put it after the closing brace, it makes no difference.
Current Projects:
Technoport

xorioz

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Font loading problem (Crash)
« Reply #8 on: March 20, 2014, 07:47:51 am »
so after running the debugger i get this.
Error while reading shared library symbols for C:\Program Files (x86)\CodeBlocks\MinGW\bin\libstdc++-6.dll:
Program received signal SIGSEGV, Segmentation fault.
At c:\progra~2\codebl~1\mingw\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algobase.h:697

I also have this in my call stack.
#0 68F8351B   std::__fill_a<unsigned char>(__first=0x0, __last=0x344 <Address 0x344 out of bounds>, __c=@0x28f33f: 255 '\377') (c:/progra~2/codebl~1/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_algobase.h:697)
#1 68F82404   std::__fill_n_a<unsigned int, unsigned char>(__first=0x0, __n=836, __c=@0x28f33f: 255 '\377') (c:/progra~2/codebl~1/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_algobase.h:754)
#2 68F8333F   std::fill_n<unsigned char*, unsigned int, unsigned char>(__first=0x0, __n=836, __value=@0x28f33f: 255 '\377') (c:/progra~2/codebl~1/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_algobase.h:780)
#3 68F7AB37   std::__uninitialized_fill_n<true>::__uninit_fill_n<unsigned char*, unsigned int, unsigned char>(__first=0x0, __n=836, __x=@0x28f33f: 255 '\377') (c:/progra~2/codebl~1/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_uninitialized.h:206)
#4 68F82933   std::uninitialized_fill_n<unsigned char*, unsigned int, unsigned char>(__first=0x0, __n=836, __x=@0x28f33f: 255 '\377') (c:/progra~2/codebl~1/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_uninitialized.h:225)
#5 68F82D7B   std::__uninitialized_fill_n_a<unsigned char*, unsigned int, unsigned char, unsigned char>(__first=0x0, __n=836, __x=@0x28f33f: 255 '\377') (c:/progra~2/codebl~1/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_uninitialized.h:336)
#6 68F7DE4B   std::vector<unsigned char, std::allocator<unsigned char> >::_M_fill_insert(this=0x28fe14, __position=..., __n=836, __x=@0x28f3c8: 255 '\377') (c:/progra~2/codebl~1/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/vector.tcc:464)
#7 68F7E2FB   std::vector<unsigned char, std::allocator<unsigned char> >::insert(this=0x28fe14, __position=..., __n=836, __x=@0x28f3c8: 255 '\377') (c:/progra~2/codebl~1/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_vector.h:1004)
#8 68F7E35B   std::vector<unsigned char, std::allocator<unsigned char> >::resize(this=0x28fe14, __new_size=836, __x=255 '\377') (c:/progra~2/codebl~1/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_vector.h:687)
#9 68EC2E66   sf::Font::loadGlyph(this=0x28fde8, codePoint=70, characterSize=30, bold=false) (D:\Projects\SFML-master\src\SFML\Graphics\Font.cpp:467)
#10 68EC26AC   sf::Font::getGlyph(this=0x28fde8, codePoint=70, characterSize=30, bold=false) (D:\Projects\SFML-master\src\SFML\Graphics\Font.cpp:282)
#11 68EDFCCB   sf::Text::updateGeometry(this=0x28fe1c) (D:\Projects\SFML-master\src\SFML\Graphics\Text.cpp:303)
#12 68EDEFBF   sf::Text::setFont(this=0x28fe1c, font=...) (D:\Projects\SFML-master\src\SFML\Graphics\Text.cpp:78)
#13 00401839   Game::Game(this=0x28fab0) (D:\Projects\C++\Engine base\Game.cpp:23)
#14 00402480   main() (D:\Projects\C++\Engine base\main.cpp:5)


don't know if that helps at all :)

xorioz

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Font loading problem (Crash)
« Reply #9 on: March 20, 2014, 08:08:44 am »
oh. and just to be safe i moved all class member initialization to the constructor in Game.cpp and removed all semicolons after closing brackets :)

xorioz

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Font loading problem (Crash)
« Reply #10 on: March 20, 2014, 03:08:11 pm »
On a side note i found another "obvious" flaw in my programming.
void Game::calculateFPS(sf::Time deltaTime)
{
    FPS++;
    if(deltaTime >= FPSTime)
    {
        std::stringstream ss;
        ss << FPS;
        showStr = baseStr + ss.str();
        mText.setString(showStr);
        FPS = 0;
        deltaTime = sf::seconds(0.0f);
    }
}
 

notice how i set deltaTime to 0 if it's over 1
This does not do a thing as the sf::Time a pass to the function isn't changed :)
So this makes the FPS text update every frame instead of every second and it just prints out FPS: 1.

I'm using cout until i can get the font loading fixed.

xorioz

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Font loading problem (Crash)
« Reply #11 on: March 21, 2014, 02:17:24 pm »
oh well.
I'm not good at doing nothing so i'm gonna delete all sfml libraries on my computer.
Remove mingw and grab a new one.
set mingw up from scratch making sure all my environment vars are set properly.
Redownload SFML 2.1 source.
use cmake and my newly acquired mingw to compile SFML and then set up my project from scratch.
hoping this will fix my problem or at least rule out any linker or dependency problems.

xorioz

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Font loading problem (Crash)
« Reply #12 on: March 21, 2014, 03:01:28 pm »
ok...Note to self. "and other noobs"
Dont use the newest version of MinGW.
It craps all over SFML...

Can anyone recomend a good c++1.1 compiler.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Font loading problem (Crash)
« Reply #13 on: March 21, 2014, 03:08:56 pm »
I recommend a version from the MinGW Builds project. You might want to use the POSIX thread model incase you'd ever want to use std::thread. Here's a link to MinGW-w64 32bit POSIX, GCC 4.8.1, release. Whether you want to use SJLJ or DWARF exception model is your decision (explanation).

Get that, get the SFML source, get CMake and build SFML. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Font loading problem (Crash)
« Reply #14 on: March 21, 2014, 03:17:15 pm »
Can anyone recomend a good c++1.1 compiler.

I'm using clang 3.4 on Linux and I'm very happy with it and its C++11 support.