zoukankan      html  css  js  c++  java
  • 第二章 cpp-empty-test:还是那个HelloWorld!

    第二章   cpp-empty-test:还是那个HelloWorld!

       上一章我们讲到说以前的HelloWorld演示更名为cpp-empty-test。本章我们来分析一下这个cpp-empty-test


           运行程序,我们可以看到熟悉的HelloWorld程序:




           与之前cocos2d-x2.x版本的HelloCpp看起来没太大差别,主要有三点:1,标题文字显示为Cpp Empty Test2,按钮由下面改到了上面。3,左下角的信息显示有所不同,以前显示的是(1)批次(2)每帧的平均运行秒数(3FPS数,现在改成了(1OPENGL的顶点数量(2OPENGL的批次(3FPS/每帧的平均运行秒数。


           现在来具体看下工程代码:



    工程的目录有两个


    Classes:程序中的类。


           AppDelegate.h/cppCocos2d-x程序框架


           AppMacros.h:所用到的宏,主要是设置分辩率及对应的资源目录                 C


           HelloWorldScene.h/cpp:场景显示层


     


    win32:WIN32程序所涉及的主函数


           main.cpp:winMain主函数


    在WinMain函数中,只有一个实例化程序并运行它的过程:

    1. int APIENTRY _tWinMain(HINSTANCE hInstance,  
    2.                        HINSTANCE hPrevInstance,  
    3.                        LPTSTR    lpCmdLine,  
    4.                        int       nCmdShow)  
    5. {  
    6.     UNREFERENCED_PARAMETER(hPrevInstance);  
    7.     UNREFERENCED_PARAMETER(lpCmdLine);  
    8.   
    9.     // 创建一个应用程序对象。  
    10.     AppDelegate app;  
    11.     // 运行它。  
    12.     return Application::getInstance()->run();  
    13. }  

            一切,都被封装到程序类AppDelegate中。这是一个基于Cocos2d-x的cocos2d::Application

    类的派生类。它将程序框架封装为一个类,提供了统一的多平台上基本程序框架的实现。

    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. //程序初始化函数  
    21. bool AppDelegate::applicationDidFinishLaunching() {  
    22.     // 取得设备  
    23.     auto director = Director::getInstance();  
    24.     // 取得OpenGL窗口  
    25.     auto glview = director->getOpenGLView();  
    26.     if(!glview) {  
    27.         //如果为空,则创建以" Cpp Empty Test"为窗口标题的窗口。  
    28.         glview = GLView::create("Cpp Empty Test");    
    29.         //设置设备使用的窗口,此句可以去掉。  
    30.         director->setOpenGLView(glview);  
    31.     }  
    32.      //设置设备使用的窗口。  
    33.     director->setOpenGLView(glview);  
    34.   
    35.     // 如果是WP8平台,设置分辩率  
    36. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)  
    37.     // 在WP8上跑DX11,使用ResolutionPolicy::NO_BORDER模式设置分辩率会有一个BUG,这里改为ResolutionPolicy::SHOW_ALL模式。  
    38.   
    39.     glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::SHOW_ALL);  
    40. #else  
    41.     glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);  
    42. #endif  
    43.   
    44.     //取得了视窗的大小  
    45.     Size frameSize = glview->getFrameSize();  
    46.       
    47.     vector<string> searchPath;  
    48.   
    49.     //根据视窗大小与分辩率的大小选择相应的资源目录。  
    50.   
    51.     //ipadhd  
    52.     if (frameSize.height > mediumResource.size.height)  
    53.     {  
    54.         searchPath.push_back(largeResource.directory);  
    55.   
    56.         director->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width));  
    57.     }  
    58.     //ipad  
    59.     else if (frameSize.height > smallResource.size.height)  
    60.     {  
    61.         searchPath.push_back(mediumResource.directory);  
    62.           
    63.         director->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width));  
    64.     }  
    65.     //iphone  
    66.     else  
    67.     {  
    68.         searchPath.push_back(smallResource.directory);  
    69.   
    70.         director->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width));  
    71.     }  
    72.       
    73.     // 设置资源目录  
    74.     FileUtils::getInstance()->setSearchPaths(searchPath);  
    75.       
    76.     // 打开FPS显示  
    77.     director->setDisplayStats(true);  
    78.   
    79.     // 设置每秒60帧  
    80.     director->setAnimationInterval(1.0 / 60);  
    81.   
    82.     // 创建HelloWorld场景  
    83.     auto scene = HelloWorld::scene();  
    84.   
    85.     // 运行场景  
    86.     director->runWithScene(scene);  
    87.   
    88.     return true;  
    89. }  
    90.   
    91. // 当收到电话时,游戏转入后台服务,响应这句  
    92. void AppDelegate::applicationDidEnterBackground() {  
    93.     Director::getInstance()->stopAnimation();  
    94.   
    95.     // 如果使用声音,下面可以用这句代码暂停  
    96.     // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();  
    97. }  
    98.   
    99. // 当电话完成,选择恢复游戏时,响应这句  
    100. void AppDelegate::applicationWillEnterForeground() {  
    101.     Director::getInstance()->startAnimation();  
    102.   
    103.     // 如果使用声音,下面可以用这句代码恢复  
    104.     // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();  
    105. }  

            

            OK,代码跟之前版本差别不大,不过这里要注意,3.0使用了auto自动类型变量,这个是C++11的新标准,比如原来要指定变量是int还是float,现在可以用auto,在赋值的时候,编译器自动识别类型。

            下面我们来看一下HelloWorld场景,它是一个基于cocos2d::Layer的派生类。cocos2d::Layer是什么?在这里,我想打个比方来建立一些基本的认知,比方说我们生活在地球上,地球属于宇宙内的一部分。从Cocos2d-x的框架体系来看,我们是Sprite精灵,地球是Layer,而宇宙是Scene。

            一个程序要想表现出精彩的世界,要先建立一个宇宙Scene,然后增加地球,月球,太阳等Layer,然后在这些Layer上增加相应的物体。而我们站在地球上,地球运动,我们也会跟着一起运动。

      OK,现在我们来看一下如何创建Scene和Layer:

    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.     // 初始化  
    10.     virtual bool init();    
    11.   
    12.     // 静态函数创建Scene  
    13.     static cocos2d::Scene* scene();  
    14.       
    15.     // 响应按钮退出程序  
    16.     void menuCloseCallback(Ref* sender);  
    17.       
    18.     // 增加一个静态的create函数来创建实例。  
    19.     CREATE_FUNC(HelloWorld);  
    20. };  
    21.   
    22. #endif // __HELLOWORLD_SCENE_H__  
    23.      
    24.     HelloWorldScene.cpp:  
    25.   
    26. #include "HelloWorldScene.h"  
    27. #include "AppMacros.h"  
    28. //使用Cocos2d-x命名空间  
    29. USING_NS_CC;  
    30. //静态函数创建场景  
    31. Scene* HelloWorld::scene()  
    32. {  
    33.     // 创建一个Scene,即宇宙  
    34.     auto scene = Scene::create();  
    35.       
    36.     // 创建一个Layer,即地球  
    37.     HelloWorld *layer = HelloWorld::create();  
    38.   
    39.     // 将地球放到宇宙中  
    40.     scene->addChild(layer);  
    41.   
    42.     return scene;  
    43. }  
    44.   
    45. // 初始化  
    46. bool HelloWorld::init()  
    47. {  
    48.     //先进行初始化  
    49.     if ( !Layer::init() )  
    50.     {  
    51.         return false;  
    52.     }  
    53.     //取得分辩率的大小及原点坐标  
    54.     auto visibleSize = Director::getInstance()->getVisibleSize();  
    55.     auto origin = Director::getInstance()->getVisibleOrigin();  
    56.   
    57.     // 创建一个菜单项,它由两张图片来表现普通状态和按下状态,设置按下时调用menuCloseCallback函数响应关闭  
    58.     auto closeItem = MenuItemImage::create(  
    59.                                         "CloseNormal.png",  
    60.                                         "CloseSelected.png",  
    61.                                         CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));  
    62.       
    63.     closeItem->setPosition(origin + Point(visibleSize) - Point(closeItem->getContentSize() / 2));  
    64.   
    65.     //由菜单项创建菜单.  
    66.     auto menu = Menu::create(closeItem, NULL);  
    67.     menu->setPosition(Point::ZERO);  
    68.     this->addChild(menu, 1);  
    69.       
    70.     //创建一个文字标签  
    71.     auto label = LabelTTF::create("Hello World""Arial", TITLE_FONT_SIZE);  
    72.       
    73.     // 设置居中显示  
    74.     label->setPosition(Point(origin.x + visibleSize.width/2,  
    75.                             origin.y + visibleSize.height - label->getContentSize().height));  
    76.   
    77.     // 将文字标签放到当前Layer中。  
    78.     this->addChild(label, 1);  
    79.   
    80.     // 增加一个图片精灵  
    81.     auto sprite = Sprite::create("HelloWorld.png");  
    82.   
    83.     // 设置居中显示  
    84.     sprite->setPosition((ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));  
    85.     //将Sprite放到当前Layer中。  
    86.     this->addChild(sprite);  
    87.     return true;  
    88. }  
    89.   
    90. //响应菜单按下时的事件处理  
    91. void HelloWorld::menuCloseCallback(Ref* sender)  
    92. {  
    93.     //如果是WP8平台,弹出消息框提示一下。  
    94. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)  
    95.     MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");  
    96.     return;  
    97. #endif  
    98.     //否则,终止程序。  
    99.     Director::getInstance()->end();  
    100.     //退出程序  
    101. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
    102.     exit(0);  
    103. #endif  
    104. }  

    Layer中增加了精灵,按钮,文字等表现物,有了这些表现物,一个Layer才有价值。

            OK,简单的程序总是讲的快。最后希望大家记住这样宇宙,地球,你,我,他。


  • 相关阅读:
    docker 的官方PHP镜像 如何修改 php.ini 配置
    数据结构和算法
    接口的幂等性
    linux ftp服务器设置,只允许用户访问指定的文件夹,禁止访问其他文件夹
    rabitmq + php
    微信上传客服消息图片
    golang Printf 函数有超过 10 个转义字符
    golang bufio.NewScarme
    springboot(一)
    springboot加入第三方jar,使用mvn打包
  • 原文地址:https://www.cnblogs.com/Anzhongliu/p/6091972.html
Copyright © 2011-2022 走看看