1. cocos2D工程文件分析(HelloWorld)
1.1. main.h分析
main.h文件内容
1: #ifndef __MAIN_H__
2: #define __MAIN_H__
3:
4: #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
5:
6: // Windows Header Files:
7: #include <windows.h>
8: #include <tchar.h>
9:
10: // C RunTime Header Files
11: #include "CCStdC.h" //cocos2d包含了一些常用的头文件
12:
13: #endif // __MAIN_H__
14:
15:
16: //CCStd.h
17: /*****************************************
18: #ifndef __CC_STD_C_H__
19: #define __CC_STD_C_H__
20:
21: #include "platform/CCPlatformMacros.h"
22: #include <float.h>
23: #include <math.h>
24: #include <string.h>
25: #include <stdarg.h>
26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <time.h>
29: #include <sys/time.h>
30: #include <stdint.h>
31:
32: #ifndef MIN
33: #define MIN(x,y) (((x) > (y)) ? (y) : (x))
34: #endif // MIN
35:
36: #ifndef MAX
37: #define MAX(x,y) (((x) < (y)) ? (y) : (x))
38: #endif // MAX
39:
40: #endif // __CC_STD_C_H__
41: *****************************************/
1.2. main.cpp分析
1: #include "main.h"
2: #include "AppDelegate.h"
3: #include "CCEGLView.h"
4:
5: USING_NS_CC; //分号不可少
6: //#define USING_NS_CC using namespace cocos2d
7:
8: //windows程序的入口点,参数暂时不用管它
9: int APIENTRY _tWinMain(HINSTANCE hInstance,
10: HINSTANCE hPrevInstance,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 11: LPTSTR lpCmdLine,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 12: int nCmdShow)
13: {
14: UNREFERENCED_PARAMETER(hPrevInstance);
15: UNREFERENCED_PARAMETER(lpCmdLine);
16: //在winNT.h中的一个宏定义,什么都没有做
17: //#define UNREFERENCED_PARAMETER(P) (P)
18:
19: // create the application instance
20: AppDelegate app; //创建一个应用实例
21: //生成一个OpenGLView,单例模式CCEGLView.h头文件中
22: CCEGLView* eglView = CCEGLView::sharedOpenGLView();
23: //设置view显示的name和尺寸大小
24: eglView->setViewName("day01");
25: eglView->setFrameSize(480,320);
26: //下面是真正的应用程序入口了
27: return CCApplication::sharedApplication()->run();
28: //return reinterpret_cast<CCApplication*>(&app)->run();
29:
30: /////////////////////////////////////////////////////////////////////////
31: //sharedApplication返回CCApplication类型的一个指针,然后调用
32: //其run方法。run中会调用applicationDidFinishLaunching,这个函数在
33: //AppDelegate中实现。这是基类指针调用派生类的实现,因为这是一个虚函数。
34: //sharedApplication返回的sm_pSharedApplication是指向一个派生类
35: //AppDelegate的对象的指针
36: /*
37: CCApplication* CCApplication::sharedApplication()
38: {
39: //#define CC_ASSERT(cond) assert(cond)
40: CC_ASSERT(sm_pSharedApplication); //断言,检查sm_pSharedApplication是否为NULL
41: return sm_pSharedApplication; //sm_pSharedApplication已经被赋值,直接返回
42: }
43:
44: // sharedApplication pointer 外部定义的指针,看它是如何被赋值就知道了
45: CCApplication * CCApplication::sm_pSharedApplication = 0;
46: //在其构造函数中,为其赋值为调用构造函数的对象的地址
47: CCApplication::CCApplication()
48: : m_hInstance(NULL)
49: ,m_hAccelTable(NULL)
50: {
51: m_hInstance = GetModuleHandle(NULL);
52: m_nAnimationInterval.QuadPart = 0;
53: CC_ASSERT(! sm_pSharedApplication);
54: sm_pSharedApplication = this;
55: //是app构造的时候调用的基类的构造函数,所以这里的this是AppDelegate类的指针,app的地址
56: }
57: *//////////////////////////////////////////////////////////////////////////
58: }
原文链接:https://www.f2er.com/cocos2dx/346114.html对于最后一句return CCApplication::sharedApplication()->run();就相当于是&app->run();
改了之后也是可以运行的。