Windows Phone 版 Cocos2d-x 程序的结构
我们已经创建了一个 MyGame 的初始应用,这个应用的结构是什么样的呢?
一、应用程序入口
在 cpp-tests 中,app.xaml.cs 是标准的应用程序入口。与普通的 Windows Phone 应用并没有什么不同。
MainPage.xaml 才是我们应用的第一个界面。这是在 Properties 文件中的 WMAppManifest.xml 中定义的。

如果你希望改变第一个界面,到这里将 Navigation Page: 修改为你的新页面就可以了。
MainPage.xaml.cs 中就包含了许多新的内容了。
主要就是一个与 Direct3D 互操作的类
// Direct3DInterop 与 Direct3D 互操作的类
private Direct3DInterop m_d3dInterop = null;
在 private void DrawingSurfaceBackground_Loaded(object sender, RoutedEventArgs e) 中创建了这个对象的实例。
private void DrawingSurfaceBackground_Loaded(object sender, RoutedEventArgs e)
{
// 是否已经实例化 Direct3DInterop
if (m_d3dInterop == null)
{
PageOrientation pageOrientation = (PageOrientation)GetValue(OrientationProperty);
DisplayOrientations displayOrientation; switch (pageOrientation)
{
case PageOrientation.Portrait:
case PageOrientation.PortraitUp:
displayOrientation = DisplayOrientations.Portrait;
break;
case PageOrientation.PortraitDown:
displayOrientation = DisplayOrientations.PortraitFlipped;
break;
case PageOrientation.Landscape:
case PageOrientation.LandscapeLeft:
displayOrientation = DisplayOrientations.Landscape;
break;
case PageOrientation.LandscapeRight:
displayOrientation = DisplayOrientations.LandscapeFlipped;
break;
default:
displayOrientation = DisplayOrientations.Landscape;
break;
}
m_d3dInterop = new Direct3DInterop(displayOrientation); // Set WindowBounds to size of DrawingSurface
m_d3dInterop.WindowBounds = new Windows.Foundation.Size(
(float)Application.Current.Host.Content.ActualWidth,
(float)Application.Current.Host.Content.ActualHeight
); // Hook-up native component to DrawingSurfaceBackgroundGrid
DrawingSurfaceBackground.SetBackgroundContentProvider(m_d3dInterop.CreateContentProvider());
DrawingSurfaceBackground.SetBackgroundManipulationHandler(m_d3dInterop); // Hook-up Cocos2d-x delegates
m_d3dInterop.SetCocos2dEventDelegate(OnCocos2dEvent);
m_d3dInterop.SetCocos2dMessageBoxDelegate(OnCocos2dMessageBoxEvent);
m_d3dInterop.SetCocos2dEditBoxDelegate(OpenEditBox);
}
}
在这里,你看到 cocos2d 将本地代码与 C# 连接了起来。
在 Direct3DInterop.h 中,定义了一个私有变量 m_render;
private:
Cocos2dRenderer^ m_renderer;
Direct3DInterop.cpp 中,你会看到如下代码来初始化 CocosdRender 的对象实例。
Direct3DInterop::Direct3DInterop(Windows::Graphics::Display::DisplayOrientations orientation)
: mCurrentOrientation(orientation), m_delegate(nullptr)
{
m_renderer = ref new Cocos2dRenderer(mCurrentOrientation);
}
Cocos2dRender.cpp 也定义在 cpp-testsComponent 中。在这个类的构造函数中,我们可以看到熟悉的 AppDelegate 了。
Cocos2dRenderer::Cocos2dRenderer(Windows::Graphics::Display::DisplayOrientations orientation): mInitialized(false), m_loadingComplete(false), m_delegate(nullptr), m_messageBoxDelegate(nullptr)
{
mApp = new AppDelegate();
m_orientation = orientation;
}
而 AppDelegate.cpp 就是我们所熟悉的程序入口。
AppDelegate::AppDelegate() {
}
AppDelegate::~AppDelegate()
{
}
// 应用开启入口
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLView::create("My Game");
director->setOpenGLView(glview);
}
// turn on display FPS
director->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0 / );
// create a scene. it's an autorelease object
auto scene = HelloWorld::createScene();
// 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();
}
在这里通过调用 HelloWorld:createScene() 创建了一个场景。这个方法定义在 HelloWorldScene.cpp 中。
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;
}
在调用 HelloWorld::create 方法的时候,会自动调用类本身的 init 方法来初始化。这个方法是从 CCLayer.cpp 中继承过来的。
Layer *Layer::create()
{
Layer *ret = new Layer();
if (ret && ret->init())
{
ret->autorelease();
return ret;
}
else
{
CC_SAFE_DELETE(ret);
return nullptr;
}
}
在 init() 方法中,定义了我们所看到的实际内容。
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
} Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin(); /////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// 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/ ,
origin.y + closeItem->getContentSize().height/)); // create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, ); /////////////////////////////
// 3. add your codes below... // add a label shows "Hello World"
// create and initialize a label auto label = LabelTTF::create("Hello World", "Arial", ); // position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/,
origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer
this->addChild(label, ); // add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/ + origin.x, visibleSize.height/ + origin.y)); // add the sprite as a child to this layer
this->addChild(sprite, ); return true;
}
最后,让我们加一行代码,将这个图片在 5s 内,放大两倍。
auto action = ScaleTo::create(5.0f, 2.0f);
sprite->runAction(action);
再次运行程序,就会看到,程序启动之后,cocos2d-x 的图片慢慢放大了 2 倍。
调试
如果需要调试的话,需要在 Windows Phone 项目的属性中,配置调试的模式
Native Only 表示可以调试本地代码,也就是 C++ 代码。
Managed Only 表示调试托管代码,也就是我们的 C# 代码。

Windows Phone 版 Cocos2d-x 程序的结构的更多相关文章
- Python学习手册(第4版) - 专业程序员的养成完整版PDF免费下载_百度云盘
Python学习手册(第4版) - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:g7v1 作者简介 作为全球Python培训界的领军人物,<Python学习手册:第4版>作者M ...
- 「MoreThanJava」Day 1:环境搭建和程序基本结构元素
「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...
- 微软颜龄Windows Phone版开发小记
随着微软颜龄中文网cn.how-old.net的上线,她也顺势来到了3大移动平台. 用户在微软颜龄这一应用中选择一张包含若干人脸的照片,就可以通过云计算得到他们的性别和年龄. 今天我们就和大家分享一下 ...
- windows下调用外部exe程序 SHELLEXECUTEINFO
本文主要介绍两种在windows下调用外部exe程序的方法: 1.使用SHELLEXECUTEINFO 和 ShellExecuteEx SHELLEXECUTEINFO 结构体的定义如下: type ...
- WordPress版微信小程序3.2版发布
WordPress版微信小程序(下称开源版)距离上次更新已经过去大半年了,在此期间,我开发新的专业版本-微慕小程序(下称微慕版),同时开源版的用户越来越多,截止到2018年11月26日,在github ...
- WordPress版微信小程序2.2.8版发布
距离上次更新已经一个月了,这期间对WordPress版微信小程序 做的不少小的更新和性能的优化,此次版本更新推出了两个比较重点的功能:点赞和赞赏.同时,优化了文章页面的功能布局,在评论区把常用的功能: ...
- Zend 3.3.0安装 ZendOptimizer 3.3.0 for Windows 稳定版 下载
用的某php网站系统今天打开时乱码了(zend 200407...),但phpmyadmin能正常使用: 搜索下,重新安装zend可以解决,系统上原来的版本是Zend 3.3.0:下了个,安装后果然把 ...
- 给windows右键添加快捷启动程序
给windows右键添加快捷启动程序 修改点击空白处的右键 运行--redegit 打开注册表 展开第一个H..C..R 找到 Direcory,展开 找到Background 展开 右键shell, ...
- ELF Format 笔记(十一)—— 程序头结构
ilocker:关注 Android 安全(新手) QQ: 2597294287 程序头表 (program header table) 是一个结构体数组,数组中的每个结构体元素是一个程序头 (pro ...
- MySQL for Windows 解压缩版安装 和 多实例安装
MySQL 5.6 for Windows 解压缩版配置安装 http://jingyan.baidu.com/album/f3ad7d0ffc061a09c3345bf0.html?picindex ...
随机推荐
- sql2008 将行转为字符串, 将字符串转为行 互转
--将行转为字符串 select stuff((select top 20 ','+ QQ from dl_QQ where uiid=1 order by tim desc for xml path ...
- nova分析(7)—— nova-scheduler
Nova-Scheduler主要完成虚拟机实例的调度分配任务,创建虚拟机时,虚拟机该调度到哪台物理机上,迁移时若没有指定主机,也需要经过scheduler.资源调度是云平台中的一个很关键问题,如何做到 ...
- Neutron分析(2)——neutron-server启动过程分析
neutron-server启动过程分析 1. /etc/init.d/neutron-server DAEMON=/usr/bin/neutron-server DAEMON_ARGS=" ...
- Discuz!NT 3.9.913 Beta DIY过程
前提: 论坛的源码版本为dnt_3.9.913_sqlserver_beta.zip,以下例子都以这个版本为原型修改 dnt_3.9.913数据字典:下载 目前(2013年10月21日)官网的asp. ...
- [转]phonegap 2.9 IOS Xcode 搭建环境
phonegap 2.9 IOS Xcode 搭建环境 一:下载phoneGap2.9和安装Xcode5(目前最新版) 选择2.9是因为3.0以上坑爹版本编译神马的要在有网络情况. 二: 下载ph ...
- 样本方差:为嘛分母是n-1
在样本方差计算式中,我们使用Xbar代替随机变量均值μ. 容易证明(参考随便一本会讲述样本方差的教材),只要Xbar不等于μ,sigma(Xi-Xbar)2必定小于sigma(Xi-μ)2. 然而,要 ...
- Softerra LDAP Browser 使用及配置 有图有真相
Softerra LDAP Browser 4.5 我使用Softerra LDAP Browser的目的,是为了查找公司的人员信息.网上关于Softerra LDAP Browser配置太少了,所以 ...
- POJ 2411 Mondriaan'sDream(状压DP)
题目大意:一个矩阵,只能放1*2的木块,问将这个矩阵完全覆盖的不同放法有多少种. 解析:如果是横着的就定义11,如果竖着的定义为竖着的01,这样按行dp只需要考虑两件事儿,当前行&上一行,是不 ...
- Oracle 数据库中不同事务并发访问的问题
现象 以SQL/Helper为例,打开不同的SQL窗口,对同一个表格进行操作,如下所示. 窗口1:当执行更新任务.紧接着执行查询时获得一组查询结果.结果是对的. 窗口2:而在另外一个SQL查询窗口中执 ...
- 关于java MulticastSocket中的joinGroup(SocketAddress mcastAddr,NetworkInterface netif)
今天复习了一下java网络编程这方面(其实是之前没有学好),之前在linux下用c来做过一些例子,不过不好久没有用也就忘得一干二净了.不知道c/c++的东西不太好记,还是当初没好好学. 关于组播这方面 ...