#include "stdafx.h"
#include "Global.h"
#include "Explosion.h"
#include "DxWrapper.h"

Explosion::Explosion()
{
    LPDIRECT3DDEVICE9 d3ddev = DxWrapper::Instance()->d3ddev;

    CUSTOM_VERTEX_STRUCT initVert[] = 
    {
        { -EXPLOSION_IMAGE_RADIUS, EXPLOSION_IMAGE_RADIUS, 0.0f, D3DCOLOR_ARGB(255, 255, 255, 255), 0.25, 0, 0 },			
        { EXPLOSION_IMAGE_RADIUS, EXPLOSION_IMAGE_RADIUS, 0.0f, D3DCOLOR_ARGB(255, 255, 255, 255), 0, 0, 0 },	
        { -EXPLOSION_IMAGE_RADIUS, -EXPLOSION_IMAGE_RADIUS, 0.0f, D3DCOLOR_ARGB(255, 255, 255, 255), 0.25, 0.25, 0 },		
        { EXPLOSION_IMAGE_RADIUS, -EXPLOSION_IMAGE_RADIUS, 0.0f, D3DCOLOR_ARGB(255, 255, 255, 255), 0, 0.25, 0}	    
        
		        
    };

    SetCurrentDirectory(DxWrapper::Instance()->applicationBaseDirectory);
    SetCurrentDirectory(FILEPATH_RESOURCES);

    D3DXCreateTextureFromFileEx(d3ddev,    // the device pointer
        L"explode2.png",    // the file name
        D3DX_DEFAULT,    // default width
        D3DX_DEFAULT,    // default height
        D3DX_DEFAULT,    // no mip mapping
        NULL,    // regular usage
        D3DFMT_A8R8G8B8,                // 32-bit pixels with alpha
        D3DPOOL_MANAGED,                // typical memory handling
        D3DX_DEFAULT,                   // no filtering
        D3DX_DEFAULT,                   // no mip filtering
        D3DCOLOR_XRGB(255, 0, 255),    // the hot-pink color key
        NULL,                           // no image info struct
        NULL,                           // not using 256 colors
        &m_pFilmstrip);

    // create a vertex buffer interface called t_buffer
    d3ddev->CreateVertexBuffer(4 * sizeof(Explosion::CUSTOM_VERTEX_STRUCT),       // we need memory for 4 vertices
        0,						                                                    // special flags for advanced techniques
        Explosion::CUSTOM_FVF,
        D3DPOOL_MANAGED,			  // flag that the buffer will be located in Video RAM
        &t_buffer,				  // pointer to the vertex buffer interface we are creating; will be filled out by d3d
        NULL);					  // "reserved" and always NULL. So be it.


    VOID* pVoid;    // a void pointer

    // lock t_buffer and load the vertices into it
    t_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, initVert, sizeof(initVert));
    t_buffer->Unlock();

    m_Active = false;
};

Explosion::~Explosion()
{

};



void Explosion::Draw()
  {
    LPDIRECT3DDEVICE9 d3ddev = DxWrapper::Instance()->d3ddev;

    d3ddev->SetFVF(Explosion::CUSTOM_FVF);
    
    // select the vertex buffer to display
    d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(Explosion::CUSTOM_VERTEX_STRUCT));
    
    D3DXMATRIX matTranslate;                                                            // a matrix to store the translation for triangle A
    D3DXMatrixTranslation(&matTranslate, m_Position.x, m_Position.y, m_Position.z);     // build matrices to rotate and translate the model
    d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate));

    D3DXMATRIX matTranslateTexture;
    D3DXMatrixTranslation(&matTranslateTexture, m_FrameTranslationX, m_FrameTranslationY, 0);    
    
	   
	d3ddev->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );    // Set up the matrix for the desired transformation.
    d3ddev->SetTransform( D3DTS_TEXTURE0, &matTranslateTexture );

    DWORD formerLightingState;  // den alten Wert sichern
    d3ddev->GetRenderState(D3DRS_LIGHTING, &formerLightingState);
    
    d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);    // turn off the 3D lighting

    // keep the alpha blending in
    d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);    // turn on the color blending
    d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);    // set source factor
    d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);    // set dest factor
    d3ddev->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);    // set the operation

    d3ddev->SetTexture(0, m_pFilmstrip);    // set the texture (for all draw primitives from now on).Der erste Paramter ("Sampler") ist advanced, kommt später
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

    d3ddev->SetRenderState(D3DRS_LIGHTING, formerLightingState);    // den vorherigen Wert wiederherstellen
	
}

void Explosion::ExplodeAt( const CPos &position )
{
    m_Position = position;
    m_Active = true;
    m_AnimationTimer = 0;
}

void Explosion::Cycle( float deltaMs )
{
    if(m_Active)
    {
        int animationFrame = (int)(m_AnimationTimer / ANIMATION_FRAMETIME);
        int frameRowIndex = animationFrame / ANIMATION_FRAMES_COLS;
        int frameColIndex = animationFrame % ANIMATION_FRAMES_ROWS;
      
        m_FrameTranslationX = (1.0f / ANIMATION_FRAMES_COLS) * frameColIndex;                
        m_FrameTranslationY = (1.0f / ANIMATION_FRAMES_ROWS) * frameRowIndex;
        
        m_AnimationTimer += deltaMs;
        if (m_AnimationTimer >= ANIMATION_DURATION)
        {
            m_Active = false;
        }
    }
}