title: DirectX background image
body: <p>I tried to follow a tutorial from my 3D directx book with some modifications. The problem I encountered is that I want to draw an image but can't get this working. The example is working with a camera since it's based on a small game but I just want to load the image without any fancy camera transformations. (Draw the image as it is without resizing + alpha blending).</p>
  
  <p>This is my code which should contain the relevant parts.</p>
  
  <p>.h</p>
  
  <pre><code>class Screen
  {
  private:
      IDirect3DTexture9* m_BGImage;
      ID3DXSprite* m_Sprite;
  
      IDirect3DDevice9* m_Device;
  
  public:
      Screen();
      ~Screen();
  
      void setDevice(IDirect3DDevice9* device);  
      void setBGImage(std::string path);
  
      void Draw();
      void onLostDevice();
      void onResetDevice();
  
      void Clean();
  };
  </code></pre>
  
  <p>.cpp</p>
  
  <pre><code>Screen::Screen() {}
  
  Screen::~Screen()
  {
      Clean();
  }
  
  void Screen::setDevice(IDirect3DDevice9* device) 
  {
      m_Device = device;
      D3DXCreateSprite(m_Device, &amp;m_Sprite);
  }
  
  void Screen::setBGImage(std::string path)
  {
      D3DXCreateTextureFromFileA(m_Device, path.c_str(), &amp;m_BGImage);
  }
  
  void Screen::Draw()
  {
      m_Sprite-&gt;Begin(D3DXSPRITE_DONOTMODIFY_RENDERSTATE); // This is (so I believe) what causes the problem. If I use D3DXSPRITE_OBJECTSPACE|D3DXSPRITE_DONOTMODIFY_RENDERSTATE like described in the example from my book I only get a black screen.
  
      m_Sprite-&gt;Draw(m_BGImage, 0, &amp;D3DXVECTOR3(256.0f, 256.0f, 0.0f), 0, D3DCOLOR_XRGB(255, 255, 255));
      m_Sprite-&gt;Flush();
  
      m_Sprite-&gt;End();
  }
  
  void Screen::Clean()
  {
      ReleaseCOM(m_Sprite);
      ReleaseCOM(m_BGImage);
  }
  
  void Screen::onLostDevice()
  {
      m_Sprite-&gt;OnLostDevice();
  }
  
  void Screen::onResetDevice()
  {
      m_Sprite-&gt;OnResetDevice();
  
      m_Device-&gt;SetRenderState(D3DRS_ALPHAREF, 10);
      m_Device-&gt;SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
  
      m_Device-&gt;SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
      m_Device-&gt;SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
      m_Device-&gt;SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
      m_Device-&gt;SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  
      m_Device-&gt;SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
  }
  </code></pre>
  
  <p>Edit: Almost forgot:</p>
  
  <pre><code>#define ReleaseCOM(x) { if(x){ x-&gt;Release(); x = 0; } }
  </code></pre>