SFML community forums
Help => Window => Topic started by: nxzdr on May 18, 2011, 09:22:48 am
-
Alright so I've been playing around with SFML for a few days now, just for a bit of fun. I've decided to try to get it working with two threads, one to handle input, audio and the like, and the other to loop through a 3d scene and perform all the rendering tasks. The trouble is, I'm not sure if I'm getting this down correctly, presently I have a black filled window, that handles input and everything fine, but nothing from the rendering thread draws to the window. I've debugged and checked to make sure everything is running as I expect it to, so I'm guessing I've misinterpreted the docs on how to accomplish this. Here's what I've got thus far (just the important parts obviously):
Main Thread
App has a sf::Window as a member
App::Init()
{
// Creates and sf::Window
sf::VideoMode videoMode(iWidth, iHeight, 32);
mWindow.Create(videoMode, "AppNameHere");
}
App::Loop()
{
while(!quit)
{
mWindow.SetActive();
// Process Events for close, resize ect here
mWindow.Display();
}
mWindow.Close();
}
Render Thread
Renderer Class has a sf::Context as a member
Renderer::Init()
{
// Set GL States and Projection Matrix
}
Renderer::Loop()
{
while(!quit)
{
mContext.SetActive();
// OpenGL Commands here
}
}
This is what I gathered from searching about thus far. Any help would be greatly appreciated =]
-
You're drawing in your sf::Context, so you're not drawing in your window ;)
You must do this instead:
App::Loop()
{
while(!quit)
{
// Process Events for close, resize ect here
}
mWindow.Close();
}
Renderer::Loop()
{
// mWindow is a reference to the window which is created in the main thread
mWindow.SetActive();
while(!quit)
{
// OpenGL Commands here
mwindow.Display();
}
}
-
So I don't need the sf::Context at all then?
-
Yup. You need a sf::Context only when you can't use another existing context (most likely a window).
And contexts only share their resources, so if you use a dummy context it can only be for stuff like loading images; if you draw in a dummy context you won't see anything.
-
So if I made a loading thread to load and bind textures, that would need a context. But my renderer is fine with using the window pointer.
Cool, thanks for your help! =D
-
Hmm no luck, I made the change, and now it keeps signalling GL_INVALID_OPERATION for everything, which is due to no GLContext.
Any reason this might happen? I have the App class pass a pointer to the sf::Window it creates to the renderer just before the thread starts. then the rendering thread sets the window to active before calling any gl functions.
Is there anything I'm leaving out?
-
You can activate a context/window in a thread only if it's not already active in other threads. So you must call mwindow.SetActive(false) in the main thread before launching the thread.
-
Success! =D Thanks!
So I presume that the window sets itself as Active upon creation (which I probably should have thought about before =p)
-
So I presume that the window sets itself as Active upon creation
Absolutely ;)