⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 application.cpp

📁 load .x file to application
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "application.h"


LRESULT WINAPI MainMessageProcedure( HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam );

CApplication::CApplication()
{
    m_hWnd = NULL;
	m_hInstance = NULL;
    m_bPaused = false;
    m_bAlive = true;
    m_bActive = true;
    m_bWindowed = false;
    m_iWndWidth = 640;
    m_iWndHeight = 480;
    strcpy(m_strTitle, "Loading XFile Meshes");
    strcpy(m_strClass, "TripleBuffer");
    m_pD3D = NULL;
    m_pD3DDevice = NULL;
	ZeroMemory(&m_d3dpp, sizeof(D3DPRESENT_PARAMETERS));    
	ZeroMemory(&m_d3ddcp, sizeof(D3DDEVICE_CREATION_PARAMETERS));

    ZeroMemory(&m_bKey, sizeof(bool)*256);

    m_bShowStats = true;

	// Set sprite interface to null.
	m_pD3DSprite = NULL;
}

CApplication::~CApplication()
{
	// Reelase the sprite interface
	SAFE_RELEASE(m_pD3DSprite);
    SAFE_RELEASE(m_pD3DDevice);
    SAFE_RELEASE(m_pD3D);

    if( m_hWnd )
		DestroyWindow( m_hWnd );

    UnregisterClass( m_strClass, m_hInstance );
}

bool CApplication::InitializeWindow( int iWidth, int iHeight, bool bWindowed )
{   
    WNDCLASSEX wc;
    
    wc.cbSize = sizeof(WNDCLASSEX);

	wc.style = CS_CLASSDC;
    wc.lpfnWndProc = MainMessageProcedure;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = m_hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = NULL;  
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	wc.lpszClassName = m_strClass; 
	wc.lpszMenuName = NULL; 

    RegisterClassEx(&wc);   

    m_iWndWidth = iWidth;
    m_iWndHeight = iHeight;

    if(!bWindowed)
    {
        iWidth = GetSystemMetrics(SM_CXFULLSCREEN);
        iHeight = GetSystemMetrics(SM_CYFULLSCREEN);
    }

    m_hWnd = CreateWindow(m_strClass,  
                          m_strTitle, 
                          ((bWindowed)?WS_OVERLAPPEDWINDOW:WS_POPUPWINDOW), 
                          0, 
                          0,
                          iWidth,
                          iHeight,
                          NULL, 
                          NULL,  
                          wc.hInstance,  
                          NULL);            

    if( m_hWnd == NULL )
        return Error("Failed to create the window");
    
	ShowWindow( m_hWnd, SW_SHOW );
	UpdateWindow( m_hWnd );

    m_bWindowed = bWindowed;

    if( m_bWindowed )
    {
        RECT cr, wr;
        GetClientRect( m_hWnd, &cr );
        GetWindowRect( m_hWnd, &wr );

        SetWindowPos(m_hWnd, 
            HWND_TOP, 
            0,         
            0,    
            m_iWndWidth + (cr.left - wr.left) + (wr.right - cr.right),  
            m_iWndHeight + (cr.top - wr.top) + (wr.bottom - cr.bottom), 
            SWP_SHOWWINDOW);
    }

    return true;
}

bool CApplication::InitializeD3D( D3DDEVICE_CREATION_PARAMETERS dcp )
{
    D3DDISPLAYMODE d3ddm;

    m_d3ddcp.hFocusWindow = dcp.hFocusWindow;
    m_d3ddcp.AdapterOrdinal = dcp.AdapterOrdinal;
    m_d3ddcp.DeviceType = dcp.DeviceType;
    m_d3ddcp.BehaviorFlags = dcp.BehaviorFlags;

    if( FAILED( m_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
        return Error("Could not get adapter display mode in initialize D3D");
	

    m_d3dpp.BackBufferCount = 1; 
    m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    m_d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
    m_d3dpp.EnableAutoDepthStencil = TRUE; 
    m_d3dpp.AutoDepthStencilFormat = D3DFMT_D16; 
	m_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

    if( m_bWindowed )
    {
        m_d3dpp.BackBufferWidth = m_iWndWidth; 
        m_d3dpp.BackBufferHeight = m_iWndHeight;  
        m_d3dpp.BackBufferFormat = d3ddm.Format; 
        m_d3dpp.Windowed = true;  
    }
    else
    {
        m_d3dpp.BackBufferWidth = d3ddm.Width;
        m_d3dpp.BackBufferHeight = d3ddm.Height;
        m_d3dpp.BackBufferFormat = d3ddm.Format;
        m_d3dpp.Windowed = false;  
        m_d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    }

    if( FAILED( m_pD3D->CreateDevice( dcp.AdapterOrdinal,  dcp.DeviceType, dcp.hFocusWindow, 
        dcp.BehaviorFlags, &m_d3dpp, &m_pD3DDevice ) ) )
        return Error( "Failed to create the D3D Device interface" ); 

	// Now initialize the sprite interface
	if( FAILED( D3DXCreateSprite( m_pD3DDevice, &m_pD3DSprite ) ) )
		return Error("Failed to create the D3D Sprite interface");


    SetPerspective();

    return true;
}

bool CApplication::IsDeviceThere()
{
    HRESULT hr;
    hr = m_pD3DDevice->TestCooperativeLevel();

    if( FAILED(hr) )
        return false;

    return true;
}

bool CApplication::ResetDevice()
{
    if( !m_statsFont.Invalidate() )
        return Error("Failed to invalidate font interface." );

    if( !CDXFont::InvalidateFontSprite() )
        return Error("Failed to invalidate font sprite interface." );

	if( FAILED( m_pD3DSprite->OnLostDevice() ) )
        return Error("Failed to invalidate main sprite interface." );

    if( FAILED( m_pD3DDevice->Reset( &m_d3dpp ) ) )
        return Error("Reset was called but it failed");

	if( FAILED( m_pD3DSprite->OnLostDevice() ) )
        return Error("Failed to restore main sprite interface." );

    if( !CDXFont::RestoreFontSprite() )
        return Error("Failed to restore font sprite interface." );

    if( !m_statsFont.Restore() )
        return Error("Failed to restore font interface." );

    return true;
}

bool CApplication::CanReset()
{
    HRESULT hr;
    hr = m_pD3DDevice->TestCooperativeLevel();

    if( hr == D3DERR_DEVICENOTRESET )
        return true;

    return false;
}

void CApplication::Run( HINSTANCE hInstance, bool bWindowed )
{
	// Store the HINSTANCE
	m_hInstance = hInstance;

    D3DDEVICE_CREATION_PARAMETERS dcp;

    if( !InitializeWindow( m_iWndWidth, m_iWndHeight, bWindowed) )
        return;

    // Create d3d interface here    
    m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
    if( m_pD3D == NULL )
    {
        Error("Could not create the Direct3D interface");
        return;
    }

    D3DCAPS9 caps;   

    m_pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps );

	if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
    {
        strcpy( m_strVertexProcessing, "\n- Using : Hardware vertex processing" );
        dcp.BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
    }
    else
    {
        strcpy( m_strVertexProcessing, "\n- Using : Software vertex processing" );
        dcp.BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
    }

    dcp.AdapterOrdinal = D3DADAPTER_DEFAULT; 
    dcp.DeviceType = D3DDEVTYPE_HAL;          
    dcp.hFocusWindow = m_hWnd;           

    if( InitializeD3D(dcp) )
    {
        if( FirstInitialize() )
        {
            if( RestoreObjects() )
            {    
                InitializeTimer();
                MyGameLoop();
                InvalidateObjects();
            }

            FinalCleanup();
        }
    }
}

void CApplication::MyGameLoop()
{
    MSG msg;
    ZeroMemory(&msg, sizeof(MSG));

    while( msg.message != WM_QUIT && m_bAlive != false ) 
	{
		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) 
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		} 
		else 
		{
            if( m_bActive )
            {
                if( IsDeviceThere() )
                {
                    if( !m_bPaused )
                    {
                        ProcessTimer();

                        if( !PreRender() )
                            break;

                        if( !Render() )
                            break;
                    }
                    else
                    {
                        PauseTimer();
                    }
                }
                else
                {
                    if( CanReset() )
                    {
                        if( !InvalidateObjects() )
                            return;

                        if( !ResetDevice() )
                            return;

                        if( !RestoreObjects() )
                            return;
                    }
                }
            }
            else
            {
                PauseTimer();
            }
        }
    }
}

LRESULT WINAPI CApplication::MessageProcedure(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    static bool s_wasMax = false;
    switch(iMsg) 
	{
    case WM_ACTIVATE:
		switch(wParam)
		{
		case WA_ACTIVE:
            m_bActive = true;
			break;
		case WA_CLICKACTIVE:
            m_bActive = true;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -