Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / MFC

RaceX - A 2D racing game using DirectDraw

Rate me:
Please Sign up or sign in to vote.
4.92/5 (71 votes)
30 Aug 200214 min read 559.3K   25.1K   150  
This is a 2D racing game that uses a DirectX wrapper library. The game has single player and multiplayer support.
  • racex_src.zip
    • RaceX
      • AppLib
      • cCompetition.cpp
      • cCompetition.h
      • cRaceCar.cpp
      • cRaceCar.h
      • cRaceTrack.cpp
      • cRaceTrack.h
      • cTextWriter.cpp
      • cTextWriter.h
      • dsutil.cpp
      • dsutil.h
      • dxutil.cpp
      • dxutil.h
      • icon1.ico
      • RaceX.cpp
      • RaceX.dsp
      • RaceX.dsw
      • RaceX.h
      • racexres.rc
      • ReadMe.txt
      • res
        • Alpha.BMP
        • bmp_background1.bmp
        • bmp_background2.bmp
        • bmp_background3.bmp
        • bmp_background4.bmp
        • bmp_bigcar_blue.bmp
        • bmp_bigcar_green.bmp
        • bmp_bigcar_red.bmp
        • bmp_bigcar_yeallow.bmp
        • bmp_blackptq0.bmp
        • bmp_blackptq1.bmp
        • bmp_blackptq2.bmp
        • bmp_blackptq3.bmp
        • bmp_bluecar.bmp
        • bmp_caret.bmp
        • bmp_currentconnected_title.bmp
        • bmp_cursor.bmp
        • bmp_curveq0.bmp
        • bmp_curveq1.bmp
        • bmp_curveq2.bmp
        • bmp_curveq3.bmp
        • bmp_diagonalq0.bmp
        • bmp_diagonalq1.bmp
        • bmp_diagonalq2.bmp
        • bmp_diagonalq3.bmp
        • bmp_endcurveq0.bmp
        • bmp_endcurveq1.bmp
        • bmp_endcurveq2.bmp
        • bmp_endcurveq3.bmp
        • bmp_endmediumcurveq0p1.bmp
        • bmp_endmediumcurveq0p2.bmp
        • bmp_endmediumcurveq0p3.bmp
        • bmp_endmediumcurveq1p1.bmp
        • bmp_endmediumcurveq1p2.bmp
        • bmp_endmediumcurveq1p3.bmp
        • bmp_endmediumcurveq2p1.bmp
        • bmp_endmediumcurveq2p2.bmp
        • bmp_endmediumcurveq2p3.bmp
        • bmp_endmediumcurveq3p1.bmp
        • bmp_endmediumcurveq3p2.bmp
        • bmp_endmediumcurveq3p3.bmp
        • bmp_finishcomp_panel.bmp
        • bmp_fullroadq0.bmp
        • bmp_fullroadq1.bmp
        • bmp_fullroadq10.bmp
        • bmp_fullroadq11.bmp
        • bmp_fullroadq12.bmp
        • bmp_fullroadq13.bmp
        • bmp_fullroadq14.bmp
        • bmp_fullroadq15.bmp
        • bmp_fullroadq16.bmp
        • bmp_fullroadq17.bmp
        • bmp_fullroadq2.bmp
        • bmp_fullroadq3.bmp
        • bmp_fullroadq4.bmp
        • bmp_fullroadq5.bmp
        • bmp_fullroadq6.bmp
        • bmp_fullroadq7.bmp
        • bmp_fullroadq8.bmp
        • bmp_fullroadq9.bmp
        • bmp_greencar.bmp
        • bmp_halfroadq0.bmp
        • bmp_halfroadq1.bmp
        • bmp_halfroadq2.bmp
        • bmp_halfroadq3.bmp
        • bmp_hz_enddiagq0.bmp
        • bmp_hz_enddiagq1.bmp
        • bmp_hz_enddiagq2.bmp
        • bmp_hz_enddiagq3.bmp
        • bmp_hz_startdiagq0.bmp
        • bmp_hz_startdiagq1.bmp
        • bmp_hz_startdiagq2.bmp
        • bmp_hz_startdiagq3.bmp
        • bmp_mediumcurveq0p1.bmp
        • bmp_mediumcurveq0p2.bmp
        • bmp_mediumcurveq0p3.bmp
        • bmp_mediumcurveq1p1.bmp
        • bmp_mediumcurveq1p2.bmp
        • bmp_mediumcurveq1p3.bmp
        • bmp_mediumcurveq2p1.bmp
        • bmp_mediumcurveq2p2.bmp
        • bmp_mediumcurveq2p3.bmp
        • bmp_mediumcurveq3p1.bmp
        • bmp_mediumcurveq3p2.bmp
        • bmp_mediumcurveq3p3.bmp
        • bmp_middiagq0.bmp
        • bmp_middiagq1.bmp
        • bmp_middiagq2.bmp
        • bmp_middiagq3.bmp
        • bmp_nextraceinfo.bmp
        • bmp_positions.bmp
        • bmp_raceresults_title.bmp
        • bmp_redcar.bmp
        • bmp_sraceroadq0.bmp
        • bmp_sraceroadq1.bmp
        • bmp_sraceroadq2.bmp
        • bmp_sraceroadq3.bmp
        • bmp_startdiagq0.bmp
        • bmp_startdiagq1.bmp
        • bmp_startdiagq2.bmp
        • bmp_startdiagq3.bmp
        • bmp_titlecomp.bmp
        • bmp_vr_enddiagq0.bmp
        • bmp_vr_enddiagq1.bmp
        • bmp_vr_enddiagq2.bmp
        • bmp_vr_enddiagq3.bmp
        • bmp_vr_startdiagq0.bmp
        • bmp_vr_startdiagq1.bmp
        • bmp_vr_startdiagq2.bmp
        • bmp_vr_startdiagq3.bmp
        • bmp_yeallowcar.bmp
        • Car_Pannel.bmp
        • Car_Pannel_Competition.bmp
        • changeoption.WAV
        • COMP_STATUS_TITLE.bmp
        • completed.bmp
        • cooper_tophy.bmp
        • Digital_Small.bmp
        • engine.wav
        • explode_0_blue.bmp
        • explode_0_green.bmp
        • explode_0_red.bmp
        • explode_0_yeallow.bmp
        • explode_135_blue.bmp
        • explode_135_green.bmp
        • explode_135_red.bmp
        • explode_135_yeallow.bmp
        • explode_180_blue.bmp
        • explode_180_green.bmp
        • explode_180_red.bmp
        • explode_180_yeallow.bmp
        • explode_225_blue.bmp
        • explode_225_green.bmp
        • explode_225_red.bmp
        • explode_225_yeallow.bmp
        • explode_270_blue.bmp
        • explode_270_green.bmp
        • explode_270_red.bmp
        • explode_270_yeallow.bmp
        • explode_315_blue.bmp
        • explode_315_green.bmp
        • explode_315_red.bmp
        • explode_315_yeallow.bmp
        • explode_45_blue.bmp
        • explode_45_green.bmp
        • explode_45_red.bmp
        • explode_45_yeallow.bmp
        • explode_90_blue.bmp
        • explode_90_green.bmp
        • explode_90_red.bmp
        • explode_90_yeallow.bmp
        • explosion.wav
        • FinishedTrack.wav
        • firstlight.bmp
        • flags.bmp
        • gold_tophy.bmp
        • grass.bmp
        • helmet.bmp
        • Lap_Results.bmp
        • multiplayerstatus_title.bmp
        • none_tophy.bmp
        • Race_Results.bmp
        • Race_Stats.bmp
        • race_title.bmp
        • sand.bmp
        • Select.WAV
        • semaphore.WAV
        • silver_tophy.bmp
        • Thumbs.db
        • title.bmp
        • Toolbar.bmp
        • type.WAV
        • Verdana.BMP
      • resource.h
      • StdAfx.cpp
      • StdAfx.h
      • Thumbs.db
      • Track01.rxt
      • Track02.rxt
      • Track03.rxt
      • Track04.rxt
      • Track05.rxt
      • Track06.rxt
      • Track07.rxt
      • Track08.rxt
      • Track09.rxt
  • racex_demo.zip
    • RaceX.exe
    • Track01.rxt
    • Track02.rxt
    • Track03.rxt
    • Track04.rxt
    • Track05.rxt
    • Track06.rxt
    • Track07.rxt
    • Track08.rxt
    • Track09.rxt
  • trackeditor_src.zip
    • TrackEditor
      • CheckPointDlg.cpp
      • CheckPointDlg.h
      • cMatrix.cpp
      • cMatrix.h
      • MainFrm.cpp
      • MainFrm.h
      • res
        • bmp_blackptq0.bmp
        • bmp_blackptq1.bmp
        • bmp_blackptq2.bmp
        • bmp_blackptq3.bmp
        • bmp_curveq0.bmp
        • bmp_curveq1.bmp
        • bmp_curveq2.bmp
        • bmp_curveq3.bmp
        • bmp_diagonalq0.bmp
        • bmp_diagonalq1.bmp
        • bmp_diagonalq2.bmp
        • bmp_diagonalq3.bmp
        • bmp_endcurveq0.bmp
        • bmp_endcurveq1.bmp
        • bmp_endcurveq2.bmp
        • bmp_endcurveq3.bmp
        • bmp_endmediumcurveq0p1.bmp
        • bmp_endmediumcurveq0p2.bmp
        • bmp_endmediumcurveq0p3.bmp
        • bmp_endmediumcurveq1p1.bmp
        • bmp_endmediumcurveq1p2.bmp
        • bmp_endmediumcurveq1p3.bmp
        • bmp_endmediumcurveq2p1.bmp
        • bmp_endmediumcurveq2p2.bmp
        • bmp_endmediumcurveq2p3.bmp
        • bmp_endmediumcurveq3p1.bmp
        • bmp_endmediumcurveq3p2.bmp
        • bmp_endmediumcurveq3p3.bmp
        • bmp_fullroadq0.bmp
        • bmp_fullroadq1.bmp
        • bmp_fullroadq10.bmp
        • bmp_fullroadq11.bmp
        • bmp_fullroadq12.bmp
        • bmp_fullroadq13.bmp
        • bmp_fullroadq14.bmp
        • bmp_fullroadq15.bmp
        • bmp_fullroadq16.bmp
        • bmp_fullroadq17.bmp
        • bmp_fullroadq2.bmp
        • bmp_fullroadq3.bmp
        • bmp_fullroadq4.bmp
        • bmp_fullroadq5.bmp
        • bmp_fullroadq6.bmp
        • bmp_fullroadq7.bmp
        • bmp_fullroadq8.bmp
        • bmp_fullroadq9.bmp
        • bmp_halfroadq0.bmp
        • bmp_halfroadq1.bmp
        • bmp_halfroadq2.bmp
        • bmp_halfroadq3.bmp
        • bmp_hz_enddiagq0.bmp
        • bmp_hz_enddiagq1.bmp
        • bmp_hz_enddiagq2.bmp
        • bmp_hz_enddiagq3.bmp
        • bmp_hz_startdiagq0.bmp
        • bmp_hz_startdiagq1.bmp
        • bmp_hz_startdiagq2.bmp
        • bmp_hz_startdiagq3.bmp
        • bmp_mediumcurveq0p1.bmp
        • bmp_mediumcurveq0p2.bmp
        • bmp_mediumcurveq0p3.bmp
        • bmp_mediumcurveq1p1.bmp
        • bmp_mediumcurveq1p2.bmp
        • bmp_mediumcurveq1p3.bmp
        • bmp_mediumcurveq2p1.bmp
        • bmp_mediumcurveq2p2.bmp
        • bmp_mediumcurveq2p3.bmp
        • bmp_mediumcurveq3p1.bmp
        • bmp_mediumcurveq3p2.bmp
        • bmp_mediumcurveq3p3.bmp
        • bmp_middiagq0.bmp
        • bmp_middiagq1.bmp
        • bmp_middiagq2.bmp
        • bmp_middiagq3.bmp
        • bmp_sraceroadq0.bmp
        • bmp_sraceroadq1.bmp
        • bmp_sraceroadq2.bmp
        • bmp_sraceroadq3.bmp
        • bmp_vr_enddiagq0.bmp
        • bmp_vr_enddiagq1.bmp
        • bmp_vr_enddiagq2.bmp
        • bmp_vr_enddiagq3.bmp
        • bmp_vr_startdiagq0.bmp
        • bmp_vr_startdiagq1.bmp
        • bmp_vr_startdiagq2.bmp
        • bmp_vr_startdiagq3.bmp
        • Thumbs.db
        • Toolbar.bmp
        • TrackEditor.ico
        • TrackEditor.rc2
        • TrackEditorDoc.ico
      • Resource.h
      • SetDimensionsDlg.cpp
      • SetDimensionsDlg.h
      • StdAfx.cpp
      • StdAfx.h
      • TrackEditor.clw
      • TrackEditor.cpp
      • TrackEditor.dsp
      • TrackEditor.dsw
      • TrackEditor.h
      • TrackEditor.rc
      • TrackEditorDoc.cpp
      • TrackEditorDoc.h
      • TrackEditorView.cpp
      • TrackEditorView.h
  • trackeditor_demo.zip
    • TrackEditor.exe
//-----------------------------------------------------------------------------
// File: DXUtil.cpp
//
// Desc: Shortcut macros and functions for using DX objects
//
//
// Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved
//-----------------------------------------------------------------------------
#define STRICT
#include <windows.h>
#include <mmsystem.h>
#include <tchar.h>
#include <stdio.h> 
#include <stdarg.h>
#include "DXUtil.h"




//-----------------------------------------------------------------------------
// Name: DXUtil_GetDXSDKMediaPath()
// Desc: Returns the DirectX SDK media path
//-----------------------------------------------------------------------------
const TCHAR* DXUtil_GetDXSDKMediaPath()
{
    static TCHAR strNull[2] = _T("");
    static TCHAR strPath[MAX_PATH];
    DWORD dwType;
    DWORD dwSize = MAX_PATH;
    HKEY  hKey;

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

    lResult = RegQueryValueEx( hKey, _T("DX81SDK Samples Path"), NULL,
                              &dwType, (BYTE*)strPath, &dwSize );
    RegCloseKey( hKey );

    if( ERROR_SUCCESS != lResult )
        return strNull;

    _tcscat( strPath, _T("\\Media\\") );

    return strPath;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_FindMediaFile()
// Desc: Returns a valid path to a DXSDK media file
//-----------------------------------------------------------------------------
HRESULT DXUtil_FindMediaFile( TCHAR* strPath, TCHAR* strFilename )
{
    HANDLE file;
    TCHAR strFullPath[1024];
    TCHAR *strShortName;
    DWORD cchPath;

    if( NULL==strFilename || NULL==strPath )
        return E_INVALIDARG;

    // Build full path name from strFileName (strShortName will be just the leaf filename)
    cchPath = GetFullPathName(strFilename, sizeof(strFullPath)/sizeof(TCHAR), strFullPath, &strShortName);
    if ((cchPath == 0) || (sizeof(strFullPath)/sizeof(TCHAR) <= cchPath))
        return E_FAIL;

    // first try to find the filename given a full path
    file = CreateFile( strFullPath, GENERIC_READ, FILE_SHARE_READ, NULL, 
                       OPEN_EXISTING, 0, NULL );
    if( INVALID_HANDLE_VALUE != file )
    {
        _tcscpy( strPath, strFullPath );
        CloseHandle( file );
        return S_OK;
    }
    
    // next try to find the filename in the current working directory (path stripped)
    file = CreateFile( strShortName, GENERIC_READ, FILE_SHARE_READ, NULL, 
                       OPEN_EXISTING, 0, NULL );
    if( INVALID_HANDLE_VALUE != file )
    {
        _tcscpy( strPath, strShortName );
        CloseHandle( file );
        return S_OK;
    }
    
    // last, check if the file exists in the media directory
    _stprintf( strPath, _T("%s%s"), DXUtil_GetDXSDKMediaPath(), strShortName );

    file = CreateFile( strPath, GENERIC_READ, FILE_SHARE_READ, NULL, 
                       OPEN_EXISTING, 0, NULL );
    if( INVALID_HANDLE_VALUE != file )
    {
        CloseHandle( file );
        return S_OK;
    }

    // On failure, just return the file as the path
    _tcscpy( strPath, strFilename );
    return E_FAIL;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ReadStringRegKey()
// Desc: Helper function to read a registry key string
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadStringRegKey( HKEY hKey, TCHAR* strRegName, TCHAR* strValue, 
                                 DWORD dwLength, TCHAR* strDefault )
{
    DWORD dwType;

    if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
                                          (BYTE*)strValue, &dwLength ) )
    {
        _tcscpy( strValue, strDefault );
    }

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_WriteStringRegKey()
// Desc: Helper function to write a registry key string
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteStringRegKey( HKEY hKey, TCHAR* strRegName,
                                  TCHAR* strValue )
{
    if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_SZ, 
                                        (BYTE*)strValue, 
                                        (_tcslen(strValue)+1)*sizeof(TCHAR) ) )
        return E_FAIL;

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ReadIntRegKey()
// Desc: Helper function to read a registry key int
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD* pdwValue, 
                              DWORD dwDefault )
{
    DWORD dwType;
    DWORD dwLength = sizeof(DWORD);

    if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
                                          (BYTE*)pdwValue, &dwLength ) )
    {
        *pdwValue = dwDefault;
    }

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_WriteIntRegKey()
// Desc: Helper function to write a registry key int
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD dwValue )
{
    if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD, 
                                        (BYTE*)&dwValue, sizeof(DWORD) ) )
        return E_FAIL;

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ReadBoolRegKey()
// Desc: Helper function to read a registry key BOOL
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL* pbValue, 
                              BOOL bDefault )
{
    DWORD dwType;
    DWORD dwLength = sizeof(BOOL);

    if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
                                          (BYTE*)pbValue, &dwLength ) )
    {
        *pbValue = bDefault;
    }

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_WriteBoolRegKey()
// Desc: Helper function to write a registry key BOOL
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL bValue )
{
    if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD, 
                                        (BYTE*)&bValue, sizeof(BOOL) ) )
        return E_FAIL;

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ReadGuidRegKey()
// Desc: Helper function to read a registry key guid
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID* pGuidValue, 
                               GUID& guidDefault )
{
    DWORD dwType;
    DWORD dwLength = sizeof(GUID);

    if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
                                          (LPBYTE) pGuidValue, &dwLength ) )
    {
        *pGuidValue = guidDefault;
    }

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_WriteGuidRegKey()
// Desc: Helper function to write a registry key guid
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID guidValue )
{
    if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_BINARY, 
                                        (BYTE*)&guidValue, sizeof(GUID) ) )
        return E_FAIL;

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_Timer()
// Desc: Performs timer opertations. Use the following commands:
//          TIMER_RESET           - to reset the timer
//          TIMER_START           - to start the timer
//          TIMER_STOP            - to stop (or pause) the timer
//          TIMER_ADVANCE         - to advance the timer by 0.1 seconds
//          TIMER_GETABSOLUTETIME - to get the absolute system time
//          TIMER_GETAPPTIME      - to get the current time
//          TIMER_GETELAPSEDTIME  - to get the time that elapsed between 
//                                  TIMER_GETELAPSEDTIME calls
//-----------------------------------------------------------------------------
FLOAT __stdcall DXUtil_Timer( TIMER_COMMAND command )
{
    static BOOL     m_bTimerInitialized = FALSE;
    static BOOL     m_bUsingQPF         = FALSE;
    static BOOL     m_bTimerStopped     = TRUE;
    static LONGLONG m_llQPFTicksPerSec  = 0;

    // Initialize the timer
    if( FALSE == m_bTimerInitialized )
    {
        m_bTimerInitialized = TRUE;

        // Use QueryPerformanceFrequency() to get frequency of timer.  If QPF is
        // not supported, we will timeGetTime() which returns milliseconds.
        LARGE_INTEGER qwTicksPerSec;
        m_bUsingQPF = QueryPerformanceFrequency( &qwTicksPerSec );
        if( m_bUsingQPF )
            m_llQPFTicksPerSec = qwTicksPerSec.QuadPart;
    }

    if( m_bUsingQPF )
    {
        static LONGLONG m_llStopTime        = 0;
        static LONGLONG m_llLastElapsedTime = 0;
        static LONGLONG m_llBaseTime        = 0;
        double fTime;
        double fElapsedTime;
        LARGE_INTEGER qwTime;
        
        // Get either the current time or the stop time, depending
        // on whether we're stopped and what command was sent
        if( m_llStopTime != 0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
            qwTime.QuadPart = m_llStopTime;
        else
            QueryPerformanceCounter( &qwTime );

        // Return the elapsed time
        if( command == TIMER_GETELAPSEDTIME )
        {
            fElapsedTime = (double) ( qwTime.QuadPart - m_llLastElapsedTime ) / (double) m_llQPFTicksPerSec;
            m_llLastElapsedTime = qwTime.QuadPart;
            return (FLOAT) fElapsedTime;
        }
    
        // Return the current time
        if( command == TIMER_GETAPPTIME )
        {
            double fAppTime = (double) ( qwTime.QuadPart - m_llBaseTime ) / (double) m_llQPFTicksPerSec;
            return (FLOAT) fAppTime;
        }
    
        // Reset the timer
        if( command == TIMER_RESET )
        {
            m_llBaseTime        = qwTime.QuadPart;
            m_llLastElapsedTime = qwTime.QuadPart;
            m_llStopTime        = 0;
            m_bTimerStopped     = FALSE;
            return 0.0f;
        }
    
        // Start the timer
        if( command == TIMER_START )
        {
            if( m_bTimerStopped )
                m_llBaseTime += qwTime.QuadPart - m_llStopTime;
            m_llStopTime = 0;
            m_llLastElapsedTime = qwTime.QuadPart;
            m_bTimerStopped = FALSE;
            return 0.0f;
        }
    
        // Stop the timer
        if( command == TIMER_STOP )
        {
            m_llStopTime = qwTime.QuadPart;
            m_llLastElapsedTime = qwTime.QuadPart;
            m_bTimerStopped = TRUE;
            return 0.0f;
        }
    
        // Advance the timer by 1/10th second
        if( command == TIMER_ADVANCE )
        {
            m_llStopTime += m_llQPFTicksPerSec/10;
            return 0.0f;
        }

        if( command == TIMER_GETABSOLUTETIME )
        {
            fTime = qwTime.QuadPart / (double) m_llQPFTicksPerSec;
            return (FLOAT) fTime;
        }

        return -1.0f; // Invalid command specified
    }
    else
    {
        // Get the time using timeGetTime()
        static double m_fLastElapsedTime  = 0.0;
        static double m_fBaseTime         = 0.0;
        static double m_fStopTime         = 0.0;
        double fTime;
        double fElapsedTime;
        
        // Get either the current time or the stop time, depending
        // on whether we're stopped and what command was sent
        if( m_fStopTime != 0.0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
            fTime = m_fStopTime;
        else
            fTime = timeGetTime() * 0.001;
    
        // Return the elapsed time
        if( command == TIMER_GETELAPSEDTIME )
        {   
            fElapsedTime = (double) (fTime - m_fLastElapsedTime);
            m_fLastElapsedTime = fTime;
            return (FLOAT) fElapsedTime;
        }
    
        // Return the current time
        if( command == TIMER_GETAPPTIME )
        {
            return (FLOAT) (fTime - m_fBaseTime);
        }
    
        // Reset the timer
        if( command == TIMER_RESET )
        {
            m_fBaseTime         = fTime;
            m_fLastElapsedTime  = fTime;
            m_fStopTime         = 0;
            m_bTimerStopped     = FALSE;
            return 0.0f;
        }
    
        // Start the timer
        if( command == TIMER_START )
        {
            if( m_bTimerStopped )
                m_fBaseTime += fTime - m_fStopTime;
            m_fStopTime = 0.0f;
            m_fLastElapsedTime  = fTime;
            m_bTimerStopped = FALSE;
            return 0.0f;
        }
    
        // Stop the timer
        if( command == TIMER_STOP )
        {
            m_fStopTime = fTime;
            m_fLastElapsedTime  = fTime;
            m_bTimerStopped = TRUE;
            return 0.0f;
        }
    
        // Advance the timer by 1/10th second
        if( command == TIMER_ADVANCE )
        {
            m_fStopTime += 0.1f;
            return 0.0f;
        }

        if( command == TIMER_GETABSOLUTETIME )
        {
            return (FLOAT) fTime;
        }

        return -1.0f; // Invalid command specified
    }
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertAnsiStringToWide()
// Desc: This is a UNICODE conversion utility to convert a CHAR string into a
//       WCHAR string. cchDestChar defaults -1 which means it 
//       assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertAnsiStringToWide( WCHAR* wstrDestination, const CHAR* strSource, 
                                     int cchDestChar )
{
    if( wstrDestination==NULL || strSource==NULL )
        return;

    if( cchDestChar == -1 )
        cchDestChar = strlen(strSource)+1;

    MultiByteToWideChar( CP_ACP, 0, strSource, -1, 
                         wstrDestination, cchDestChar-1 );

    wstrDestination[cchDestChar-1] = 0;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertWideStringToAnsi()
// Desc: This is a UNICODE conversion utility to convert a WCHAR string into a
//       CHAR string. cchDestChar defaults -1 which means it 
//       assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertWideStringToAnsi( CHAR* strDestination, const WCHAR* wstrSource, 
                                     int cchDestChar )
{
    if( strDestination==NULL || wstrSource==NULL )
        return;

    if( cchDestChar == -1 )
        cchDestChar = wcslen(wstrSource)+1;

    WideCharToMultiByte( CP_ACP, 0, wstrSource, -1, strDestination, 
                         cchDestChar-1, NULL, NULL );

    strDestination[cchDestChar-1] = 0;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertGenericStringToAnsi()
// Desc: This is a UNICODE conversion utility to convert a TCHAR string into a
//       CHAR string. cchDestChar defaults -1 which means it 
//       assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertGenericStringToAnsi( CHAR* strDestination, const TCHAR* tstrSource, 
                                        int cchDestChar )
{
    if( strDestination==NULL || tstrSource==NULL || cchDestChar == 0 )
        return;

#ifdef _UNICODE
    DXUtil_ConvertWideStringToAnsi( strDestination, tstrSource, cchDestChar );
#else
    if( cchDestChar == -1 )
    {
        strcpy( strDestination, tstrSource );
    }
    else
    {
        strncpy( strDestination, tstrSource, cchDestChar );
        strDestination[cchDestChar-1] = '\0';
    }
#endif
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertGenericStringToWide()
// Desc: This is a UNICODE conversion utility to convert a TCHAR string into a
//       WCHAR string. cchDestChar defaults -1 which means it 
//       assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertGenericStringToWide( WCHAR* wstrDestination, const TCHAR* tstrSource, 
                                        int cchDestChar )
{
    if( wstrDestination==NULL || tstrSource==NULL || cchDestChar == 0 )
        return;

#ifdef _UNICODE
    if( cchDestChar == -1 )
    {
        wcscpy( wstrDestination, tstrSource );
    }
    else
    {
        wcsncpy( wstrDestination, tstrSource, cchDestChar );
        wstrDestination[cchDestChar-1] = L'\0';
    }
#else
    DXUtil_ConvertAnsiStringToWide( wstrDestination, tstrSource, cchDestChar );
#endif
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertAnsiStringToGeneric()
// Desc: This is a UNICODE conversion utility to convert a CHAR string into a
//       TCHAR string. cchDestChar defaults -1 which means it 
//       assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertAnsiStringToGeneric( TCHAR* tstrDestination, const CHAR* strSource, 
                                        int cchDestChar )
{
    if( tstrDestination==NULL || strSource==NULL || cchDestChar == 0 )
        return;
        
#ifdef _UNICODE
    DXUtil_ConvertAnsiStringToWide( tstrDestination, strSource, cchDestChar );
#else
    if( cchDestChar == -1 )
    {
        strcpy( tstrDestination, strSource );
    }
    else
    {
        strncpy( tstrDestination, strSource, cchDestChar );
        tstrDestination[cchDestChar-1] = '\0';
    }
#endif
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertAnsiStringToGeneric()
// Desc: This is a UNICODE conversion utility to convert a WCHAR string into a
//       TCHAR string. cchDestChar defaults -1 which means it 
//       assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertWideStringToGeneric( TCHAR* tstrDestination, const WCHAR* wstrSource, 
                                        int cchDestChar )
{
    if( tstrDestination==NULL || wstrSource==NULL || cchDestChar == 0 )
        return;

#ifdef _UNICODE
    if( cchDestChar == -1 )
    {
        wcscpy( tstrDestination, wstrSource );
    }
    else
    {
        wcsncpy( tstrDestination, wstrSource, cchDestChar );
        tstrDestination[cchDestChar-1] = L'\0';
    }
#else
    DXUtil_ConvertWideStringToAnsi( tstrDestination, wstrSource, cchDestChar );
#endif
}




//-----------------------------------------------------------------------------
// Name: _DbgOut()
// Desc: Outputs a message to the debug stream
//-----------------------------------------------------------------------------
HRESULT _DbgOut( TCHAR* strFile, DWORD dwLine, HRESULT hr, TCHAR* strMsg )
{
    TCHAR buffer[256];
    wsprintf( buffer, _T("%s(%ld): "), strFile, dwLine );
    OutputDebugString( buffer );
    OutputDebugString( strMsg );

    if( hr )
    {
        wsprintf( buffer, _T("(hr=%08lx)\n"), hr );
        OutputDebugString( buffer );
    }

    OutputDebugString( _T("\n") );

    return hr;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_Trace()
// Desc: Outputs to the debug stream a formatted string with a variable-
//       argument list.
//-----------------------------------------------------------------------------
VOID DXUtil_Trace( TCHAR* strMsg, ... )
{
#if defined(DEBUG) | defined(_DEBUG)
    TCHAR strBuffer[512];
    
    va_list args;
    va_start(args, strMsg);
    _vsntprintf( strBuffer, 512, strMsg, args );
    va_end(args);

    OutputDebugString( strBuffer );
#else
    UNREFERENCED_PARAMETER(strMsg);
#endif
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertStringToGUID()
// Desc: Converts a string to a GUID
//-----------------------------------------------------------------------------
BOOL DXUtil_ConvertStringToGUID( const TCHAR* strIn, GUID* pGuidOut )
{
    UINT aiTmp[10];

    if( _stscanf( strIn, TEXT("{%8X-%4X-%4X-%2X%2X-%2X%2X%2X%2X%2X%2X}"),
                    &pGuidOut->Data1, 
                    &aiTmp[0], &aiTmp[1], 
                    &aiTmp[2], &aiTmp[3],
                    &aiTmp[4], &aiTmp[5],
                    &aiTmp[6], &aiTmp[7],
                    &aiTmp[8], &aiTmp[9] ) != 11 )
    {
        ZeroMemory( pGuidOut, sizeof(GUID) );
        return FALSE;
    }
    else
    {
        pGuidOut->Data2       = (USHORT) aiTmp[0];
        pGuidOut->Data3       = (USHORT) aiTmp[1];
        pGuidOut->Data4[0]    = (BYTE) aiTmp[2];
        pGuidOut->Data4[1]    = (BYTE) aiTmp[3];
        pGuidOut->Data4[2]    = (BYTE) aiTmp[4];
        pGuidOut->Data4[3]    = (BYTE) aiTmp[5];
        pGuidOut->Data4[4]    = (BYTE) aiTmp[6];
        pGuidOut->Data4[5]    = (BYTE) aiTmp[7];
        pGuidOut->Data4[6]    = (BYTE) aiTmp[8];
        pGuidOut->Data4[7]    = (BYTE) aiTmp[9];
        return TRUE;
    }
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertGUIDToString()
// Desc: Converts a GUID to a string 
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertGUIDToString( const GUID* pGuidIn, TCHAR* strOut )
{
    _stprintf( strOut, TEXT("{%0.8X-%0.4X-%0.4X-%0.2X%0.2X-%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X}"),
               pGuidIn->Data1, pGuidIn->Data2, pGuidIn->Data3,
               pGuidIn->Data4[0], pGuidIn->Data4[1],
               pGuidIn->Data4[2], pGuidIn->Data4[3],
               pGuidIn->Data4[4], pGuidIn->Data4[5],
               pGuidIn->Data4[6], pGuidIn->Data4[7] );
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Brazil Brazil
Mauricio Ritter lives in Brazil, in the city of Porto Alegre. He is working with software development for about 8 years, and most of his work was done at a bank, within a home and office banking system.
Mauricio also holds MCSD, MCSE, MCDBA, MCAD and MCT Microsoft certifications and work as a trainer/consultant in some MS CTEC in his city.
Mauricio also works in his own programming site, aimed to Brazilian Developers: http://www.dotnetmaniacs.com.br

In his spare time he studys korean language...

Comments and Discussions