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

📄 cdxapp.cpp

📁 一个Direct3DRM的包装类的例子
💻 CPP
📖 第 1 页 / 共 5 页
字号:
     */

    *pdwDXVersion = 0x500;

    pSurf->Release();
    pDDraw->Release();
    FreeLibrary(DDHinst);
}

CDXApp::CDXApp( HINSTANCE hInstance,HINSTANCE hPrevInstance,LPCSTR lpszCmdLine,int nCmdShow )
{
    /*  Constructor  */

    WNDCLASS        wc;
    RECT            rc;

    /*  Catch-all to prevent usage of NULL for pApp. This gets around chicken and  */
    /*  egg syndrome                                                               */

    pApp = this;

    /*  Initialise our members  */

    ZeroMemory( this,sizeof(*this) );

    /*  Check for DirectX version  */

    ::GetDXVersion( &m_dwDirectXVersion,&m_dwPlatformVersion );

    /*  Manually initialize what we can  */

    m_hInstance     = hInstance;
    m_hPrevInstance = hPrevInstance;
    m_lpszCmdLine   = lpszCmdLine;
    m_hWndParent    = NULL;

    SetClassName( "CDXApp" );
    SetWindowTitle( "CDXApp Example" );
    m_sizeScreen.cx = 320;
    m_sizeScreen.cy = 200;
    m_nBitDepth     = 8;
    SetRenderFlags( D3DRMLIGHT_ON|D3DRMFILL_SOLID|D3DRMSHADE_GOURAUD );
    SetDither( FALSE );
    SetTextureFlags( D3DRMTEXTURE_NEAREST );
    SetPreferedColorModel( 0 );                 /*  Used as auto  */

    /*  Determine if we have MMX technology  */

    int     nFeatureFlags;

    _asm    mov eax, 1
    _asm    cpuid
    _asm    mov nFeatureFlags, edx

    m_bIsMMX    = ((nFeatureFlags&0x800000)!=0);

    m_dwStyles  = WS_VISIBLE|WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX;

    /*  Allow app to set up other stuff  */

    ::AppInitHook();

    /*  Register the window class */

    wc.style            = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc      = CDXWindowProc;
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = sizeof(DWORD);
    wc.hInstance        = GetHInstance();
    wc.hIcon            = ::LoadIcon( GetHInstance(),"AppIcon" );
    wc.hCursor          = ::LoadCursor( NULL,IDC_ARROW );
    wc.hbrBackground    = (HBRUSH)::GetStockObject( BLACK_BRUSH );
    wc.lpszMenuName     = NULL;
    wc.lpszClassName    = GetClassName();

    if ( !::RegisterClass(&wc) )
    {
        ErrHandler( "CDXApp::CDXApp() Failure to register window class!" );
        return;
    }

    /*  Create the window  */

    if ( IsFullScreen() ) m_dwStyles=WS_VISIBLE;

    ::SetRect( &rc,0,0,m_sizeScreen.cx,m_sizeScreen.cy );
    ::AdjustWindowRect( &rc,GetStyles(),(GetHMenu()!=NULL && !IsFullScreen()) );

    if ( rc.left<0 )
    {
        rc.right += -rc.left;
        rc.left   = 0;
    }
    if ( rc.top<0 )
    {
        rc.bottom += -rc.top;
        rc.top     = 0;
    }

    DWORD   dwExStyles=(GetStyles()&WS_SYSMENU)?WS_EX_APPWINDOW:0;

    m_hWnd = ::CreateWindowEx(  dwExStyles,
                                m_szClassName,
                                m_szWindowTitle,
                                GetStyles(),
                                rc.left,rc.top,
                                rc.right,rc.bottom,
                                GetParent(),
                                IsFullScreen()?NULL:GetHMenu(),
                                GetHInstance(),
                                NULL );

    if ( !m_hWnd )
    {
        ErrHandler( "CDXApp::CDXApp() Failure to create window!" );
        return;
    }

    /*  Display the window  */

    ::ShowWindow( GetHWnd(),nCmdShow );
    ::UpdateWindow( GetHWnd() );

    /*  Initialise D3D  */

    if ( !InitialiseD3DRM() ) return;

#ifdef _DEBUG
    if ( !InitialiseStatistics() )
    {
        ErrHandler( "CDXApp::CDXApp() Failure to initialise stats" );
    }
#endif
}

CDXApp::~CDXApp()
{
    CleanUp();

#ifdef _DEBUG
    if ( m_hDebugHandle!=NULL )
    {
        ::CloseHandle( m_hDebugHandle );
        m_hDebugHandle = NULL;
    }
#endif
}

BOOL CDXApp::RenderFrame()
{
    /*  Notch everything on a bit  */

    HRESULT     err;

    /*  Tick the scene  */

    if ( (err=GetScene()->Move(D3DVAL(1.0)))!=D3DRM_OK )
    {
        ErrHandler( "CDXApp::RenderFrame()","Scene Move Failed",err );
        return( FALSE );
    }

    /*  Clear the viewport  */

    if ( (err=GetViewport()->Clear())!=D3DRM_OK )
    {
        ErrHandler( "CDXApp::RenderFrame()","Viewport Clear Failed",err );
        return( FALSE );
    }

    /*  Render the scene to the viewport  */

    if ( (err=GetViewport()->Render(GetScene()))!=D3DRM_OK )
    {
        ErrHandler( "CDXApp::RenderFrame()","Viewport Render Failed",err );
        return( FALSE );
    }

    /*  If stats enabled, show them  */

#ifdef _DEBUG
    if ( m_bEnableStats ) DisplayStats();
#endif

    /*  Force full update if required  */

    if ( m_bForceFullUpdate )
    {
        GetViewport()->ForceUpdate( 0,0,m_sizeScreen.cx-1,m_sizeScreen.cy-1 );
        m_bForceFullUpdate = FALSE;
    }

    /*  Update the window  */

    if ( (err=Get3DDevice()->Update())!=D3DRM_OK )
    {
        ErrHandler( "CDXApp::RenderFrame()","Render Update Failed",err );
        return( FALSE );
    }

    /*  Flip if full screen  */

    if ( IsFullScreen() && !IsPaused() )
    {
        if ( (err=m_pPrimarySurface->Flip(NULL,DDFLIP_WAIT))!=DD_OK )
        {
            ErrHandler("CDXApp::RenderFrame()","Flip Failed",err );
            return( FALSE );
        }
    }

    return( TRUE );
}

void CDXApp::SetClassName( LPCSTR lpszClass )
{
    ZeroMemory( m_szClassName,sizeof(m_szClassName) );
    lstrcpyn( m_szClassName,lpszClass,sizeof(m_szClassName)-1 );
}

void CDXApp::SetWindowTitle( LPCSTR lpszTitle )
{
    ZeroMemory( m_szWindowTitle,sizeof(m_szWindowTitle) );
    lstrcpyn( m_szWindowTitle,lpszTitle,sizeof(m_szWindowTitle)-1 );

    /*  Tell Win32 what our new title is  */

    if ( GetHWnd()!=NULL )
    {
        ::SetWindowText( GetHWnd(),m_szWindowTitle );
    }
}

BOOL CDXApp::SetVideoMode( int nWidth,int nHeight,int nBitDepth,BOOL bFullScreen )
{
    /*  Called by the app, this defines what we need to set up.  */

    BOOL        bWasInitialised=IsInitialised();
    BOOL        bWasFullScreen=IsFullScreen();

    if ( bWasInitialised )
    {
        /*  If original mode was full screen need to restore original while  */
        /*  we still have exclusive mode on                                  */

        if ( bWasFullScreen )
        {
            HRESULT     err=m_pDD2->RestoreDisplayMode();

            if ( err!=DD_OK )
            {
                ErrHandler( "CDXApp::SetVideoMode","RestoreDisplayMode Failed",err );
            }
        }

        /*  A mode change coming.  Need to destroy original objects  */

        CleanUp();
    }

    m_sizeScreen.cx = nWidth;
    m_sizeScreen.cy = nHeight;
    m_nBitDepth     = nBitDepth;
    m_bIsFullScreen = bFullScreen;

    /*  If we've just had a mode change, re-create objects  */

    if ( bWasInitialised )
    {
        /*  Adjust window styles  */

        if ( IsFullScreen() ) m_dwStyles=WS_VISIBLE;

        ::SetWindowLong( GetHWnd(),GWL_STYLE,GetStyles() );

        /*  Need to change size of screen  */

        RECT        r;

        ::SetRect( &r,0,0,nWidth,nHeight );
        ::SetMenu( GetHWnd(),(IsFullScreen()?NULL:GetHMenu()) );
        ::AdjustWindowRect( &r,GetStyles(),(GetHMenu()!=NULL && !IsFullScreen()) );

        if ( r.left<0 )
        {
            r.right += -r.left;
            r.left   = 0;
        }

        if ( r.top<0 )
        {
            r.bottom += -r.top;
            r.top     = 0;
        }

        ::MoveWindow( GetHWnd(),r.left,r.top,r.right-r.left,r.bottom-r.top,TRUE );

        if ( !InitialiseD3DRM() ) return( FALSE );

        /*  Re-initialise stats  */

#ifdef _DEBUG
        InitialiseStatistics();
#endif
    }

    return( TRUE );
}

LPCSTR CDXApp::GetCurrentDriverName()
{
    if ( m_nCurrDriver==-1 ) return( NULL );

    return( (LPCSTR)m_szDriverName[m_nCurrDriver] );
}

GUID CDXApp::GetCurrentDriverGUID()
{
    if ( m_nCurrDriver==-1 ) return( m_DriverGUID[0] );

    return( m_DriverGUID[m_nCurrDriver] );
}

#ifdef _DEBUG

BOOL CDXApp::SetDebugOptions( LPCSTR lpszFileName, DWORD dwOptions )
{
    /*  Set up the debugging system  */

    if ( m_hDebugHandle!=NULL )
    {
        TRACE( "Closing DEBUG file %s",m_lpszDebugFileName );
        ::CloseHandle( m_hDebugHandle );
        m_hDebugHandle = NULL;
    }

    m_dwDebugOptions    = dwOptions;
    m_lpszDebugFileName = lpszFileName;

    /*  Create/Open the new debugging file  */

    if ( m_dwDebugOptions&DBF_FILE && m_lpszDebugFileName!=NULL )
    {
        HANDLE  handle=::CreateFile( m_lpszDebugFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL );

        if ( handle!=INVALID_HANDLE_VALUE )
        {
            m_hDebugHandle = handle;
        }
        else
        {
            TRACE( "Error creating debug file %s. Error=%d",m_lpszDebugFileName,handle );
            return( FALSE );
        }
    }

    return( TRUE );
}

void __cdecl TRACE( LPSTR fmt, ... )
{
    char        buff[256];
    va_list     args;

    va_start(args, fmt);
    wvsprintf(buff, fmt, args);
    va_end(args);

    /*  TRACE doesn't output to Window  */

    DWORD   dwOptions=pApp->GetDebugOptions();

⌨️ 快捷键说明

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