Subversion Repositories spk

Rev

Blame | Last modification | View Log | RSS feed

//#include "stdafx.h"
#include "RenderTexture.h"

#include "../Hook/MyDirect3DDevice9.h"

CRenderTexture::CRenderTexture ( MyDirect3DDevice9 *device ) : CRenderObject ()
{
        m_texture = NULL;
}

CRenderTexture::~CRenderTexture ()
{
        if ( m_texture )
                m_texture->Release ();
}

void CRenderTexture::OpenTextureFile ( CyString filename )
{
        D3DXCreateTextureFromFile ( m_pDevice, filename.c_str(), &m_texture );
}

void CRenderTexture::ParseSetting ( COverlay *overlay, CyString cmd, CyString rest, CyString section )
{
        if ( (section == "MOUSEOVER") || (section == "ONCLICK") )
        {
                if ( cmd == "FRAME" )
                {
                        SMOFrame *c = new SMOFrame;
                        c->iFrame = rest.ToInt();
                        AddMouseEvent ( section, c );
                }
                else if ( cmd == "ANIMATION" )
                {
                        SMOAnimation *c = new SMOAnimation;
                        c->iAnimation = rest.ToInt();
                        AddMouseEvent ( section, c );
                }
                else
                        CRenderObject::ParseSetting ( overlay, cmd, rest, section );
        }
        else if ( section.Empty() )
        {
                if ( cmd == "FILE" )
                        OpenTextureFile ( rest );
                else
                        CRenderObject::ParseSetting ( overlay, cmd, rest, section );
        }
        else
                CRenderObject::ParseSetting ( overlay, cmd, rest, section );
}

void CRenderTexture::render ( MyDirect3DDevice9 *device, int x, int y, LPD3DXSPRITE sprite )
{
        if ( !m_texture )
                return;

        D3DXMATRIX mat;
        // render top left corner
        D3DXMatrixTransformation2D ( &mat, NULL, 0.0, &D3DXVECTOR2(1, 1), NULL, 0, &D3DXVECTOR2 ( (float)(x + m_iX), (float)(y + m_iY)) );
        sprite->SetTransform(&mat);

        short *c = m_iColour;

        LPDIRECT3DTEXTURE9  texture = m_texture;
        if ( m_bMouseOver )
        {
                for ( SMouseOver *mo = m_lMouseOver.First(); mo; mo = m_lMouseOver.Next() )
                {
                        if ( mo->iType == MO_COLOUR )
                                c = ((SMOColour *)mo)->iColour;
                        else if ( mo->iType == MO_TEXTURE )
                                texture = ((SMOTexture *)mo)->pTexture;
                }               
        }

        D3DCOLOR color = D3DCOLOR_RGBA ( c[0], c[1], c[2], m_iAlpha );
        sprite->Draw ( texture, NULL, NULL, NULL, color );
}

bool CRenderTexture::CheckMouse ( int x, int y, int offsetx, int offsety )
{
        if ( !m_texture )
                return false;

        int iX = offsetx + m_iX;
        int iY = offsety + m_iY;

        D3DSURFACE_DESC desc;
        m_texture->GetLevelDesc ( 0, &desc );
        
        if ( x < iX )
                return false;
        if ( y < iY )
                return false;
        if ( x > (iX + (long)desc.Width) )
                return false;
        if ( y > (iY + (long)desc.Height) )
                return false;

        return true;
}