//----------------------------------------------------------------------------
//  File: Movie.cpp
//
//  Desc:   DirectShow sample code
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------

#include "stdafx.h"
#include "project.h"
#include <wmsdk.h>

BOOL IsFileASavedFG( WCHAR* FileName );


//
//  Implementation of CMovie
//

//-------------------------------------------------------------------------
//  CMovie constructor
//-------------------------------------------------------------------------
CMovie::CMovie() :
    m_MediaEvent(NULL),
    m_TimeFormat(TIME_FORMAT_MEDIA_TIME),
    m_dwUserID(NULL),
    m_lpDDTexture(NULL),
    m_Bf(NULL),
    m_Fg(NULL),
    m_Gb(NULL),
    m_Mc(NULL),
    m_Ms(NULL),
    m_Me(NULL),
    m_bInitialized( false),
    m_pAP(NULL),
    m_SAN(NULL),
    m_bDirectedFlips(FALSE),
    m_bAlpha(0x7F),
    m_bUseInTheScene(FALSE),
    m_bDelete(FALSE),
    m_bPresented(FALSE)

{
    ZeroMemory( m_achPath, sizeof(m_achPath));
    ZeroMemory( &m_rcSrc, sizeof(RECT));
    ZeroMemory( &m_rcDst, sizeof(RECT));
    ZeroMemory( &m_VideoSize, sizeof(SIZE));
    ZeroMemory( &m_VideoAR, sizeof(SIZE));

    ZeroMemory( m_Vdef, 4 * sizeof(Vertex) );
    ZeroMemory( m_Vcur, 4 * sizeof(Vertex) );

}

//-------------------------------------------------------------------------
//  ~CMovie destructor
//-------------------------------------------------------------------------
CMovie::~CMovie()
{
    Release();
}

//-------------------------------------------------------------------------
//  Release
//  Unadvises custom AP and stops the movie
//-------------------------------------------------------------------------
void CMovie::Release()
{
	try
	{
		if( m_SAN)
		{
			m_SAN->AdviseSurfaceAllocator(m_dwUserID, NULL);
		}
		if( m_Mc )
		{
			m_Mc->Stop();
		}
		SAFERELEASE( m_pAP );
		m_bInitialized = false;
	}
	catch(...)
	{}
}

// configuring functions

//-------------------------------------------------------------------------
//  Initialize
//  sets movie parameters
//-------------------------------------------------------------------------
void CMovie::Initialize( sMovieInfo * pMovInf, CMultiSAP * pSAP)
{
    ASSERT( pMovInf );
    ASSERT( pSAP );

    m_pAP = pSAP;
    m_pAP->AddRef();

    wcscpy(m_achPath, pMovInf->achPath);
    m_dwUserID = pMovInf->pdwUserID;
    m_bInitialized = true;
}


//-------------------------------------------------------------------------
//  OpenMovie
//  Creates filter graph for specified movie, advises custom AP and render media source
//-------------------------------------------------------------------------
HRESULT CMovie::OpenMovie()
{
    CComPtr<IUnknown>   pUnk;
    HRESULT             hres;
    WCHAR               FileName[MAX_PATH];
    CComPtr<IObjectWithSite> pObjWithSite;

    wcsncpy(FileName, m_achPath, NUMELMS(FileName));
/*
    hres = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    if(hres == S_FALSE)
    {
        CoUninitialize();
    }
*/
    hres = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
                            IID_IUnknown, (LPVOID *)&pUnk);

//	IUnknown *punkCert;
//	HRESULT hr = WMCreateCertificate(&punkCert);

    if(SUCCEEDED(hres))
    {
        hres = pUnk->QueryInterface(IID_IFilterGraph, (LPVOID *)&m_Fg);
        if(FAILED(hres))
        {
            pUnk = NULL;
            return hres;
        }

        // hook verifier to the filter graph to prevent unwanted Video Renderers
        // and have control over whether graph utilizes our customized VMR
        hres = m_Fg->QueryInterface(IID_IObjectWithSite, (LPVOID*)&pObjWithSite);
        if( SUCCEEDED( hres ) && m_pAP)
        {
            hres = pObjWithSite->SetSite( (IAMGraphBuilderCallback*)(this->m_pAP));
//			hres = pObjWithSite->SetSite( punkCert );
        }

        hres = AddVideoMixingRendererToFG();
        if(FAILED(hres))
        {
            pObjWithSite = NULL;
            m_Fg = NULL;
            return hres;
        }

        hres = pUnk->QueryInterface(IID_IGraphBuilder, (LPVOID *)&m_Gb);
        if(FAILED(hres))
        {
            if( m_SAN )
            {
                m_SAN->AdviseSurfaceAllocator(m_dwUserID, NULL);
            }
            pObjWithSite = NULL;
            pUnk = NULL;
            m_Gb = NULL;
            return hres;
        }

        hres = m_Gb->RenderFile(FileName, NULL);
        if(FAILED(hres) || VFW_E_NOT_CONNECTED == CheckVMRConnection())
        {
			IEnumFilters *pEnum = NULL;
			HRESULT hr = m_Gb->EnumFilters(&pEnum);
			if (SUCCEEDED(hr))
			{
				IBaseFilter *pFilter = NULL;
				CComPtr<IGraphConfig>              GC; 
				m_Gb->QueryInterface(IID_IGraphConfig, (LPVOID *)&GC);
				while (S_OK == pEnum->Next(1, &pFilter, NULL))
				{
					// Remove the filter.
					//pVideoObject->m_Gb->RemoveFilter(pFilter);
					GC->RemoveFilterEx(pFilter, 0);
					// Reset the enumerator.
					pEnum->Reset();
					pFilter->Release();
				}
				pEnum->Release();
			}
			//m_SAN->AdviseSurfaceAllocator(m_dwUserID, NULL);

			m_Gb = NULL;
			m_Bf = NULL;
			m_Fg = NULL;
			m_SAN = NULL;
			m_pAP = NULL;

			m_lpDDTexture = NULL ;
			m_lpDDDecode = NULL;

			if( SUCCEEDED(hres))
			{
				hres = VFW_E_NOT_CONNECTED;
			}

            return hres;
        }

        // if we managed to build the graph, we do not need verifier anymore
        hres = pObjWithSite->SetSite( NULL );
        pObjWithSite = NULL;

        hres = pUnk->QueryInterface(IID_IMediaControl, (LPVOID *)&m_Mc);
        if(FAILED(hres))
        {
            if( m_SAN )
            {
                m_SAN->AdviseSurfaceAllocator(m_dwUserID, NULL);
            }
            pUnk = NULL;
            m_Fg = NULL;
            m_Gb = NULL;
            return hres;
        }

        //
        // Not being able to get the IMediaEvent interface does not
        // necessarily mean that we can't play the graph.
        //
        pUnk->QueryInterface(IID_IMediaEvent, (LPVOID *)&m_Me);
        pUnk->QueryInterface(IID_IMediaSeeking, (LPVOID *)&m_Ms);

        m_llDuration = 0L;
        if( m_Ms )
        {
            m_Ms->GetDuration( &m_llDuration );
        }

        //GetMovieEventHandle();
        //pUnk = NULL;

        return S_OK;

    } // if FilterGraph was coCreated successfully
    else
    {
        m_Fg = NULL;
    }

    return hres;
}

//-------------------------------------------------------------------------
//  AddVideoMixingRendererToFG
//  creates instance of VMR, configures it, and advises custom AP
//-------------------------------------------------------------------------
HRESULT CMovie::AddVideoMixingRendererToFG()
{
    HRESULT hRes = CoCreateInstance(CLSID_VideoMixingRenderer, NULL, CLSCTX_INPROC_SERVER,
                                    IID_IBaseFilter, (LPVOID *)&m_Bf);

    if(SUCCEEDED(hRes))
    {
        WCHAR wcFilterName[MAX_PATH];
        swprintf( wcFilterName, L"VMR for VMRMulti 0x%08x\0", m_dwUserID);

        hRes = m_Fg->AddFilter(m_Bf, wcFilterName);

        if(SUCCEEDED(hRes))
        {
            IVMRFilterConfig* pConfig = NULL;
            hRes = m_Bf->QueryInterface(__uuidof(IVMRFilterConfig), (LPVOID *)&pConfig);

			/*hRes = m_Bf->QueryInterface(IID_IVMRMixerBitmap, (void**)&m_pAP->m_pBMP);
			if(SUCCEEDED(hRes))
			{
			}*/

            if(SUCCEEDED(hRes))
            {
                pConfig->SetRenderingMode(VMRMode_Renderless);
                pConfig->SetNumberOfStreams(1);
                pConfig->Release();
            }

			if(SUCCEEDED(hRes))
            {
                hRes = m_Bf->QueryInterface(__uuidof(IVMRSurfaceAllocatorNotify),
                                          (LPVOID *)&m_SAN);
            }

            if(SUCCEEDED(hRes))
            {
                // IMPORTANT: this is the moment we advise custom AP
                hRes = m_SAN->AdviseSurfaceAllocator(m_dwUserID, m_pAP);
            }

            if(SUCCEEDED(hRes))
            {
                hRes = m_SAN->SetDDrawDevice(m_pAP->GetDDObject(), m_pAP->GetMonitor());
            }
            if(SUCCEEDED(hRes))
            {
                hRes = m_SAN->SetBorderColor(NULL);
            }
        }
    }

    if(FAILED(hRes))
    {
        if(m_SAN)
        {
            m_SAN = NULL;
        }
    }

    return hRes;
}

//-------------------------------------------------------------------------
//  CheckVMRConnection
//-------------------------------------------------------------------------
HRESULT CMovie::CheckVMRConnection()
{
    HRESULT hr = S_OK;
    CComPtr<IEnumPins> pEnum;
    CComPtr<IPin> pPin;
    bool bConnected = false;

    if( !m_Bf )
        return E_UNEXPECTED;

    hr = m_Bf->EnumPins( &pEnum );
    if( FAILED(hr ))
    {
        return hr;
    }
    hr = pEnum->Next(1, &pPin, NULL);
    while( SUCCEEDED(hr) && pPin )
    {
        CComPtr<IPin> pPinUpstream;
        hr = pPin->ConnectedTo( &pPinUpstream );
        if( S_OK == hr )
        {
            bConnected = true;
            break;
        }
        pPin = NULL;
        hr = pEnum->Next(1, &pPin, NULL);
    }// while
    if( false == bConnected)
    {
        return VFW_E_NOT_CONNECTED;
    }
    return  S_OK;
}

//-------------------------------------------------------------------------
//  CloseMovie
//-------------------------------------------------------------------------
HRESULT CMovie::CloseMovie()
{
    if (m_Mc) 
    {
        if (m_Me) 
        {
            m_MediaEvent = NULL;
            m_Me = NULL;
        }

        if (m_Ms) 
        {
            m_Ms = NULL;
        }

        m_Mc = NULL;

        if (m_Gb) 
        {
            m_Gb = NULL;
        }

        if (m_Fg) 
        {
            m_Fg = NULL;
        }
    }
    return S_OK;
}

// command functions

//-------------------------------------------------------------------------
//  PlayMovie
//  Calls IMediaControl::Run() method; if we are at the end of the stream,
//  we reset it to the beginning
//-------------------------------------------------------------------------
HRESULT CMovie::PlayMovie(BOOL bPause)
{
    HRESULT hr = S_OK;
//  LONGLONG llCur = 0L;
//  LONGLONG llD = 0L;

    if( !m_Mc )
        return E_POINTER;

    if( m_Ms && !bPause )
    {
//       hr = m_Ms->GetCurrentPosition( &llCur);
//       hr = m_Ms->GetDuration( &llD);
//       if( llCur >= llD )
//       {
		LONGLONG llStart = 0L;
		hr = m_Ms->SetPositions(&llStart, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning);
//       }
    }

    hr = m_Mc->Run();
    return hr;
}

//-------------------------------------------------------------------------
//  PauseMovie
//-------------------------------------------------------------------------
HRESULT CMovie::PauseMovie()
{
    HRESULT hr = S_OK;

    if( !m_Mc )
        return E_POINTER;

    hr = m_Mc->Pause();
    return hr;
}

//-------------------------------------------------------------------------
//  StopMovie
//-------------------------------------------------------------------------
HRESULT CMovie::StopMovie()
{
    HRESULT hr = S_OK;

    if( !m_Mc )
        return E_POINTER;

    hr = m_Mc->Stop();
    return hr;
}

//-------------------------------------------------------------------------
//  SeekToPosition
//  seeks to specified media position
//-------------------------------------------------------------------------
BOOL CMovie::SeekToPosition(REFTIME rt, BOOL bFlushData)
{
    HRESULT hr=S_OK;
    LONGLONG llTime = LONGLONG( m_TimeFormat == TIME_FORMAT_MEDIA_TIME ? rt * double(UNITS) : rt );

    if (m_Ms != NULL) 
    {
        FILTER_STATE fs;

        m_Mc->GetState(100, (OAFilterState *)&fs);
        m_Ms->SetPositions(&llTime, AM_SEEKING_AbsolutePositioning, NULL, 0);

        // This gets new data through to the renderers
        if (fs == State_Stopped && bFlushData)
        {
            m_Mc->Pause();
            hr = m_Mc->GetState(INFINITE, (OAFilterState *)&fs);
            m_Mc->Stop();
        }

        if (SUCCEEDED(hr)) 
        {
            return TRUE;
        }
    }
    return FALSE;
}

// "get" functions

//-------------------------------------------------------------------------
//  GetDuration
//  Returns the duration of the current movie
//-------------------------------------------------------------------------
REFTIME CMovie::GetDuration()
{
	if( !m_Ms )
		return 0;

    HRESULT hr;
    LONGLONG Duration;

    if (m_TimeFormat != TIME_FORMAT_MEDIA_TIME) 
    {
        hr = m_Ms->GetDuration(&Duration);
        if (SUCCEEDED(hr)) 
        {
            return double(Duration);
        }
    } 
    else if (m_Ms != NULL) 
    {
        hr = m_Ms->GetDuration(&Duration);
        if (SUCCEEDED(hr)) 
        {
            return double(Duration) / UNITS;
        }
    }

    return 0;
}

//-------------------------------------------------------------------------
//  GetMovieEventHandle
//  returns result of IMediaEvent::GetEventHandle()
//-------------------------------------------------------------------------
HANDLE CMovie::GetMovieEventHandle()
{
    HRESULT     hr;

    if(m_Me != NULL)
    {
        if(m_MediaEvent == NULL)
        {
            hr = m_Me->GetEventHandle((OAEVENT *)&m_MediaEvent);
        }
    }
    else
    {
        m_MediaEvent = NULL;
    }

    return m_MediaEvent;
}

//-------------------------------------------------------------------------
//  GetMovieEventCode
//  returns output of IMediaEvent::GetEvent()
//-------------------------------------------------------------------------
long CMovie::GetMovieEventCode()
{
    HRESULT  hr;
    long     lEventCode;
    LONG_PTR lParam1, lParam2;

    if (m_Me != NULL) 
    {
        hr = m_Me->GetEvent(&lEventCode, &lParam1, &lParam2, 0);
        if (SUCCEEDED(hr))
        {
            m_Me->FreeEventParams(lEventCode, lParam1, lParam2);
            return lEventCode;
        }
    }

    return 0L;
}

//-------------------------------------------------------------------------
//  GetCurrentPosition
//  returns current media position, in REFTIME
//-------------------------------------------------------------------------
REFTIME CMovie::GetCurrentPosition()
{
    REFTIME rt = (REFTIME)0;
    HRESULT hr;
    LONGLONG Position;

    if (m_TimeFormat != TIME_FORMAT_MEDIA_TIME) 
    {
        hr = m_Ms->GetPositions(&Position, NULL);
        if (SUCCEEDED(hr)) 
        {
            return double(Position);
        }
    } 
    else if (m_Ms != NULL) 
    {
        hr = m_Ms->GetPositions(&Position, NULL);
        if (SUCCEEDED(hr)) 
        {
            return double(Position) / UNITS;
        }
    }

    return rt;
}


//-------------------------------------------------------------------------
//  GetStateMovie
//  returns state of IMediaControl of the movie (running, paused, or stopped)
//-------------------------------------------------------------------------
OAFilterState CMovie::GetStateMovie()
{
    OAFilterState state = State_Running;

    if( m_Mc )
    {
        m_Mc->GetState(3, &state);
    }

    return state;
}

// helper functions

//-------------------------------------------------------------------------
//  FindInterfaceFromFilterGraph
//
//  Useful function for finding specific filters - not 100% robust though
//-------------------------------------------------------------------------
HRESULT CMovie::FindInterfaceFromFilterGraph(   REFIID iid, // interface to look for
                                                LPVOID *lp )// place to return interface pointer in
{
    IEnumFilters*   pEF;
    IBaseFilter*    pFilter;

    // Grab an enumerator for the filter graph.
    HRESULT hr = m_Fg->EnumFilters(&pEF);

    if (FAILED(hr)) 
        return hr;

    // Check out each filter.
    while (pEF->Next(1, &pFilter, NULL) == S_OK)
    {
        hr = pFilter->QueryInterface(iid, lp);
        pFilter->Release();

        if (SUCCEEDED(hr)) 
        {
            break;
        }
    }

    pEF->Release();

    return hr;
}

BOOL CMovie::GetNativeMovieSize(LONG *cx, LONG *cy)
{
	return TRUE;
}

//-------------------------------------------------------------------------
//  IsFileASavedFG
//  Checks if the requested media source is a saved GraphEdit datafile
//-------------------------------------------------------------------------
BOOL IsFileASavedFG( WCHAR* FileName )
{
    BOOL fOK = FALSE;
    HANDLE hf = CreateFileW(FileName, GENERIC_READ, FILE_SHARE_READ,
                            NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
                            NULL);

    if (hf != INVALID_HANDLE_VALUE) 
    {
        DWORD data[2];
        DWORD dwRead;

        if (ReadFile(hf, data, sizeof(data), &dwRead, NULL) &&
            dwRead == sizeof(data)) 
        {
            if (data[0] == 0xe011cfd0 && data[1] == 0xe11ab1a1) 
            {
                fOK = TRUE;
            }

        }
        CloseHandle(hf);
    }

    return fOK;
}

//
// CMovieList class implementation
//

//-------------------------------------------------------------------------
//  constructor
//-------------------------------------------------------------------------
CMovieList::CMovieList()
    : m_nsize(0)
    , m_dwSelectedMovieID(NULL)
{
    ZeroMemory( m_ppMovies, MaxNumberOfMovies * sizeof(CMovie*) );
    m_rcDefaultTarget.left = m_rcDefaultTarget.top = 0;
    m_rcDefaultTarget.right = 1024;
    m_rcDefaultTarget.bottom = 768;
}

//-------------------------------------------------------------------------
//  destructor
//-------------------------------------------------------------------------
CMovieList::~CMovieList()
{
}

//-------------------------------------------------------------------------
//  GetDefaultTarget
//  All the coordinate computation is performed for a fixed render target 
//  rectangle; At the moment of presenting image on the screen, 
//  coordinates are simly scaled according to current video window size
//-------------------------------------------------------------------------
RECT CMovieList::GetDefaultTarget()
{
    return m_rcDefaultTarget;
}

//-------------------------------------------------------------------------
//  SetDefaultTarget
//  All the coordinate computation is performed for a fixed render target 
//  rectangle; At the moment of presenting image on the screen, 
//  coordinates are simly scaled according to current video window size
//-------------------------------------------------------------------------
void CMovieList::SetDefaultTarget( RECT rcDT)
{
    m_rcDefaultTarget = rcDT;
}

//-------------------------------------------------------------------------
//  GetMovie
//  returns pointer to the movie with specified dwUserID, or NULL if such movie
//  is not in the list
//-------------------------------------------------------------------------
CMovie * CMovieList::GetMovie( DWORD_PTR dwID )
{
    CMovie *pres = NULL;

    for( int i=0; i<m_nsize; i++)
    {
        if( m_ppMovies[i]->m_dwUserID == dwID )
        {
            pres = m_ppMovies[i];
            break;
        }
    }

    return pres;
}

//-------------------------------------------------------------------------
//  GetMovieByIndex
//  returns pointer to the movie that is n'th in the List, or NULL otherwise
//-------------------------------------------------------------------------
CMovie * CMovieList::GetMovieByIndex( int n )
{
    CMovie *pres = NULL;

    if( n > -1 && n < m_nsize )
    {
        pres = m_ppMovies[n];
    }
    
    return pres;
}

//-------------------------------------------------------------------------
//  GetSelectedMovie
//  returns pointer to the movie that is selected as "main channel"
//-------------------------------------------------------------------------
CMovie* CMovieList::GetSelectedMovie()
{ 
    CMovie *pres = NULL;

    for( int i=0; i<m_nsize; i++)
    {
        if( m_dwSelectedMovieID == m_ppMovies[i]->m_dwUserID )
        {
            pres = m_ppMovies[i];
            break;
        }
    }

    return pres; 
}

//-------------------------------------------------------------------------
//  GetMovieFromRTPoint
//  returns pointer to the movie that contains point (xRT, yRT) in 
//  render target rectangle (See class declaration if not sure what is it)
//-------------------------------------------------------------------------
CMovie * CMovieList::GetMovieFromRTPoint( float xRT, float yRT)
{
    CMovie *pmovie = NULL;

    if( !( m_rcDefaultTarget.left <= xRT && xRT <= m_rcDefaultTarget.right ) ||
        !( m_rcDefaultTarget.top <= yRT && yRT <= m_rcDefaultTarget.bottom ) )
    {
        return NULL;
    }

    for( int i=0; i<m_nsize; i++)
    {
        // check if the requested point is within any of CMovie::m_Vcur
        if( m_ppMovies[i]->m_Vcur[0].x <= xRT && xRT <= m_ppMovies[i]->m_Vcur[1].x &&
            m_ppMovies[i]->m_Vcur[0].y <= yRT && yRT <= m_ppMovies[i]->m_Vcur[2].y )
        {
            pmovie = m_ppMovies[i];
            break;
        }
    }

    return pmovie;
}

//-------------------------------------------------------------------------
//  GetSelectedMovieID
//  returns dwUserID of the selected movie
//-------------------------------------------------------------------------
DWORD_PTR CMovieList::GetSelectedMovieID()
{
    return m_dwSelectedMovieID;
}

//-------------------------------------------------------------------------
//  SelectMovie
//  changes 
//-------------------------------------------------------------------------
BOOL CMovieList::SelectMovie( DWORD_PTR pdwID)
{
    CMovie *pres = NULL;

    pres = GetMovie( pdwID);
    if( !pres )
    {
        return FALSE;
    }
    else
    {
        m_dwSelectedMovieID = (DWORD) pres->m_dwUserID;
    }

    return TRUE;
}

//-------------------------------------------------------------------------
//  Add
//  adds new movie to the list
//-------------------------------------------------------------------------
BOOL CMovieList::Add( CMovie *pmovie)
{
 /*   if( m_nsize >= MaxNumberOfMovies )
    {
        return FALSE;
    }
*/
    m_ppMovies[m_nsize++] = pmovie;
    if( 1 == m_nsize ) // first movie becomes selected by default
    {
        m_dwSelectedMovieID = (DWORD) pmovie->m_dwUserID;
    }

    return TRUE;
}

//-------------------------------------------------------------------------
//  Delete
//  deletes movie from the list
//-------------------------------------------------------------------------
BOOL CMovieList::Delete( DWORD_PTR dwID)
{
    CMovie * pres = NULL;
    BOOL bRes = FALSE;
    int nIndex = -1;
    
    for( int i=0; i<m_nsize; i++)
    {
        if( dwID == m_ppMovies[i]->m_dwUserID )
        {
            pres = m_ppMovies[i];
            nIndex = i;
            break;
        }
    }
    
    if( pres )
    {
        if( m_nsize == 1 )
        {
            m_dwSelectedMovieID = NULL;
        }
        else if( pres->m_dwUserID == m_dwSelectedMovieID )
        {
            if( nIndex == m_nsize-1 )
            {
                m_dwSelectedMovieID = (DWORD) m_ppMovies[0]->m_dwUserID;
            }
            else
            {
                m_dwSelectedMovieID = (DWORD) m_ppMovies[nIndex+1]->m_dwUserID;
            }
        }
        
      //  delete pres;
        
        for( int i = nIndex; i < m_nsize - 1; i++)
        {
            m_ppMovies[i] = m_ppMovies[i+1];
        }

        m_ppMovies[m_nsize-1] = NULL;
        m_nsize--;
        bRes = TRUE;
    }
    
    return bRes;
}

//-------------------------------------------------------------------------
//  SortByZ
//  sorts movies by z-order
//-------------------------------------------------------------------------
void CMovieList::SortByZ()
{
    int i;
    int j;
    CMovie *pswap;

    for( i=0; i<m_nsize-1; i++)
    {
        for( j=0; j<m_nsize-i-1; j++)
        {
            if( m_ppMovies[j]->m_fZ > m_ppMovies[j+1]->m_fZ )
            {
                pswap = m_ppMovies[j+1];
                m_ppMovies[j+1] = m_ppMovies[j];
                m_ppMovies[j] = pswap;
            }
        }
    }
}

//-------------------------------------------------------------------------
//  ActivateAll
//  sets flag m_bUseInTheScene for all movies in the list
//-------------------------------------------------------------------------
void CMovieList::ActivateAll()
{
    for( int i=0; i<m_nsize; i++)
    {
        m_ppMovies[i]->m_bUseInTheScene = TRUE;
    }
}

//-------------------------------------------------------------------------
//  RemoveDeletedMovies
//  deletes all the movies singed to be deleted
//-------------------------------------------------------------------------
void CMovieList::RemoveDeletedMovies()
{
    bool bContinue = true;

    while( bContinue )
    {
        bContinue = false;
        for( int i=0; i<m_nsize; i++)
        {
            if( m_ppMovies[i]->m_bDelete )
            {
                bContinue = true;
                Delete( m_ppMovies[i]->m_dwUserID);
                break;
            }
        }
    }
}


