#include "SM_CommonFXPCH.h"
#include "SM_Engine3DPCH.h"
#include "SM_DemoEffect.h"
#include "SM_AviDecoder.h"


class AviVideoFX : public SM_DemoEffect
{
public:              
  AviVideoFX(char const* pcName) : SM_DemoEffect(pcName)
  {
    m_iTexture = -1;
  }

  virtual          ~AviVideoFX()
  {
  }

  int      Init(const char* pcCommand)
  {
    char* pcCopy =0;
    char* pcToken;
    int   iReturn=-1;
    
    pcCopy=new char[strlen(pcCommand)+1];
    strcpy(pcCopy, pcCommand);

    pcToken=strtok(pcCopy, " \t");

    if (pcToken)
    if (strcmp(pcToken, "FILL THIS UP")==0)
    {
    }

    char pcPath[1024]="data\\videos\\";
    strcat(pcPath, pcCommand);

    if (m_Avi.Init(pcPath) != 0 )
    {
      goto FAILED;
    }

    m_iTexture = ResourceManager::CreateTexture(512, 512, D3DFMT_X8R8G8B8, 1);

    iReturn  = 0;
  
    
FAILED:
    if (pcCopy) { delete[] pcCopy; pcCopy=0; }

    return iReturn;        
  }

  int      Shutdown()
  {
    if (m_iTexture != -1)
    {
      ResourceManager::ReleaseTexture(m_iTexture);
      m_iTexture = -1;
    }
    m_Avi.Shutdown();
    return (0);
  }

  int      Start(float fTime)
  {
    return (0);
  }

  int      Stop()
  {
    return (0);
  }

  int      Reset()
  {
    return 0;
  }

  int      Run(float fTime)
  {
    static int kk = 0;

    kk++;
    kk%=50;
    m_Avi.GetFrame(kk);

    return 1;
  }

  int      Command           (float fTime, const char* pcCommand)
  {
    SM_DemoEffect::Command(fTime, pcCommand);
    char* pcCopy =0;
    char* pcToken;
    int   iReturn=-1;
    
    pcCopy=new char[strlen(pcCommand)+1];
    strcpy(pcCopy, pcCommand);

    pcToken=strtok(pcCopy, " \t");

    if (pcToken)
    if (strcmp(pcToken, "FILL THIS UP")==0)
    {
    }

    iReturn  = 0;
    
    if (pcCopy) { delete[] pcCopy; pcCopy=0; }
    return iReturn;
  }  


  SM_AviDecoder m_Avi;
  float         m_fStartTime;
  int           m_iTexture;
};

AviVideoFX Video1("VIDEO1");
AviVideoFX Video2("VIDEO2");

DEFINE_EFFECT(AviVideoFX)

