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

📄 utils.cpp

📁 load .x file to application
💻 CPP
字号:
#include "utils.h"
#include <cstdio>


bool DXUTFindMediaSearchTypicalDirs( CHAR* strSearchPath, int cchSearch, LPCSTR strLeaf, CHAR* strExePath, CHAR* strExeName, LPCSTR strMediaDir );
bool DXUTFindMediaSearchParentDirs( CHAR* strSearchPath, int cchSearch, CHAR* strStartAt, CHAR* strLeafName );


bool DXUTGetDXSDKMediaPathCch( CHAR* strDest, int cchDest )
{
    if( strDest == NULL || cchDest < 1 )
        return false;

    lstrcpy( strDest, "" );

    // Open the appropriate registry key
    HKEY  hKey;
    LONG lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                                "Software\\Microsoft\\DirectX SDK",
                                0, KEY_READ, &hKey );
    if( ERROR_SUCCESS != lResult ) 
        return false;

    DWORD dwType;
    DWORD dwSize = cchDest * sizeof(CHAR);
    lResult = RegQueryValueEx( hKey, "DX9S4SDK Samples Path", NULL,
                              &dwType, (BYTE*)strDest, &dwSize );
    strDest[cchDest-1] = 0; // RegQueryValueEx doesn't NULL term if buffer too small
    RegCloseKey( hKey );

    if( ERROR_SUCCESS != lResult )
        return false;

    const CHAR* strMedia = "\\Media\\";
    if( lstrlen(strDest) + lstrlen(strMedia) < cchDest )
        strcat( strDest, strMedia );
    else
        return false;

    return true;
}

bool DXUTFindDXSDKMediaFileCch( CHAR* strDestPath, int cchDest, LPCSTR strFilename )
{
    bool bFound;
    CHAR strSearchFor[MAX_PATH];
    
    if( NULL==strFilename || strFilename[0] == 0 || NULL==strDestPath || cchDest < 10 )
        return false;

    // Get the DirectX SDK's media dir if the SDK is installed
    CHAR strMediaDir[MAX_PATH] = {0};
    DXUTGetDXSDKMediaPathCch( strMediaDir, MAX_PATH );

    // Get the exe name, and exe path
    CHAR strExePath[MAX_PATH] = {0};
    CHAR strExeName[MAX_PATH] = {0};
    CHAR* strLastSlash = NULL;
    GetModuleFileName( NULL, strExePath, MAX_PATH );
    strExePath[MAX_PATH-1]=0;
    strLastSlash = strrchr( strExePath, '\\' );
    if( strLastSlash )
    {
        lstrcpyn( strExeName, &strLastSlash[1], MAX_PATH );

        // Chop the exe name from the exe path
        *strLastSlash = 0;

        // Chop the .exe from the exe name
        strLastSlash = strrchr( strExeName, '.' );
        if( strLastSlash )
            *strLastSlash = 0;
    }

    // Typical directories:
    //      .\
    //      ..\
    //      ..\..\
    //      %EXE_DIR%\
    //      %EXE_DIR%\..\
    //      %EXE_DIR%\..\..\
    //      %EXE_DIR%\..\%EXE_NAME%
    //      %EXE_DIR%\..\..\%EXE_NAME%
    //      DXSDK media path

    // Typical directory search
    bFound = DXUTFindMediaSearchTypicalDirs( strDestPath, cchDest, strFilename, strExePath, strExeName, strMediaDir );
    if( bFound )
        return true;

    // Typical directory search again, but also look in a subdir called "\media\" 
    _snprintf( strSearchFor, MAX_PATH, "media\\%s", strFilename ); strSearchFor[MAX_PATH-1] = 0;
    bFound = DXUTFindMediaSearchTypicalDirs( strDestPath, cchDest, strSearchFor, strExePath, strExeName, strMediaDir );
    if( bFound )
        return true;

    CHAR strLeafName[MAX_PATH] = {0};

    // Search all parent directories starting at .\ and using strFilename as the leaf name
    strncpy( strLeafName, strFilename, MAX_PATH ); strLeafName[MAX_PATH-1] = 0;
    bFound = DXUTFindMediaSearchParentDirs( strDestPath, cchDest, ".", strLeafName );
    if( bFound )
        return true;

    // Search all parent directories starting at the exe's dir and using strFilename as the leaf name
    bFound = DXUTFindMediaSearchParentDirs( strDestPath, cchDest, strExePath, strLeafName );
    if( bFound )
        return true;

    // Search all parent directories starting at .\ and using "media\strFilename" as the leaf name
    _snprintf( strLeafName, MAX_PATH, "media\\%s", strFilename ); strLeafName[MAX_PATH-1] = 0;
    bFound = DXUTFindMediaSearchParentDirs( strDestPath, cchDest, ".", strLeafName );
    if( bFound )
        return true;

    // Search all parent directories starting at the exe's dir and using "media\strFilename" as the leaf name
    bFound = DXUTFindMediaSearchParentDirs( strDestPath, cchDest, strExePath, strLeafName );
    if( bFound )
        return true;

    // On failure, return the file as the path but also return an error code
    strncpy( strDestPath, strFilename, cchDest );
    strDestPath[cchDest-1] = 0;

    return false;
}

//--------------------------------------------------------------------------------------
// Search a set of typical directories
//--------------------------------------------------------------------------------------
bool DXUTFindMediaSearchTypicalDirs( CHAR* strSearchPath, int cchSearch, LPCSTR strLeaf, 
                                     CHAR* strExePath, CHAR* strExeName, LPCSTR strMediaDir )
{
    // Typical directories:
    //      .\
    //      ..\
    //      ..\..\
    //      %EXE_DIR%\
    //      %EXE_DIR%\..\
    //      %EXE_DIR%\..\..\
    //      %EXE_DIR%\..\%EXE_NAME%
    //      %EXE_DIR%\..\..\%EXE_NAME%
    //      DXSDK media path

    // Search in .\  
    strncpy( strSearchPath, strLeaf, cchSearch ); strSearchPath[cchSearch-1] = 0;
    if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
        return true;

    // Search in ..\  
    _snprintf( strSearchPath, cchSearch, "..\\%s", strLeaf ); strSearchPath[cchSearch-1] = 0;
    if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
        return true;

    // Search in ..\..\ 
    _snprintf( strSearchPath, cchSearch, "..\\..\\%s", strLeaf ); strSearchPath[cchSearch-1] = 0;
    if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
        return true;

    // Search in ..\..\ 
    _snprintf( strSearchPath, cchSearch, "..\\..\\%s", strLeaf ); strSearchPath[cchSearch-1] = 0;
    if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
        return true;

    // Search in the %EXE_DIR%\ 
    _snprintf( strSearchPath, cchSearch, "%s\\%s", strExePath, strLeaf ); strSearchPath[cchSearch-1] = 0;
    if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
        return true;

    // Search in the %EXE_DIR%\..\ 
    _snprintf( strSearchPath, cchSearch, "%s\\..\\%s", strExePath, strLeaf ); strSearchPath[cchSearch-1] = 0;
    if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
        return true;

    // Search in the %EXE_DIR%\..\..\ 
    _snprintf( strSearchPath, cchSearch, "%s\\..\\..\\%s", strExePath, strLeaf ); strSearchPath[cchSearch-1] = 0;
    if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
        return true;

    // Search in "%EXE_DIR%\..\%EXE_NAME%\".  This matches the DirectX SDK layout
    _snprintf( strSearchPath, cchSearch, "%s\\..\\%s\\%s", strExePath, strExeName, strLeaf ); strSearchPath[cchSearch-1] = 0;
    if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
        return true;

    // Search in "%EXE_DIR%\..\..\%EXE_NAME%\".  This matches the DirectX SDK layout
    _snprintf( strSearchPath, cchSearch, "%s\\..\\..\\%s\\%s", strExePath, strExeName, strLeaf ); strSearchPath[cchSearch-1] = 0;
    if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
        return true;

    // Search in DirectX SDK's media dir 
    _snprintf( strSearchPath, cchSearch, "%s%s", strMediaDir, strLeaf ); strSearchPath[cchSearch-1] = 0;
    if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
        return true;

    return false;
}



//--------------------------------------------------------------------------------------
// Search parent directories starting at strStartAt, and appending strLeafName
// at each parent directory.  It stops at the root directory.
//--------------------------------------------------------------------------------------
bool DXUTFindMediaSearchParentDirs( CHAR* strSearchPath, int cchSearch, CHAR* strStartAt, CHAR* strLeafName )
{
    CHAR strFullPath[MAX_PATH] = {0};
    CHAR strFullFileName[MAX_PATH] = {0};
    CHAR strSearch[MAX_PATH] = {0};
    CHAR* strFilePart = NULL;

    GetFullPathName( strStartAt, MAX_PATH, strFullPath, &strFilePart );
    if( strFilePart == NULL )
        return false;
   
    while( strFilePart != NULL && *strFilePart != '\0' )
    {
        _snprintf( strFullFileName, MAX_PATH, "%s\\%s", strFullPath, strLeafName ); strFullFileName[MAX_PATH-1] = 0;       
        if( GetFileAttributes( strFullFileName ) != 0xFFFFFFFF )
        {
            strncpy( strSearchPath, strFullFileName, cchSearch ); strSearchPath[cchSearch-1] = 0; 
            return true;
        }

        _snprintf( strSearch, MAX_PATH, "%s\\..", strFullPath ); strSearch[MAX_PATH-1] = 0;
        GetFullPathName( strSearch, MAX_PATH, strFullPath, &strFilePart );
    }

    return false;
}



//////////////////////////////////////////////////////////////////////////////////////////////
// This file is copyright 

⌨️ 快捷键说明

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