jjzjj

windows - 如何在Windows Mobile 7 中实现SpriteSheet (Atlas)?

coder 2024-06-13 原文

如何在Windows Mobile 7 上实现SpriteSheet (Atlas)?

最佳答案

移动设备面临的挑战之一是如何在应用程序的生命周期内加载大量图像并仍然从移动设备上获得良好的性能。 下面是关于如何在 Windows Mobile 7 中使用 Sprite Sheet 的简短说明。 什么是 Sprite Sheet,我们为什么需要它?

当我们创建应用程序或游戏时,通常我们需要呈现许多图像。 在代码中单独调用每个图像会在应用程序的生命周期内为我们带来巨大的性能开销。 当涉及到硬件资源有限的移动设备时,非常重要的是 有效地使用这些 Sprite (图像)以获得良好的性能。 那么 spritesheet 如何帮助我们呢? Sprite Sheet 是包含许多小 Sprite (图像)的大图像文件, 因此,我们只使用一个图像文件,而不是使用许多图像! 我们的代码调用了一次,因为图像是有序存储在该图像文件中的 它还可以节省不必要的未使用空间,从而在加载时减少内存。 这是有关如何使用 Windows Mobile 7 执行此操作的说明。 我对 Microsoft 发布的原始 KB 进行了一些修改。

  1. http://create.msdn.com/en-US/education/catalog/sample/sprite_sheet下载代码
  2. 提取它。
  3. 要在您自己的游戏中重用此代码,请将 SpriteSheetPipeline 和 SpriteSheetRuntime 项目添加到您的解决方案中。

一个。使管道项目可用于构建您的内容

  1. 右键单击内容 |内容项目中的引用项目。
  2. 点击添加引用。
  3. 单击项目选项卡,然后选择 SpriteSheetPipeline 项目。

B.使 SpriteSheet 类可用于您的游戏

  1. 右键单击主游戏项目中的 References 项。
  2. 点击添加引用。
  3. 单击项目选项卡,然后选择 SpriteSheetRuntime 项目

现在要实际使用我们导入的代码,我们首先将创建新的 xml 文件并将其放在内容目录中,XML 的格式应如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <Asset Type ="System.String[]">
         <Item>L01_480_0.png</Item>
         <Item>L01_480_1.png</Item>
         <Item>L01_480_2.png</Item>
         <Item>L01_480_3.png</Item>
         <Item>L01_480_4.png</Item>
         <Item>L01_480_5.png</Item>
         <Item>L01_480_6.png</Item>
         <Item>L01_480_7.png</Item>
         <Item>L01_480_8.png</Item>
         <Item>L01_480_9.png</Item>
         <Item>L01_480_10.png</Item>
         <Item>L01_480_11.png</Item>
         <Item>L01_480_12.png</Item>
  </Asset>
</XnaContent>

如您所见,此 xml 包含我们将从中创建 SpriteSheet(atlas) 的图像的名称,无需需要通过 visual studio 将这些 Sprite 图像添加到您的项目中,只需复制图像到物理内容目录,当然需要您通过 visual studio 将 XML 文件添加到 Content 文件夹项目(这不是主项目,所以请注意) 现在要实际使用 XML 文件,您需要做一些事情。 我们需要设置xml文件的属性。 内容导入器将是 XML 内容 - XNA Framework 内容处理器将是 SpriteSheetProcessor 设置属性后,我们实际上可以调用该文件。 首先,我们将告诉我们的代码使用 SpriteSheetRuntime

所以我们要添加

using SpriteSheetRuntime;

我们将声明新的 spritebatch 和 spritesheet 对象

namespace SpriteSheetGame
    {

      /// This is the main type for your game

      public class Game: Microsoft.Xna.Framework.Game
      {
       /* Handles the configuration and management of the graphics device.*/

        GraphicsDeviceManager graphics;

     /* Enables a group of sprites to be
        drawn using the same settings */

        SpriteBatch spriteBatch;

     /* A sprite sheet contains many individual sprite images, packed into different
        areas of a single larger texture, along with information describing where in
        that texture each sprite is located */

         SpriteSheet spriteSheet;
       }

在加载内容中我们会做以下事情:

       protected override void LoadContent()
        {
          spriteBatch = new SpriteBatch(GraphicsDevice);
          spriteSheet = Content.Load<SpriteSheet>("SpriteSheet");
        }

本例中的 XML 文件将是 SpriteSheet.xml,就像上面的例子一样。 现在我们需要通过动画显示 Sprite 或一次显示所有 Sprite .. 所以我们将使用下面的 spriteBatch.Draw,但在此之前我们将启动 spriteBatch

protected override void Draw(GameTime gameTime)
{
    spriteBatch.Begin();

如果您查看类 (SpriteBatch),您会发现在屏幕上绘制的选项很少,您可以选择最适合您的:

public void Draw(Texture2D texture, Vector2 position, Color color);

public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color);

public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color);

public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth);

public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);

public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);

所以最终我们可以写这样的东西:

protected override void Draw(GameTime gameTime)
{
    spriteBatch.Begin();




// Draw an animating effect, by rapidly cycling
      // through 13 slightly different sprite images.

      const int animationFramesPerSecond = 2;
      const int animationFrameCount = 13;

      // Look up the index of the first sprite.

      int index = spriteSheet.GetIndex("L01_480_0");

      // Modify the index to select the current frame of the animation.

      index += (int)(time * animationFramesPerSecond) % animationFrameCount;

      // Draw the current sprite.

       spriteBatch.Draw(spriteSheet.Texture, new Rectangle(0, 0, 100, 100),
                       spriteSheet.SourceRectangle(index), Color.White);

      spriteBatch.End();
            base.Draw(gameTime);
}

关于windows - 如何在Windows Mobile 7 中实现SpriteSheet (Atlas)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5782998/

有关windows - 如何在Windows Mobile 7 中实现SpriteSheet (Atlas)?的更多相关文章

  1. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  2. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  3. ruby - 在 Ruby 中实现 `call_user_func_array` - 2

    我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)

  4. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  5. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  6. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  7. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  8. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  9. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  10. ruby-on-rails - 如何在 ruby​​ 交互式 shell 中有多行? - 2

    这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式ruby​​shell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f

随机推荐