第二章引擎(上)

Cocos2d-x学习笔记


Cocos2d-x 引擎

  1. 游戏引擎:一些已经编写好的游戏程序模块(渲染引擎、物理引擎、碰撞检测系统、音效、脚本引擎、动画、人工智能、网络引擎以及场景管理)
  2. 游戏引擎主要分为2D和3D,2D主要有Cocos2d-xCocos2d-iphoneCorona SDK等,3D主要是Unity3DShiVa3DMarmalade

Cocos2d-x的HelloWorld程序

初始的HelloWorld程序文件有四个,分别是AppDelegate.hAppDelegate.cppHelloWorld.hHelloWorld.cpp文件

1. AppDelegate.h

#ifndef  _APP_DELEGATE_H_
#define  _APP_DELEGATE_H_

#include "cocos2d.h"

class  AppDelegate : private cocos2d::Application
{
public:
    AppDelegate();//构造函数
    virtual ~AppDelegate();//析构函数

    virtual void initGLContextAttrs();//初始化OpenGL上下文属性

    virtual bool applicationDidFinishLaunching();//游戏启动时调用的函数,初始化导演对象和场景对象

    virtual void applicationDidEnterBackground();//游戏进入后台调用的函数

    virtual void applicationWillEnterForeground();//游戏进入前台调用的函数
};

#endif // _APP_DELEGATE_H_

2. AppDelegate.cpp

#include "AppDelegate.h"
#include "HelloWorldScene.h"

USING_NS_CC;//使用的Cocos2d-x中的宏,代替using namespace cocos2d

//定义几种分辨率尺寸
static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320);
static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320);
static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768);
static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536);

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate() 
{
}

//设置OpenGL上下文属性
void AppDelegate::initGLContextAttrs()
{
    //设置OpenGL上下文属性,现在可以设置6种属性
    //red,green,blue,alpha,depth(深度缓存),stencil(模板缓存)
    GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};

    GLView::setGLContextAttrs(glContextAttrs);
}

// 如果要使用包管理更多的包,不要修改或者删除该函数
static int register_all_packages()
{
    return 0; //flag for packages manager
}

bool AppDelegate::applicationDidFinishLaunching() {
    // 初始化导演对象
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();

    //判断运行平台
    if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
        glview = GLViewImpl::createWithRect("HelloWorld", Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
#else
        glview = GLViewImpl::create("HelloWorld");
#endif
        director->setOpenGLView(glview);
    }

    // 打开FPS显示
    director->setDisplayStats(true);

    // 设置FPS
    director->setAnimationInterval(1.0 / 60);

    // 设定分辨率
    glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
    Size frameSize = glview->getFrameSize();
    // if the frame's height is larger than the height of medium size.
    if (frameSize.height > mediumResolutionSize.height)
    {        
        director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
    }
    // if the frame's height is larger than the height of small size.
    else if (frameSize.height > smallResolutionSize.height)
    {        
        director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
    }
    // if the frame's height is smaller than the height of medium size.
    else
    {        
        director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
    }

    register_all_packages();

    // 创造一个场景
    auto scene = HelloWorld::createScene();

    // 运行
    director->runWithScene(scene);

    return true;
}

// 这个函数将被在app不能使用时调用
void AppDelegate::applicationDidEnterBackground() {
    //停止场景中的动画
    Director::getInstance()->stopAnimation();

    // 如果你使用SimpleAudioEngine播放音乐,则在该方法中暂停
    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// 这个函数在app被运行时调用
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation();

    // 如果你使用SimpleAudioEngine播放音乐,则在该方法中恢复
    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}

3. HelloWorld.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer//层
{
public:
    //创建当前层所在的静态函数
    static cocos2d::Scene* createScene();

    virtual bool init();

    // 菜单回调函数
    void menuCloseCallback(cocos2d::Ref* pSender);

    // cocos2d-x定义的宏
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

4. HelloWorld.cpp

#include "HelloWorldScene.h"

USING_NS_CC;

//creatScene函数在游戏启动时调用
Scene* HelloWorld::createScene()
{
    // 'scene'是自动释放的对象
    auto scene = Scene::create();

    // 'layer' 是自动释放的对象
    auto layer = HelloWorld::create();

    // 将层加入场景
    scene->addChild(layer);

    // 返回场景
    return scene;
}

// 在init函数中初始化实例
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. 首先初始化父类
    if ( !Layer::init() )
    {
        return false;
    }

    //得到可视化尺寸
    Size visibleSize = Director::getInstance()->getVisibleSize()
    //得到可视化远点;
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    // 2. 添加一个退出功能
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

    closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // 创建一个菜单,是自动释放的对象
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);

    /////////////////////////////
    // 3. 添加你自己的代码

    // add a label shows "Hello World"
    // create and initialize a label

    auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);

    // position the label on the center of the screen
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(label, 1);

    // add "HelloWorld" splash screen"
    auto sprite = Sprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
    this->addChild(sprite, 0);

    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

你可能感兴趣的:(Cocos2d-x)