Yui的狗窝
Yui的狗窝
direct3d的初始化

为了能够在visual studio2017上运行龙书上的例程,首先要在微软官网上下载direct的开发工具包。下载

在安装完开发工具包后,需要在visual studio里对项目进行一些必要的配置,具体操作步骤如下。
1.项目属性→vc++目录→包含目录,填写好开发工具包安装地址下,文件夹include的地址。
2.项目属性→vc++目录→库目录,填写工具包安装地址下,文件夹lib的路径。
3.项目属性→链接器→输入→附加依赖项,增加库文件"d3d9.lib"、"d3d9x.lib"、"winmm.lib"。

//header1.h
#pragma once
#include<d3d9.h>
#include<d3dx9.h>
IDirect3D9Ex        *pD3D9 = 0;
IDirect3DDevice9    *pDevice = 0;
D3DCAPS9             caps;
bool InitD3d9x(HWND hWnd)
{
    Direct3DCreate9Ex(D3D_SDK_VERSION,&pD3D9);
    pD3D9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
    int iFlag;
    if (caps.DevCaps&D3DDEVCAPS_HWTRANSFORMANDLIGHT)
        iFlag = D3DCREATE_HARDWARE_VERTEXPROCESSING;
    else
        iFlag = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
    D3DPRESENT_PARAMETERS d3dpp;
    d3dpp.BackBufferWidth = 800;
    d3dpp.BackBufferHeight = 600;
    d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
    d3dpp.BackBufferCount = 1;
    d3dpp.MultiSampleQuality = 0;
    d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hWnd;
    d3dpp.Windowed = true;
    d3dpp.EnableAutoDepthStencil = true;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
    d3dpp.Flags = 0;
    d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
    if (FAILED(pD3D9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING,
        &d3dpp, &pDevice)))
    {
        MessageBox(NULL, L"Initialize d3d9x failed", NULL, NULL);
        return false;
    }
    return true;
}
void Display()
{
    if (pDevice)
    {
        pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffcccc, 1.0f, 0);
        pDevice->Present(NULL, NULL, NULL, NULL);
    }
}
//source1.cpp
#include<windows.h>
#include"header1.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void InitWindow(HINSTANCE, int);
int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE prevInstance,LPWSTR cmdLine,int cmdShow)
{
    InitWindow(hInstance, cmdShow);
    MSG msg = { 0 };
    while (msg.message != WM_QUIT)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    pDevice->Release();
    return msg.wParam;
}
void InitWindow(HINSTANCE hInstance, int cmdShow)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(wc);
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wc.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = L"initD3D";
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClassEx(&wc);
    HWND hWnd = CreateWindowEx(NULL,L"initD3D", L"Init D3D", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    ShowWindow(hWnd, cmdShow);
    UpdateWindow(hWnd);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    switch (message)
    {
    case WM_CREATE:
        InitD3d9x(hWnd);
        break;
    case WM_KEYDOWN:
        if (wParam == VK_ESCAPE)
            DestroyWindow(hWnd);
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        Display();
        EndPaint(hWnd, &ps);
        break;
    case WM_SIZE:
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

成功运行后,程序生成一个窗口,并且显示区域为粉色。

没有标签
首页      读书笔记      directx龙书的学习笔记      direct3d的初始化
https://secure.gravatar.com/avatar/d0fe7f1b17d5e9122db921e3a8cc327f?s=256&d=mm&r=g

Suzumiya, Yui

文章作者

发表评论

textsms
account_circle
email

Yui的狗窝

direct3d的初始化
为了能够在visual studio2017上运行龙书上的例程,首先要在微软官网上下载direct的开发工具包。下载 在安装完开发工具包后,需要在visual studio里对项目进行一些必要的配置,具体操作步…
扫描二维码继续阅读
2018-04-07


没有激活的小工具