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.


Topics - Pinco

Pages: [1]
1
Window / Switching from debug to release triggers access violation
« on: January 29, 2020, 04:26:38 pm »
Hello,
let me preface this by acknowledging that declaring an sf::Window globally is a bad idea and it leads to undefined behavior, however I'd like to know more about this issue. I'm not looking for a solution, I'm trying to learn.
Due to how (poorly) I designed one of my apps I resorted to declaring the Window globally, the compiler (VS2019 IDE) compiles without issues in both Debug and Release mode x86. My problem starts at runtime: it goes smoothly in Release but in Debug it wont open the window throwing an access violation in the sf::Window constructor.

[TLDR] What changes between debug and release? Is it a compiler thing (does the order/schedule of compiling change?)? Is it OS dependent?
Can anyone point me in the right direction?

Note: I'm not looking for a solution to a badly design piece of software, I'm trying to understand the library (and programming in general) on a lower level.

I know it's kind of a weird and useless question so if an admin feels that this post doesn't belong please let me know, I'll be happy to remove it and ask someplace else. Cheers.

2
Graphics / [SOLVED] Can't load same font file twice
« on: January 28, 2018, 01:14:29 pm »
I have two classes, 'Textbox' and 'Label', that display text and in order to have the font locally declared I'm loading the font every time in the constructor. However while in 'Label' it loads without issues in 'Textbox' it fails to load, is it because you can load a font from the same file only once?

Would a solution be loading the font in a sf::InputStream globally and then setting the font individually (in each constructor)?

Textbox constructor
Textbox::Textbox(int max_chars, bool numeric) {
        // Object constructor, creates a white box with black outlines.

        max_length = max_chars;
        m_gshape.setSize(Vector2f(6 + 15 * max_length, 30));
        m_gshape.setFillColor(Color::White);
        m_gshape.setOutlineThickness(2);
        m_gshape.setOutlineColor(Color(60, 60, 60));

        if (!default_font.loadFromFile(FONT_LOCATION))
                cout << "ERROR LOADING FONT" << endl;

        m_textbox.setFont(default_font);
        m_textbox.setCharacterSize(25);
        m_textbox.setFillColor(Color::Black);

        // Fill the TB with tight amount of chars in order to build the box around it.
        if (max_chars > 1)
                m_textbox.setString(to_string((int)pow(10, max_chars - 1)));    // Sets the length of the box relative to the maxium amount of chars that can be entered.
        else
                m_textbox.setString("0");       // You can only enter 1 char so 0 is just a placeholder to build the TB

        if (numeric) {  // Values between ASCII 47->58 are the numbers 0->9
                min_ascii = 47;
                max_ascii = 58;
        }
}

Label constructor
Label::Label(string text) {
        // Object constructor, black text 25 pixel high.
        if (!default_font.loadFromFile(FONT_LOCATION))
                cout << "ERROR LOADING FONT" << endl;

        m_label.setFont(default_font);
        m_label.setCharacterSize(25);
        m_label.setFillColor(Color::Black);
        m_label.setString(text);

        m_label.setOrigin(m_label.getGlobalBounds().left, m_label.getGlobalBounds().top);
}

sf::Font default_font is declared locally in the header of each class as a private member.

3
Graphics / [SOLVED] Can't set origin of sf::Text inside class constructor
« on: January 26, 2018, 08:48:03 pm »
So I have this 'Label' class based on sf::Text and I'm trying to make it so that Label.getPosition() returns the top-left coordinates of the text (as you would do by adding the .top and .left offset). However my code sets the new origin to (0, -25) thus making it invisible, how can I fix this?

Label constructor:
Label::Label(string text) {
        // Object constructor, black text 25 pixel high.

        m_label.setFont(default_font);
        m_label.setCharacterSize(25);
        m_label.setFillColor(Color::Black);
        m_label.setString(text);
        m_label.setOrigin(m_label.getLocalBounds().width - m_label.getLocalBounds().left, m_label.getLocalBounds().height - m_label.getLocalBounds().top);
       
}

Pseudo code for main loop:
Label text("Hello World!");

int main() {
        // Basic window function and event handling here

        text.setPosition(10, 10);
       
        window.clear();
        window.draw(text);
        window.display();
}

The outcome is just an empty window.

[S O L U T I O N]
Looks like eXpl0it3r was TOTALLY correct, the font must be declared locally. You are an experienced dev!
Thanks for the help.

Pages: [1]