zoukankan      html  css  js  c++  java
  • DirectX 初始化DirectX(手写和红龙书里面的方式)

    上次介绍了如何初始化Direct3D,这次手写一次初始化代码,都是一样的方式不过看起来整洁一点。 创建一个Win32空项目添加一个空类增加以下代码即可.

    #include "CreateDevice.h"
    #include <d3d9.h>
    
    #pragma region 初始化函数
    CreateDevice::CreateDevice()
    {
    }
    
    
    CreateDevice::~CreateDevice()
    {
    }
    
    
    #pragma endregion
    
    LPDIRECT3D9 g_pD3D;
    LPDIRECT3DDEVICE9    g_pd3dDevice;
    
    HRESULT InitD3D(HWND hWnd)
    {
        if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
            return E_FAIL;
    
        D3DPRESENT_PARAMETERS d3dpp;
        ZeroMemory(&d3dpp, sizeof(d3dpp));
        d3dpp.Windowed = TRUE;//不是全屏
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    
        if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
            D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
            &d3dpp, &g_pd3dDevice)))
        {
            return E_FAIL;
        }
        return S_OK;
    }
    
    //渲染s
    VOID Render()
    {
        if (NULL == g_pd3dDevice)
            return;
    
        g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 0), 1.0f, 0);
    
        if (SUCCEEDED(g_pd3dDevice->BeginScene()))
        {
            //渲染
    
            g_pd3dDevice->EndScene();
        }
        g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
    }
    
    //释放
    VOID Cleanup()
    {
        if (g_pd3dDevice != NULL)
        {
            g_pd3dDevice->Release();
        }
    
        if (g_pD3D != NULL)
        {
            g_pD3D->Release();
        }
    }
    
    LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch (msg)
        {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage(0);
            return 0;
        case WM_PAINT:
            Render();
            ValidateRect(hWnd, NULL);
            return 0;
        }
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    
    INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
    {
        //1.注册窗口类
        WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC,
            MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL,
            NULL, NULL, "D3D Tutorial", NULL };
        RegisterClassEx(&wc);
    
        //2.用注册的窗口类创建窗口
        HWND hWnd = CreateWindow("D3D Tutorial", "D3D Tutorial",
            WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, wc.hInstance, NULL);
    
        //3. 初始化D3D
        if (SUCCEEDED(InitD3D(hWnd)))
        {
            //显示和刷新窗口
            ShowWindow(hWnd, SW_SHOWDEFAULT);
    
            UpdateWindow(hWnd);
    
            MSG msg;
    
            //消息循环
            while (GetMessage(&msg, NULL, 0, 0))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
    
            UnregisterClass("D3D Tutorial", wc.hInstance);
            return 0;
        }
    
    }

    项目下载地址: http://yunpan.cn/cHnpK9vFhQ2p9  访问密码 a518

    如果你感兴趣,你可以把你妹妹介绍给我
  • 相关阅读:
    cocos2d 中判断CGPoint或者CGSize是否相等
    object-c cocos2d-x 写程序时注意调试的技巧
    object-c [self class] 和 [self _cmd]
    object-c 协议和委托
    ios cocos2d TexturePacker生成文件后的使用方法
    object-c 要理解协议的几个重要概念
    Linux Mint 没有 language support 语言支持解决方案
    cocos2d ARCH_OPTIMAL_PARTICLE_SYSTEM这个未定义的问题
    CCSpriteBatchNode的优化性能
    Codeforces Round #190 DIV.2 A. Ciel and Dancing
  • 原文地址:https://www.cnblogs.com/plateFace/p/4854060.html
Copyright © 2011-2022 走看看