//-----------------------------------------------------------------------------
// File: FullScreenDialog.cpp
//
// Desc: This sample demonstrates how to bring up a dialog box, or any other
// type of window while running in DirectDraw full-screen exclusive mode.
//
// Copyright (c) 1999-2000 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include <windows.h>
#include <basetsd.h>
#include <ddraw.h>
#include <mmsystem.h>
#include "resource.h"
#include "ddutil.h"
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything and calls
// UpdateFrame() when idle from the message pump.
//-----------------------------------------------------------------------------
int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int nCmdShow )
{
MSG msg;
HWND hWnd;
HACCEL hAccel;
// Show the dialog immediately
g_hWndDlg = CreateDialog( hInst, MAKEINTRESOURCE(IDD_DIALOG_SAMPLE),
hWnd, (DLGPROC) SampleDlgProc );
ShowWindow( g_hWndDlg, SW_SHOWNORMAL );
g_dwLastTick = timeGetTime();
while( TRUE )
{
// Look for messages, if none are found then
// update the state and display it
if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
{
if( 0 == GetMessage(&msg, NULL, 0, 0 ) )
{
// WM_QUIT was posted, so exit
return (int)msg.wParam;
}
// Translate and dispatch the message. If the dialog is showing,
// translate messages for it since it's a modeless dialog.
if( g_hWndDlg == NULL || !IsDialogMessage( g_hWndDlg, &msg ) )
{
if( 0 == TranslateAccelerator( hWnd, hAccel, &msg ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
}
else
{
if( g_bActive )
{
// Move the sprites, blt them to the back buffer, then
// flip or blt the back buffer to the primary buffer
if( FAILED( ProcessNextFrame() ) )
{
SAFE_DELETE( g_pDisplay );
MessageBox( hWnd, TEXT("Displaying the next frame failed. ")
TEXT("The sample will now exit. "), TEXT("DirectDraw Sample"),
MB_ICONERROR | MB_OK );
return FALSE;
}
}
else
{
// Make sure we go to sleep if we have nothing else to do
WaitMessage();
//-----------------------------------------------------------------------------
// Name: InitDirectDraw()
// Desc: Create the DirectDraw object, and init the surfaces
//-----------------------------------------------------------------------------
HRESULT InitDirectDraw( HWND hWnd )
{
HRESULT hr;
int iSprite;
g_pDisplay = new CDisplay();
if( FAILED( hr = g_pDisplay->CreateFullScreenDisplay( hWnd, SCREEN_WIDTH,
SCREEN_HEIGHT, SCREEN_BPP ) ) )
{
MessageBox( hWnd, TEXT("This display card does not support 640x480x8. "),
TEXT("DirectDraw Sample"), MB_ICONERROR | MB_OK );
return hr;
}
// Check if the device supports DDCAPS2_CANRENDERWINDOWED.
// If it does, then it supports GDI writing directly to the primary surface
// Otherwise, to do GDI displaying on these cards, you you must create a
// bitmap of the GDI window and BitBlt the bitmap to the backbuffer
// then flip as normal. However, this sample does not show this.
DDCAPS ddcaps;
ZeroMemory( &ddcaps, sizeof(ddcaps) );
ddcaps.dwSize = sizeof(ddcaps);
g_pDisplay->GetDirectDraw()->GetCaps( &ddcaps, NULL );
if( (ddcaps.dwCaps2 & DDCAPS2_CANRENDERWINDOWED) == 0 )
{
MessageBox( hWnd, TEXT("This display card can not render GDI."),
TEXT("DirectDraw Sample"), MB_ICONERROR | MB_OK );
return E_FAIL;
}
// Create a clipper so DirectDraw will not blt over the GDI dialog
if( FAILED( hr = g_pDisplay->InitClipper() ) )
return hr;
// Create a surface, and draw text to it.
if( FAILED( hr = g_pDisplay->CreateSurfaceFromText( &g_pTextSurface, NULL, HELPTEXT,
RGB(0,0,0), RGB(255, 255, 0) ) ) )
return hr;
// Create a surface, and draw a bitmap resource on it.
if( FAILED( hr = g_pDisplay->CreateSurfaceFromBitmap( &g_pLogoSurface, MAKEINTRESOURCE( IDB_DIRECTX ),
SPRITE_DIAMETER, SPRITE_DIAMETER ) ) )
return hr;
// Set the color key for the logo sprite to black
if( FAILED( hr = g_pLogoSurface->SetColorKey( 0 ) ) )
return hr;
// Init all the sprites. All of these sprites look the same,
// using the g_pDDSLogo surface.
for( iSprite = 0; iSprite < NUM_SPRITES; iSprite++ )
{
// Set the sprite's position and velocity
g_Sprite[iSprite].fPosX = (float) (rand() % SCREEN_WIDTH);
g_Sprite[iSprite].fPosY = (float) (rand() % SCREEN_HEIGHT);
case WM_SIZE:
// Check to see if we are losing our window...
if( SIZE_MAXHIDE==wParam || SIZE_MINIMIZED==wParam )
g_bActive = FALSE;
else
g_bActive = TRUE;
break;
case WM_EXITMENULOOP:
// Ignore time spent in menu
g_dwLastTick = timeGetTime();
break;
case WM_EXITSIZEMOVE:
// Ignore time spent resizing
g_dwLastTick = timeGetTime();
break;
case WM_SYSCOMMAND:
// Prevent moving/sizing and power loss in fullscreen mode
switch( wParam )
{
case SC_MOVE:
case SC_SIZE:
case SC_MAXIMIZE:
case SC_MONITORPOWER:
return TRUE;
}
break;
case WM_DESTROY:
// Cleanup and close the app
FreeDirectDraw();
PostQuitMessage( 0 );
return 0L;
}
//-----------------------------------------------------------------------------
// Name: SampleDlgProc()
// Desc: A simple dialog that has some standard controls, so you can see that
// they update properly.
//-----------------------------------------------------------------------------
INT_PTR CALLBACK SampleDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
static TCHAR* pszCombo[] = {"One","Two","Three","Four","Five","Six"};
switch (msg)
{
case WM_INITDIALOG:
{
for( int i = 0; i < 6; i++ )
SendDlgItemMessage( hDlg, IDC_COMBO1, CB_ADDSTRING,
0, (LPARAM) pszCombo[i] );
case WM_COMMAND:
switch( LOWORD(wParam) )
{
case IDCANCEL:
case IDOK:
EndDialog( hDlg, TRUE );
return TRUE;
}
break;
case WM_MOVE:
// The window is moving around, so re-draw the backbuffer
DisplayFrame();
break;
case WM_DESTROY:
g_hWndDlg = NULL;
break;
}
return FALSE;
}
//-----------------------------------------------------------------------------
// Name: ProcessNextFrame()
// Desc: Move the sprites, blt them to the back buffer, then
// flips the back buffer to the primary buffer
//-----------------------------------------------------------------------------
HRESULT ProcessNextFrame()
{
HRESULT hr;
// Figure how much time has passed since the last time
DWORD dwCurrTick = timeGetTime();
DWORD dwTickDiff = dwCurrTick - g_dwLastTick;
// Don't update if no time has passed
if( dwTickDiff == 0 )
return S_OK;
g_dwLastTick = dwCurrTick;
// Move the sprites according to how much time has passed
for( int iSprite = 0; iSprite < NUM_SPRITES; iSprite++ )
UpdateSprite( &g_Sprite[ iSprite ], dwTickDiff / 1000.0f );
// Display the sprites on the screen
if( FAILED( hr = DisplayFrame() ) )
{
if( hr != DDERR_SURFACELOST )
return hr;
// The surfaces were lost so restore them
RestoreSurfaces();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: UpdateSprite()
// Desc: Move the sprite around and make it bounce based on how much time
// has passed
//-----------------------------------------------------------------------------
VOID UpdateSprite( SPRITE_STRUCT* pSprite, FLOAT fTimeDelta )
{
// Update the sprite position
pSprite->fPosX += pSprite->fVelX * fTimeDelta;
pSprite->fPosY += pSprite->fVelY * fTimeDelta;
// Clip the position, and bounce if it hits the edge
if( pSprite->fPosX < 0.0f )
{
pSprite->fPosX = 0;
pSprite->fVelX = -pSprite->fVelX;
}
//-----------------------------------------------------------------------------
// Name: DisplayFrame()
// Desc: Blts a the sprites to the back buffer, then flips the
// back buffer onto the primary buffer.
//-----------------------------------------------------------------------------
HRESULT DisplayFrame()
{
HRESULT hr;
// Fill the back buffer with black, ignoring errors until the flip
g_pDisplay->Clear( 0 );
// Blt the help text on the backbuffer, ignoring errors until the flip
g_pDisplay->Blt( 10, 10, g_pTextSurface, NULL );
// Blt all the sprites onto the back buffer using color keying,
// ignoring errors until the last call. Note that all of these sprites
// use the same DirectDraw surface.
for( int iSprite = 0; iSprite < NUM_SPRITES; iSprite++ )
{
g_pDisplay->Blt( (DWORD)g_Sprite[iSprite].fPosX,
(DWORD)g_Sprite[iSprite].fPosY,
g_pLogoSurface, NULL );
}
// Updating the primary buffer with a blt
LPDIRECTDRAWSURFACE7 pddsFrontBuffer = g_pDisplay->GetFrontBuffer();
LPDIRECTDRAWSURFACE7 pddsBackBuffer = g_pDisplay->GetBackBuffer();
// No need to re-create the surface, just re-draw it.
if( FAILED( hr = g_pTextSurface->DrawText( NULL, HELPTEXT,
0, 0, RGB(0,0,0), RGB(255, 255, 0) ) ) )
return hr;
// No need to re-create the surface, just re-draw it.
if( FAILED( hr = g_pLogoSurface->DrawBitmap( MAKEINTRESOURCE( IDB_DIRECTX ),
SPRITE_DIAMETER, SPRITE_DIAMETER ) ) )
return hr;