// // WowGameCocosManager.m // Runner // // Created by xiaoyu on 2024/4/16. // #import "WowGameCocosManager.h" #include "cocos2d.h" #import "CocosAppDelegate.h" #import @interface WowGameCocosManager () { cocos2d::Application *_app; } @end @implementation WowGameCocosManager static CocosAppDelegate cocosAppDelegate; + (instancetype)sharedManager { static dispatch_once_t onceToken; static WowGameCocosManager *_manager = nil; dispatch_once(&onceToken, ^{ _manager = [[WowGameCocosManager alloc] init]; [_manager setupCocosEnvironment]; }); return _manager; } - (void)setupCocosEnvironment { //Launching the app with the arguments -NSAllowsDefaultLineBreakStrategy NO to force back to the old behavior. if ( [[UIDevice currentDevice].systemVersion floatValue] >= 13.0f) { [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NSAllowsDefaultLineBreakStrategy"]; } cocos2d::Application *app = cocos2d::Application::getInstance(); app->initGLContextAttrs(); cocos2d::GLViewImpl::convertAttrs(); _app = app; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitGame) name:@"_kwowgamecocosexitgame" object:nil]; } - (void)exitGame { [self.gameViewController dismissViewControllerAnimated:YES completion:nil]; } static DMProgressHUD *hud; - (void)runGame:(NSInteger)gameid { UIWindow *window = UIApplication.sharedApplication.delegate.window; UIViewController *rootvc = window.rootViewController; hud = [DMProgressHUD showHUDAddedTo:window maskType:DMProgressHUDMaskTypeClear]; hud.loadingType = DMProgressHUDLoadingTypeIndicator;//默认 hud.text = @"Waiting..."; if ([rootvc isKindOfClass:WowGameViewController.class] || [rootvc.presentedViewController isKindOfClass:WowGameViewController.class]) { if ([NSThread isMainThread]) { [hud dismiss]; } else { dispatch_async(dispatch_get_main_queue(), ^{ [hud dismiss]; }); } return; } if (!self.gameViewController) { WowGameViewController *cocosvc = [[WowGameViewController alloc] init]; cocosvc.wantsFullScreenLayout = YES; self.gameViewController = cocosvc; cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView((__bridge void *)cocosvc.view); cocos2d::Director::getInstance()->setOpenGLView(glview); _app->run(); } else { cocos2d::Director::getInstance()->resume(); } if ([NSThread isMainThread]) { [hud dismiss]; } else { dispatch_async(dispatch_get_main_queue(), ^{ [hud dismiss]; }); } self.gameViewController.modalPresentationStyle = UIModalPresentationFullScreen; [rootvc presentViewController:self.gameViewController animated:YES completion:nil]; [self startGameWithID:gameid]; //// debug exit game // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // [self exitGame]; // }); } -(void)startGameWithID:(NSInteger)gameid { [self clearData]; cocos2d::Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] { // CCLOG("start gameid---> %d ", gameid); cocos2d::EventCustom evtEnter("start_gameid"); evtEnter.setUserData((void *) gameid); cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&evtEnter); }); } - (void)popGame { [self.gameViewController dismissViewControllerAnimated:YES completion:nil]; // [self cocosDidEnterBackground]; } - (void)clearData { cocos2d::SpriteFrameCache::getInstance()->removeSpriteFrames(); cocos2d::Director::getInstance()->getTextureCache()->removeAllTextures(); //清理搜索路径 std::vector searchPathArray; searchPathArray.push_back("res"); cocos2d::FileUtils::getInstance()->setSearchPaths(searchPathArray); } - (void)detoryCocosEnvironment { } - (void)cocosDidEnterBackground { cocos2d::Application::getInstance()->applicationDidEnterBackground(); } - (void)cocosWillEnterForeground { cocos2d::Application::getInstance()->applicationWillEnterForeground(); } @end