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

Author Topic: Failed to activate OpenGL context: Failed to activate the window's context  (Read 607 times)

0 Members and 1 Guest are viewing this topic.

dimoooooch

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Hello. I was working on my project and when i made a thread that would clean the window from all the objects (buttons and background) the c++ part was working, but sfml part wasn't. After i pressed "Chess" button it gave me this text:

Chess button pressed!
smth
Failed to activate OpenGL context:   .
Failed to activate the window's context

Here's my code:

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;

RenderWindow window(VideoMode::getFullscreenModes()[0], "Chess Fantasy", Style::Fullscreen);

void thread1()
{
    cout << "smth" << endl;
    window.clear();
}

Thread chessThread(&thread1);

int main()
{
   

    Texture texture1, texturebg;
    if (!texture1.loadFromFile("texture1.png") ||
        !texturebg.loadFromFile("texturebg.png"))
    {
        cerr << "Failed to load textures." << endl;
        return 1;
    }

    RectangleShape chessButton(Vector2f(800, 200)); // Прямоугольник для кнопки Chess
    chessButton.setTexture(&texture1);
    chessButton.setOutlineThickness(5);
    chessButton.setOutlineColor(Color(255, 255, 255));
    RectangleShape mazeButton(Vector2f(800, 200)); // Прямоугольник для кнопки Maze
    mazeButton.setTexture(&texture1);
    mazeButton.setOutlineThickness(5);
    mazeButton.setOutlineColor(Color(255, 255, 255));
    RectangleShape quitButton(Vector2f(800, 200)); // Прямоугольник для кнопки Quit
    quitButton.setTexture(&texture1);
    quitButton.setOutlineThickness(5);
    quitButton.setOutlineColor(Color(255, 255, 255));
    RectangleShape background(Vector2f(window.getSize().x, window.getSize().y)); // Прямоугольник для фона
    background.setTexture(&texturebg);

    Font font;
    if (!font.loadFromFile("arial.ttf"))
    {
        cerr << "Failed to load font." <<  endl;
        return 1;
    }

    Text chessText("Chess", font, 72);
    Text mazeText("Maze", font, 72);

    // Уточненные позиции текстов и кнопок для точного выравнивания
    float centerX = window.getSize().x / 2.0f;
    float centerY = window.getSize().y / 2.0f;

    chessText.setPosition(centerX - chessText.getLocalBounds().width / 2, centerY - 300);
    chessButton.setPosition(centerX - chessButton.getLocalBounds().width / 2, centerY - 350);

    mazeText.setPosition(centerX - mazeText.getLocalBounds().width / 2, centerY - 50);
    mazeButton.setPosition(centerX - mazeButton.getLocalBounds().width / 2, centerY - 100);

    Text quitText("Quit", font, 72);
    quitText.setPosition(centerX - quitText.getLocalBounds().width / 2, centerY + 200);
    quitButton.setPosition(centerX - quitButton.getLocalBounds().width / 2, centerY + 150);

    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();

            if (event.type == Event::MouseButtonPressed)
            {
                if (event.mouseButton.button == Mouse::Left)
                {
                    if (chessButton.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y))
                    {
                        cout << "Chess button pressed!" << endl;

                        chessThread.launch();
                    }
                    else if (mazeButton.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y))
                    {
                        cout << "Maze button pressed!" << endl;
                    }
                    else if (quitButton.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y))
                    {
                        window.close();
                    }
                }
            }
        }

        window.draw(background);
        window.draw(chessButton);
        window.draw(chessText);
        window.draw(mazeButton);
        window.draw(mazeText);
        window.draw(quitButton);
        window.draw(quitText);
        window.display();
    }

   

    return 0;
}

« Last Edit: February 05, 2024, 09:33:58 am by eXpl0it3r »

kojack

  • Sr. Member
  • ****
  • Posts: 329
  • C++/C# game dev teacher.
    • View Profile
Re: Failed to activate OpenGL context: Failed to activate the window's context
« Reply #1 on: February 05, 2024, 08:47:58 am »
Threads with SFML rendering need special care. https://www.sfml-dev.org/tutorials/2.6/window-opengl.php#rendering-from-threads
Basically an opengl context can only be active on one thread at a time. You need to deactivate the window's context on the main thread and enable the window's context in the thread before rendering there will work.
(Then deactivate it in the thread and reactivate in the main thread to do the other rendering).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10895
    • View Profile
    • development blog
    • Email
Re: Failed to activate OpenGL context: Failed to activate the window's context
« Reply #2 on: February 05, 2024, 09:36:56 am »
Better yet, don't use multiple threads. There are a lot of things to get wrong and many things you have to understand in order to effectively use multiple threads (mutex, locks, race conditions, resource synchronization, etc.).

There's also little need to have a separate thread to "clean the window from all the objects". Just add some more logic to your existing loop to no render anything in such a case.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3364
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Failed to activate OpenGL context: Failed to activate the window's context
« Reply #3 on: February 07, 2024, 04:44:38 pm »
It's also worth noting that the Render Window is global. SFML resources should never* be global objects.

*However, pointers are okay as long as they're initialized as a null pointer. This is because you decide when to create the object meaning that they get called after main has started in a similar way to if it was created from inside main. (you can basically create it as the first line of main)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

hdsiria

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Failed to activate OpenGL context: Failed to activate the window's context
« Reply #4 on: February 14, 2024, 10:53:53 am »
I dread and hate using multithreading. Threads are not safe. difficult.I always avoid using threads