_Ramblings in Real-Time_ by Michael Abrash Listing One // try to create a primary buffer and set its format, to configure hardware #include #include LPDIRECTSOUNDBUFFER SetSoundHWFormat (LPDIRECTSOUND pDS, int channels, int bits_per_sample, int samples_per_sec) { DSBUFFERDESC dsbuf; WAVEFORMATEX format; LPDIRECTSOUNDBUFFER pDSPBuf; memset (&format, 0, sizeof(format)); format.wFormatTag = WAVE_FORMAT_PCM; format.nChannels = channels; format.wBitsPerSample = bits_per_sample; format.nSamplesPerSec = samples_per_sec; format.nBlockAlign = format.nChannels*format.wBitsPerSample / 8; format.cbSize = 0; format.nAvgBytesPerSec = format.nSamplesPerSec*format.nBlockAlign; memset (&dsbuf, 0, sizeof(dsbuf)); dsbuf.dwSize = sizeof(DSBUFFERDESC); dsbuf.dwFlags = DSBCAPS_PRIMARYBUFFER; dsbuf.dwBufferBytes = 0; dsbuf.lpwfxFormat = NULL; if (pDS->lpVtbl->CreateSoundBuffer(pDS, &dsbuf, &pDSPBuf, NULL) == DS_OK) { if (pDSPBuf->lpVtbl->SetFormat (pDSPBuf, &format) == DS_OK) return pDSPBuf; // succeeded in configuring hardware else return NULL; // didn't succeed in configuring hardware } else { return NULL; // couldn't create a primary buffer } } Listing Two // determines whether there's real DirectSound support in the sound // driver, or just wave-based emulation #include #include int RealDirectSoundDriver (LPDIRECTSOUND pDS) { DSCAPS dscaps; dscaps.dwSize = sizeof(dscaps); if (pDS->lpVtbl->GetCaps (pDS, &dscaps) != DS_OK) return 0; // couldn't get capabilities if (dscaps.dwFlags & DSCAPS_EMULDRIVER) return 0; // no real DirectSound driver support, just waves else return 1; // real DirectSound driver support } Listing Three // functions to manipulate the desktop video mode #include // attempts to set the desktop video mode to the specified // resolution and bits per pixel int SetVideoMode (int bpp, int width, int height) { DEVMODE gdevmode; gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; gdevmode.dmBitsPerPel = bpp; gdevmode.dmPelsWidth = width; gdevmode.dmPelsHeight = height; // CDS_FULLSCREEN keeps icons and windows from being moved to // fit within the new dimensions of the desktop if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) { return 0; } return 1; } // puts back the default desktop video mode void RestoreDefaultVideoMode (void) { ChangeDisplaySettings (NULL, 0); } Listing Four // functions to start up and shutdown the game configuration for // the mouse, and to accumulate mouse moves over multiple calls // during a frame and to calculate the total move for the frame #include extern int window_center_x, window_center_y; // the two 0 values disable the two acceleration thresholds, and the // 1 value specifies medium mouse speed static int originalmouseparms[3], newmouseparms[3] = {0, 0, 1}; static int mouse_x_accum, mouse_y_accum; static POINT current_pos; int StartGameMouse (void) { if (!SystemParametersInfo (SPI_GETMOUSE, 0, originalmouseparms, 0)) return 0; if (!SystemParametersInfo (SPI_SETMOUSE, 0, newmouseparms, 0)) return 0; ShowCursor (FALSE); SetCursorPos (window_center_x, window_center_y); return 1; } void StopGameMouse (void) { SystemParametersInfo (SPI_SETMOUSE, 0, originalmouseparms, 0); ShowCursor (TRUE); } void AccumulateGameMouseMove (void) { GetCursorPos (¤t_pos); mouse_x_accum += current_pos.x - window_center_x; mouse_y_accum += current_pos.y - window_center_y; // force the mouse to the center, so there's room to move SetCursorPos (window_center_x, window_center_y); } void GetGameMouseMoveForFrame (int * mouse_x_move, int * mouse_y_move) { GetCursorPos (¤t_pos); *mouse_x_move = current_pos.x - window_center_x + mouse_x_accum; *mouse_y_move = current_pos.y - window_center_y + mouse_y_accum; mouse_x_accum = 0; mouse_y_accum = 0; // force the mouse to the center, so there's room to move SetCursorPos (window_center_x, window_center_y); }