通过简单的 ResourceManager 管理 XNA 中的资源,WPXNA(二)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛。在这里分享一下经验,仅为了和各位朋友交流经验。平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXNA 吧,最后请高手绕道而行吧,以免浪费时间。(为了突出重点和减少篇幅,有些示例代码可能不够严谨。)
资源的类型
在一些简单的 2D 游戏设计中,我们通常用到的资源是一些字体,图像和声音。所以这里就不涉及视频和模型这些内容了,要制作视频和 3D 模型往往需要花费很多时间。
这里,平方通过枚举类型 ResourceType 来区分不同的资源:
internal enum ResourceType {Image,Font,Sound,Music, }在上面的代码中,Image 表示图像资源,Font 表示字体资源。Sound 和 Music 都表示声音资源,不同的是 Music 通过 MediaPlayer 类播放(一般是 mp3 文件),而 Sound 一般指一些小的 wav 文件。
标识资源
平方创建了一个 Resource 结构来标识一个资源,他有三个公开的字段,Name 是资源的名称,这个名称不能和其他资源重复。Type 是资源的类型,也就是上面提到的 ResourceType。Path 是资源的访问路径,也就是资源文件在项目中的位置,如下图所示:
internal struct Resource {internal readonly string Name;internal readonly string Path;internal readonly ResourceType Type;internal Resource ( string name, ResourceType type, string path ){if ( string.IsNullOrEmpty ( name ) || string.IsNullOrEmpty ( path ) )throw new ArgumentNullException ( "name, path", "name, path can't be null" );this.Name = name;this.Type = type;this.Path = path;}}因为,只有正确的载入资源,游戏才能正常运行,所以在 Resource 类的构造函数中,平方对参数作了一些检查。
管理资源
然后,我们可以通过 ResourceManager 来管理资源。
在构造函数中,我们接受一个 Resource 结构数组,他们也就是需要管理的资源。
internal readonly IList<Resource> Resources;internal ResourceManager ( IList<Resource> resources ) { this.Resources = null == resources ? new Resource[] { } : resources; }当然,ResourceManager 本身并不能够载入资源,所以我们需要使用 ContentManager 类:
private ContentManager contentManager; internal World World;private readonly Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D> ( ); private readonly Dictionary<string, SpriteFont> fonts = new Dictionary<string, SpriteFont> ( ); private readonly Dictionary<string, SoundEffectInstance> sounds = new Dictionary<string, SoundEffectInstance> ( ); private readonly Dictionary<string, Song> music = new Dictionary<string, Song> ( );public void LoadContent ( ) {if ( null == this.contentManager )this.contentManager = new ContentManager ( this.World.Services, contentDirectory );try{foreach ( Resource resource in this.Resources )switch ( resource.Type ){case ResourceType.Image:this.textures.Add ( resource.Name, this.contentManager.Load<Texture2D> ( resolution + resource.Path ) );break;case ResourceType.Font:this.fonts.Add ( resource.Name, this.contentManager.Load<SpriteFont> ( resource.Path ) );break;case ResourceType.Sound:this.sounds.Add ( resource.Name, this.contentManager.Load<SoundEffect> ( resource.Path ).CreateInstance ( ) );break;case ResourceType.Music:this.music.Add ( resource.Name, this.contentManager.Load<Song> ( resource.Path ) );break;}}catch { }}调用 ResourceManager 的 LoadContent 方法来载入资源,在 LoadContent 方法中,我们会创建一个新的 ContentManager 对象,并通过他的 Load 方法载入我们事先接受的资源。
要创建 ContentManager,我们还需要设置 ResourceManager 的 World 字段(你也可以将他修改为属性)。所以,在调用 LoadContent 之前,你需要确保 World 字段不为空。
当你觉得这些资源已经没有用了的时候,调用 UnloadContent 方法来释放资源:
public void UnloadContent ( ) {foreach ( Texture2D texture in this.textures.Values )texture.Dispose ( );foreach ( SoundEffectInstance sound in this.sounds.Values )sound.Dispose ( );foreach ( Song song in this.music.Values )song.Dispose ( );this.textures.Clear ( );this.fonts.Clear ( );this.sounds.Clear ( );this.music.Clear ( );if ( !this.Resources.IsReadOnly )this.Resources.Clear ( );if ( null != this.contentManager )this.contentManager.Unload ( );}在上面的代码中,我们先对所有的资源调用了 Dispose 方法,然后调用了 ContentManager 的 Unload 方法。
使用资源
最后,我们在 World 类中具体的使用一下 ResourceManager 类。
private readonly ResourceManager resourceManager;public World ( Color backgroundColor ): base ( ) {// ...this.resourceManager = new ResourceManager ( new Resource[] {new Resource ( "bird", ResourceType.Image, @"image\bird" ),new Resource ( "click", ResourceType.Sound, @"sound\click" )} );this.resourceManager.World = this;}在 World 的构造函数中,我们创建了一个 ResourceManager,并指出我们需要一个图片和一个波形文件。
protected override void OnNavigatedTo ( NavigationEventArgs e ) {// ...this.resourceManager.LoadContent ( );base.OnNavigatedTo ( e ); }当页面载入之后,我们调用 LoadContent 方法来载入图片和声音。之后,我们在 OnUpdate 和 OnDraw 方法中使用他们。
private void OnUpdate ( object sender, GameTimerEventArgs e ) {this.resourceManager.GetSound ( "click" ).Play ( ); }private void OnDraw ( object sender, GameTimerEventArgs e ) {this.GraphicsDevice.Clear ( this.BackgroundColor );this.spiritBatch.Begin ( );this.spiritBatch.Draw ( this.resourceManager.GetTexture ( "bird" ), new Vector2 ( 20, 20 ), Color.White );this.spiritBatch.End ( ); }通过 ResourceManager 的 GetTexture 和 GetSound 方法可以获取指定的图片和声音,只需要传递资源的名称即可。
最后,你还需要在一个适合的位置调用 ResourceManager 的 UnloadContent 方法。
本期视频 http://v.youku.com/v_show/id_XNTYwOTA2ODgw.html
项目地址 http://wp-xna.googlecode.com/
更多内容 WPXNA
平方开发的游戏 http://zoyobar.lofter.com/
QQ 群 213685539
欢迎平方在其他位置发布的同一文章:http://www.wpgame.info/post/decc4_6329f7
转载于:https://www.cnblogs.com/zoyobar/archive/2013/05/23/wpxna2.html
通过简单的 ResourceManager 管理 XNA 中的资源,WPXNA(二)相关推荐
- 利用Kubernetes名称空间来管理内存和CPU资源(二)
女主宣言 众所周知,Kubernetes是允许指定CPU和RAM的请求和限制的,这一特性对于单独的pod的资源消耗管理非常有用.在本系列文章中,我们将向大家展示集群资源的高效管理的三种策略. PS:丰 ...
- Kubernetes安全三步谈:如何监控与控制Kubernetes中的资源消耗问题
或许很多人可能认为资源消耗并非安全问题,但实际上不合理的资源消耗会让黑客有可乘之机,来攻击K8s的组件.本文将介绍如何处理资源消耗或noisy neighbor问题,包括如何管理Pods中的资源以及管 ...
- XNA中的Render State管理
XNA中的Render State管理 仅供个人学习使用,请勿转载,勿用于任何商业用途. The Problem: XNA中一个设计的非常不好的地方,就是把各种render state定 ...
- 使用 Label 类在 XNA 中显示文本,WPXNA(七)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 高效管理 Elasticsearch 中基于时间的索引——本质是在利用滚动模式做数据的冷热分离,热索引可以用ssd...
高效管理 Elasticsearch 中基于时间的索引 转自:http://stormluke.me/es-managing-time-based-indices-efficiently/ 用 Ela ...
- Python简单主机批量管理工具
Python简单主机批量管理工具 一.程序介绍 需求: 简单主机批量管理工具需求:1.主机分组2.主机信息使用配置文件3.可批量执行命令.发送文件,结果实时返回4.主机用户名密码.端口可以不同5.执行 ...
- 阿里巴巴 Kubernetes 应用管理实践中的经验与教训
作者 | 孙健波(阿里巴巴技术专家).赵钰莹 导读:云原生时代,Kubernetes 的重要性日益凸显.然而,大多数互联网公司在 Kubernetes 上的探索并非想象中顺利,Kubernetes 自 ...
- 操作系统核心原理-5.内存管理(中):分页内存管理
在上一篇介绍的几种多道编程的内存管理模式中,以交换内存管理最为灵活和先进.但是这种策略也存在很多重大问题,而其中最重要的两个问题就是空间浪费和程序大小受限.那么有什么办法可以解决交换内存存在的这些问题 ...
- [Winform]一个简单的账户管理工具
最近一直觉得注册的账户越来越多,帐号密码神马的容易弄混.自己就折腾了一个简单的账户管理工具,其实实现也挺简单,将每个账户的密码及相关密码提示信息,经aes算法加密之后保存到数据库,当前登录用户可以查询 ...
最新文章
- 制作一个简易的QQ×××
- 加州大学研发全柔性汗液传感器,实时监控人体健康
- 在word中使用notepad++实现代码的语法高亮
- python视频网站分类_用Python爬取b站视频
- 用栈实现计算器c语言报告,请问,用c语言做一个计算器 包括+-*/()的运算 用栈 该怎么做...
- python pexpect输出_关于多线程:如何使用pexpect获取python中子进程的自发输出
- 新建和删除文件夹js代码
- 【Kafka】Kafka WARN Property ssl.keystore.location is not valid (kafka.utils.VerifiableProperties)
- 山东大学网络教育计算机基础考试题,山东大学网络教育计算机网络基础期末考试复习...
- PHP+jQuery实现翻板抽奖
- Docker入门之常用命令
- [转载] 柯受良-柯受良飞跃黄河
- buu [QCTF2018]Xman-RSA
- 为什么程序员容易猝死
- 这是一个基于Threejs的商品VR展示系统的 VR模型展示Demo
- Putty的安装和使用
- 数据库审计是什么意思?作用是什么?
- 抖音短视频如何添加抖音小程序;个人与企业该怎么运营小程序。
- python语言的特点和优缺点
- 局域网伪造源地址DDoS攻击解决方法(转)
热门文章
- blog被封了文章全被删除了,郁闷
- 单独获取ShopEx商品类型中的扩展参数,如配货时间
- [python 练习] 计算个税
- Jzoj4891 摆书
- 关于datediff(year,开始日期,结束日期)中year格式的说明,特别注意year两边不能加引号,否则报错...
- ORACLE 分区表 PARTITION table
- Mac node管理器nvm
- 构建一个虚拟DOM并转换为真实DOM
- 互联网人的恶梦是加班?不,是饥荒!
- php下载https图片,php下载https图片报错Failed to enable crypto