Do i have to use Win32 API if i want to use DirectX11?

Started by
10 comments, last by SeanMiddleditch 10 years, 3 months ago

I have started learning DirectX11 and i just wanted to ask something. Do i have to use the Win32 API to use DX11 to create games or can i use a alternative such as Qt and Git? Or do i have to deal with the Win32 API?

I know how the Win32 API works but i honestly do not like using at all, its a downright mess when i have to program a simple window with all of the unclean and capitalized syntax.

This is also why i wanted to work with Open GL first, because it had good Window Context libraries like GLFW and SFML which are plenty time's better than the Win32 API.

I mean this the Win32 API code for a simple window:



#include <windows.h>
#include <windowsx.h>

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam);

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    // the handle for the window, filled by a function
    HWND hWnd;
    // this struct holds information for the window class
    WNDCLASSEX wc;

    // clear out the window class for use
    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    // fill in the struct with the needed information
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = L"WindowClass1";

    // register the window class
    RegisterClassEx(&wc);

    // create the window and use the result as the handle
    hWnd = CreateWindowEx(NULL,
                          L"WindowClass1",    // name of the window class
                          L"Our First Windowed Program",   // title of the window
                          WS_OVERLAPPEDWINDOW,    // window style
                          300,    // x-position of the window
                          300,    // y-position of the window
                          500,    // width of the window
                          400,    // height of the window
                          NULL,    // we have no parent window, NULL
                          NULL,    // we aren't using menus, NULL
                          hInstance,    // application handle
                          NULL);    // used with multiple windows, NULL

    // display the window on the screen
    ShowWindow(hWnd, nCmdShow);

    // enter the main loop:

    // this struct holds Windows event messages
    MSG msg;

    // wait for the next message in the queue, store the result in 'msg'
    while(GetMessage(&msg, NULL, 0, 0))
    {
        // translate keystroke messages into the right format
        TranslateMessage(&msg);

        // send the message to the WindowProc function
        DispatchMessage(&msg);
    }

    // return this part of the WM_QUIT message to Windows
    return msg.wParam;
}

// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    // sort through and find what code to run for the message given
    switch(message)
    {
        // this message is read when the window is closed
        case WM_DESTROY:
            {
                // close the application entirely
                PostQuitMessage(0);
                return 0;
            } break;
    }

    // Handle any messages the switch statement didn't
    return DefWindowProc (hWnd, message, wParam, lParam);
}

This is GFLW's code for a simple window:


int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

The GLFW code is much better and quicker to get a window and context up and running rather than having to deal with all the stuff in the Win32 API.

The DirectX code itself looks much more readable and better than the Win32 Api.

Any help would be greatly aprreciated.

Advertisement

You can use D3D with any Window library that lets you access the Win32 HWND handle for the window. At work we do use D3D with Qt, and back when I was first starting out I used to use SDL to make the window and then use D3D for rendering.

If you're using windows 8.x you can also use WinRT. From what little I've looked into it, it seems pretty clean and straightforward.

Thanks a lot guys and thank goodness that i do not have to use Win32 API, it is honestly the most horrid API to use.

WinRT sounds interesting but i won't move to Windows 8 unless necesary.

Also would using SDL for windowing with DirectX make good combo for a 3D Game?

Sorry for the double post. I thought i was in the the editing section of my last post.

At the time (6 or 7 years ago) it worked fine. I used SDL to setup a window, get keyboard/mouse/gamepad input, and play audio. They just released SDL2 which has a lot of changes, so I can't say that I've personally tried it. However they explictly mention OpenGL and D3D support, so you should be fine with the new version.

Thanks MJP. I just hope they have DX10+ Support.

Thanks MJP. I just hope they have DX10+ Support.

Thankfully it doesn't need any explicit support for it. SDL just has to create a window and provide the HWND for DXGI to consume. Everything else is up to you. SDL includes built-in integration with OpenGL but mostly because OpenGL _requires_ that integration because the GL API has no cross-platform way of creating contexts or binding them to windows and SDL has to paper over all the different platform-specific APIs (WGL, AGL, GLX, EGL, etc.). D3D11 is bundled with DXGI and has no need for SDL to do anything special to work. DXGI just takes the HWND and uses that to create a proper SwapChain for the window, either from an existing Device or via D3D11CreateDeviceAndSwapChain.

Sean Middleditch – Game Systems Engineer – Join my team!

Thanks MJP. I just hope they have DX10+ Support.

Thankfully it doesn't need any explicit support for it. SDL just has to create a window and provide the HWND for DXGI to consume. Everything else is up to you. SDL includes built-in integration with OpenGL but mostly because OpenGL _requires_ that integration because the GL API has no cross-platform way of creating contexts or binding them to windows and SDL has to paper over all the different platform-specific APIs (WGL, AGL, GLX, EGL, etc.). D3D11 is bundled with DXGI and has no need for SDL to do anything special to work. DXGI just takes the HWND and uses that to create a proper SwapChain for the window, either from an existing Device or via D3D11CreateDeviceAndSwapChain.

I see, so in other words SDL has nothing to do with DirectX other than providing a window and HWND and DXGI handles other DirectX related stuff if i am correct?

Thanks for the great info.

I see, so in other words SDL has nothing to do with DirectX other than providing a window and HWND and DXGI handles other DirectX related stuff if i am correct?


Close. SDL just needs to provide the HWND.

You are responsible for everything DXGI. There's not much to it though in the common case; just pass the HWND in to your call to D3D11CreateDeviceAndSwapChain and you're golden (it does all of the DXGI calls internally). You only really have to mess with the rest of DXGI if you're trying to enumerate devices (handy for NSight support), create your Device and SwapChain in separate calls, etc.

Sean Middleditch – Game Systems Engineer – Join my team!

This topic is closed to new replies.

Advertisement