cocos2d-x-3.2 cpp-empty-test代码注释
AppDelegate.cpp
1 #include "AppDelegate.h" 2 3 #include <vector> 4 #include <string> 5 6 #include "HelloWorldScene.h" 7 #include "AppMacros.h" 8 9 USING_NS_CC; 10 using namespace std; 11 12 AppDelegate::AppDelegate() { 13 14 } 15 16 AppDelegate::~AppDelegate() 17 { 18 } 19 // 程序初始化函数 20 bool AppDelegate::applicationDidFinishLaunching() { 21 // initialize director 22 auto director = Director::getInstance(); // 取得设备 23 auto glview = director->getOpenGLView(); // 取得OpenGL窗口 24 if(!glview) { 25 // 如果为空,则创建以" Cpp Empty Test"为窗口标题的窗口。 26 glview = GLView::create("Cpp Empty Test"); 27 // 设置设备使用的窗口,此句可以去掉。 28 director->setOpenGLView(glview); 29 } 30 // 设置设备使用的窗口。 31 director->setOpenGLView(glview); 32 33 // Set the design resolution 34 // 如果是WP8平台,设置分辩率 35 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) 36 // a bug in DirectX 11 level9-x on the device prevents ResolutionPolicy::NO_BORDER from working correctly 37 // 在WP8上跑DX11,使用ResolutionPolicy::NO_BORDER模式设置分辩率会有一个BUG,这里改为ResolutionPolicy::SHOW_ALL模式。 38 glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::SHOW_ALL); 39 #else 40 glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER); 41 #endif 42 43 // 取得了视窗的大小 44 Size frameSize = glview->getFrameSize(); 45 46 vector<string> searchPath; 47 48 49 50 //根据视窗大小与分辩率的大小选择相应的资源目录。 51 52 // In this demo, we select resource according to the frame's height. 53 // If the resource size is different from design resolution size, you need to set contentScaleFactor. 54 // We use the ratio of resource's height to the height of design resolution, 55 // this can make sure that the resource's height could fit for the height of design resolution. 56 57 // if the frame's height is larger than the height of medium resource size, select large resource. 58 // ipadhd 59 if (frameSize.height > mediumResource.size.height) 60 { 61 searchPath.push_back(largeResource.directory); 62 63 director->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width)); 64 } 65 // if the frame's height is larger than the height of small resource size, select medium resource. 66 // ipad 67 else if (frameSize.height > smallResource.size.height) 68 { 69 searchPath.push_back(mediumResource.directory); 70 71 director->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width)); 72 } 73 // if the frame's height is smaller than the height of medium resource size, select small resource. 74 // iphone 75 else 76 { 77 searchPath.push_back(smallResource.directory); 78 79 director->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width)); 80 } 81 82 // set searching path 83 // 设置资源目录 84 FileUtils::getInstance()->setSearchPaths(searchPath); 85 86 // turn on display FPS 87 director->setDisplayStats(true); // 打开FPS显示 88 89 // set FPS. the default value is 1.0/60 if you don't call this 90 director->setAnimationInterval(1.0 / 60); // 设置每秒60帧 91 92 // create a scene. it's an autorelease object 93 auto scene = HelloWorld::scene(); // 创建HelloWorld场景 94 95 // run 96 director->runWithScene(scene); // 运行场景 97 98 return true; 99 } 100 101 // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 102 // 当收到电话时,游戏转入后台服务,响应这句 103 void AppDelegate::applicationDidEnterBackground() { 104 Director::getInstance()->stopAnimation(); 105 106 // if you use SimpleAudioEngine, it must be pause 107 // 如果使用声音,下面可以用这句代码暂停 108 // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); 109 } 110 111 // this function will be called when the app is active again 112 // 当电话完成,选择恢复游戏时,响应这句 113 void AppDelegate::applicationWillEnterForeground() { 114 Director::getInstance()->startAnimation(); 115 116 // if you use SimpleAudioEngine, it must resume here 117 // 如果使用声音,下面可以用这句代码恢复 118 // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); 119 }
HelloWorldScene.h
1 #ifndef __HELLOWORLD_SCENE_H__ 2 #define __HELLOWORLD_SCENE_H__ 3 4 #include "cocos2d.h" 5 6 class HelloWorld : public cocos2d::Layer 7 { 8 public: 9 // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone 10 // 初始化 11 virtual bool init(); 12 13 // there's no 'id' in cpp, so we recommend returning the class instance pointer 14 // 静态函数创建Scene 15 static cocos2d::Scene* scene(); 16 17 // a selector callback 18 // 响应按钮退出程序 19 void menuCloseCallback(Ref* sender); 20 21 // implement the "static node()" method manually 22 // 增加一个静态的create函数来创建实例。 23 CREATE_FUNC(HelloWorld); 24 }; 25 26 #endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
1 #include "HelloWorldScene.h" 2 #include "AppMacros.h" 3 4 // 使用Cocos2d-x命名空间 5 USING_NS_CC; 6 7 // 静态函数创建场景 8 Scene* HelloWorld::scene() 9 { 10 // 'scene' is an autorelease object 11 // 创建一个Scene,即宇宙 12 auto scene = Scene::create(); 13 14 // 'layer' is an autorelease object 15 // 创建一个Layer,即地球 16 HelloWorld *layer = HelloWorld::create(); 17 18 // add layer as a child to scene 19 // 将地球放到宇宙中 20 scene->addChild(layer); 21 22 // return the scene 23 return scene; 24 } 25 26 // on "init" you need to initialize your instance 27 // 初始化 28 bool HelloWorld::init() 29 { 30 ////////////////////////////// 31 // 1. super init first 32 // 先进行初始化 33 if ( !Layer::init() ) 34 { 35 return false; 36 } 37 // 取得分辩率的大小及原点坐标 38 auto visibleSize = Director::getInstance()->getVisibleSize(); 39 auto origin = Director::getInstance()->getVisibleOrigin(); 40 41 ///////////////////////////// 42 // 2. add a menu item with "X" image, which is clicked to quit the program 43 // you may modify it. 44 45 // add a "close" icon to exit the progress. it's an autorelease object 46 // 创建一个菜单项,它由两张图片来表现普通状态和按下状态,设置按下时调用menuCloseCallback函数响应关闭 47 auto closeItem = MenuItemImage::create( 48 "CloseNormal.png", 49 "CloseSelected.png", 50 CC_CALLBACK_1(HelloWorld::menuCloseCallback,this)); 51 52 closeItem->setPosition(origin + Vec2(visibleSize) - Vec2(closeItem->getContentSize() / 2)); 53 54 // create menu, it's an autorelease object 55 // 由菜单项创建菜单. 56 auto menu = Menu::create(closeItem, NULL); 57 menu->setPosition(Vec2::ZERO); 58 this->addChild(menu, 1); 59 60 ///////////////////////////// 61 // 3. add your codes below... 62 63 // add a label shows "Hello World" 64 // create and initialize a label 65 // 创建一个文字标签 66 auto label = LabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE); 67 68 // position the label on the center of the screen 69 // 设置居中显示 70 label->setPosition(Vec2(origin.x + visibleSize.width/2, 71 origin.y + visibleSize.height - label->getContentSize().height)); 72 73 // add the label as a child to this layer 74 // 将文字标签放到当前Layer中。 75 this->addChild(label, 1); 76 77 // add "HelloWorld" splash screen" 78 // 增加一个图片精灵 79 auto sprite = Sprite::create("HelloWorld.png"); 80 81 // position the sprite on the center of the screen 82 // 设置居中显示 83 sprite->setPosition(Vec2(visibleSize / 2) + origin); 84 85 // add the sprite as a child to this layer 86 // 将Sprite放到当前Layer中。 87 this->addChild(sprite); 88 89 return true; 90 } 91 92 // 响应菜单按下时的事件处理 93 void HelloWorld::menuCloseCallback(Ref* sender) 94 { 95 // 如果是WP8平台,弹出消息框提示一下。 96 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) 97 MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); 98 return; 99 #endif 100 // 否则,终止程序。 101 Director::getInstance()->end(); 102 103 // 退出程序 104 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 105 exit(0); 106 #endif 107 }