介绍
Cocos2d-x是一个在MIT许可下发布的开源联通2D游戏框架。 Cocos2d-x支持跨平台,当前版本号为v4.x。
Cocos2d-x官网()
官网截图
github链接()
创建项目
首先创建一个HelloWorld项目。 源码下载后游戏引擎源码分析,打开源码目录下的tools/cocos2d-console/bin/。 上面有一个cocos console程序,我们用它来创建项目。
新参数介绍
创建项目的命令参数是new,后面是项目名称、使用的语言、项目目录。
这样,我的Workspace/cocos2d下就创建了一个HelloWorld项目。
进入我们的项目,我们可以看到如下的目录结构:
其中proj开头的是不同平台的入口文件,cocos2d文件夹是引擎源码,Classes是我们自己的类,Resources是游戏资源文件。
创建一个构建文件夹并单步执行:
cmake ..
因为我的系统是linux,所以生成了makefile并执行:
make
编译将开始。 编译完成后,build/bin/目录下会有可执行文件。 跑步:
节目入口
程序首先是如何启动的?
我们打开我们对应平台的文件夹。 我这里是linux,所以当我们打开proj.linux时,我们会发现下面有一个main.cpp:
我们打开这个文件:
上面有一个main函数,可以确定这就是我们程序的入口点。 上面定义了一个AppDelegate对象,然后返回Application:getInstance()->run()。 我们先来看看AppDelegate类。 它的位置在我们项目的Classes目录下:
这是AppDelegate.h的代码:
#ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_
#include "cocos2d.h"
/**
@brief The cocos2d Application.
Private inheritance here hides part of interface from Director.
*/
class AppDelegate : private cocos2d::Application
{
public:
AppDelegate();
virtual ~AppDelegate();
virtual void initGLContextAttrs();
/**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
virtual bool applicationDidFinishLaunching();
/**
@brief Called when the application moves to the background
@param the pointer of the application
*/
virtual void applicationDidEnterBackground();
/**
@brief Called when the application reenters the foreground
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
};
#endif // _APP_DELEGATE_H_
可以看到它继承自Application,所以我们来寻找Application类。
首先,我们要找到cocos2d.h。 引擎头文件目录在cmake中,所以我们只能在代码中写文件名。 后面构建的时候我们会手动找到头文件的位置。 我们的cocos2d.h在cocos2d/cocos目录下。
打开cocos2d.h:
可以看到该宏会根据平台的不同导出不同的头文件。 我这里是linux,所以打开platform/linux/CCApplication-linux.h:
#pragma once
#include "platform/CCCommon.h"
#include "platform/CCApplicationProtocol.h"
#include
NS_CC_BEGIN
class Rect;
class Application : public ApplicationProtocol
{
public:
/**
* @js ctor
*/
Application();
/**
* @js NA
* @lua NA
*/
virtual ~Application();
/**
@brief Callback by Director for limit FPS.
@param interval The time, which expressed in second in second, between current frame and next.
*/
virtual void setAnimationInterval(float interval) override;
/**
@brief Run the message loop.
*/
int run();
/**
@brief Get current application instance.
@return Current application instance pointer.
*/
static Application* getInstance();
/** @deprecated Use getInstance() instead */
CC_DEPRECATED_ATTRIBUTE static Application* sharedApplication();
/* override functions */
virtual LanguageType getCurrentLanguage() override;
/**
@brief Get current language iso 639-1 code
@return Current language iso 639-1 code
*/
virtual const char * getCurrentLanguageCode() override;
/**
@brief Get application version
*/
virtual std::string getVersion() override;
/**
@brief Open url in default browser
@param String with url to open.
@return true if the resource located by the URL was successfully opened; otherwise false.
*/
virtual bool openURL(const std::string &url) override;
/**
* Sets the Resource root path.
* @deprecated Please use FileUtils::getInstance()->setSearchPaths() instead.
*/
CC_DEPRECATED_ATTRIBUTE void setResourceRootPath(const std::string& rootResDir);
/**
* Gets the Resource root path.
* @deprecated Please use FileUtils::getInstance()->getSearchPaths() instead.
*/
CC_DEPRECATED_ATTRIBUTE const std::string& getResourceRootPath();
/**
@brief Get target platform
*/
virtual Platform getTargetPlatform() override;
protected:
long _animationInterval; //micro second
std::string _resourceRootPath;
static Application * sm_pSharedApplication;
};
NS_CC_END
Application继承的类只是一个socket,我们这里直接看Application即可。
我们看一下构造函数的定义:
// sharedApplication pointer
Application * Application::sm_pSharedApplication = nullptr;
Application::Application()
: _animationInterval(1.0f/60.0f*1000.0f)
{
CC_ASSERT(! sm_pSharedApplication);
sm_pSharedApplication = this;
}
首先是初始化一个staticApplication指针为nullptr,然后在构造函数中,初始化这个指针到this,也就是这个类的对象游戏引擎源码分析,所以这个指针是一个全局的单例对象,而在我们的程序中,虽然这个指针是main.cpp 中的 app 对象。 然后看getInstance函数:
//////////////////////////////////////////////////////////////////////////
// static member function
//////////////////////////////////////////////////////////////////////////
Application* Application::getInstance()
{
CC_ASSERT(sm_pSharedApplication);
return sm_pSharedApplication;
}
这是一个静态函数,这个函数返回我们的全局对象。 所以main上面的run函数实际上就是Application的run:
int Application::run()
{
initGLContextAttrs();
// Initialize instance and cocos2d.
if (! applicationDidFinishLaunching())
{
return 0;
}
long lastTime = 0L;
long curTime = 0L;
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
// Retain glview to avoid glview being released in the while loop
glview->retain();
while (!glview->windowShouldClose())
{
lastTime = getCurrentMillSecond();
director->mainLoop();
glview->pollEvents();
curTime = getCurrentMillSecond();
if (curTime - lastTime isOpenGLReady())
{
director->end();
director->mainLoop();
director = nullptr;
}
glview->release();
return EXIT_SUCCESS;
}
值得注意的是,Application上面的虚函数实际上是由AppDelegate在运行时调用的。
上面的循环逻辑实际上是director->mainLoop()。 我们将在下一篇教程中讲解director的mainLoop。
文档基于:安装
MindSpore 1.0 可以安装在昇腾、GPU 和 CPU 环境中,看来笔者的机器配置 GPU 还远远不够,昇腾还很远,所以我不得不选择 CPU 来获得安装体验。
其实电脑笔记本安装只能选择X86版本。因此,您只能选择上述模式进行安装。
这时,你可以充分利用之前 Atlas200DK 做的 Ubuntu 18.04 环境。当时的备份很有用...
检查虚拟机是否为 18.04。确认后,暂停,将虚拟机的目录复制到另一个目录,然后更改名称。之后游戏体验站源码,打开(恢复)。
出现提示时,选择“我已复制此虚拟机”。虚拟机已重命名:
与原始机器完全相同...
最后游戏体验站源码,让我们添加机器环境:
计算机笔记本电脑环境:
CPU:英特尔酷睿 7-8750H@2.20GHz 六核处理器
显存:64GDDR42666
硬盘: 海康威视 C20002000Pro2TSSD
主板:英伟达Quardo P1000(4G)。
操作系统: 视窗 10
Python 运行时环境:Anaconda
Python 开发环境:PyCharm2020.1 试用版
---------------------------------
虚拟机环境:
目前不大开:(可以调整)。
中央处理器: 2
显存:8G
硬盘:50G
操作系统: 乌班图 18.04
蟒
运行环境:从源代码编译的Python 3.7.5