Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about DeviceContext and Windows WM_PAINT #112

Open
toptensoftware opened this issue Aug 13, 2018 · 2 comments
Open

Question about DeviceContext and Windows WM_PAINT #112

toptensoftware opened this issue Aug 13, 2018 · 2 comments

Comments

@toptensoftware
Copy link

Hi,

This is more a question more than an issue, but wasn't sure where else to post it. I'm looking to integrate OpenGL.NET into my custom Windows UI library which draws using traditional Windows WM_PAINT messages rather than running a regularly updating game/animation loop.

I noticed that DeviceContext creates it's own internal hDC with Windows' GetDC(hWnd) function and releases it when the DeviceContext is disposed. How does one get it to paint to the hDC returned from BeginPaint()?

Currently I'm handling it something like this:

case Win32.WM_PAINT:
    // Render using OpenGL.NET's DeviceContext (_dc)
    _dc.MakeCurrent(_ctx);
    OnRender();
    _dc.SwapBuffers();
    _dc.MakeCurrent(IntPtr.Zero);

    // Validate the window
    Win32.ValidateRect(_handle, IntPtr.Zero);
    break;

ie: No BeginPaint/EndPaint calls.

This seems to work, but I'm wondering if this is the correct way to handle it or is there a better way to do it?

Any advice appreciated.
Brad

@luca-piccioni
Copy link
Owner

luca-piccioni commented Aug 13, 2018

I don't see any major issue in the code you posted; I would avoid repeated _dc.MakeCurrent(_ctx); and _dc.MakeCurrent(IntPtr.Zero); since they would reload all GL context function pointers at each WM_PAINT message (and this is an expensive task). Ideally, you set a current GL context at window creation, and keep it current as long the window exists. (Hint: see here)

BeginPaint returns an HDC that should be used within the WM_PAINT handler. From what I understand, it is not guaranteed that the returned HDC is always the same, and I think that creating a GL context on it is not a good model to implement (if the HDC is destroyed, even the GL context would be invalid).

For what it worths, this is a partial Win32 window implementation integrated with OpenGL.Net.

@toptensoftware
Copy link
Author

Hi Luca,

Thanks for the reply. Unfortunately I can't keep the context current for the entire lifetime of the window. My app loads third party VST plugins - some of which also use OpenGL for rendering so my app and the loaded plugins all need to keep their contexts separate. Is there some way to keep the context and it's associated function pointers cached and just re-activate them with the context?

Brad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants