locked
Need help on Directx 11.X and win32 RRS feed

  • Question

  • Hello everybody

    for a short time i was around learning c++ and directx 11 (since it came) . but i never had enough time and base knowledge to step trough the process of learning these ...

    Long story short , my problem is i want to start learning c++ (win32) and directx 11.2 . my aim is to learn how to develop small indie games , and beside that i have a project in mind which requires my software to have a three dimensional environment to import obj files and tweak them (like maya,3ds max, udk and so on ...) i know that sounds tough but i need to do it in future .

    my biggest problem started since Microsoft released windows 8 and started what's now we called Windows store apps. and implemented DirectX SDK within the Microsoft Windows SDK . before this we used to do our DirectX coding using Win32 and all that jazz but now everywhere i look is Windows store DirectX app which makes me dizzy . i understand that Microsoft is opening a market for touch screen devices and thats cool . but i feel like they are pushing us to make what they want . i want to program directX using win32 but it seems like they have closed all bridges (like what they have done to Directx 9.c and C#) , i tried to write a simple win32 Direct3D app using <windows.h> and <d3D11_2> headers buy all i get is linker error .

    clarify me please , Can we Develop Software with Direct3D like the old days or not ?

    if the answer is yes why im getting linker error with this piece of code ?

    //--------------------------------------------------------------------------------------
    // File: Tutorial01.cpp
    //
    // This application demonstrates creating a Direct3D 11 device
    //
    // http://msdn.microsoft.com/en-us/library/windows/apps/ff729718.aspx
    //
    // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
    // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
    // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    // PARTICULAR PURPOSE.
    //
    // Copyright (c) Microsoft Corporation. All rights reserved.
    //--------------------------------------------------------------------------------------
    #include <windows.h>
    #include <d3d11_1.h>
    #include <directxcolors.h>
    #include "resource.h"
    
    using namespace DirectX;
    
    //--------------------------------------------------------------------------------------
    // Global Variables
    //--------------------------------------------------------------------------------------
    HINSTANCE               g_hInst = nullptr;
    HWND                    g_hWnd = nullptr;
    D3D_DRIVER_TYPE         g_driverType = D3D_DRIVER_TYPE_NULL;
    D3D_FEATURE_LEVEL       g_featureLevel = D3D_FEATURE_LEVEL_11_0;
    ID3D11Device*           g_pd3dDevice = nullptr;
    ID3D11DeviceContext*    g_pImmediateContext = nullptr;
    IDXGISwapChain*         g_pSwapChain = nullptr;
    ID3D11RenderTargetView* g_pRenderTargetView = nullptr;
    
    
    //--------------------------------------------------------------------------------------
    // Forward declarations
    //--------------------------------------------------------------------------------------
    HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow );
    HRESULT InitDevice();
    void CleanupDevice();
    LRESULT CALLBACK    WndProc( HWND, UINT, WPARAM, LPARAM );
    void Render();
    
    
    //--------------------------------------------------------------------------------------
    // Entry point to the program. Initializes everything and goes into a message processing 
    // loop. Idle time is used to render the scene.
    //--------------------------------------------------------------------------------------
    int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow )
    {
        UNREFERENCED_PARAMETER( hPrevInstance );
        UNREFERENCED_PARAMETER( lpCmdLine );
    
        if( FAILED( InitWindow( hInstance, nCmdShow ) ) )
            return 0;
    
        if( FAILED( InitDevice() ) )
        {
            CleanupDevice();
            return 0;
        }
    
        // Main message loop
        MSG msg = {0};
        while( WM_QUIT != msg.message )
        {
            if( PeekMessage( &msg, nullptr, 0, 0, PM_REMOVE ) )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }
            else
            {
                Render();
            }
        }
    
        CleanupDevice();
    
        return ( int )msg.wParam;
    }
    
    
    //--------------------------------------------------------------------------------------
    // Register class and create window
    //--------------------------------------------------------------------------------------
    HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow )
    {
        // Register class
        WNDCLASSEX wcex;
        wcex.cbSize = sizeof( WNDCLASSEX );
        wcex.style = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc = WndProc;
        wcex.cbClsExtra = 0;
        wcex.cbWndExtra = 0;
        wcex.hInstance = hInstance;
        wcex.hIcon = LoadIcon( hInstance, ( LPCTSTR )IDI_TUTORIAL1 );
        wcex.hCursor = LoadCursor( nullptr, IDC_ARROW );
        wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
        wcex.lpszMenuName = nullptr;
        wcex.lpszClassName = L"TutorialWindowClass";
        wcex.hIconSm = LoadIcon( wcex.hInstance, ( LPCTSTR )IDI_TUTORIAL1 );
        if( !RegisterClassEx( &wcex ) )
            return E_FAIL;
    
        // Create window
        g_hInst = hInstance;
        RECT rc = { 0, 0, 640, 480 };
        AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
        g_hWnd = CreateWindow( L"TutorialWindowClass", L"Direct3D 11 Tutorial 1: Direct3D 11 Basics", WS_OVERLAPPEDWINDOW,
                               CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, hInstance,
                               nullptr );
        if( !g_hWnd )
            return E_FAIL;
    
        ShowWindow( g_hWnd, nCmdShow );
    
        return S_OK;
    }
    
    
    //--------------------------------------------------------------------------------------
    // Called every time the application receives a message
    //--------------------------------------------------------------------------------------
    LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
    {
        PAINTSTRUCT ps;
        HDC hdc;
    
        switch( message )
        {
            case WM_PAINT:
                hdc = BeginPaint( hWnd, &ps );
                EndPaint( hWnd, &ps );
                break;
    
            case WM_DESTROY:
                PostQuitMessage( 0 );
                break;
    
            default:
                return DefWindowProc( hWnd, message, wParam, lParam );
        }
    
        return 0;
    }
    
    
    //--------------------------------------------------------------------------------------
    // Create Direct3D device and swap chain
    //--------------------------------------------------------------------------------------
    HRESULT InitDevice()
    {
        HRESULT hr = S_OK;
    
        RECT rc;
        GetClientRect( g_hWnd, &rc );
        UINT width = rc.right - rc.left;
        UINT height = rc.bottom - rc.top;
    
        UINT createDeviceFlags = 0;
    #ifdef _DEBUG
        createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
    #endif
    
        D3D_DRIVER_TYPE driverTypes[] =
        {
            D3D_DRIVER_TYPE_HARDWARE,
            D3D_DRIVER_TYPE_WARP,
            D3D_DRIVER_TYPE_REFERENCE,
        };
        UINT numDriverTypes = ARRAYSIZE( driverTypes );
    
        D3D_FEATURE_LEVEL featureLevels[] =
        {
            D3D_FEATURE_LEVEL_11_1,
            D3D_FEATURE_LEVEL_11_0,
            D3D_FEATURE_LEVEL_10_1,
            D3D_FEATURE_LEVEL_10_0,
        };
    	UINT numFeatureLevels = ARRAYSIZE( featureLevels );
    
        DXGI_SWAP_CHAIN_DESC sd;
        ZeroMemory( &sd, sizeof( sd ) );
        sd.BufferCount = 1;
        sd.BufferDesc.Width = width;
        sd.BufferDesc.Height = height;
        sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        sd.BufferDesc.RefreshRate.Numerator = 60;
        sd.BufferDesc.RefreshRate.Denominator = 1;
        sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
        sd.OutputWindow = g_hWnd;
        sd.SampleDesc.Count = 1;
        sd.SampleDesc.Quality = 0;
        sd.Windowed = TRUE;
    
        for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ )
        {
            g_driverType = driverTypes[driverTypeIndex];
            hr = D3D11CreateDeviceAndSwapChain( nullptr, g_driverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels,
                                                D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext );
    
            if ( hr == E_INVALIDARG )
            {
                // DirectX 11.0 platforms will not recognize D3D_FEATURE_LEVEL_11_1 so we need to retry without it
                hr = D3D11CreateDeviceAndSwapChain( nullptr, g_driverType, nullptr, createDeviceFlags, &featureLevels[1], numFeatureLevels - 1,
                                                    D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext );
            }
    
            if( SUCCEEDED( hr ) )
                break;
        }
        if( FAILED( hr ) )
            return hr;
    
        // Create a render target view
        ID3D11Texture2D* pBackBuffer = nullptr;
        hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );
        if( FAILED( hr ) )
            return hr;
    
        hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, nullptr, &g_pRenderTargetView );
        pBackBuffer->Release();
        if( FAILED( hr ) )
            return hr;
    
        g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, nullptr );
    
        // Setup the viewport
        D3D11_VIEWPORT vp;
        vp.Width = (FLOAT)width;
        vp.Height = (FLOAT)height;
        vp.MinDepth = 0.0f;
        vp.MaxDepth = 1.0f;
        vp.TopLeftX = 0;
        vp.TopLeftY = 0;
        g_pImmediateContext->RSSetViewports( 1, &vp );
    
        return S_OK;
    }
    
    
    //--------------------------------------------------------------------------------------
    // Render the frame
    //--------------------------------------------------------------------------------------
    void Render()
    {
        // Just clear the backbuffer
        g_pImmediateContext->ClearRenderTargetView( g_pRenderTargetView, Colors::MidnightBlue );
        g_pSwapChain->Present( 0, 0 );
    }
    
    
    //--------------------------------------------------------------------------------------
    // Clean up the objects we've created
    //--------------------------------------------------------------------------------------
    void CleanupDevice()
    {
        if( g_pImmediateContext ) g_pImmediateContext->ClearState();
    
        if( g_pRenderTargetView ) g_pRenderTargetView->Release();
        if( g_pSwapChain ) g_pSwapChain->Release();
        if( g_pImmediateContext ) g_pImmediateContext->Release();
        if( g_pd3dDevice ) g_pd3dDevice->Release();
    }
    

    Tuesday, May 6, 2014 4:36 PM

Answers

  • I tried to write a simple win32 Direct3D app using <windows.h> and <d3D11_2> headers buy all i get is linker error"

    Well, you have to link to d3d11.lib. You can add d3d11.lib in Project Properties, Linker, Input, Additional Dependencies. Or you can put a

    #pragma comment(lib, "d3d11.lib")

    somewhere in your code.

    "Can we Develop Software with Direct3D like the old days or not ?"

    Of course you can. IMO it's even simpler because you don't have to install a separate SDK, all the necessary headers and libraries are already installed with VS now.

    Tuesday, May 6, 2014 5:17 PM

All replies

  • I tried to write a simple win32 Direct3D app using <windows.h> and <d3D11_2> headers buy all i get is linker error"

    Well, you have to link to d3d11.lib. You can add d3d11.lib in Project Properties, Linker, Input, Additional Dependencies. Or you can put a

    #pragma comment(lib, "d3d11.lib")

    somewhere in your code.

    "Can we Develop Software with Direct3D like the old days or not ?"

    Of course you can. IMO it's even simpler because you don't have to install a separate SDK, all the necessary headers and libraries are already installed with VS now.

    Tuesday, May 6, 2014 5:17 PM
  • wow,it worked thnx alot :)
    Tuesday, May 6, 2014 5:42 PM