Game Development Community

dev|Pro Game Development Curriculum

TGB: GetVideoAccelerationLevel (code inside)

by amaranthia · 07/09/2008 (2:44 pm) · 6 comments

Hi everyone, I wanted to post the first of two functions that Bryce wrote so that we can determine what level of video acceleration a user has. We've found that in a lot of cases, a user may have hardware acceleration turned on, but only to 0, 1, 2, or 3. This function will help you determine what level your player is using.

In the next post, I'll post SetVideoAccelerationLevel, which will let you change the acceleration level for your users.

From Bryce:

In WinVideo.h, add the following line:

static int GetVideoAccelerationLevel(void);


In WinVideo.CC, add the following code:

int WinDisplayDevice::GetVideoAccelerationLevel()
{
    // Open HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\VIDEO
    // Get Content of \Device\Video0 key
    // Take the GUID from the end and open HKLM\System\CurrentControlSet\Control\Video\{GUID}[[60c1c2cdecbb3]]00
    // Now, look for "Acceleration.Level"
    // If the key is NOT present, the level is on full HW assist
    // If the key is present, the level is 1 to 5 with 5 being the NO HW Support, and 1 being almost full HW support!
    //
    HKEY hKey;
    int nRet = -1;              
    if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\DEVICEMAP\VIDEO"), 0, KEY_QUERY_VALUE , &hKey))
    {
        LPSTR lpszBuffer = NULL;
        DWORD dwType, dwSize;
        LPCSTR lpszSearchKey = "\Device\Video0";                            
        dwType = dwSize = 0;
        if (ERROR_SUCCESS == RegQueryValueExA(hKey, lpszSearchKey, 0, &dwType, (LPBYTE)lpszBuffer, &dwSize))
        {
            HANDLE hHeap = GetProcessHeap(); 
            if (lpszBuffer = (LPSTR)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwSize))
            {
                dwType = REG_SZ;
                if (ERROR_SUCCESS == RegQueryValueExA(hKey, lpszSearchKey, 0, &dwType, (LPBYTE)lpszBuffer, &dwSize))
                {                                              
                    LPCSTR lpszRet = dStrchr(lpszBuffer, '{');
                    HKEY hKeyVideo;
                    char tszRegKey[128] = {"SYSTEM\CurrentControlSet\Control\Video\"};
                    lstrcpyA(&tszRegKey[39], lpszRet);
                    if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE, tszRegKey, 0, KEY_QUERY_VALUE , &hKeyVideo))
                    {
                        DWORD dwAccelVal;
                        LPCTSTR lpszOutputMsg;
                        dwType = REG_DWORD;
                        nRet   = 5;            // Set the return to 5 here to use to invert the level in the registry. 
                        if (ERROR_SUCCESS == RegQueryValueEx(hKeyVideo, TEXT("Acceleration.Level"), 0, &dwType, (LPBYTE)&dwAccelVal, &dwSize))
                        {
                            nRet -= dwAccelVal;
                         }
                        RegCloseKey(hKeyVideo);
                    }
                    else
                    {
                        // Set the default to '1' here to reflect that the hardware is on... the default, which is provided when no key is present.
                        nRet = 1;
                    }
                } 
                HeapFree(hHeap, 0, lpszBuffer);
            }
        }                
        RegCloseKey(hKey);
    }
    return nRet;
}


Lastly, provide a scripting interface for the new service routine... I put this one in WinWindow.CC under PlatformWin32, as it is Win32 specific.

//--------------------------------------
// Returns VideoAccelerationLevel:
// The range is 1 to 5 with 1 being the NO HW Support, and 5 being full HW support.
//--------------------------------------
ConsoleFunction( GetVideoAccelerationLevel, int, 1, 1, "int GetVideoAccelerationLevel(void)" )
{
    return WinDisplayDevice::GetVideoAccelerationLevel();
}

About the author

Recent Blogs


#1
07/09/2008 (3:16 pm)
Sweet. TGEA only, correct?
#2
07/09/2008 (4:29 pm)
Holy nested indents!
#3
07/09/2008 (10:29 pm)
This is for TGB, but should work for TGEA as well... but not likely with the same file names, of course.

As for the indents... it's not that bad... it's just a formatting issue caused by the copy from VS to the forums.
#4
07/09/2008 (10:34 pm)
Sorry about that! I removed the extra tabs. :D
#5
07/10/2008 (6:06 am)
As always: thank you, thank you, THANK YOU!
#6
03/07/2010 (1:50 pm)
Sorry to raise the ancient thread, but has anyone successfully compiled or used this?

VS2005 is not enjoying all of the escape characters floating around in the strings...

char tszRegKey[128] = {"SYSTEM\CurrentControlSet\Control\Video\"};

in particular is giving it 'newline in constant' fits.