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 ...
随机推荐
- SVN-钩子祥解
钩子脚本的具体写法就是操作系统中shell脚本程序的写法,请根据自己SVN所在的操作系统和shell程序进行相应的写作 所谓钩子就是与一些版本库事件触发的程序,例如新修订版本的创建,或是未版本化属性的 ...
- LintCode "Number of Islands II"
A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah ...
- 【oracle】数据库、表空间、用户、数据表之间的关系
来自为知笔记(Wiz) 附件列表 新建_032515_030437_PM.jpg
- Redis启动警告错误解决
启动错误 (1)WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxc ...
- usaco 2010年3月银组题解
usaco银组解题报告 一.石子游戏如果把‘O’当作0,‘X’当做1,则N个洞的每一种状态都可以看做是一个N位二进制数.于是,这个问题就变成了求环绕的N位格雷码.幸运的是,这个结构很容易就能够用一个简 ...
- c++中字符串处理函数
c++11中提供了to_string函数,定义在string中, 重载了一大片, std::string to_string( int value ); () (since C++) std::str ...
- eclipse导入html、js、xml报错的问题
今天重新安装eclipse,在导入部分html.js.xml文件,报错,解决办法如下: eclipse->window->preferences->Team,点击validation ...
- Php检测文件编码方法
<?php /** * 检测文件编码 * @param string $file 文件路径 * @return string|null 返回 编码名 或 null */ function det ...
- 灵活控制 Hibernate 的日志或 SQL 输出(包含参数),以便于诊断
首先参考:http://blog.csdn.net/zljjava/article/details/7534349 [灵活控制 Hibernate 的日志或 SQL 输出,以便于诊断] 我的具体配置 ...
- PLSQL_性能优化效能跟踪工具SQL Trace分析(案例)
2014-06-25 Created By BaoXinjian