mac应用从AppController.mm源文件的applicationDidFinishLaunching函数启动:
1 1、
2 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
3 {
4 [self installUncaughtExceptionHandler];
5
6 //创建player
7 auto player = player::PlayerMac::create();
8 player->setController(self);
9
10 _isAlwaysOnTop = NO;
11 _debugLogFile = 0;
12
13 _buildTask = nil;
14 _isBuildingFinished = YES;
15
16 // load QUICK_V3_ROOT from ~/.QUICK_V3_ROOT
17 // 从~/.QUICK_V3_ROOT获取quick根目录,我的:Users/staff/Documents/quick-3.3
18 NSMutableString *path = [NSMutableString stringWithString:NSHomeDirectory()];
19 [path appendString:@"/.QUICK_V3_ROOT"];
20 NSError *error = nil;
21 NSString *env = [NSString stringWithContentsOfFile:path
22 encoding:NSUTF8StringEncoding
23 error:&error];
24 if ([error code] || env.length == 0)
25 {
26 //没有这个配置则报错,提示你执行setup_mac.sh脚本
27 [self showAlertWithoutSheet:@"Please run "setup_mac.sh", set Quick-Cocos2dx-Community root path."
28 withTitle:@"quick player error"];
29 [[NSApplication sharedApplication] terminate:self];
30 }
31
32 //_project.setQuickCocos2dxRootPath设置qucik根目录
33 env = [env stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
34 _project.setQuickCocos2dxRootPath([env cStringUsingEncoding:NSUTF8StringEncoding]);
35
36 //分析命令行参数,设置_project的值,这个后面详细分析
37 [self updateProjectFromCommandLineArgs:&_project];
38
39 //创建WindowAndGLView,2分析
40 [self createWindowAndGLView];
41
42 //注册监听事件
43 [self registerEventsHandler];
44
45 //启动,3分析
46 [self startup];
47 }
48
49 2、
50 - (void) createWindowAndGLView
51 {
52 //设置GLContextAttrs
53 GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
54 GLView::setGLContextAttrs(glContextAttrs);
55
56 float frameScale = _project.getFrameScale();
57
58 // create opengl view
59 cocos2d::Size frameSize = _project.getFrameSize();
60
61 //创建GLView,GLViewImpl是针对desktop平台实现的 文件在platform/desktop/CCGLViewImpl-desktop.h
62 const cocos2d::Rect frameRect = cocos2d::Rect(0, 0, frameSize.width, frameSize.height);
63 GLViewImpl *eglView = GLViewImpl::createWithRect("player", frameRect, frameScale, _project.isResizeWindow());
64
65 auto director = Director::getInstance();
66 director->setOpenGLView(eglView);
67
68 _window = eglView->getCocoaWindow();
69 [NSApp setDelegate:self];
70 [_window center];
71
72 if (_project.getProjectDir().length())
73 {
74 [self setZoom:_project.getFrameScale()];
75 Vec2 pos = _project.getWindowOffset();
76 if (pos.x != 0 && pos.y != 0)
77 {
78 [_window setFrameOrigin:NSMakePoint(pos.x, pos.y)];
79 }
80 }
81 }
82
83 3、
84 - (void) startup
85 {
86 FileUtils::getInstance()->setPopupNotify(false);
87
88 //quick根目录
89 std::string path = _project.getQuickCocos2dxRootPath();
90
91 //如果设置了项目目录,则把项目目录设置为setDefaultResourceRootPath资源搜索默认目录
92 const string projectDir = _project.getProjectDir();
93 if (projectDir.length())
94 {
95 FileUtils::getInstance()->setDefaultResourceRootPath(projectDir.c_str());
96 //如果设置了log输出文件
97 if (_project.isWriteDebugLogToFile())
98 {
99 [self writeDebugLogToFile:_project.getDebugLogFilePath()];
100 }
101 }
102
103 // set framework path
104 // 设置quick框架文件目录加入到搜索目录中_project.getQuickCocos2dxRootPath() + "quick/"
105 if (!_project.isLoadPrecompiledFramework())
106 {
107 FileUtils::getInstance()->addSearchPath(_project.getQuickCocos2dxRootPath() + "quick/");
108 }
109
110 //设置可写目录
111 const string writablePath = _project.getWritableRealPath();
112 if (writablePath.length())
113 {
114 FileUtils::getInstance()->setWritablePath(writablePath.c_str());
115 }
116
117 //显示Console,并且输出Configuration配置信息
118 if (_project.isShowConsole())
119 {
120 [self openConsoleWindow];
121 CCLOG("%s
",Configuration::getInstance()->getInfo().c_str());
122 }
123
124 //加载lua配置
125 [self loadLuaConfig];
126 [self adjustEditMenuIndex];
127 if (!_project.isAppMenu())
128 {
129 NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
130 NSArray *menuArray = [mainMenu itemArray];
131 for (int i = 1; i < [menuArray count]; i++)
132 {
133 id onemenu = [menuArray objectAtIndex:i];
134 [mainMenu removeItem:onemenu];
135 }
136 }
137
138 // app 运行app
139 _app = new AppDelegate();
140 _app->setProjectConfig(_project);
141 Application::getInstance()->run();
142 // After run, application needs to be terminated immediately.
143 [NSApp terminate: self];
144 }
145
146 4、
147 - (void) loadLuaConfig
148 {
149 LuaEngine* pEngine = LuaEngine::getInstance();
150 ScriptEngineManager::getInstance()->setScriptEngine(pEngine);
151
152 luaopen_PlayerLuaCore(pEngine->getLuaStack()->getLuaState());
153 luaopen_PlayerLuaCore_Manual(pEngine->getLuaStack()->getLuaState());
154
155
156 NSMutableString *path = [NSMutableString stringWithString:NSHomeDirectory()];
157 [path appendString:@"/"];
158
159
160 //
161 // set user home dir
162 //
163 lua_pushstring(pEngine->getLuaStack()->getLuaState(), path.UTF8String);
164 lua_setglobal(pEngine->getLuaStack()->getLuaState(), "__USER_HOME__");
165
166
167 //
168 // ugly: Add the opening project to the "Open Recents" list
169 //
170 lua_pushstring(pEngine->getLuaStack()->getLuaState(), _project.getProjectDir().c_str());
171 lua_setglobal(pEngine->getLuaStack()->getLuaState(), "__PLAYER_OPEN_TITLE__");
172
173 lua_pushstring(pEngine->getLuaStack()->getLuaState(), _project.makeCommandLine().c_str());
174 lua_setglobal(pEngine->getLuaStack()->getLuaState(), "__PLAYER_OPEN_COMMAND__");
175
176 //
177 // load player.lua file
178 // 加载player.lua文件
179 //
180 string playerCoreFilePath = _project.getQuickCocos2dxRootPath() + "quick/welcome/src/player.lua";
181 pEngine->executeScriptFile(playerCoreFilePath.c_str());
182 }