2014-02-23 5 views
0

여러 스프라이트에서 임의의 섬을 만드는 XNA 게임을 만들고 있습니다. 별도의 스레드에서 생성 한 다음 RenderTarget2D를 사용하여 단일 텍스처로 컴파일합니다.XNA RenderToTarget을위한 두 번째 그래픽 장치

내 RenderTarget2D를 만들려면 그래픽 장치가 필요합니다. 자동으로 생성 된 그래픽 장치를 사용하면 주 게임 스레드의 그리기 호출과 충돌하는 것을 제외하고는 대부분 정상적으로 작동합니다. 그래픽 장치에서 lock()을 사용하면 깜박임이 발생하고 텍스처가 제대로 만들어지지 않는 경우가 있습니다.

내 자신의 그래픽 장치를 만들면 충돌이 없지만 섬이 올바르게 렌더링되지 않고 순수한 흑백으로 나옵니다. 왜 이런 일이 일어 나는지 나는 모른다. 기본적으로 나는 검정색/흰색 대신 동일한 결과를 얻을 수있는 두 번째 그래픽 장치를 만드는 방법이 필요합니다. 누구든지 아이디어가있어?

var presParams = game.GraphicsDevice.PresentationParameters.Clone(); 
      // Configure parameters for secondary graphics device 
      GraphicsDevice2 = new GraphicsDevice(game.GraphicsDevice.Adapter, GraphicsProfile.HiDef, presParams); 

저는 여기에 하나의 텍스처 내 섬을 렌더링하는 데 사용하고 코드입니다 : 여기

내가 시도하고 TextureBuilder에 의해 전용 내 두 번째 그래픽 장치를 만드는 데 사용하고 코드의

여기
public IslandTextureBuilder(List<obj_Island> islands, List<obj_IslandDecor> decorations, SeaGame game, Vector2 TL, Vector2 BR, int width, int height) 
    { 
     gDevice = game.Game.GraphicsDevice; //default graphics 
     //gDevice = game.GraphicsDevice2 //created graphics 

     render = new RenderTarget2D(gDevice, width, height, false, SurfaceFormat.Color, DepthFormat.None); 

     this.islands = islands; 
     this.decorations = decorations; 
     this.game = game; 

     this.width = width; 
     this.height = height; 

     this.TL = TL; //top left coordinate 
     this.BR = BR; //bottom right coordinate 
    } 

    public Texture2D getTexture() 
    { 
     lock (gDevice) 
     { 
      //Set render target. Clear the screen. 
      gDevice.SetRenderTarget(render); 
      gDevice.Clear(Color.Transparent); 

      //Point camera at the island 
      Camera cam = new Camera(gDevice.Viewport); 
      cam.Position = TL; 
      cam.Update(); 

      //Draw all of the textures to render 
      SpriteBatch batch = new SpriteBatch(gDevice); 
      batch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.Transform); 
      { 
       foreach (obj_Island island in islands) 
       { 
        island.Draw(batch); 
       } 
       foreach (obj_IslandDecor decor in decorations) 
       { 
        decor.Draw(batch); 
       } 
      } 
      batch.End(); 

      //Clear render target 
      gDevice.SetRenderTarget(null); 

      //Copy to texture2D for permanant storage 
      Texture2D texture = new Texture2D(gDevice, render.Width, render.Height); 
      Color[] color = new Color[render.Width * render.Height]; 
      render.GetData<Color>(color); 
      texture.SetData<Color>(color); 

      Console.WriteLine("done"); 

      return texture; 
     } 

투명 배경으로 발생해야하는 (그리고 보통 나는 기본 장치를 사용하는 경우 않습니다) http://i110.photobucket.com/albums/n81/taumonkey/GoodIsland.png

기본 장치가 충돌하여 주 스레드가 Clear()를 호출 할 때 발생합니다. (잠겨 있어도 그대로 유지됩니다.) NotSoGoodIsland.png (평판이 필요합니다 ....)

내 자신의 것을 사용하면 어떻게 될까요? 그래픽 장치 http://i110.photobucket.com/albums/n81/taumonkey/BadIsland.png

제공된 도움말을 미리 감사드립니다!

답변

0

RenderToTarget 코드를 Draw() 메서드로 이동하고 Draw() 메서드를 처음 호출 할 때 주 스레드에서 호출하여이 문제를 해결했을 수도 있습니다.