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

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

0 Members and 1 Guest are viewing this topic.

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #15 on: January 11, 2014, 06:11:36 pm »
Explain what is wrong with the code I posted, zsbzsb.
« Last Edit: January 11, 2014, 06:24:34 pm by Users.RandomUser(); »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Multithreading Results in Window Context Activation Fail
« Reply #16 on: January 11, 2014, 06:33:33 pm »
Explain what is wrong with the code I posted, zsbzsb.
It contradicts your explanations. You said:
Quote
I was declaring window in main, and I passed &window through the thread arguments. I typed something like bool* glRunning.
Neither applies to the code you have shown us. We don't like to guess, please show the most recent code and explain what's wrong with it (not another code you once used).

Furthermore, keep the code minimal, remove everything that's not needed (initGL() which does nothing, lots of member variables and initialization settings). Read also this post.
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 #17 on: January 11, 2014, 06:37:13 pm »
I at one time, but not currrently used bool* glRunning. I will clean up the code right now.
EDIT: How is it now?
« Last Edit: January 11, 2014, 06:45:50 pm by Users.RandomUser(); »

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 #18 on: January 11, 2014, 07:26:08 pm »
Explain what is wrong with the code I posted, zsbzsb.

Sorry  :-\

Didn't see you had edited your first post  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #19 on: January 12, 2014, 07:18:40 pm »
Does everyone get this error?

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #20 on: January 13, 2014, 12:15:16 am »
With your currently posted code I am getting

error C2864: 'gameClass::cSettings' : only static const integral data members can be initialized within a class

for this line:
Code: [Select]
sf::ContextSettings cSettings = sf::ContextSettings(24,8,16,4,3);

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #21 on: January 13, 2014, 02:39:24 am »
I use Code::Blocks.  I used to use Visual Studio, and that looks similar to its errors with the "C2864".  Anyway, if it is unsupported by something, I'll fix that by setting the values outside the class, as I may want to change those settings during runtime in the future.

EDIT:  Interesting, it calls the struct a class.  Must be used for both.
« Last Edit: January 13, 2014, 02:49:45 am by Users.RandomUser(); »

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #22 on: January 14, 2014, 10:11:56 pm »
Gobbles, see if the code works now.

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #23 on: January 14, 2014, 10:28:02 pm »
Yes it compiles, although I still see the context issue. I took the liberty to move it all into a class design ( within my own game ) and I am not seeing this issue at all.

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #24 on: January 14, 2014, 11:16:18 pm »
What does your class design look like?  It would be nice to know what I could change to fix this.

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #25 on: January 14, 2014, 11:50:31 pm »
mostly just messing around with it but in my Game.h I have:
Code: [Select]
std::unique_ptr<sf::Thread> CharLoadThread;
Then in the .cpp constructor I have
Code: [Select]
CharLoadThread = std::unique_ptr<sf::Thread>(new sf::Thread(&Game::LoadCharacter, this));
CharLoadThread->launch();
and Load Character is pretty standard, but I had to add a sf::Context context;
Code: [Select]
void Game::LoadCharacter()
{
/* Stuff I'm Loading */
sf::Context context;
}
That's it that's all.

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #26 on: January 15, 2014, 10:33:45 pm »
None of that seems to make a difference.  Is it possible for you to post all of your code in a file?  I really want to see a working version of this.

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #27 on: January 15, 2014, 11:42:12 pm »
I removed all non-essential code, but try these 3 files

main:
[
#include "Game.h"
#include <stdlib.h>
#include <time.h>
//DEBUG CODE
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define DEBUG_NEW new(_CLIENT_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

int main()
{
        _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
        std::unique_ptr<Game> game(new Game());
        srand(time(NULL));
        game->Run();

    return EXIT_SUCCESS;
}
 

Game.h
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <sstream>
#include <cmath>
#include <iostream>

class Game
{
public:
        Game();
        ~Game();
        void Run();

private:
        std::unique_ptr<sf::RenderWindow> window;

        //Loading Thread
        std::unique_ptr<sf::Thread> CharLoadThread;
};
 

and Game.cpp

#include "Game.h"

Game::Game()
{
screenSize = sf::Vector2f(1024,768);
        window = std::unique_ptr<sf::RenderWindow>(new sf::RenderWindow(sf::VideoMode(static_cast<int>(screenSize.x), static_cast<int>(screenSize.y)), "Re:Volved ver 0.0.04a", sf::Style::Close));

        window->setKeyRepeatEnabled(false);
        //LoadCharacter();
        CharLoadThread = std::unique_ptr<sf::Thread>(new sf::Thread(&Game::LoadCharacter, this));
        CharLoadThread->launch();
}

Game::~Game()
{
}

void Game::LoadCharacter()
{
        std::cout << "Start Loading\n";
        //groundMap = std::make_shared<Map>(MainCamera);
        //groundMap->Read();
    std::cout << "Map Read\n";
        //charDef = std::unique_ptr<CharDef>(new CharDef("skeleton"));
        std::cout << "CharDef Loaded\n";
        //character = std::unique_ptr<Character>(new Character(sf::Vector2f(500.f, 100.f), *charDef, 0));
        std::cout << "Character Loaded\n";
        //character->BodypartsInit();
        std::cout << "Character Bodyparts Loaded\n";
    //enemy = std::unique_ptr<Enemy>(new Enemy(sf::Vector2f(800.f, 100.f), *charDef, 1));
        std::cout << "Enemy Loaded\n";
    //enemy->BodypartsInit();
        std::cout << "Enemy BodyParts Loaded\n";
        //sf::Context context;
        Loaded = true;
}

void Game::Run()
{
        sf::Clock clock;
        sf::Time timeSinceLastUpdate = sf::Time::Zero;
        static const sf::Time timePerFrame = sf::seconds(1.f / 60.f);

        fpsText.setFont(fpsFont);

        while (window->isOpen())
    {
                timeSinceLastUpdate += clock.restart();

               
                while(timeSinceLastUpdate > timePerFrame)
                {
                        timeSinceLastUpdate -= timePerFrame;
                        std::cout << "Updating\n";
                }
        }
}
 

Let me know if this causes any problems for you, I think I have everything in there.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10911
    • View Profile
    • development blog
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #28 on: January 16, 2014, 09:16:56 am »
Although it's not really necessary to put the window or the thread into unique_ptr, it shouldn't have anything to do with the described problem.

Let's do some checking before moving on: What GPU do you have and is your driver up-to-date?
What OpenGL version does SFML return? (Get the ContextSettings from the window and check the major and minor property.)
Are you now using Code::Blocks or Visual Studio? What version are you using? Which OS are you using?

Since the error is a bit non descriptive, you'll need to debug further. Make sure you're in debug mode and obviously use the debug libraries of SFML. Next make sure the debugger can find SFML's source code. Then set a break point (or step into the application until you get) to the line linked here. When you're there with the debugger check each of the three parts of the boolean equation. Post here which ever is interpreted as false.

Alternatively you can just pick one and google a bit, to see what issues exist. For example "wglMakeCurrent fails" gives you around 16k results.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Users.RandomUser();

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Multithreading Results in Window Context Activation Fail
« Reply #29 on: January 16, 2014, 05:33:54 pm »
OS: Windows 7 Home Premium 64-bit
GPU: GeForce 650 with frequently updated driver
IDE: Code::Blocks

I will debug once I get home.

EDIT: m_context->setActive(active) in Window.cpp returns false (that outputs the error in console)
EDIT2: The m_context returns true, however, as that is the only way the error could be displayed
« Last Edit: January 16, 2014, 05:53:31 pm by Users.RandomUser(); »