Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Zero_Breaker   Members   -  Reputation: 367

Like
0Likes
Like

Posted 15 January 2014 - 01:39 PM

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.

 



Sponsor:

#2 MJP   Moderators   -  Reputation: 11450

Like
5Likes
Like

Posted 15 January 2014 - 01:54 PM

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.



#3 mark ds   Members   -  Reputation: 1290

Like
1Likes
Like

Posted 15 January 2014 - 02:32 PM

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.



#4 Zero_Breaker   Members   -  Reputation: 367

Like
-1Likes
Like

Posted 15 January 2014 - 02:42 PM

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.



#5 Zero_Breaker   Members   -  Reputation: 367

Like
0Likes
Like

Posted 15 January 2014 - 02:53 PM

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.


Edited by Zero_Breaker, 15 January 2014 - 03:05 PM.


#6 MJP   Moderators   -  Reputation: 11450

Like
3Likes
Like

Posted 15 January 2014 - 04:09 PM

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.



#7 Zero_Breaker   Members   -  Reputation: 367

Like
0Likes
Like

Posted 15 January 2014 - 04:18 PM

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



#8 SeanMiddleditch   Members   -  Reputation: 6104

Like
4Likes
Like

Posted 15 January 2014 - 06:28 PM

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.



#9 Zero_Breaker   Members   -  Reputation: 367

Like
0Likes
Like

Posted 15 January 2014 - 06:45 PM

 

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.



#10 SeanMiddleditch   Members   -  Reputation: 6104

Like
3Likes
Like

Posted 16 January 2014 - 12:11 AM

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.

#11 Zero_Breaker   Members   -  Reputation: 367

Like
0Likes
Like

Posted 16 January 2014 - 06:52 AM

I see, thanks for the help it helped me understand better.



#12 SeanMiddleditch   Members   -  Reputation: 6104

Like
2Likes
Like

Posted 17 January 2014 - 02:50 AM

As followup, since I started doing this to my own personal project in preparation for emscripten support, getting the native handle is quite easy as you just need to use SDL_GetWindowWMInfo():

// extra header needed for SDL_GetWindowWMInfo and SDL_SysWMInfo
#include <SDL_syswm.h> 

int main() {
  // initialize SDL and create window
  SDL_Init(0);
  SDL_Window* window = SDL_CreateWindow("", 0, 0, 0, 0, 0);

  // retrieve native handle info
  SDL_SysWMInfo info;
  SDL_VERSION(&info.version); // required before calling GetWindowWMInfo!
  SDL_GetWindowWMInfo(window, &info);

  // this bit is Win32-specific, but then so is D3D;
  // other OSes have different sub-structs here
  HWND handle = info.info.win.window;

  do_stuff_with_handle(handle);
}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS