2021-07-21

游戏平台引擎简介

​ 游戏引擎是指一些已经编写好的游戏程序模块,游戏引擎包含以下子系统:渲染引擎,物理引擎,碰撞检测系统,音效,脚本引擎,电脑动画,人工智能,网络引擎以及场景管理。

​ 目前移动平台游戏的引擎有cocos2d-iphone,cocos2d-x,coroona sdk,construct 2,wiengine,unity3d,unreal,development kit,shiva 3d,armalade

创建一个cocos项目

控制台输入

cocos new 项目名 -p 包名 -l 语言 -d 目录 不能包含中文路径

代码解释

Classes文件夹中有是4个文件,AppDelegate.h,AppDelegate.cpp,HelloWorldScene.h,HelloWorldScene.cpp

class  AppDelegate : private cocos2d::Application
{
public:
    AppDelegate();
    virtual ~AppDelegate();

    virtual void initGLContextAttrs();

	/**
	游戏启动时调用的函数,在这里初始化导演对象和场景对象
	*/
    virtual bool applicationDidFinishLaunching();
	/*
	游戏进入后台时调用的函数
	*/
    virtual void applicationDidEnterBackground();

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

#endif // _APP_DELEGATE_H_

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

// 宏用来替换using namespace cocos2d
USING_NS_CC;

/**
	界面尺寸
*/
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() 
{
}

//if you want a different context,just modify the value of glContextAttrs
//it will takes effect on all platforms
void AppDelegate::initGLContextAttrs()
{
    //set OpenGL context attributions,now can only set six attributions:
    //red,green,blue,alpha,depth,stencil
    GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};

    GLView::setGLContextAttrs(glContextAttrs);
}

// If you want to use packages manager to install more packages, 
// don't modify or remove this function
static int register_all_packages()
{
    return 0; //flag for packages manager
}

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
	// 初始化Director
    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);// 设置导演类的OpenGL视图
    }

    // 是否选用显示FPS帧数
    director->setDisplayStats(true);

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

    // Set the design resolution
    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();

    // create a scene. it's an autorelease object
    auto scene = HelloWorld::createScene(); // 创建场景对象Scene

    // run
    director->runWithScene(scene);// 运行该场景

    return true;
}

// 游戏进入后台调用的函数
void AppDelegate::applicationDidEnterBackground() {
    Director::getInstance()->stopAnimation(); // 停止场景动画

    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); // 停止播放背景音乐
}

// 游戏进入前台调用的函数
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation(); // 开始场景动画

    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); // 继续播放背景音乐
}

HelloWorldScene


#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer // HelloWorld是一个Lay 不是 Scene
{
public:
    // 创建当前层HelloWorld所在场景的静态函数
    static cocos2d::Scene* createScene();

    // 声明初始化层HelloWorld实例函数
    virtual bool init();
    
    // 声明菜单回调函数,用于触摸菜单事件的回调
    void menuCloseCallback(cocos2d::Ref* pSender);
    
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld); // 是一个宏相当于下面的代码
    
    static HelloWorld *create()
    {
        static HelloWorld *pRet = new HelloWorld();
        if( pRet && pRet-> init())
        {
            pRet->autorelease();
            return pRet;
        }
        else
        {
            delete pRet;
            pRet = NULL;
            return NULL;
        }
    }
    // 由此可见CREATE_FUNC宏的作用是创建一个静态函数create,该函数用来创建层
};

#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"

USING_NS_CC;

/*
	该函数是在游戏应用启动的时候,在AppDelegate的applicationDidFinishLaunching()函数中通过 auto scene = HelloWorld::createScene()语句调用的,该函数做了三件事,首先创建HelloWorld层所在的场景对象,其次创建了HelloWorld层,最后将HelloWorld层添加到场景scene中,当调用HelloWorld::create()语句创建层的时候,会调用HelloWorld的实例函数init(),达到初始化HelloWorld层的目的
*/
Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1.初始化父类
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize(); // 定义视图的可视化尺寸
    Vec2 origin = Director::getInstance()->getVisibleOrigin(); // 定义视图的可视化原点

    /
    // 2. 添加一个菜单项,单击它的时候退出程序
    // 创建一个图片菜单对象,点击菜单项的时候回调menuCloseCallback函数
    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)); 
    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);// 设置菜单对象的位置,
    this->addChild(menu, 1); // 把菜单对象添加到当前HelloWorld层上,

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

    // add a label shows "Hello World"
    // create and initialize a label
    
    /*
    	将一个HelloWorld标签对象放到层中,
    	过程:创建对象 -> 设置对象的位置 -> 把对象添加到层上。
    */
    auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); // 创建LableTTF标签对象,
    
    // 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
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值