Tuesday, May 8, 2012

Windows: Getting the Device Context without painful setup

So I use OpenGL a lot. Because of this I use GLFW a lot. GLFW is like GLUT or Free GLUT or SDL or SFML except it's not lame (not saying these kits are all lame but GLUT is lame and SDL is lame lol ). GLFW is a lot more light weight and for me easier to work with.

Using GLFW to create your window and context is painless. However there is no obvious way to get your window's context from GLFW once it's made. In my case, I needed the context for an OpenGL OpenCL interop.

To get the window handle and device context you can go right after your glfwInit() and glfwOpenWindow(...) and call...


HWND *ptr_hwnd = GetForegroundWindow();
HDC ptr_dc = GetDC( *ptr_hwnd );


And that should do it! No pain, some gain. So long window setup code hello actually doing wtf you wanted to make the window for.

2 comments:

  1. Hey, just found out this post.

    I've tried using GetForegroundWindow() but I get an "undefined identifier" error.

    Any idea what's the name of the function on GLFW 2.7?

    Thanks

    ReplyDelete
    Replies
    1. Ok so after looking up some GLFW results on the issue I'm pretty sure the "GetForegroundWindow()" function is there in 2.7.

      If your project is small enough could you pastebin the start up code where you call "glfwInit()" and what not.

      Undefined identifier sounds like a library thing where the function isn't being defined (declaration or definition). If that's the case your GLFW library isn't being fond or linked for use. Does the project find/use the rest of the GLFW functions? Like, does a baseline GLFW "hello world triangle" work for you?

      Delete