#include "splash.h"

CSplash::CSplash()
{
Init();
}

void CSplash::Init(void)
{
m_FadeSpeed = 1;

if(NULL != m_pPalette){
delete [] m_pPalette;
m_pPalette = NULL;
}
}

void CSplash::SetPalette(u16 *pPalette)
{
if(NULL != m_pPalette){
delete [] m_pPalette;
m_pPalette = NULL;
}

m_pPalette = pPalette;
ham_LoadBGPal((void*)m_pPalette, sizeof(m_pPalette)*sizeof(u16));
}

void CSplash::SetTexture(u8 *pTexture)
{
TOOL_DMA1_SET(pTexture,
MEM_BG_PTR,
SIZEOF_32BIT(*pTexture),
DMA_TRANSFER_32BIT,
DMA_STARTAT_NOW)
}

void CSplash::WaitFrames(u32 val)
{
u32 i=0;

while(i++ < val)
WaitVSync();
}

void CSplash::FadeOut(void)
{
u16 wait=0;

m_Counter = 0;

while(m_Counter++ < 32)
{
ham_FadePal(0, m_FadeSpeed);

this->WaitFrames(2500);
wait=0;
}
}

void CSplash::FadeIn(void)
{
u16 wait=0;
u8 Idx;
u16 val=0;
  
m_Counter = 0;

while(m_Counter++ < 32)
{
  val+=m_FadeSpeed;

  for(Idx=0; Idx < (sizeof(m_pPalette)*sizeof(u16)); Idx++)
  {
   u16 Color = (u16)(*(m_pPalette+Idx));
  
   u8 r = RGB_GET_R_VALUE(Color) >> 3;
   u8 g = RGB_GET_G_VALUE(Color) >> 3;
   u8 b = RGB_GET_B_VALUE(Color) >> 3;
  
if(val < r){ r = val; }
   if(val < g){ g = val; }
   if(val < b){ b = val; }
  
   ACCESS_16(MEM_PAL_COL_PTR(Idx)) = RGB_SET(r, g, b);
  }

this->WaitFrames(2500);
wait=0;
}
}


//**********************
// Function: WaitVSync()
// Purpose:
//**********************
void WaitVSync()
{
  while(F_VCNT_CURRENT_SCANLINE < 160)
  {
   // do nothing :p
  }

  return;
} // End of WaitVSync()


/* END OF FILE */