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 ...
随机推荐
- 转载:mybatis自动生成
MyBatis Generator中文文档 MyBatis Generator中文文档地址: http://generator.sturgeon.mopaas.com/ 该中文文档由于尽可能和原文内容 ...
- 转: ExtJS中xtype一览
转: ExtJS中xtype一览 基本组件: xtype Class 描述 button Ext.Button 按钮 splitbutton Ext.SplitButton 带下拉菜单的按钮 cycl ...
- VS2010提示error TRK0002: Failed to execute command解决方法
昨天windows8自动更新Microsoft .NET Framework 3.5和4.5.1安全更新程序,今天用VS2010编译时提示如下错误信息 TRACKER : error TRK0002: ...
- c# string 数组转 list
string str = "1,11,121,131"; var arr = str.Split(','); List<string> list = new List& ...
- 使用laravel的Eloquent模型获取数据库的指定列
使用laravel的Eloquent模型获取数据库的指定列 使用Laravel的ORM——Eloquent时,时常遇到的一个操作是取模型中的其中一些属性,对应的就是在数据库中取表的特定列. 如果使 ...
- Js闭包函数
一.变量的作用域要理解闭包,首先必须理解Javascript特殊的变量作用域.变量的作用域无非就是两种:全局变量和局部变量.Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量. ( ...
- 如何解决WebkitBrowser使用出错“Failed to initialize activation context”
本文转载自:http://www.cnblogs.com/supjia/p/4695671.html 本篇文章主要介绍了"如何解决WebkitBrowser使用出错“Failed to in ...
- CF 161D Distance in Tree 树形DP
一棵树,边长都是1,问这棵树有多少点对的距离刚好为k 令tree(i)表示以i为根的子树 dp[i][j][1]:在tree(i)中,经过节点i,长度为j,其中一个端点为i的路径的个数dp[i][j] ...
- python (2)xpath与定向爬虫
内容来自:极客学院,教学视频: 写在前面: 提取Item 选择器介绍 我们有很多方法从网站中提取数据.Scrapy 使用一种叫做 XPath selectors的机制,它基于 XPath表达式. 这是 ...
- ADO.NET Entity Framework(EF)
ylbtech-Miscellaneos: ADO.NET Entity Framework(EF) A,返回顶部 1, ADO.NET Entity Framework 是微软以 ADO.NET 为 ...