Blame view

ios/Runner/Wowgame/WowGameClasses/WowGameCocosManager.mm 4.24 KB
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
1
2
3
4
5
6
7
8
9
  //
  //  WowGameCocosManager.m
  //  Runner
  //
  //  Created by xiaoyu on 2024/4/16.
  //
  
  #import "WowGameCocosManager.h"
  
68a4c50a   xiaoyu   Merge remote-trac...
10
11
  #include "cocos2d.h"
  
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
12
  #import "CocosAppDelegate.h"
22495953   xiaoyu   打开多点触控,开启游戏时使用hud遮罩
13
  #import <DMProgressHUD.h>
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  
  @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;
68a4c50a   xiaoyu   Merge remote-trac...
46
47
48
49
50
      
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitGame) name:@"_kwowgamecocosexitgame" object:nil];
  }
  
  - (void)exitGame {
f29687b2   biao   修复iOS偶现按钮消失问题,练习页...
51
      
68a4c50a   xiaoyu   Merge remote-trac...
52
      [self.gameViewController dismissViewControllerAnimated:YES completion:nil];
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
53
54
  }
  
22495953   xiaoyu   打开多点触控,开启游戏时使用hud遮罩
55
  static DMProgressHUD *hud;
68a4c50a   xiaoyu   Merge remote-trac...
56
  - (void)runGame:(NSInteger)gameid {
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
57
58
      UIWindow *window = UIApplication.sharedApplication.delegate.window;
      UIViewController *rootvc = window.rootViewController;
22495953   xiaoyu   打开多点触控,开启游戏时使用hud遮罩
59
60
61
62
63
64
65
66
67
68
69
70
71
72
      
      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];
              });
          }
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
73
74
75
76
77
          return;
      }
      if (!self.gameViewController) {
          WowGameViewController *cocosvc = [[WowGameViewController alloc] init];
          cocosvc.wantsFullScreenLayout = YES;
68a4c50a   xiaoyu   Merge remote-trac...
78
          self.gameViewController = cocosvc;
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
79
80
81
82
          
          cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView((__bridge void *)cocosvc.view);
          cocos2d::Director::getInstance()->setOpenGLView(glview);
          _app->run();
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
83
      } else {
68a4c50a   xiaoyu   Merge remote-trac...
84
          cocos2d::Director::getInstance()->resume();
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
85
      }
22495953   xiaoyu   打开多点触控,开启游戏时使用hud遮罩
86
87
88
89
90
91
92
      if ([NSThread isMainThread]) {
          [hud dismiss];
      } else {
          dispatch_async(dispatch_get_main_queue(), ^{
              [hud dismiss];
          });
      }
68a4c50a   xiaoyu   Merge remote-trac...
93
      
251f54fe   xiaoyu   修复ipad进入游戏显示不全的问题
94
      self.gameViewController.modalPresentationStyle = UIModalPresentationFullScreen;
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
95
96
      [rootvc presentViewController:self.gameViewController animated:YES completion:nil];
      
68a4c50a   xiaoyu   Merge remote-trac...
97
98
99
100
101
102
103
104
105
      [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 {
f29687b2   biao   修复iOS偶现按钮消失问题,练习页...
106
      [self clearData];
68a4c50a   xiaoyu   Merge remote-trac...
107
108
109
110
111
      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);
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
112
113
114
115
116
117
      });
  }
  
  - (void)popGame {
      [self.gameViewController dismissViewControllerAnimated:YES completion:nil];
      
68a4c50a   xiaoyu   Merge remote-trac...
118
  //    [self cocosDidEnterBackground];
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
119
120
121
  }
  
  
f29687b2   biao   修复iOS偶现按钮消失问题,练习页...
122
123
124
125
126
127
128
129
130
131
  - (void)clearData
  {
      cocos2d::SpriteFrameCache::getInstance()->removeSpriteFrames();
      cocos2d::Director::getInstance()->getTextureCache()->removeAllTextures();
      //清理搜索路径
      std::vector<std::string> searchPathArray;
      searchPathArray.push_back("res");
      cocos2d::FileUtils::getInstance()->setSearchPaths(searchPathArray);
  }
  
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  - (void)detoryCocosEnvironment {
      
  }
  
  - (void)cocosDidEnterBackground {
      cocos2d::Application::getInstance()->applicationDidEnterBackground();
  }
  
  - (void)cocosWillEnterForeground {
      cocos2d::Application::getInstance()->applicationWillEnterForeground();
  }
  
  
  @end