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

Author Topic: Multithreading Results in Window Context Activation Fail  (Read 10628 times)

0 Members and 1 Guest are viewing this topic.

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Multithreading Results in Window Context Activation Fail
« on: January 07, 2014, 06:07:42 pm »
Hi,

My code is causing errors in the console window:
"Failed to activate the window's context" and sometimes something about creating the OpenGL context.
The errors are thrown repeatedly.
#include <SFML/Graphics.hpp>
#include <atomic>
#include <iostream>

struct gameClass
{
    gameClass(): cSettings(24,8,16,4,4), window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, cSettings) {}
    sf::ContextSettings cSettings;
    sf::Window window;
    std::atomic_bool glHasControl;
};

int drawGLScene(gameClass* gameCore)                // Here's where we draw
{
    gameCore->window.setActive(true);

    while (gameCore->glHasControl.load(std::memory_order_acquire))
        gameCore->window.display();

    gameCore->window.setActive(false);
    return 1;                    // Everything went OK
}

int main()
{
    gameClass gameCore;

    sf::ContextSettings settings = gameCore.window.getSettings();
    std::cout << "depth bits:" << settings.depthBits << std::endl;
    std::cout << "stencil bits:" << settings.stencilBits << std::endl;
    std::cout << "antialiasing level:" << settings.antialiasingLevel << std::endl;
    std::cout << "version:" << settings.majorVersion << "." << settings.minorVersion << std::endl;

    gameCore.window.setVerticalSyncEnabled(true);
    gameCore.glHasControl.store(true, std::memory_order_release);
    sf::Thread displayThread(&drawGLScene, &gameCore);
    displayThread.launch();
    while (gameCore.window.isOpen())
    {
        sf::Event event;
        while (gameCore.window.pollEvent(event))
        {
            gameCore.glHasControl.store(false, std::memory_order_release);
            displayThread.wait();
            gameCore.window.setActive(true);

            if (event.type == sf::Event::Closed)
                gameCore.window.close();
        }
    }
    return 0;
}
 

-Jared
« Last Edit: January 18, 2014, 05:59:23 pm by Users.RandomUser(); »

amir ramezani

  • Jr. Member
  • **
  • Posts: 81
  • i'm a programmer who can't see well
    • View Profile
    • download useful software!
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #1 on: January 07, 2014, 07:19:38 pm »
read the SFML's window tutorial first and try to fix it!
I've got your code
but, I haven't time to fix that
if you can't see well, you can't test your applications and operating system well
my game engine:
allegro game creator
my operating system:
AmirOS

amir ramezani

  • Jr. Member
  • **
  • Posts: 81
  • i'm a programmer who can't see well
    • View Profile
    • download useful software!
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #2 on: January 07, 2014, 08:14:34 pm »
I've uploaded the modified source with the given project as the static library in code::blocks
I've compiled it, it worked fine for me ;)
if you can't see well, you can't test your applications and operating system well
my game engine:
allegro game creator
my operating system:
AmirOS

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #3 on: January 07, 2014, 09:41:11 pm »
I fixed the | and made it a ||, but project still starts with a ton of "failed to activate the window's context" errors in the black console window.



Thanks for the quick response, I enjoy SFML and it's community now.  BEST MULTIMEDIA LIBRARY EVERRRRRRRRR!

-Jared
« Last Edit: January 07, 2014, 10:09:24 pm by Users.RandomUser(); »

amir ramezani

  • Jr. Member
  • **
  • Posts: 81
  • i'm a programmer who can't see well
    • View Profile
    • download useful software!
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #4 on: January 08, 2014, 05:12:06 am »
add -mwindows to linker flags
if you can't see well, you can't test your applications and operating system well
my game engine:
allegro game creator
my operating system:
AmirOS

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #5 on: January 08, 2014, 05:51:11 pm »
That only removes the command line, but it's throwing errors!!!  Wouldn't fixing the errors be a better solution?

-Jared

amir ramezani

  • Jr. Member
  • **
  • Posts: 81
  • i'm a programmer who can't see well
    • View Profile
    • download useful software!
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #6 on: January 08, 2014, 06:16:08 pm »
I think that fixe's your error
if you can't see well, you can't test your applications and operating system well
my game engine:
allegro game creator
my operating system:
AmirOS

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #7 on: January 08, 2014, 08:37:45 pm »
Why, though?  How does it work?  What does it do?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Multithreading Results in Window Context Activation Fail
« Reply #8 on: January 08, 2014, 08:53:50 pm »
Do not keep any OpenGL context related objects as global variables. There are known issues with render windows and other SFML types when used globally. In your case, this concerns the gameCore variable.

In general, it's a good idea to avoid global/static variables wherever possible (see also here).

By the way, try to keep your code minimal, so that you can also insert it directly in the post with code tags. A lot of people are scared off when they have to download files just to answer the question.
« Last Edit: January 08, 2014, 08:57:38 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #9 on: January 08, 2014, 09:47:09 pm »
Thanks for the response!  I tried pointing the window to the thread's function at first, but it didn't work.  However, I was just pointing to the address of the variables; they themselves weren't initially pointers.  How should I go about passing the window context, and should I go back to pointers instead of storing data in gameCore?  I knew to avoid globals, but I didn't know to what extent.  I'll try to remember not to post files as the only source of code, too.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Multithreading Results in Window Context Activation Fail
« Reply #10 on: January 08, 2014, 09:54:50 pm »
However, I was just pointing to the address of the variables
What do you mean? The sf::Window in your class is not a pointer, so it will be created with the global variable.

Why don't you declare the gameCore in main() and pass it as an argument to the constructors and functions that need it?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #11 on: January 09, 2014, 12:41:33 am »
I was declaring window in main, and I passed &window through the thread arguments.  In the function, I typed something like bool* glRunning.

I will try passing a gameCore pointer to the function.  I may not have time until tomorrow.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Multithreading Results in Window Context Activation Fail
« Reply #12 on: January 09, 2014, 01:37:18 am »
Can you show the current code here? Try to keep it as minimal as possible (but still complete).

By the way, if you use bool* glRunning as a thread flag, that's not threadsafe. You might want to use atomic bools instead.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #13 on: January 11, 2014, 05:33:42 pm »
Sadly, the error persists.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #14 on: January 11, 2014, 05:58:58 pm »
Can you show the current code here? Try to keep it as minimal as possible (but still complete).
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor