Files
wnmj/Classes/AppDelegate.cpp
2026-02-13 17:40:07 +08:00

212 lines
5.6 KiB
C++

#include "AppDelegate.h"
#include "MainScene.h"
#include "GameFrameBase.h"
#include "LogoScene.h"
#include "LogonScene.h"
#include "GlobalJosn.h"
#include "ChatScene.h"
#include "GameCreator.h"
//#include "WN_CreateScene.h"
#include "WN_GameScene.h"
#include "DDZ_GameScene.h"
#include "NN_GameScene.h"
#include "DZ_GameScene.h"
#include "13S_GameScene.h"
#include "ZJH_GameScene.h"
#include "PDK_GameScene.h"
USING_NS_CC;
/*******************************注册游戏****************************************/
// 游戏选项
struct tagGameItem
{
WORD wKindID;
GAME_CREATE_SELECTOR create;
GAME_CREATE_NODE createNode;
};
static tagGameItem s_GameList[] =
{
// 斗地主;
{ DDZ_KIND_ID, GAME_CREATE_SELECTOR(DDZ_SPACE::DDZGameScene::create), GAME_CREATE_NODE(nullptr) },
// 万年麻将;
{ WNMJ_SPACE::KIND_ID, GAME_CREATE_SELECTOR(WNMJ_SPACE::WN_GameScene::create), GAME_CREATE_NODE(nullptr) },
// 牛牛;
{ NN_KIND_ID, GAME_CREATE_SELECTOR(NiuNiu_SPACE::NNGameScene::create), GAME_CREATE_NODE(nullptr) },
// 打炸;
{ DZ_KIND_ID, GAME_CREATE_SELECTOR(DZ_SPACE::DZGameScene::create), GAME_CREATE_NODE(nullptr) },
// 十三水;
{ SSS_KIND_ID, GAME_CREATE_SELECTOR(SSS_SPACE::SSSGameScene::create), GAME_CREATE_NODE(nullptr) },
// 炸金花;
{ ZJH_KIND_ID, GAME_CREATE_SELECTOR(ZJH_SPACE::ZJHGameScene::create), GAME_CREATE_NODE(nullptr) },
// 跑得快;
{ PDK_KIND_ID, GAME_CREATE_SELECTOR(PDK_SPACE::PDKGameScene::create), GAME_CREATE_NODE(nullptr) },
};
/******************************************************************************/
AppDelegate::AppDelegate()
{
m_dispathMsgNode = NULL;
}
AppDelegate::~AppDelegate()
{
//// 释放资源;
//YvVoiceManager::GetInstance()->Cleanup();
if (m_dispathMsgNode != NULL)
{
m_dispathMsgNode->stopDispatch();
m_dispathMsgNode->release();
m_dispathMsgNode = NULL;
}
}
//if you want a different context,just modify the value of glContextAttrs
//it will takes effect on all platforms
void AppDelegate::initGLContextAttrs()
{
//set OpenGL context attributions,now can only set six attributions:
//red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
// If you want to use packages manager to install more packages,
// don't modify or remove this function
static int register_all_packages()
{
return 0; //flag for packages manager
}
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect("GameClient", Rect(0, 0, TARGET_WIDTH, TARGET_HEIGHT));
#else
glview = GLViewImpl::create("GameClient");
#endif
director->setOpenGLView(glview);
}
// turn on display FPS
director->setDisplayStats(false);
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0f / 60);
//设置资源搜索路径
//FileUtils::getInstance()->addSearchPath("UI/");
//FileUtils::getInstance()->addSearchPath("NCMJ/");
register_all_packages();
initGameConfig();
GlobalJosn::getInstance()->loadConfig();
//设置常亮不锁屏
Device::setKeepScreenOn(true);
// create a scene. it's an autorelease object
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32||CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
Scene *pScene = LogonScene::create();
srand(time(0));
// run
director->runWithScene(pScene);
#else
Scene *pScene = LogoScene::create();
if (pScene == nullptr)
{
pScene = LogonScene::create();
}
srand(time(0));
// run
director->runWithScene(pScene);
#endif
glview->setDesignResolutionSize(TARGET_WIDTH, TARGET_HEIGHT, ResolutionPolicy::EXACT_FIT);
if (m_dispathMsgNode == NULL)
{
m_dispathMsgNode = DispatchMsgNode::create();
m_dispathMsgNode->retain();
m_dispathMsgNode->startDispatch();
}
YvVoiceManager::GetInstance()->Init();
return true;
}
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
#if CC_TARGET_PLATFORM != CC_PLATFORM_WIN32
Director::getInstance()->stopAnimation();
Scene* pScene = Director::getInstance()->getRunningScene();
if(pScene!=nullptr && pScene->getTag()==SCENE_TAG_GAME)
{
//applicationDidEnterBackground();
}
#endif
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
//设置常亮不锁屏
Device::setKeepScreenOn(true);
Scene* pScene = Director::getInstance()->getRunningScene();
if (pScene != nullptr)
{
int nTag = pScene->getTag();
if ( nTag == SCENE_TAG_GAME )
{
GameFrameBase* pMJScene = (GameFrameBase*)pScene;
pMJScene->ReconnectServer();
}
else if ( nTag == SCENE_TAG_MAIN )
{
MainScene* pMainScene = (MainScene*)pScene;
pMainScene->appWillEnterForeground();
}
}
#endif
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
void AppDelegate::initGameConfig()
{
// 注册游戏
int gameCount = sizeof(s_GameList) / sizeof(tagGameItem);
for (int i = 0; i < gameCount; i++)
{
CGameCreator::getInstance()->addGame(s_GameList[i].wKindID, CGameCreator::PRIVATE, s_GameList[i].create, s_GameList[i].createNode);
}
}