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

Author Topic: Weird sf::Font problem  (Read 3339 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Weird sf::Font problem
« on: September 22, 2012, 05:00:09 pm »
Hey guys,

I'm trying to display some simple text and im getting weird errors, now this piece of code works
gEntity.h

#ifndef g_Entity_H
#define g_Entity_H

//includes
#include "g_Rec.h"
#include "g_Image_Manager.h"
#include <iostream>
#include <string>
//Game Includes
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>   

class gEntity{
    private:
    sf::Sprite tex;
        bool spr,txt,btn;
        rect pos;
        sf::Font fontStyle;
                sf::Text textDisplay;
    public:
    gEntity();
        void  setup(int id, gImageManager& imgManage);
        sf::Sprite rtnTex(){return tex;}
        bool rtnSpr(){return spr;}
        bool rtnTxt(){return txt;}
        bool rtnBtn(){return btn;}
        rect rtnPos(){return pos;}
        void intText(int set);
        void stringText(std::string set);
                sf::Text rtnText(){return textDisplay;}
};

#endif
 

//skip alot of the cpp file
 case 1:
                        {
                fontStyle.loadFromFile("arial.ttf");
                //textDisplay.setFont(fontStyle);
                textDisplay.setString("Booty Swag");
                textDisplay.setPosition(100,100);
                spr = false; btn = false; txt = true;
        break;
 

gState code just to draw stuff
for(int i = 0; i < entities.size(); i++ )
                {
                        if (entities.at(i).rtnBtn() == true)
                        {

                        }

                        if (entities.at(i).rtnTxt() == true)
                        {
                                screen.draw(entities.at(i).rtnText());
                        }

                        if (entities.at(i).rtnSpr() == true)
                        {
                                screen.draw(entities.at(i).rtnTex());
                        }
                }
 

now this works fine, but if i close my window sometimes it will stay status is 0 or a random number and throw a error as i close, and the other error is if i uncomment the line which sets the font style at the draw method for the rtnText it displays memory violation... im using SFML 2.0, can anyone help me out?

Cheers

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Weird sf::Font problem
« Reply #1 on: September 22, 2012, 05:20:53 pm »
I bet it's some problem in your state machine/manager, which we can't find out with the given code, also we don't really want to dig through your code, thus it's probably the easiest if you'd just go on it on your own, with a debugger and by minimizing the codebase. ;)
The debugger/call stack will tell you exactly where the problem originated from and by taking away all the parts that don't have anything to do with the error, you end up with a small/minimal example that reproduces the error.
If it's a problem with SFML itself then it's mostly possible to reproduce the problem with a simple main function. ;)

Are you making sure that the state doesn't get freed/deleted before the member variable aren't used anymore? Otherwise you'd end up with an invalid object, which can blow things up quite easily. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Weird sf::Font problem
« Reply #2 on: September 22, 2012, 05:31:36 pm »
Well using my gEntity class it works 100% fine for sprites, but text its just abit weird on, at the moment im trying to get sf::Font in my gState and pass it in via parameter, but it doesnt seem to like that. Is there a way i can pass a sf::Font via parameters?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Weird sf::Font problem
« Reply #3 on: September 22, 2012, 05:46:03 pm »
Is there a way i can pass a sf::Font via parameters?
Yes as (const) reference or (smart) pointer.

void foo(const sf::Font& font)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Weird sf::Font problem
« Reply #4 on: September 22, 2012, 05:59:49 pm »
Ok i have just used const and &, and it works, now i understand that a const will not be deleted, and it will never change, but one thing I don't understand, if i actually pass it as a solid sf::Font it comes up with memory violation, but if i send it as a reference it is fine, could you clear that up for me???

Cheers

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Weird sf::Font problem
« Reply #5 on: September 22, 2012, 06:05:53 pm »
sf::Font is a resource and thus should not be copied around, when you pass with not as reference nor as pointer (nor with move sematics) you pass it by value, i.e. you get a copy of the original. This doesn't work with OpenGL, so you create one and pass references to that one object around.
Now why exactly it crashes, I can't really say... ;)

now i understand that a const will not be deleted...
This is wrong, const only means that 'it' can not change. The 'it' can either relate to pointers, values or functions. You may want to google a bit for const correctness.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Weird sf::Font problem
« Reply #6 on: September 22, 2012, 06:09:29 pm »
k cheers mate, I do have one question though, i can compile my project and close it fine, my friend however can open it fine, but when he closes it, he gets this error
it says access violation reading. He has the same dll's as me and everything is the same, as have just given it to him via a USB pen, could this be to do with something else??? or abit random?

Canvas

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Weird sf::Font problem
« Reply #7 on: September 22, 2012, 06:17:59 pm »
It depends on what causes the error which one could find out with a debugger...

I guess it's something with cleaning up the context etc. Are you using global objects at some point?

Also do you both have either an ATI or a Nvidia graphics card?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Weird sf::Font problem
« Reply #8 on: September 22, 2012, 06:23:01 pm »
Nope no global instances or variables, we both running ATI chips as we on laptops, what debugger would help? the visual studios express 2010 C++ says that status is 0, this is what we get

crt0dat.c
this is the method it displays
void __cdecl __crtExitProcess (
        int status
        )
{
        __crtCorExitProcess(status);

        /*
         * Either mscoree.dll isn't loaded,
         * or CorExitProcess isn't exported from mscoree.dll,
         * or CorExitProcess returned (should never happen).
         * Just call ExitProcess.
         */


        ExitProcess(status);
}
 

he has mscoree.dll in System32,

we are using SFML 2.0 RC
« Last Edit: September 22, 2012, 06:38:30 pm by Canvas »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Weird sf::Font problem
« Reply #9 on: September 22, 2012, 06:31:54 pm »
If you have use Visual Studio, then use the Visual Studio compiler, you attach it to the application and when the application crashes the debugger will tell exactly which functions call where made till the crash occurred (= call stack).
Are you using the SFML 2rc binaries?
If so try to initialize the sf::Text objects directly (i.e. in the initialization list of the constructor) with your own font, so that no default font can get loaded.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Weird sf::Font problem
« Reply #10 on: September 22, 2012, 06:54:42 pm »
Have just tried another project and it seems fine with no crashes, so it seems there is something wrong with the code but its ok, its just for a 48 hour project and its just for fun :)

 

anything