void ID3DApplication::HandleWindowSizeChange(void)
{
    if (IsWindowed() && IsActive() && m_pDevice)
    {
        // compare new and old size and reset the device
        // if necesasry
        if (m_Present.BackBufferWidth != (DWORD) GetWindowRect().right ||
            m_Present.BackBufferHeight != (DWORD) GetWindowRect().bottom)
        {
            // window size has changed, get the new size
            m_Present.BackBufferWidth = GetWindowRect().right;
            m_Present.BackBufferHeight = GetWindowRect().bottom;

            OnDeviceLost();

            // reset the 3d device
            m_pDevice->Reset(&m_Present);
            SetDeviceDefaults();

            if (FAILED(OnRestore()))
            {
                Close();
            }
        }
    }
}
HRESULT ID3DApplication::Create(    int iWidth,
                                    int iHeight,
                                    int iBPP,
                                    BOOL bWindowed,
                                    LPCTSTR strTitle,
                                    DWORD dwFlags)
{
    if (!CreateAppWindow(    iWidth,
                            iHeight,
                            bWindowed,
                            strTitle))
    {
        return E_FAIL;
    }

    // create the Direct3D
    m_pD3D = ::Direct3DCreate9(D3D_SDK_VERSION);
    if (!m_pD3D)
    {
        return E_FAIL;
    }

    bool aa = Config::getInstance()->getValueAsBool("antialias", false);
    D3DMULTISAMPLE_TYPE samples = D3DMULTISAMPLE_NONE;
    if (aa) {
        if( FAILED(m_pD3D->CheckDeviceMultiSampleType( D3DADAPTER_DEFAULT, 
					        D3DDEVTYPE_HAL , D3DFMT_R8G8B8, FALSE, 
                            samples, NULL ) ) )
                            return E_FAIL;

        switch (Config::getInstance()->getValueAsInt("antialias samples", 2))
        {
        case 2: samples = D3DMULTISAMPLE_2_SAMPLES; break;
        case 3: samples = D3DMULTISAMPLE_3_SAMPLES; break;
        case 4: samples = D3DMULTISAMPLE_4_SAMPLES; break;
        case 5: samples = D3DMULTISAMPLE_5_SAMPLES; break;
        case 6: samples = D3DMULTISAMPLE_6_SAMPLES; break;
        case 7: samples = D3DMULTISAMPLE_7_SAMPLES; break;
        case 8: samples = D3DMULTISAMPLE_8_SAMPLES; break;
        case 9: samples = D3DMULTISAMPLE_9_SAMPLES; break;
        case 10: samples = D3DMULTISAMPLE_10_SAMPLES; break;
        case 11: samples = D3DMULTISAMPLE_11_SAMPLES; break;
        case 12: samples = D3DMULTISAMPLE_12_SAMPLES; break;
        case 13: samples = D3DMULTISAMPLE_13_SAMPLES; break;
        case 14: samples = D3DMULTISAMPLE_14_SAMPLES; break;
        case 15: samples = D3DMULTISAMPLE_15_SAMPLES; break;
        case 16: samples = D3DMULTISAMPLE_16_SAMPLES; break;
        default:
            return E_FAIL;
        }
    }

    // initialise the present parameters
    HRESULT hres;
    m_Present.hDeviceWindow = GetWindow();

    // initialise automatic z-buffering
    m_Present.EnableAutoDepthStencil = TRUE;

    if (IsWindowed())
    {
        // init windowed mode present parameters
        m_Present.BackBufferWidth = GetWindowRect().right;
        m_Present.BackBufferHeight = GetWindowRect().bottom;
        m_Present.Windowed = TRUE;
        m_Present.BackBufferCount = 1;

        // antialias
        if (aa)
        {
            m_Present.SwapEffect      = D3DSWAPEFFECT_DISCARD;
            m_Present.MultiSampleType = samples;
        }
        else
        {
            m_Present.SwapEffect = D3DSWAPEFFECT_COPY;
        }

        // force z-buffer to 16 bit format, without stencil bits
        m_Present.AutoDepthStencilFormat = D3DFMT_D16;

        // get the desktop display mode
        D3DDISPLAYMODE dm;
        m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dm);
        m_Present.BackBufferFormat = dm.Format;
    }
    else
    {
        // init fullscreen mode present parameters
        m_Present.BackBufferWidth = iWidth;
        m_Present.BackBufferHeight = iHeight;
//        m_Present.SwapEffect = D3DSWAPEFFECT_FLIP;
        m_Present.FullScreen_RefreshRateInHz = 0;

        // vsync
        if (Config::getInstance()->getValueAsBool("vsync", false))
        {
            m_Present.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
        }
        else
        {
            m_Present.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
        }

        // antialias
        if (aa)
        {
            m_Present.SwapEffect      = D3DSWAPEFFECT_DISCARD;
            m_Present.MultiSampleType = samples;
        }
        else
        {
            m_Present.SwapEffect = D3DSWAPEFFECT_FLIP;
        }

        switch (iBPP)
        {
        case 8:
            // forgetaboutit... not supported anymore
            return E_FAIL;
            break;

        case 16:
            m_Present.BackBufferFormat = D3DFMT_R5G6B5;
            m_Present.AutoDepthStencilFormat = D3DFMT_D16;
            break;

        case 24:
            // forgetaboutit... not supported anymore
            return E_FAIL;
            break;

        case 32:
            m_Present.BackBufferFormat = D3DFMT_X8R8G8B8;
            m_Present.AutoDepthStencilFormat = D3DFMT_D16;
            break;

        default:
            // unknown bits per pixel value
            return E_FAIL;
        }
    }


    // Create Direct3D device
    // use the fallback mechanism to select best possible
    // 3d device type
    hres = m_pD3D->CreateDevice(    D3DADAPTER_DEFAULT,
                                    D3DDEVTYPE_HAL,
                                    GetWindow(),
                                    D3DCREATE_HARDWARE_VERTEXPROCESSING,
                                    &m_Present,
                                    &m_pDevice);
    if (FAILED(hres))
    {
        hres = m_pD3D->CreateDevice(    D3DADAPTER_DEFAULT,
                                        D3DDEVTYPE_HAL,
                                        GetWindow(),
                                        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                        &m_Present,
                                        &m_pDevice);
    }
    if (FAILED(hres))
    {
        hres = m_pD3D->CreateDevice(    D3DADAPTER_DEFAULT,
                                        D3DDEVTYPE_REF,
                                        GetWindow(),
                                        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                        &m_Present,
                                        &m_pDevice);
    }
    if (FAILED(hres))
    {
        // failed to create any meaningful 3d device...
        Release();
        return hres;
    }



    SetDeviceDefaults();


    // init the text drawing system
    hres = D3DXCreateSprite(m_pDevice, &m_pSprite);
    if (FAILED(hres))
    {
        Release();
        return hres;
    }

    // init the font
    hres = SetTextFont(NULL);
    if (FAILED(hres))
    {
        Release();
        return hres;
    }



    // init the timer
    GetTimer().Create();

    // call the pure virtual OnCreate
    hres = OnCreate();
    if (FAILED(hres))
    {
        Release();
        return hres;
    }

    SetActive(TRUE);
    return S_OK;
}
Exemple #3
0
	void Screen::SetResolution(uint32 w, uint32 h)
	{
		SetResolution(w, h, !IsWindowed());
	}