Game Development Reference
In-Depth Information
Set Your Save Game Directory
Finding the right directory for user-settable game options used to be easy. A program-
mer would simply store user data files close to the EXE and use the GetModuleFile-
Name() API. Starting with Windows XP Home, the Program Files directory is off
limits by default, and applications are nevermore allowed to write directly to this direc-
tory tree. Instead, applications must write user data to the C:\Documents and Settings
\{User name}\Application Data directory for XP, C:\Users\{User Name}\Application
Data directory for Vista, and C:\Users\{User Name}\AppData for Windows 7. Not only
can this directory be completely different from one version of Windows to another, but
some users also store these on a drive other than the C: drive. You can use a special API
to deal with this problem: SHGetSpecialFolderPath().
If you open Windows Explorer to your application data directory, you
ll see plenty of
companies who play by the rules, writing application data in the spot that will keep
Windows XP from freaking out. Usually, a software developer will create a hierarchy,
starting with his company name, maybe adding his division, then the product, and
finally the version. A Microsoft product I worked on used this path:
'
GAME_APP_DIRECTORY =
Microsoft\\Microsoft Games\\Bicycle Casino\\2.0
;
GAME_APP_DIRECTORY = Your Registry Key
The value for your GAME_APP_DIRECTORY is also a great value for a registry key. Don
t forget to add
the version number at the end. You might as well hope for a gravy train: 2.0, 3.0, 4.0, and so on.
'
It
t exist. This is made
easier with a call to SHCreateDirectoryEx() , which will create the entire direc-
tory hierarchy if it doesn
'
s up to you to make sure you create the directory if it doesn
'
'
t already exist:
const TCHAR *GetSaveGameDirectory(HWND hWnd, const TCHAR *gameAppDirectory)
{
HRESULT hr;
static TCHAR m_SaveGameDirectory[MAX_PATH];
TCHAR userDataPath[MAX_PATH];
hr = SHGetSpecialFolderPath(hWnd, userDataPath, CSIDL_APPDATA, true);
_tcscpy_s(m_SaveGameDirectory, userDataPath);
_tcscat_s(m_SaveGameDirectory, _T(
));
_tcscat_s(m_SaveGameDirectory, gameAppDirectory);
\\
// Does our directory exist?
if (0xffffffff == GetFileAttributes(m_SaveGameDirectory))
{
 
 
Search WWH ::




Custom Search