使用bink播放视频

// Video.h
#ifndef __VideoH__
#define __VideoH__
#include <d3d9.h>
#include <d3dx9.h>
#include <vector>
#include <fstream>
using namespace std;
#include "bink.h"
#pragma comment(lib, "binkw32.lib")

struct SVertexVideo
{
    D3DXVECTOR3 vPos;
    float fRHW;
    D3DXVECTOR2 vTex;
};
#define D3DFVF_VERTEXVIDEO (D3DFVF_XYZRHW | D3DFVF_TEX1)

struct SVideoItem
{
    char sFileName[50];
    HBINK hBink;
    IDirect3DTexture9* pTexture;
    float fTime;
};

class CVideo
{
public:
    IDirect3DDevice9* m_pDevice;
    IDirect3DVertexBuffer9* m_pVertexBuffer;
    vector<SVideoItem> m_vVideoItem;
    bool m_bRender;
    int m_nVideoIndex;
    float m_fCurrentTime;
public:
    void Init(IDirect3DDevice9* pDevice, char* sFileName)
    {
        // Init Device
        m_pDevice = pDevice;
        // Init Vertex Buffer
        m_pDevice->CreateVertexBuffer(4 * sizeof(SVertexVideo), D3DUSAGE_WRITEONLY, D3DFVF_VERTEXVIDEO, D3DPOOL_MANAGED, &m_pVertexBuffer, NULL);
        SVertexVideo* pV;
        m_pVertexBuffer->Lock(0, 0, (void**)&pV, 0);
        pV[0].fRHW = pV[1].fRHW = pV[2].fRHW = pV[3].fRHW = 1.0f;
        pV[0].vPos = D3DXVECTOR3(0.0f, 768.0, 0.0f);
        pV[1].vPos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
        pV[2].vPos = D3DXVECTOR3(1366.0f, 768.0, 0.0f);
        pV[3].vPos = D3DXVECTOR3(1366.0f, 0.0f, 0.0f);
        pV[0].vTex = D3DXVECTOR2(0.0f, 1.0f);
        pV[1].vTex = D3DXVECTOR2(0.0f, 0.0f);
        pV[2].vTex = D3DXVECTOR2(1.0f, 1.0f);
        pV[3].vTex = D3DXVECTOR2(1.0f, 0.0f);
        m_pVertexBuffer->Unlock();
        // Init Bink And Texture
        SVideoItem sVideoItem;
        ifstream infile;
        infile.open(sFileName, ios::in);
        while(true)
        {
            ZeroMemory(&sVideoItem, sizeof(SVideoItem));
            infile >> sVideoItem.sFileName;
            if(sVideoItem.sFileName[0] == 0) break;
            infile >> sVideoItem.fTime;
            sVideoItem.hBink = BinkOpen(sVideoItem.sFileName, BINKNOSKIP);
            if(sVideoItem.hBink == 0)
            {
                MessageBoxA(0, "BinkOpen() - FAILED", 0, 0);
                continue;
            }
            if(FAILED(D3DXCreateTexture(m_pDevice, sVideoItem.hBink->Width, sVideoItem.hBink->Height, 0, D3DUSAGE_DYNAMIC, D3DFMT_A8B8G8R8, D3DPOOL_DEFAULT, &sVideoItem.pTexture)))
            {
                MessageBoxA(0, "D3DXCreateTexture() - FAILED", 0, 0);
                continue;
            }
            m_vVideoItem.push_back(sVideoItem);
        }
    }
    void Logic(float fElapsedTime)
    {
        if(!m_bRender) return;
        m_fCurrentTime += fElapsedTime;
        if(m_fCurrentTime > m_vVideoItem[m_nVideoIndex].fTime)
        {
            m_bRender = false;
            return;
        }
        if(m_fCurrentTime > m_vVideoItem[m_nVideoIndex].fTime / m_vVideoItem[m_nVideoIndex].hBink->Frames * m_vVideoItem[m_nVideoIndex].hBink->LastFrameNum);
        {
            BinkDoFrame(m_vVideoItem[m_nVideoIndex].hBink);
            D3DLOCKED_RECT RectLocked;
            if(SUCCEEDED(m_vVideoItem[m_nVideoIndex].pTexture->LockRect(0, &RectLocked, NULL, D3DLOCK_DISCARD)))
            {
                BinkCopyToBuffer(m_vVideoItem[m_nVideoIndex].hBink, RectLocked.pBits, RectLocked.Pitch, m_vVideoItem[m_nVideoIndex].hBink->Height, 0, 0, BINKCOPYALL | BINKSURFACE32);
                m_vVideoItem[m_nVideoIndex].pTexture->UnlockRect(0);
            }
            BinkNextFrame(m_vVideoItem[m_nVideoIndex].hBink);
        }        
    }
    void Render(float fElapsedTime)
    {
        if(!m_bRender) return;
        m_pDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(SVertexVideo));
        m_pDevice->SetFVF(D3DFVF_VERTEXVIDEO);
        m_pDevice->SetTexture(0, m_vVideoItem[m_nVideoIndex].pTexture);
        m_pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
    }
    void Start(int nVideoIndex)
    {
        m_nVideoIndex = nVideoIndex;
        m_bRender = true;
        BinkGoto(m_vVideoItem[m_nVideoIndex].hBink, 0, 0);
        m_fCurrentTime = 0.0f;
    }
    void Stop()
    {
        m_bRender = false;
    }
};

#endif


 

posted on 2012-05-16 18:59  紫澜  阅读(995)  评论(1编辑  收藏  举报