簡體   English   中英

如何將字符繪制到Texture2D?

[英]How to draw characters to a Texture2D?

在C#XNA中,如何將單個字符繪制到Texture2D而不是精靈批處理? 我希望這樣做是為了用字符char \\ background數據填充bool[,]來分析它的形狀。

您可以使用渲染目標。 基本思路是將文本渲染到后台緩沖區,而不是渲染到單獨的緩沖區,然后可以為您提供Texture2D。

請看: http//msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.rendertarget(v = xnagamestudio.31).aspx

問題編輯:

經過許可我已添加到這個答案。 在撰寫本文時,MSDN上的信息已經過時並且使它看起來比它需要的更復雜,因此我編寫了自己的如何執行此操作的示例。

完成的類可能必須從IDisposable繼承並實現void Dispose(),它什么都不做。

PresentationParameters pp = graphicsDevice.PresentationParameters;
byte width = 20, height = 20; // for example

// pp.BackBufferWidth, pp.BackBufferHeight // for auto x and y sizes
RenderTarget2D render_target = new RenderTarget2D(graphicsDevice,
width, height, false, pp.BackBufferFormat, pp.DepthStencilFormat,
pp.MultiSampleCount, RenderTargetUsage.DiscardContents);

graphicsDevice.SetRenderTarget(render_target);
graphicsDevice.Clear(...); // possibly optional
spriteBatch.Begin();
// draw to the spriteBatch
spriteBatch.End();
graphicsDevice.SetRenderTarget(null); // Otherwise the SpriteBatch can't
// be used as a texture, this may also need to be done before using the
// SpriteBatch normally again to render to the screen.

// render_target can now be used as a Texture2D

這可能是有用的。 http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Texture_to_Colors.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM