【问题标题】:Resize and Load a texture2d in XNA在 XNA 中调整大小并加载 texture2d
【发布时间】:2010-12-03 20:31:02
【问题描述】:

为了以防万一,我是 XNA 的新手。我尝试做的是加载与他的原始大小不同的纹理,或者至少有可能在之后改变他的大小。我在某些地方看到了我可以使用的:

Texture2D.FromStream(GraphicsDevice graphicsDevice, Stream stream, 
                 int width, int height, bool zoom)

但我还了解到,以这种方式加载纹理会忽略 ContentManager,并且我会让垃圾收集器的工作变得更加困难。

使用 ContentManager 加载任何大小的图像的正确方法是什么? 如果这不可能,我可以按比例更改他的大小,比如使用缩放吗?

上下文: 我正在创建一个 n x n 和平板。当 n 太大时,我需要自动和平变得更小。

【问题讨论】:

  • 乔的回答是正确的。另外:无论您是否使用 ContentManager,垃圾收集器都没有区别。它只影响您必须如何卸载纹理,如果这是您的游戏需要的(例如:在级别之间切换时)。看看我的回答:stackoverflow.com/questions/4264995/….

标签: c# xna texture2d downsize


【解决方案1】:

加载纹理:

Texture2D tex = Content.Load<Texture2D>("somefile");

要调整它的大小,请使用采用“缩放”的 SpriteBatch 重载之一 http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritebatch.draw.aspx

float scale = .5f; //50% smaller
SpriteBatch.Draw(tex, position, source, Color.White, rotation, scale, SpriteEffects.None, 0f);

如果您是 XNA 的新手,我建议您阅读 this short tutorial,并在 create.msdn.com 上查看 Education Catalog

【讨论】:

  • 谢谢!,它的工作方式正是我想要的 :)...现在关于是否使用我提到的方法的问题,你认为什么是安全的,或者他的使用是其他的?
【解决方案2】:
Texture2D texture;
protected override void LoadContent()
        {
...
         texture = Content.Load<Texture2D>("Tank");
...
        }
protected override void Draw(GameTime gameTime)
        {
...
         Rectangle destinationRectangle = new Rectangle(100, 100, 30, 10);
         spriteBatch.Draw(texture, destinationRectangle, Color.White);
...
         spriteBatch.End();
         base.Draw(gameTime);
        }

【讨论】:

  • 欢迎来到 Stack Overflow!仅代码的答案不是很有帮助。请编辑您的答案以解释为什么您的代码解决了原始问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多