//@STATE=嶌惉拞
//@DESCRIPTION ---------------------------------------------------------------
// FadeImpl.cpp: FadeImpl 僋儔僗偺僀儞僾儕儊儞僥乕僔儑儞
//  丂僼僃乕僪僀儞丒僼僃乕僪傾僂僩僔乕儞僋儔僗偱偡丅
//	棜楌:
// $Log: FadeImpl.cpp,v $
// Revision 1.1  2001/10/28 21:40:36  fujiwara
// DLL偵暘妱
//
// Revision 1.5  2001/07/09 20:30:08  fujiwara
// 椺奜張棟偺慻傒崬傒
//
// Revision 1.4  2001/07/01 21:30:33  fujiwara
// 儊儌儕儕乕僋僠僃僢僋尒捈偟
//
// Revision 1.3  2001/07/01 01:03:44  fujiwara
// const 偺尒捈偟
// 攝抲僄僨傿僞偼搑拞宱夁
//
// Revision 1.2  2001/05/12 00:50:33  fujiwara
// 俠倁俽傪摫擖偟丄儘僌昞帵傪捛壛
//
//	2001/01/29 儊儌儕乕儕乕僋専弌梡儔僀僽儔儕偺捛壛 
//@DESCRIPTION_END -----------------------------------------------------------
//@AUTHOR=S.F.
// Copyright (C) 2000 Satoshi Fujiwara. All Rights Reserved.
///////////////////////////////////////////////////////////////////////////////
#pragma warning( disable : 4786 )	//STL偺寈崘奜偟

// 儊儌儕乕儕乕僋専弌梡
#include "sfdebug.h"

// System Includes //
#include <stack>
#include <list>

// Project Includes //
#include "exception.h"
#include "system.h"
#include "sound.h"
#include "console.h"
#include "Obj.h"
#include "Obj2D.h"
#include "ObjRectangle.h"
#include "ObjQuadrangle.h"
#include "input.h"
#include "Scene.h"
#include "main.h"
#include "AbstractScene.h"

#include "FadeImpl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace sf::application::scene;

// 僐儞僗僩儔僋僞 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
FadeImpl::FadeImpl()
{
	mbMove = false;
	system::System* mpSystem = system::instance();
	mpSound = mpSystem->sound();

	mStartColor = (0xff000000);
	mEndColor = 0x00000000;
	mSpeed = 0.05f;
	mType = Fade::FADE_IN;
	mbFadeVolume = true;

	mFadeObj.x(system::console::viewport::X + (system::console::viewport::WIDTH/2.0f));
	mFadeObj.y(0.0f + (system::console::viewport::HEIGHT / 2.0f));
	mFadeObj.z(0.0f);
	mFadeObj.sprite(Maininstance()->getBlankSprite());
	mFadeObj.scalingX(25.0f);
	mFadeObj.scalingY(60.0f);
	mFadeObj.color(mStartColor);
	mFadeObj.visibility(true);
	mFadeObj.drawMode(system::console::DRAW_TRANS);

	initialize();
}// FadeImpl();

// 僨僗僩儔僋僞 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
FadeImpl::~FadeImpl()
{

}// FadeImpl()

// 弶婜壔張棟 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void FadeImpl::initialize(){
	
	if(mType == Fade::FADE_IN)
		mCurrent = 1.0f;
	else
		mCurrent = 0.0f;

	mEnd = (float)((mEndColor & 0xff000000) >> 24) / 256.0f; 

	float pitch_ = (mStartColor & 0xff000000) >> 24;
	
	// 彍嶼椺奜傪偝偗傞偨傔偺張抲
	if(!pitch_) pitch_ = 0.00001f;
	if(!mSpeed) mSpeed = 0.00001f;
	
	if(mbFadeVolume){
		mVolumePitch = ((float)system::sound::volume::MIN - (float)mpSound->volumeMax()) 
			/  (((float)pitch_) / (mSpeed * 256.0f));
		pitch_ = ((mStartColor - mEndColor) & 0xff000000) >> 24;
	
		if(!pitch_) pitch_ = 0.0001f;
		if(mType == Fade::FADE_IN){
			mVolumePitch = ((float)system::sound::volume::MIN- (float)mpSound->volumeMax()) 
				/  (((float)pitch_) / (mSpeed * 256.0f));
			mVolume = (float)system::sound::volume::MIN;
		} else {
			mVolumePitch = ((float)system::sound::volume::MIN - (float)mpSound->volumeMax()) 
				/  (((float)pitch_) / (mSpeed * 256.0f));
			mVolume = (float)mpSound->volumeMax();
		}
	}
}// initialize()

// FadeImpl憖嶌 ------------------------------------------------------------------- 
void FadeImpl::move(void)
{
	mbMove = true;
	mCurrent -= mSpeed;
	
	if(mbFadeVolume)
		mVolume -= mVolumePitch;
	
	DWORD color = (((DWORD)(256.0f * mCurrent)) << 24) + (mStartColor & 0xffffff);
		
	// 俤倃俬俿僉乕偑墴偝傟偨偐?
	if(mpInput->exit(true)){
		callSceneEvent(&Listener::exitKeyPressed);
		delete this;
		return;
	}

	// 廔椆僠僃僢僋 
	if((mCurrent <= mEnd && mType == Fade::FADE_IN) || 
		(mCurrent >= mEnd && mType == Fade::FADE_OUT) || mpInput->start(true))
	{
		callSceneEvent(&Listener::endOfScene);
		delete this;
		return;
	}


	mFadeObj.color(color);

	// 壒検 
	if(mbFadeVolume)
		mpSound->masterVolume((int)mVolume);

	return;
}// FadeImpl