⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainmenu.cpp

📁 吃豆子游戏源码
💻 CPP
字号:
/**
 *	File	:	MainMenu.cpp
 *  Author	:	Kevin Lynx
 *	Date	:	2007/8/3
 */
#include "stdafx.h"
#include "MainMenu.h"
#include "App.h"
#include "ResourceMgr.h"
#include "credits/Credits.h"

MainMenu::MainMenu( IrrlichtDevice *device )
{
	mDevice = device;
}

MainMenu::~MainMenu()
{
	delete Credits::GetSingletonPtr(); 
}

bool	MainMenu::init()
{
	IGUIEnvironment *env = mDevice->getGUIEnvironment();
	IGUIButton *button;

	env->getSkin()->setFont( ResourceMgr::GetSingletonPtr()->mBigFont );
	env->getSkin()->setColor( EGDC_BUTTON_TEXT, SColor( 255, 255, 255, 10 ) );

	button = env->addButton( rect<s32>( 440, 130, 440 + 200, 130 + 50 ), 0, BTN_START_ID, L"Start" );
	setButtonPro( button );
	button = env->addButton( rect<s32>( 470, 210, 470 + 200, 210 + 50 ), 0, BTN_HELP_ID, L"Help" );
	setButtonPro( button );
	button = env->addButton( rect<s32>( 510, 290, 510 + 200, 290 + 50 ), 0, BTN_CREDITS_ID, L"Credit" );
	setButtonPro( button );
	button = env->addButton( rect<s32>( 550, 370, 550 + 200, 370 + 50 ), 0, BTN_EXIT_ID, L"Exit" );
	setButtonPro( button );

	button = env->addButton( rect<s32>( 360, 495, 360 + 200, 495 + 50 ), 0, BTN_HELP_BACK_ID, L"Back" );
	setButtonPro( button );

	button = env->addButton( rect<s32>( 310, 495, 310 + 200, 495 + 50 ), 0, BTN_CREDITS_BACK_ID, L"Back" );
	setButtonPro( button );

	mTV[0] = ResourceMgr::GetSingletonPtr()->mGUITV1;
	mTV[1] = ResourceMgr::GetSingletonPtr()->mGUITV2;
	mTV[2] = ResourceMgr::GetSingletonPtr()->mGUITV3;

	mTVNoise[0] = ResourceMgr::GetSingletonPtr()->mGUITV_Noise1;
	mTVNoise[1] = ResourceMgr::GetSingletonPtr()->mGUITV_Noise2;
	mTVNoise[2] = ResourceMgr::GetSingletonPtr()->mGUITV_Noise3;

	Credits *credits = new Credits( mDevice );
	credits->init();

	return true;
}

void	MainMenu::enter()
{
	mCurTV = rand() % TV_TEX_COUNT;
	mTVLastTime = App::GetSingletonPtr()->getRealTime();

	mCurTVNoise = 0;
	mTVNoiseLastTime = mTVLastTime;

	changeState( MENU_STATE_MAIN );
}

void	MainMenu::update( float dt )
{
	if( mState == MENU_STATE_MAIN )
	{
		u32 curTime = App::GetSingletonPtr()->getRealTime();

		if( curTime - mTVLastTime > TV_CHANGE_TIME )
		{
			mCurTV = rand() % TV_TEX_COUNT;
			mTVLastTime = curTime;
		}

		if( curTime - mTVNoiseLastTime > TVNOISE_CHANGE_TIME )
		{
			mCurTVNoise = ( mCurTVNoise + 1 ) % TVNOISE_TEX_COUNT;
			mTVNoiseLastTime = curTime;
		}
	}
	if( mState == MENU_STATE_CREDITS )
	{
		Credits::GetSingletonPtr()->update( dt ); 
	}
}

void	MainMenu::render()
{
	ResourceMgr *resMgr = ResourceMgr::GetSingletonPtr();
	IVideoDriver *driver = mDevice->getVideoDriver();

	if( mState == MENU_STATE_MAIN )
	{
		driver->draw2DImage( resMgr->mGUIMainBack, rect<s32>( 0, 0, 800, 600 ), rect<s32>(0,0,1024,768), 
			0, 0, true );
		driver->draw2DImage( mTV[mCurTV], rect<s32>( 193, 270, 230+193, 170+270 ), rect<s32>(0,0,256,192), 
			0, 0, true );
		driver->draw2DImage( mTVNoise[mCurTVNoise], rect<s32>( 193, 270, 230+193, 170+270 ), rect<s32>(0,0,128,128), 
			0, 0, true );
	}
	if( mState == MENU_STATE_HELP )
	{
		driver->draw2DImage( resMgr->mGUIHelp, position2d<s32>( 0, 0 ), rect<s32>(0,0,800,600), 0, 
			SColor( 255, 255, 255, 255 ) );
	}
	if( mState == MENU_STATE_CREDITS )
	{
		//driver->draw2DImage( resMgr->mGUIHelp, position2d<s32>( 0, 0 ), rect<s32>(0,0,800,600), 0, 
		//	SColor( 255, 255, 255, 255 ) );
		Credits::GetSingletonPtr()->render(); 
	}

}

void	MainMenu::setButtonPro( IGUIButton *button )
{
	ResourceMgr *resMgr = ResourceMgr::GetSingletonPtr(); 
	button->setImage( resMgr->mGUIButtonS, rect<s32>( 0, 0, 200, 50 ) );
	button->setPressedImage( resMgr->mGUIButtonP, rect<s32>( 0, 0, 200, 50 ) );
	button->setUseAlphaChannel( true );
	button->setDrawBorder( false );
}

bool	MainMenu::OnEvent( SEvent event )
{
	if( event.GUIEvent.EventType == EGET_BUTTON_CLICKED )
	{
		s32 id = event.GUIEvent.Caller->getID();

		if( id == BTN_START_ID )
		{
			App::GetSingletonPtr()->setGameState( App::GAME_STATE_NORMAL_PLAY ); 
		}

		if( id == BTN_EXIT_ID )
		{
			mDevice->closeDevice();
		}
		
		if( id == BTN_HELP_ID )
		{
			changeState( MENU_STATE_HELP );
		}

		if( id == BTN_CREDITS_ID )
		{
			changeState( MENU_STATE_CREDITS );
		}

		if( id == BTN_HELP_BACK_ID )
		{
			changeState( MENU_STATE_MAIN );
		}

		if( id == BTN_CREDITS_BACK_ID )
		{
			Credits::GetSingletonPtr()->leave();

			changeState( MENU_STATE_MAIN );
		}
		
	}

	return false;
}

void	MainMenu::changeState( int state )
{
	mState = state;
	
	IGUIElement *root = mDevice->getGUIEnvironment()->getRootGUIElement();
	IGUIElement *elem;
	bool bShow;

	if( mState == MENU_STATE_MAIN )
	{	
		elem = root->getElementFromId( BTN_HELP_BACK_ID );
		elem->setVisible( false );
		elem = root->getElementFromId( BTN_CREDITS_BACK_ID );
		elem->setVisible( false );
		bShow = true;
	}
	else if( mState == MENU_STATE_HELP )
	{
		elem = root->getElementFromId( BTN_HELP_BACK_ID );
		elem->setVisible( true );
		bShow = false;
	}
	else if( mState == MENU_STATE_CREDITS )
	{
		elem = root->getElementFromId( BTN_CREDITS_BACK_ID );
		elem->setVisible( true );
		bShow = false;

		Credits::GetSingletonPtr()->enter(); 
	}

#define SHOW(p) { if(p) p->setVisible(bShow);}
	

	elem = root->getElementFromId( BTN_START_ID );
	SHOW( elem );
	
	elem = root->getElementFromId( BTN_HELP_ID );
	SHOW( elem );
	
	elem = root->getElementFromId( BTN_CREDITS_ID );
	SHOW( elem );
	
	elem = root->getElementFromId( BTN_EXIT_ID );
	SHOW( elem );
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -