samedi 6 février 2021

LinkedList of Buttons in SFML getting Error: An internal OpenGL call failed in Texture.cpp(98)

I'm trying to create a Button class in SFML that contains a string and a RectangeShape. The Button class itself actually works fine when I instantiate it and call it in my main, but I would like to create a linked list of buttons for the specific project I want. This is when I run into a strange error:

An internal OpenGL call failed in Texture.cpp(98).
Expression:
   glFlush()
Error description:
   GL_INVALID_OPERATION
   The specified operation is not allowed in the current state.

I've narrowed down this error to occurring either when I use the setFont() function, or the append() function of my DoublyLinkedList class. My font is a member variable of my button class so it's not getting destroyed. I've pasted the important pieces of my code below, and you should get the same error if you were to use the same thing. Can anyone provide me more insight into this error? I've looked online but not many legible search results come up.

Button.h:

class Button : public sf::Drawable, public sf::Transformable
{
public:
    Button();
    Button(string text, sf::Vector2f position, int charSize);
    virtual void draw(sf::RenderTarget &window, sf::RenderStates state) const;
private:
    sf::Font font;
    sf::Text name;
    sf::RectangleShape box;
};

Button.cpp:

Button::Button() {}

Button::Button(string text, sf::Vector2f position, int charSize)
{
    float padding = 30;

    if (!font.loadFromFile("../ParkLaneNF.ttf"))
        cout << "Error loading font file." << endl;
    name.setFont(font);
    name.setString(text);
    name.setPosition(position.x + padding, position.y);
    name.setCharacterSize(charSize);

    sf::FloatRect bounds = name.getLocalBounds();
    box.setSize({bounds.width + 2 * padding, bounds.height + 2 * padding});
    box.setPosition(position);
    box.setFillColor(sf::Color::Green);
}

void Button::draw(sf::RenderTarget &window, sf::RenderStates state) const
{
    window.draw(box);
    window.draw(name);
}

(Parts of the) DoublyLinkedList Class:

DoublyLinkedList<T>::DoublyLinkedList() : head(nullptr), tail(nullptr) {}

void DoublyLinkedList<T>::append(T value)
{
    Node<T>* temp = new Node<T>;
    temp->data = value;
    temp->next = nullptr;

    if (empty())
    {
        head = temp;
        temp->prev = nullptr;
    }
    else
    {
        tail->next = temp;
        temp->prev = tail;
    }
    tail = temp;
}

Main Function:

int main()
{
    DoublyLinkedList<Button> buttons;
    Button btn("Hello", {0, 0}, 100);
    buttons.append(btn);

    sf::RenderWindow window(sf::VideoMode(1920, 1080, 32), "BUTTON");
    window.setFramerateLimit(60);

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) window.close();
        }
        window.clear();
        window.draw(btn);
        for (int i = 0; i < buttons.size(); i++)
            window.draw(buttons.getAt(i));
        window.display();
    }
}

Aucun commentaire:

Enregistrer un commentaire