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

Author Topic: SFML draws nothing from another thread. and can SFML coexist with GDI?  (Read 1833 times)

0 Members and 1 Guest are viewing this topic.

landings

  • Newbie
  • *
  • Posts: 2
    • View Profile
I'm writing a SFML test application with MFC. The following code runs inside an infinite loop in another thread started by AfxBeginThread() function. (Because I want to use SFML like it's in a pure WinAPI application :-\)

// SFML Window and View were initialized using CView's HWND
// Use SFML to draw
sf::Color back(255, 255, 0); // a yellow background
_psfwnd->clear(back);
_psfwnd->display();

// Then use MFC GDI functions to draw
CWnd* pwnd;
pwnd = CWnd::FromHandle(_psfwnd->getSystemHandle()); // It seems that the HWND is valid
CPaintDC dc(pwnd);
CRect r(10, 10, 100, 100);
CBrush b(0xFF000000);
dc.FillRect(&r, &b);    // draw a black rectangle

The result is a white background with a black rectangle. It seems that SFML draws nothing but MFC does.

Same code above. Now I put it into a MFC command handler (so the above code draws one frame when I click a button once).

This time the window background is yellow and MFC black rectangle is gone.

Now I am quite confused. I have two questions:

Why SFML doesn't draw the first time? It's because of threading, or initialization?

Why MFC doesn't draw when SFML does?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Have you read the tutorial/doc about threading for both SFML and MFC? What does it say?

BTW http://stackoverflow.com/questions/24246157/sfml-draws-nothing-from-another-thread-and-can-sfml-coexist-with-gdi maybe it's only me but I find it rude to post the same question in different place without saying it. People lose time to give answers when you already got one somewhere else...
SFML / OS X developer

landings

  • Newbie
  • *
  • Posts: 2
    • View Profile
Sorry my friend, it came cross my mind that here is a more specialized forum after my post on stack overflow. I checked both sites and didn't mean to waste anyone's time.
Actually I am not skilled in threading. This code is just a beginner's inspiration. I thought it would work, but after all I have a lot to learn. Thank you very much.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Are threads really necessary (e.g. to keep the MFC UI responsive)? If not, then avoid them -- multithreading is a very complex topic with loads of pitfalls. You can check some of the recent posts about it, lately a lot of beginners have unterestimated the topic.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: