I'm trying to create a simple D2D game engine (it must be able to display and move images in a window, at least), and everything went right until the moment when I decided to switch to the multithreaded version. I read this MSDN article, and it recommends using one multithreaded factory from several threads. But this article claims it would be more effective to have several single-threaded factories (though the article describes server-side rendering scenario, the principle is the same for my case, am I wrong?). When I tried to use one-thread-one-factory approach, all the images are displayed and moved, but there's terrible flickering. In my WM_PAINT handler I'm trying to do something like this:
for (CSingleThreadEngine *pElSingleThreadEngine : m_SingleThreadEngines) //each CSingleThreadEngine instance has its own D2D factory and an image collection
pElSingleThreadEngine->Draw();
and pElSingleThreadEngine->Draw() does drawing like this:
m_pRenderTarget->BeginDraw();
m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
for (CGameImage *pImage : m_GameImages)
{
if (FAILED(pImage->Draw()))
throw runtime_error("An object cannot be drawn");
}
m_pRenderTarget->EndDraw();
I think the wrong thing here is having several ID2D1HwndRenderTarget instances for just one window because if I make drawing each thread in a separate window, it works just fine. But I want to draw in one window only, and I can't avoid using multiple ID2D1HwndRenderTarget instances for this purpose. So my questions are:
What are the best practices for creating multithreaded Direct2D applications at all?
If the approach I'm using is right, what am I doing wrong and how can I fix it?
Any help would be highly appreciated.
I can't see a reason why you use several HWND render targets for a single window. Have you tried creating off-screen bitmaps for each thread and draw those to a single HWND render target instead?
Related
I am using PCL to display a point cloud under Windows. The CloudViewer and PCLVisualizer classes are available for this purpose. But when you instantiate them, they create their own window (via VTK). As I want to integrate the display window in a complete GUI, I am looking for a way to direct the display to an existing window, possibly by passing the window handle. The window is not OpenGL.
Is that possible ? Any hint ?
It seems that I have found a solution.
Several Web posts suggest to change the parent window of the RenderWindow of the viewer, which can be done with
viewer->getRenderWindow()->SetParentId(hWnd);
Anyway, this doesn't seem to have an effect. But it inspired me to reparent using the Windows function instead,
SetParent((HWND)viewer->getRenderWindow()->GetGenericWindowId(), hWnd);
This indeed makes the viewer a child of my window, while it continues to work correctly. Additional style flags can be adjusted with SetWindowLong.
For now I have also kept the first statement, just in case. It doesn't seem to bother.
I'm building Conway's game of life in MFC. I built it before using Allegro and want to try it out using MFC. So I start migrating the algorithms and stuff. It renders well using one core and it does the game of life algorithm rather beautiful just like in allegro. Conway's game of life is made up of little rectangle drawn using pDC->rectangle(x1, y1, x2, y2).
My problem is actually two, one of it is that everytime I call the pDC->rectangle(...) it draws it immideately, causing this chain reaction rendering style. I want it to display when it is done doing its job(i know you can do that in directx but I just want to do this using device context of MFC).
The other problem is the title, and probably my main problem. How do you using pDC with concurrency, I tried it and it did some weird stuff. I know I cant use pDC in OnDraw with concurrency because it is the same device context occupying the same memory but used in my 6 core. That's all, thanks in advance.
To avoid drawing immediately, you can draw in abitmap in memory and then blit on the real dc when you finished. Have a look here as a starting point, or at this article on CodeProject. For the multithreading part, you should be able to use the same in memory device Context from multiple threads, just ensure to properly coordinate the creattion of that sharted DC and the blitting/release.
I have a general question on how to develop an image viewer plugin with Firebreath. For that, I would like to incorporate a GUI framework, like wxwidget or Qt. The GUI would be used to to fire up some dialogs, adding a toolbar on top, or to open context menus with right clicking an image.
As far as I understand I have a hwnd handle and so I can draw onto a window. I also understand that I have various events I can react on, like mouse button clicks or keyboard strokes. But it fails me how I would add graphical menus, buttons, etc. I know I could use html around the window but that's not the route I like to take.
For instance, does it makes sense to render an user interface offline (in memory) onto an image and then keep somehow track of the state internally?
Has anyone done such thing? Or can anyone give me some insight on how to accomplish adding a user interface.
Assuming you only care about windows and assuming that you don't mind using a windowed plugin, which is the easiest (but no HTML elements can float over the plugin), it should be no different than creating a GUI in any other windows application.
You are given a window that shows up with the AttachedEvent; when DetachedEvent is fired you need to stop using the window. Many people create a child window inside that parent window and use that for all their actual real code which makes it a little easier to use one of those other abstractions, but that's basically all there is to it. I don't know specifically how you'd do it with QT or wxwidget but you'd create a child window of that HWND that you are given and have the abstraction do your thing for you.
As to whether or not it would be rendering things offscreen, etc, I have no idea; that would totally depend on the window system. There is no reason that I know of that you would need to do that, and most things just draw directly to the HWND, but there are a zillion different ways you could do it. It looks to me like what you really need is to understand how drawing in Windows actually works.
I hope that helps
When trying to implement a simple OpenGL application, I was surprised that while it is easy to find plenty of examples and documentation on advanced rendering stuff, the Win32 framework is poorly documented and even most samples and tutorials do not implement this properly even for basic cases, not mentioning advanced stuff like multiple monitors. Despite of several hours of searching I was unable to find a way which would handle Alt-Tab reliably.
How should OpenGL fullscreen application respond to Alt-Tab? Which messages should the app react to (WM_ACTIVATE, WM_ACTIVATEAPP)? What should the reaction be? (Change the display resolution, destroy / create the rendering context, or destroy / create some OpenGL resources?)
If the application uses some animation loop, suspend the loop, then just minimize the window. If it changed display resolution and gamma revert to the settings before changing them.
There's no need to destroy OpenGL context or resources; OpenGL uses an abstract resource model: If another program requires RAM of the GPU or other resources, your programs resources will be swapped out transparently.
EDIT:
Changing the window visibility status may require to reset the OpenGL viewport, so it's a good idea to either call glViewport apropriately in the display/rendering function, or at least set it in the resize handler, followed by a complete redraw.
I've seen things like this and I was wondering if this was possible, say I run my application
and it will show the render on whatever is below it.
So basically, rendering on the screen without a window.
Possible or a lie?
Note: Want to do this on windows and in c++.
It is possible to use your application to draw on other application's windows. Once you have found the window you want, you have it's HWND, you can then use it just like it was your own window for the purposes of drawing. But since that window doesn't know you have done this, it will probably mess up whatever you have drawn on it when it tries to redraw itself.
There are some very complicated ways of getting around this, some of them involve using windows "hooks" to intercept drawing messages to that window so you know when it has redrawn so that you can do your redrawing as well.
Another option is to use clipping regions on a window. This can allow you to give your window an unusual shape, and have everything behind it still look correct.
There are also ways to take over drawing of the desktop background window, and you can actually run an application that draws animations and stuff on the desktop background (while the desktop is still usable). At least, this was possible up through XP, not sure if it has changed in Vista/Win7.
Unfortunately, all of these options are too very complex to go in depth without more information on what you are trying to do.
You can use GetDesktopWindow(), to get the HWND of the desktop. But as a previous answer says (SoapBox), be careful, you may mess up the desktop because the OS expects that it owns it.
I wrote an open source project a few years ago to achieve this on the desktop background. It's called Uberdash. If you follow the window hierarchy, the desktop is just a window in a sort of "background" container. Then there is a main container and a front container. The front container is how windows become full screen or "always on top." You may be able to use Aero composition to render a window with alpha in the front container, but you will need to pass events on to the lower windows. It won't be pretty.
Also, there's a technology in some video cards called overlays/underlays. You used to be able to render directly to an overlay. Your GPU would apply it directly, with no interference to main memory. So even if you took a screen capture, your overlay/underlay would not show up in the screen cap. Unfortunately MS banned that technology in Vista...