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.


Messages - 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 / Re: Text wrong display position
« on: February 04, 2018, 09:51:36 pm »
Its standart Arial font.
I can manually measure offset to need me character sizes from Top / Left - but its looks like horrible decision.

Please help me normal display Text at exact pixels in window.

Okey I found - its offset in Text::getLocalBounds().

And some Topic already >> https://en.sfml-dev.org/forums/index.php?topic=20284.0

You don't have to do it manually, you can use .getGlobalBounds().top and .left, that should do the trick. I had the same problem.

If you want to set the position of the text based on the first pixel in the top-left corner you should do this:
txt.setOrigin(txt.getGlobalBounds().left, txt.getGlobalBounds().top) to compensate for the offset

EDIT: Didn't see you solved it already, I'm gonna leave this here for anyone who needs the answer.

3
Graphics / Re: xcode image loading error
« on: February 04, 2018, 09:47:03 pm »
Quote
I tried the whole path, which is:
if (!mTexture.loadFromFile("/Users/Prummi/Desktop/dev/C++/SFML/SFML Game Development Book/Media/Textures"))
but this didn't work. It gave me the same error (unable to open file)

You didn't put the file name though, try: "/Users/Prummi/Desktop/dev/C++/SFML/SFML Game Development Book/Media/Textures/Eagle.png"

4
Graphics / Re: xcode image loading error
« on: February 04, 2018, 01:07:11 pm »
Try using the whole path, I'm not really familiar with xCode but I'll try to help.

Also try posting minimal code so that we can get an idea of what you are doing wrong.

5
Graphics / Re: Can't load same font file twice
« on: February 04, 2018, 01:02:40 pm »
Check the console to know why it failed to load. Loading the same font multiple times is certainly possible.

Generally it's advised to load fonts and other resources just once. I tend to use a resource holder as Thor provides and pass resources down by reference: http://www.bromeon.ch/libraries/thor/tutorials/v2.0/resources.html

Turns out I had the FONT_LOCATION string poorly declared as a const string in the header, it's now working correctly.

6
Graphics / Re: Text Box
« on: February 04, 2018, 01:00:36 pm »
Good day!
I'm writing my TextBox so that the text does not go beyond the window, I use
glEnable(GL_SCISSOR_TEST);
glScissor(x, y, getSize().x, getSize().y);
window.draw(text);
glDisable(GL_SCISSOR_TEST);
But how do I use it if I have more than one window?

Do you want to input text even though it won't fit in the textbox or do you want to prevent the user from entering more text than what actually fits?

It's not really clear what you want to do and we are missing part of the code. Could you articulate better?

7
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.

8
I have to ask: if you want the top-left of the text to be the origin (and therefore the position) of the object, are you sure that you need the width and height of the text?

Yeah that was a mistake on my part, you just need .top and .left.

9
Graphics / Re: Can't set origin of sf::Text inside class constructor
« on: January 26, 2018, 09:47:49 pm »
Looks like eXpl0it3r was TOTALLY correct, the font must be declared locally. You are an experienced dev!
Thanks for the help.

10
Graphics / Re: Can't set origin of sf::Text inside class constructor
« on: January 26, 2018, 09:27:43 pm »
By "doesn't really" do you mean, you have made all SFML resources non-global (even your font object)? Or does it mean, you just guess the issue must be somewhere else?

I guesstimated, I've made every resource local except for the font as I need it app-wide. I'll try it anyways and get back to you.

So I tried and that's not the issue.
I also found out that the issue lies in how I'm using setOrigin() because very similar classes (where I don't change the origin) don't have this problem, but I haven't found the solution yet.

11
Graphics / Re: Can't set origin of sf::Text inside class constructor
« on: January 26, 2018, 09:21:26 pm »
By "doesn't really" do you mean, you have made all SFML resources non-global (even your font object)? Or does it mean, you just guess the issue must be somewhere else?

I guesstimated, I've made every resource local except for the font as I need it app-wide. I'll try it anyways and get back to you.

12
Graphics / Re: Can't set origin of sf::Text inside class constructor
« on: January 26, 2018, 09:05:19 pm »
Don't use globally initialized SFML resources.

You mean I should put the Label text("Hello World!") declaration inside main? That doesn't really solve the issue.

13
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]