You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
250 lines
7.3 KiB
250 lines
7.3 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: LgDirectDraw.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of LgDirectDraw.cpp
|
|
//----------------------------------------------------------------------------
|
|
#include "stdafx.h"
|
|
#include "LgDirectDraw.h"
|
|
//#include "Resource.h"
|
|
|
|
CLgDirectDraw::CLgDirectDraw()
|
|
{
|
|
m_pTextSurface = NULL;
|
|
m_bActive = FALSE;
|
|
}
|
|
|
|
CLgDirectDraw::~CLgDirectDraw()
|
|
{
|
|
FreeDirectDraw();
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Name: InitDirectDraw()
|
|
// Desc: Create the DirectDraw object, and init the surfaces
|
|
//-----------------------------------------------------------------------------
|
|
HRESULT CLgDirectDraw::InitDirectDraw( HWND hWnd )
|
|
{
|
|
LPDIRECTDRAWPALETTE pDDPal = NULL;
|
|
HRESULT hr;
|
|
|
|
RECT rect;
|
|
GetClientRect( hWnd, &rect );
|
|
int WINDOW_WIDTH = rect.right-rect.left;
|
|
int WINDOW_HEIGHT = rect.bottom-rect.top;
|
|
|
|
if( FAILED( hr = m_Display.CreateWindowedDisplay( hWnd, WINDOW_WIDTH, WINDOW_HEIGHT ) ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDraw::InitDirectDraw") , _T("Failed initializing DirectDraw") );
|
|
return hr;
|
|
}
|
|
|
|
if( FAILED( hr = m_Display.CreatePaletteFromBitmap( &pDDPal, MAKEINTRESOURCE( 0 ) ) ) )
|
|
return hr;
|
|
|
|
m_Display.SetPalette( pDDPal );
|
|
|
|
SAFE_RELEASE( pDDPal );
|
|
|
|
// Create a surface, and draw text to it.
|
|
if( FAILED( hr = m_Display.CreateSurfaceFromText( &m_pTextSurface, NULL, HELPTEXT, RGB(0,0,0), RGB(255, 255, 0) ) ) )
|
|
return hr;
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Name: FreeDirectDraw()
|
|
// Desc: Release all the DirectDraw objects
|
|
//-----------------------------------------------------------------------------
|
|
VOID CLgDirectDraw::FreeDirectDraw()
|
|
{
|
|
SAFE_DELETE( m_pTextSurface );
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Name: ProcessNextFrame()
|
|
// Desc: Move the sprites, blt them to the back buffer, then
|
|
// flip or blt the back buffer to the primary buffer
|
|
//-----------------------------------------------------------------------------
|
|
HRESULT CLgDirectDraw::ProcessNextFrame( HWND hWnd )
|
|
{
|
|
HRESULT hr;
|
|
|
|
// Check the cooperative level before rendering
|
|
if( FAILED( hr = m_Display.GetDirectDraw()->TestCooperativeLevel() ) )
|
|
{
|
|
switch( hr )
|
|
{
|
|
case DDERR_EXCLUSIVEMODEALREADYSET:
|
|
// Do nothing because some other app has exclusive mode
|
|
Sleep(10);
|
|
return S_OK;
|
|
|
|
case DDERR_WRONGMODE:
|
|
// The display mode changed on us. Update the
|
|
// DirectDraw surfaces accordingly
|
|
FreeDirectDraw();
|
|
return InitDirectDraw( hWnd );
|
|
}
|
|
return hr;
|
|
}
|
|
|
|
// 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: DisplayFrame()
|
|
// Desc: Blts a the sprites to the back buffer, then it blts or flips the
|
|
// back buffer onto the primary buffer.
|
|
//-----------------------------------------------------------------------------
|
|
HRESULT CLgDirectDraw::DisplayFrame()
|
|
{
|
|
HRESULT hr;
|
|
|
|
// Fill the back buffer with black, ignoring errors until the flip
|
|
// m_Display.Clear( 0 );
|
|
|
|
// Blt the help text on the backbuffer, ignoring errors until the flip
|
|
m_Display.Blt( 10, 10, m_pTextSurface, NULL );
|
|
|
|
// We are in windowed mode so perform a blt from the backbuffer
|
|
// to the primary, returning any errors like DDERR_SURFACELOST
|
|
if( FAILED( hr = m_Display.Present() ) )
|
|
return hr;
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Name: RestoreSurfaces()
|
|
// Desc: Restore all the surfaces, and redraw the sprite surfaces.
|
|
//-----------------------------------------------------------------------------
|
|
HRESULT CLgDirectDraw::RestoreSurfaces()
|
|
{
|
|
LPDIRECTDRAWPALETTE pDDPal = NULL;
|
|
HRESULT hr;
|
|
|
|
if( FAILED( hr = m_Display.GetDirectDraw()->RestoreAllSurfaces() ) )
|
|
return hr;
|
|
|
|
// No need to re-create the surface, just re-draw it.
|
|
if( FAILED( hr = m_pTextSurface->DrawText( NULL, HELPTEXT,
|
|
0, 0, RGB(0,0,0), RGB(255, 255, 0), FALSE ) ) )
|
|
return hr;
|
|
|
|
// We need to release and re-load, and set the palette again to
|
|
// redraw the bitmap on the surface. Otherwise, GDI will not
|
|
// draw the bitmap on the surface with the right palette
|
|
if( FAILED( hr = m_Display.CreatePaletteFromBitmap( &pDDPal, MAKEINTRESOURCE( 0 ) ) ) )
|
|
return hr;
|
|
|
|
m_Display.SetPalette( pDDPal );
|
|
|
|
SAFE_RELEASE( pDDPal );
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
void CLgDirectDraw::OnWmMove()
|
|
{
|
|
m_Display.UpdateBounds();
|
|
}
|
|
|
|
void CLgDirectDraw::OnWmPaint()
|
|
{
|
|
// Update the screen if we need to refresh. This case occurs
|
|
// when in windowed mode and the window is behind others.
|
|
// The app will not be active, but it will be visible.
|
|
|
|
// Display the new position of the sprite
|
|
if( DisplayFrame() == DDERR_SURFACELOST )
|
|
{
|
|
// If the surfaces were lost, then restore and try again
|
|
RestoreSurfaces();
|
|
DisplayFrame();
|
|
}
|
|
}
|
|
|
|
void CLgDirectDraw::OnWmQueryNewPalette()
|
|
{
|
|
// If we are in windowed mode with a desktop resolution in 8 bit
|
|
// color, then the palette we created during init has changed
|
|
// since then. So get the palette back from the primary
|
|
// DirectDraw surface, and set it again so that DirectDraw
|
|
// realises the palette, then release it again.
|
|
LPDIRECTDRAWPALETTE pDDPal = NULL;
|
|
m_Display.GetFrontBuffer()->GetPalette( &pDDPal );
|
|
m_Display.GetFrontBuffer()->SetPalette( pDDPal );
|
|
SAFE_RELEASE( pDDPal );
|
|
}
|
|
|
|
void CLgDirectDraw::OnWmDestroy()
|
|
{
|
|
// Cleanup and close the app
|
|
FreeDirectDraw();
|
|
}
|
|
|
|
void CLgDirectDraw::OnWmSize( HWND hWnd, WPARAM wParam )
|
|
{
|
|
// Check to see if we are losing our window...
|
|
if( SIZE_MAXHIDE==wParam || SIZE_MINIMIZED==wParam )
|
|
m_bActive = FALSE;
|
|
else
|
|
m_bActive = TRUE;
|
|
|
|
FreeDirectDraw();
|
|
InitDirectDraw( hWnd );
|
|
|
|
m_Display.UpdateBounds();
|
|
}
|
|
|
|
BOOL CLgDirectDraw::OnIdle( HWND hWnd )
|
|
{
|
|
static DWORD dwTime = 0;
|
|
|
|
if( m_bActive )
|
|
{
|
|
// Move the sprites, blt them to the back buffer, then
|
|
// flip or blt the back buffer to the primary buffer
|
|
DWORD dwCurTime = GetTickCount();
|
|
if( dwCurTime - dwTime >= 10 )
|
|
{
|
|
dwTime = dwCurTime;
|
|
|
|
if( FAILED( ProcessNextFrame( hWnd ) ) )
|
|
{
|
|
return FALSE;
|
|
}
|
|
}
|
|
else
|
|
Sleep(1);
|
|
}
|
|
else
|
|
{
|
|
// Make sure we go to sleep if we have nothing else to do
|
|
WaitMessage();
|
|
}
|
|
|
|
return TRUE;
|
|
}
|