本文索引:

  • 一 关于龙书
  • 二 在vs2015u2环境下编写DirectX11程序
  • 三 关于MSDN帮助文档
  • 四 官方示例项目
  • 五 自己尝试编写一个小项目
    • 1 创建一个win32项目
    • 2 配置依赖库
    • 3 添加源文件
    • 4 完成
  • 六 总结

一、 关于龙书

相信很多看想要学习DirectX编程的小伙伴都有听说过龙书,也就是官方推出的《Introduction to 3D Game Programming with Directx 9》这本书,从DirectX9开始,龙书作为最权威、详尽的DirectX编程学习资料早已广受好评,到现在Directx编程接口已经出到DirectX12了,龙书也即将推出《Introduction to 3D Game Programming with Directx 12》,但本小菜发现在之前阅读《Introduction to 3D Game Programming with Directx 11》的时候按照书上的说明已经无法找到Directx的编程SDK包。在查找资料和摸索后发现现在的Directx配置早已没有那么麻烦,于是总结一下,把经验分享出来给大家参考。

二、 在vs2015u2环境下编写DirectX11程序

从win8发售开发,DirectX编程SDK包就已经集成在windows SDK包中,所以想要使用DirectX接口进行2D和3D编程的小伙伴发现没有地方可以下载像DirectX9那样的SDK包了,那么是不是一定要下载windows SDK包呢?微软在这方面也为推广自己的IDE编程工具VISUAL提供了便利。对的,只要使用的是visual studio2015以上的编程环境,都已经预先集成了windows SDK包,区别只是不同版本的IDE和操作系统能获取到的示例工程不太一样,一般更高的版本中能够获得更全面的示例工程。
以下为官方声明:
Starting with Windows 8, the DirectX SDK is included as part of the Windows SDK.
Use a free, Visual Studio Community 2015 with Update 2 client that already includes the latest Windows 10 developer tools to get started creating innovative and compelling Universal Windows apps and Classic Windows applications. These tools include universal app templates, a code editor, a powerful debugger, Windows Mobile emulators, rich language support, and more, all ready to use in production. Includes the latest Windows Standalone SDK and mobile emulator releases.
这里官方提到的操作系统是win10,但使用win8进行编程的童鞋也可以继续使用visual studio2015,和win10下编程是一样的(但最新的direct12是在win10中才有的)。
所以使用vs2015做开发的童鞋已经不用再配置任何东西了。

三、 关于MSDN帮助文档

在VS2015中点击菜单栏“Help”->“Add and Remove Help Content”

打开“Microsoft Help View 2.0 - Visual Studio 2012文档”

单击“Recommended Documentation”=>“Windows Desktop Application Development”后面的”Add”链接,VS就会从服务器上将对应的帮助文档下载到本地。
更新完成后,可以从以下路径找到DirectX的说明文档:
“Desktop app technologies - Windows app development”=>”Graphics and Gaming - Windows app development”=>”DirectX Graphics and Gaming - Windows app development”

四、 官方示例项目

在VS2015菜单栏中依次点击”Tools”=>”Extensions and Updates”
在左侧点击”Online”=>”Samples Gallery”,左边将会展示出对应的线上示例项目列表,也可以直接在右上方的搜索框中搜索所想要的具体项目名。
找到所需要的示例项目后就可以直接点击项目后面的”Download”按钮下载项目到本地。

例如:


这个例子包含了一些最基本的接口使用,可以作为入门教程参考。

五、 自己尝试编写一个小项目

(1) 创建一个win32项目

      在VS2015中依次点击菜单栏"File"=>"New"=>"Project"

      设置好项目名称和路径后点击确定,继续填写后续设置,完成创建。

(2) 配置依赖库

      右键项目名,单击弹出菜单中的"Properties"

      在左侧点击"Configuration Properties"=>"Linker"=>"Input",点击右边"Addtional Dependencis"下的Edit

      添加以下依赖库名称:d3d11.libd3dcompiler.libdxguid.libwinmm.libcomctl32.lib

(3) 添加源文件

      创建头文件"Release.h"和源文件"Tutorial01.cpp",源码如下:

Release.h

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Tutorial01.rc
//#define IDS_APP_TITLE           103#define IDR_MAINFRAME           128
#define IDD_TUTORIAL1_DIALOG    102
#define IDD_ABOUTBOX            103
#define IDM_ABOUT               104
#define IDM_EXIT                105
#define IDI_TUTORIAL1           107
#define IDI_SMALL               108
#define IDC_TUTORIAL1           109
#define IDC_MYICON              2
#define IDC_STATIC              -1
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS#define _APS_NO_MFC                 130
#define _APS_NEXT_RESOURCE_VALUE    129
#define _APS_NEXT_COMMAND_VALUE     32771
#define _APS_NEXT_CONTROL_VALUE     1000
#define _APS_NEXT_SYMED_VALUE       110
#endif
#endif

Tutorial01.cpp

//--------------------------------------------------------------------------------------
// 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;
ID3D11Device1*          g_pd3dDevice1 = nullptr;
ID3D11DeviceContext*    g_pImmediateContext = nullptr;
ID3D11DeviceContext1*   g_pImmediateContext1 = nullptr;
IDXGISwapChain*         g_pSwapChain = nullptr;
IDXGISwapChain1*        g_pSwapChain1 = 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 loopMSG 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 classWNDCLASSEX 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 windowg_hInst = hInstance;RECT rc = { 0, 0, 800, 600 };AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );g_hWnd = CreateWindow( L"TutorialWindowClass", L"Direct3D 11 Tutorial 1: Direct3D 11 Basics",WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,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;// Note that this tutorial does not handle resizing (WM_SIZE) requests,// so we created the window without the resize border.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 _DEBUGcreateDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endifD3D_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 );for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ ){g_driverType = driverTypes[driverTypeIndex];hr = D3D11CreateDevice( nullptr, g_driverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels,D3D11_SDK_VERSION, &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 ithr = D3D11CreateDevice( nullptr, g_driverType, nullptr, createDeviceFlags, &featureLevels[1], numFeatureLevels - 1,D3D11_SDK_VERSION, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext );}if( SUCCEEDED( hr ) )break;}if( FAILED( hr ) )return hr;// Obtain DXGI factory from device (since we used nullptr for pAdapter above)IDXGIFactory1* dxgiFactory = nullptr;{IDXGIDevice* dxgiDevice = nullptr;hr = g_pd3dDevice->QueryInterface( __uuidof(IDXGIDevice), reinterpret_cast<void**>(&dxgiDevice) );if (SUCCEEDED(hr)){IDXGIAdapter* adapter = nullptr;hr = dxgiDevice->GetAdapter(&adapter);if (SUCCEEDED(hr)){hr = adapter->GetParent( __uuidof(IDXGIFactory1), reinterpret_cast<void**>(&dxgiFactory) );adapter->Release();}dxgiDevice->Release();}}if (FAILED(hr))return hr;// Create swap chainIDXGIFactory2* dxgiFactory2 = nullptr;hr = dxgiFactory->QueryInterface( __uuidof(IDXGIFactory2), reinterpret_cast<void**>(&dxgiFactory2) );if ( dxgiFactory2 ){// DirectX 11.1 or laterhr = g_pd3dDevice->QueryInterface( __uuidof(ID3D11Device1), reinterpret_cast<void**>(&g_pd3dDevice1) );if (SUCCEEDED(hr)){(void) g_pImmediateContext->QueryInterface( __uuidof(ID3D11DeviceContext1), reinterpret_cast<void**>(&g_pImmediateContext1) );}DXGI_SWAP_CHAIN_DESC1 sd;ZeroMemory(&sd, sizeof(sd));sd.Width = width;sd.Height = height;sd.Format = DXGI_FORMAT_R8G8B8A8_UNORM;sd.SampleDesc.Count = 1;sd.SampleDesc.Quality = 0;sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;sd.BufferCount = 1;hr = dxgiFactory2->CreateSwapChainForHwnd( g_pd3dDevice, g_hWnd, &sd, nullptr, nullptr, &g_pSwapChain1 );if (SUCCEEDED(hr)){hr = g_pSwapChain1->QueryInterface( __uuidof(IDXGISwapChain), reinterpret_cast<void**>(&g_pSwapChain) );}dxgiFactory2->Release();}else{// DirectX 11.0 systemsDXGI_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;hr = dxgiFactory->CreateSwapChain( g_pd3dDevice, &sd, &g_pSwapChain );}// Note this tutorial doesn't handle full-screen swapchains so we block the ALT+ENTER shortcutdxgiFactory->MakeWindowAssociation( g_hWnd, DXGI_MWA_NO_ALT_ENTER );dxgiFactory->Release();if (FAILED(hr))return hr;// Create a render target viewID3D11Texture2D* pBackBuffer = nullptr;hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), reinterpret_cast<void**>( &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 viewportD3D11_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 backbufferg_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_pSwapChain1 ) g_pSwapChain1->Release();if( g_pSwapChain ) g_pSwapChain->Release();if( g_pImmediateContext1 ) g_pImmediateContext1->Release();if( g_pImmediateContext ) g_pImmediateContext->Release();if( g_pd3dDevice1 ) g_pd3dDevice1->Release();if( g_pd3dDevice ) g_pd3dDevice->Release();
}

(4) 完成

      按F5键即可编译并运行项目,最终效果如下:

六、 总结

  • 目前在VS2015 IDE中编写DirectX11程序已经不用安装SDK包,只要简单的将所以来LIB库配置到项目中即可。

  • (更新于2016.05.05)龙书11中的示例项目尝试过目前是不能运行的,因为在win8系统发布以后微软不仅将directx的SDK包集合到win SDK中,将directx11直接集成到操作系统中,同时,也对DIRECT编程所依赖的一些库文件做了大量修改,而龙书11中的示例项目大部分是基于之前独立的DX11SDK_Jun10.exe和win7这个环境的,因此想要运行这些示例项目需要根据情况将所依赖接口更新到目前版本。
    关于DirectX里的一些组件说明,请参考这篇官方说明。

一、VS2015update2环境下DirectX11编程说明(2016.5.5更新)相关推荐

  1. Windows环境下Unicode编程总结和将ANSI转换到Unicode 将Unicode转换到ANSI

    Windows环境下Unicode编程总结 UNICODE环境设置 在安装Visual Studio时,在选择VC++时需要加入unicode选项,保证相关的库文件可以拷贝到system32下. UN ...

  2. c linux下并行编程指南,Linux环境下C编程指南(第2版)

    摘要: <Linux环境下C编程指南(第2版)>系统地介绍在Linux平台下用C语言进行程序开发的过程,通过列举大量的程序实例,使读者能够很快掌握在Linux平台下进行C程序开发的方法和技 ...

  3. windows环境下unicode编程总结

    windows环境下unicode编程总结 UNICODE环境设置 在安装Visual Studio时,在选择VC++时需要加入unicode选项,保证相关的库文件可以拷贝到system32下. UN ...

  4. linux环境下python编程指南,在Linux系统中搭建Python编程环境

    Linux系统是为编程而设计的,因此在大多数Linux计算机中都默认安装了Python. 1. 检查Python版本 在系统中运行应用程序Terminal(如果是Ubuntu,可按Ctrl+Alt+T ...

  5. S7-200 smart xp(sp3) 环境下的编程软件

    对于使用老版本xp系统的用户来说这是xp上能够运行的调试程序最高版本的smart 调试程序了. 下载地址 连接  https://download.csdn.net/download/anlog/11 ...

  6. AD环境下,KB3139398、KB4054517、SP1更新失败的解决办法

    Windows7 KB3139398 Windows7升级SP1 KB976932 Windows10 KB4054517 AD环境下,上面两个更新失败,经过多次尝试发现,原来是因为GPO中对USB端 ...

  7. 【自学编程】来看一篇中专生如何在异常恶劣环境下自学编程

    今年我28了,工作六年,刚好思考人生的时候看到了这个问题,本来我这个年纪也许不该来回答(程序员年龄增大后的职业出路是什么)这种需要一定职场经历后的问题,在真正大龄码农面前我还只是菜鸟一个,不论是技术或 ...

  8. 一文搞定Linux环境下线程编程,这些东西老师也不会教你。(史上最详细的线程攻略)

    一.线程的概念 1.1 什么是线程 线程是一个实体,是CPU调度和分派的基本单元,有时我们也可以称它为轻量级的进程.每个线程都包含有表示执行环境所必须的信息,其中包括标识线程 的线程ID.一组寄存器值 ...

  9. linux c控制进程并发量,浅谈Linux环境下并发编程中C语言fork()函数的使用

    由fork创建的新进程被称为子进程(child process).fork函数被调用一次,但返回两次.子进程的返回值是0,而父进程的返回值则是新进程的进程ID.将子进程ID返回给父进程的理由是:因为一 ...

最新文章

  1. Install pysnmp for django
  2. 【Java核心面试宝典(2),记一次美团Java研发岗的面试经历
  3. 常见的复杂网络模型都有哪些?
  4. 什么是“软件架构设计”(推荐)
  5. nginx 上传文件 405_今天教大家Nginx+tomcat负载均衡群集如何配置
  6. java11下载_JDK11 JAVA11下载安装与快速配置环境变量教程
  7. 网络爬虫终篇:向10万级网易云用户发送定向消息
  8. 大数据之-Hadoop3.x_生产调优手册_HDFS读写压测---大数据之hadoop3.x工作笔记0162
  9. iOS直播集成和问题总结(阿里云直播)
  10. disable menu item
  11. CI/CD:DevOps背后的推动力
  12. 深度学习2.0-26.Regularization减轻overfitting
  13. 百度API从经纬度坐标到地址的转换服务
  14. windows2008 网络相关
  15. 恢复系统管理员密码的五大奇招
  16. 网络编程(网络通信协议、TCP和UDP协议、TCP和UDP通信)
  17. 程序猿的创业故事:一个游走于计算机编程、高中数学、高中物理、爱好木工的全栈工程师,转行做高中教学的亲生经历!
  18. html表格可视化设计器,基于vue-element-ui的一款表格设计器table-making
  19. 怎么选?毕竟可以上网的浏览器只剩下四款了。。。
  20. 尚医通项目150-170:预约挂号、微信支付功能

热门文章

  1. 解决win10系统在不同分辨率显示屏下,第二屏幕输入法字体太小且应用程序界面模糊的问题
  2. 用elasticsearch-certutil生成证书
  3. 盛会开幕,百度超级链上线大师数字藏品为赛事助力
  4. 2023最新精美PHP导航网源码
  5. [ACTF2020 新生赛]Include1(buu一周目速通 1/2)
  6. Java持久层框架之mybatis使用
  7. 结构光3D之DLP4500的使用
  8. 大数据可视化课程笔记 7
  9. ASP.NET MVC+BUI实现表格的操作
  10. (L2-016)愿天下有情人都是失散多年的兄妹(dfs)