博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Programming 2D Games 读书笔记(第三章)
阅读量:5327 次
发布时间:2019-06-14

本文共 5283 字,大约阅读时间需要 17 分钟。

 

示例一:DirectX Window

Graphics类用于初始化Direct 3D

主流程:

仅需要粗体部分

try{        // Create Graphics object        graphics = new Graphics;        // Initialize Graphics, throws GameError        graphics->initialize(hwnd, GAME_WIDTH, GAME_HEIGHT, FULLSCREEN);        // main message loop        int done = 0;        while (!done)        {            // PeekMessage,non-blocking method for checking for Windows messages.            if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))             {                // look for quit message                if (msg.message == WM_QUIT)                    done = 1;                // decode and pass messages on to WinProc                TranslateMessage(&msg);                DispatchMessage(&msg);            } else                graphics->showBackbuffer();        }        SAFE_DELETE(graphics);  // free memory before exit        return msg.wParam;    }    catch(const GameError &err)    {        MessageBox(NULL, err.getMessage(), "Error", MB_OK);    }
  1. Direct3DCreate9创建IDirect3D9
  2. GetDeviceCaps测试是否支持硬件顶点支持
  3. initD3Dpp初始化D3DPRESENT_PARAMETERS参数
    //=============================================================================// Initialize D3D presentation parameters//=============================================================================void Graphics::initD3Dpp(){    try{        ZeroMemory(&d3dpp, sizeof(d3dpp));              // fill the structure with 0        // fill in the parameters we need        d3dpp.BackBufferWidth   = width;        d3dpp.BackBufferHeight  = height;        if(fullscreen)                                  // if fullscreen            d3dpp.BackBufferFormat  = D3DFMT_X8R8G8B8;  // 24 bit color        else            d3dpp.BackBufferFormat  = D3DFMT_UNKNOWN;   // use desktop setting        d3dpp.BackBufferCount   = 1;        d3dpp.SwapEffect        = D3DSWAPEFFECT_DISCARD;        d3dpp.hDeviceWindow     = hwnd;        d3dpp.Windowed          = (!fullscreen);        d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;    } catch(...)    {        throw(GameError(gameErrorNS::FATAL_ERROR,                 "Error initializing D3D presentation parameters"));    }}
  4. CreateDevice
  5. showBackbuffer
    //=============================================================================// Display the backbuffer//=============================================================================HRESULT Graphics::showBackbuffer(){    result = E_FAIL;    // default to fail, replace on success    // (this function will be moved in later versions)    // Clear the backbuffer to lime green     device3d->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,0), 0.0f, 0);    // Display backbuffer to screen    result = device3d->Present(NULL, NULL, NULL, NULL);    return result;}

 

//=============================================================================// Initialize DirectX graphics// throws GameError on error//=============================================================================void Graphics::initialize(HWND hw, int w, int h, bool full){    hwnd = hw;    width = w;    height = h;    fullscreen = full;    //initialize Direct3D    direct3d = Direct3DCreate9(D3D_SDK_VERSION);    if (direct3d == NULL)        throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing Direct3D"));    initD3Dpp();        // init D3D presentation parameters    // determine if graphics card supports harware texturing and lighting and vertex shaders    D3DCAPS9 caps;    DWORD behavior;    result = direct3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);    // If device doesn't support HW T&L or doesn't support 1.1 vertex     // shaders in hardware, then switch to software vertex processing.    if( (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||            caps.VertexShaderVersion < D3DVS_VERSION(1,1) )        behavior = D3DCREATE_SOFTWARE_VERTEXPROCESSING;  // use software only processing    else        behavior = D3DCREATE_HARDWARE_VERTEXPROCESSING;  // use hardware only processing    //create Direct3D device    result = direct3d->CreateDevice(        D3DADAPTER_DEFAULT,        D3DDEVTYPE_HAL,        hwnd,        behavior,        &d3dpp,         &device3d);    if (FAILED(result))        throw(GameError(gameErrorNS::FATAL_ERROR, "Error creating Direct3D device")); }

示例二:DirectX Full Screen

见initD3Dpp方法的Windowed属性

参考:

示例三:DirectX Device Capabilities

获取指定显卡支持的显示模式

全屏时检测

  1. GetAdapterModeCount:Returns the number of display modes available on this adapter.
  2. EnumAdapterModes:

Queries the device to determine whether the specified adapter supports the requested format and display mode. This method could be used in a loop to enumerate all the available adapter modes.

bool Graphics::isAdapterCompatible(){    UINT modes = direct3d->GetAdapterModeCount(D3DADAPTER_DEFAULT,                                             d3dpp.BackBufferFormat);    for(UINT i=0; i
EnumAdapterModes( D3DADAPTER_DEFAULT, d3dpp.BackBufferFormat, i, &pMode); if( pMode.Height == d3dpp.BackBufferHeight && pMode.Width == d3dpp.BackBufferWidth && pMode.RefreshRate >= d3dpp.FullScreen_RefreshRateInHz) return true; } return false;}

转载于:https://www.cnblogs.com/Clingingboy/p/3331313.html

你可能感兴趣的文章
JAVA共通関数--シングルクォーテーションをSQL用に追加する
查看>>
暑假第十一测
查看>>
POJ - 2891 中国剩余定理
查看>>
Tyvj - 1305 单调队列优化dp
查看>>
18华工校赛 小马哥的超级盐水 折半枚举
查看>>
挂钩SSDT详解附源代码
查看>>
iDoubs的编译问题以及解决方案
查看>>
[USACO07DEC]道路建设Building Roads
查看>>
Jmeter登录接口返回 status415
查看>>
iOS动画和第三方插件学习网址
查看>>
C/C++ 格式化读取和读取一行
查看>>
PHP正则表达式完全手册
查看>>
POJ 3268 Silver Cow Party
查看>>
内存和cpu
查看>>
js中Array自定义contains, indexOf, delete方法.
查看>>
重命名流程
查看>>
array 删除指定的元素的方法
查看>>
汇编语言相关图书推荐
查看>>
CBMVC For Titanium Alloy 发布!
查看>>
docker镜像的常用操作
查看>>