Click here to Skip to main content
15,885,278 members
Articles / Multimedia / DirectX

A Simple and Powerful Game Engine

Rate me:
Please Sign up or sign in to vote.
4.45/5 (17 votes)
23 Jul 20074 min read 91.6K   9.1K   74  
This article talks about a simple and powerful game engine to make game programming simpler
//DirectShow.cpp

/********************************************************

Description: Game Direct Show

Date: 1/7/2007

Auther: Shalom Keller

Contact: szkeller@gmail.com

Note:	If you find any bugs or you think that some thing 
		is wrong, Please send me an e-mail on it.

********************************************************/


#include <AfxWin.h>
//#include <Windows.h>// If you replace the code that uses MFC, you can include Windows.h instead of AfxWin.h
#include <dshow.h>
#include "DirectShow.h"
#include "DirectX3D.h"
#include "WinMain.h"


///////////////////////////////////////
//
///////////////////////////////////////
CDirectShow::CDirectShow()
{
	m_pGraph			=0;
	m_pMediaControl		=0;
	m_pVidWin			=0;
	m_pEvent			=0;
}
///////////////////////////////////////
//
///////////////////////////////////////
BOOL CDirectShow::Play(LPCWSTR lpwstrFile,HWND hWnd)
{
    CoInitialize(NULL);

	HRESULT ret;
	//-----------------------------------
    // Create the filter graph manager.
    CoCreateInstance(CLSID_FilterGraph,
		NULL,
		CLSCTX_INPROC, 
        IID_IGraphBuilder,
		(void **)&m_pGraph);

	if(!m_pGraph)
	{
		DisplayErrorMessage("CDirectShow::Play !IID_IGraphBuilder");
		return FALSE;
	}
	//-----------------------------------
	//Get a Media Control Interface.
    m_pGraph->QueryInterface(
		IID_IMediaControl,
		(void **)&m_pMediaControl);

	if(!m_pMediaControl)
	{
		DisplayErrorMessage("CDirectShow::Play !IID_IMediaControl");
		return FALSE;
	}
	//-----------------------------------
	//Get a Media Event Interface.
	m_pGraph->QueryInterface(
		IID_IMediaEvent,
		(void **)&m_pEvent);

	if(!m_pEvent)
	{
		DisplayErrorMessage("CDirectShow::Play !IID_IMediaEvent");
		return FALSE;
	}
	//-----------------------------------
	//Get a Video Window Interface.
	m_pGraph->QueryInterface(
		IID_IVideoWindow, 
		(void **)&m_pVidWin);

	if(!m_pVidWin)
	{
		DisplayErrorMessage("CDirectShow::Play !IID_IVideoWindow");
		return FALSE;
	}
	//-----------------------------------
    // Build the graph. 
	ret=m_pGraph->RenderFile(
		lpwstrFile, 
		NULL);
	if(ret!=S_OK)
	{
		DisplayDirectXErrorCode(ret);
		DisplayErrorMessage("CDirectShow::Play RenderFile");
		return FALSE;
	}
	//-----------------------------------

    //Set the Video window.
    ret=m_pVidWin->put_Owner(
		(OAHWND)hWnd);

	if(ret!=S_OK)
	{
		DisplayDirectXErrorCode(ret);
		DisplayErrorMessage("CDirectShow::Play put_Owner");
		return FALSE;
	}
	//-----------------------------------
	//Set Window Style.
    ret=m_pVidWin->put_WindowStyle(
		WS_CHILD | WS_CLIPSIBLINGS);

	if(ret!=S_OK)
	{
		DisplayDirectXErrorCode(ret);
		DisplayErrorMessage("CDirectShow::Play put_WindowStyle");
		return FALSE;
	}
	//-----------------------------------
	//Set Window Position
    RECT grc;
    GetClientRect(hWnd, &grc);
    ret=m_pVidWin->SetWindowPosition(0, 
								0,
								grc.right, 
								grc.bottom);
	if(ret!=S_OK)
	{
		DisplayDirectXErrorCode(ret);
		DisplayErrorMessage("CDirectShow::Play SetWindowPosition");
		return FALSE;
	}
	//-----------------------------------
    // Run the graph.
    ret=m_pMediaControl->Run();

	if(ret!=S_OK)//Try Again.
	{
		::Sleep(2*1000);//Aleep for 2 seconds.
		// Run the graph.
		ret=m_pMediaControl->Run();
	}

	if(ret!=S_OK)
	{
		DisplayDirectXErrorCode(ret);
		DisplayErrorMessage("CDirectShow::Play Run");
		return FALSE;
	}
	//-----------------------------------
	//Don't Continue until the video is finish.
	long EvCode;
	MSG msg;
	while(1)
	{
		ret=m_pEvent->WaitForCompletion(
			10,//INFINITE
			&EvCode);

		if( ret==E_ABORT )
		{
			if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
			{
				if( !GetMessage( &msg, NULL, 0, 0 ) )
					return msg.wParam;

				TranslateMessage(&msg); 
				DispatchMessage(&msg);
			}
		}
		else if(ret!=S_OK)
		{
			DisplayErrorMessage("CDirectShow::Play WaitForCompletion");
			DisplayDirectXErrorCode(ret);
			return FALSE;
		}
		else if( ret==S_OK)
		{
			break;
		}
	}
	
	//-----------------------------------
	return TRUE;
}
///////////////////////////////////////
//
///////////////////////////////////////
void CDirectShow::Release()
{
	//-----------------------------------
    if(m_pVidWin)
		m_pVidWin->put_Visible(OAFALSE);
    if(m_pVidWin)
		m_pVidWin->put_Owner(NULL);
    if(m_pMediaControl)
		m_pMediaControl->Release();
    if(m_pVidWin)
		m_pVidWin->Release();
    if(m_pEvent)
		m_pEvent->Release();
    if(m_pGraph)
		m_pGraph->Release();

	m_pGraph			=NULL;
	m_pMediaControl		=NULL;
	m_pVidWin			=NULL;
	m_pEvent			=NULL;
	//-----------------------------------
}
///////////////////////////////////////
//
///////////////////////////////////////
bool CDirectShow::Stop()
{
	if(!m_pMediaControl)
		return false;

	HRESULT hr;

	hr = m_pMediaControl->Stop();

	if(hr!=S_OK)
	{
		DisplayErrorMessage("CDirectShow::Stop");
		DisplayDirectXErrorCode(hr);
		return FALSE;
	}

	return true;
}

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
Systems Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions