title: SDL2 Transparent surface
body: <p>I am trying to create a transparent surface, blit it bigger surface (size of my screen), then create a texture of it, copy to the renderer, and finaly render present.</p>
  
  <p>I have seen many other forums saying I have to use SetBlendMode (for surface, texture, and renderer), ColorKey, SetSurfaceBlendMode. I tried them all but I can't seem to get it to work. I read that SDL_BlitScaled is not suitable for combining surfaces that have transparent pixels, but I am completely lost as to what I have to do instead. </p>
  
  <p>What I noticed, when I use SDL_CreateRGBSurface (with an alpha value) to create a surface, the surface's PixelFormat is RGB888 instead of RGBA8888 (what I am expecting since I provide the alpha value). Using SDL_ConvertSurfaceFormat did not help. </p>
  
  <p><strong>Can somebody tell me what I am missing?</strong></p>
  
  <p>Complete code: <em>removed, my appologies</em></p>
  
  <p><em>Please note that I have removed the attempts to get transparency working</em></p>
  
  <p>The renderer:  </p>
  
  <pre><code>mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED);
  </code></pre>
  
  <p>My render loop:</p>
  
  <pre><code>void CApp::Render()
  {
      SDL_RenderClear(mRenderer);
  
      mBackGround-&gt;Render(mRenderer);
      mForeGround-&gt;Render(mRenderer);
  
      SDL_RenderPresent(mRenderer);
  }
  </code></pre>
  
  <p>The big surface I am blitting to: </p>
  
  <pre><code>mSurface = SDL_CreateRGBSurface(0, CApp::Window_W(), CApp::Window_H(), 32, 0, 0, 0, 0);
  </code></pre>
  
  <p>The code to get tiles from a spritesheet:</p>
  
  <pre><code>bool Map::GetTilesFromSpriteSheet(SDL_Surface *pSpriteSheet, int pTile_w, int pTile_h)
  {
      if(pSpriteSheet-&gt;w % pTile_w == 0 &amp;&amp; pSpriteSheet-&gt;h % pTile_h == 0) {
          SDL_Rect srcRect;
          srcRect.w = pTile_w;
          srcRect.h = pTile_h;
          for(int y = 0; y &lt; pSpriteSheet-&gt;h / pTile_h; y++) {
              for(int x = 0; x &lt; pSpriteSheet-&gt;w / pTile_w; x++) {
                  srcRect.x = x*pTile_w;
                  srcRect.y = y*pTile_h;
                  SDL_Surface* tempSurface = SDL_CreateRGBSurface(0, pTile_w, pTile_h, 32, 0, 0, 0, 0);
                  if(SDL_BlitSurface(pSpriteSheet, &amp;srcRect, tempSurface, nullptr)==0) {
                      mTiles.push_back(tempSurface);
                  } else {
                      Log("Error extracting tile (%d,%d)(w,h): %s", x, y, SDL_GetError());
                      return false;
                  }
              }
          }
          Log("Number of tiles: %d", static_cast&lt;int&gt;(mTiles.size()));
      } else {
          Log("Background spritesheet is incompatible with tile dimensions (%d,%d)(w,h).", pTile_w, pTile_h);
          return false;
      }
      return true;
  }
  </code></pre>
  
  <p>This is where I combine the tiles to create the big surface:</p>
  
  <pre><code>bool Map::GenerateMap(std::vector&lt;std::vector&lt;int&gt;&gt; &amp;pMap, std::vector&lt;SDL_Surface*&gt; &amp;pTiles, SDL_Surface* pDestination)
  {
      SDL_Rect rect;
      rect.w = mDstTile_W;
      rect.h = mDstTile_H;
      SDL_Surface* transparent = SDL_CreateRGBSurface(0, mDstTile_W, mDstTile_H, 32, 0, 0, 0, 0);
  
      for(int y = 0; y &lt; static_cast&lt;int&gt;(pMap.size()); y++) {
          for(int x = 0; x &lt; static_cast&lt;int&gt;(pMap.at(static_cast&lt;unsigned long&gt;(y)).size()); x++) {
              rect.x = x*mDstTile_W;
              rect.y = y*mDstTile_H;
              int index = static_cast&lt;int&gt;(pMap.at(static_cast&lt;unsigned long&gt;(y)).at(static_cast&lt;unsigned long&gt;(x)));
              if(index &lt; 0) {
                  if(SDL_BlitScaled(transparent, nullptr, pDestination, &amp;rect) != 0) {
                      Log("Error blitting transparent surface to destination: %s", SDL_GetError());
                  }
              } else if(SDL_BlitScaled(pTiles[static_cast&lt;unsigned long&gt;(index)],
                                nullptr, pDestination, &amp;rect) != 0) {
                  Log("Error blitting surface to destination: %s", SDL_GetError());
                  return false;
              }
          }
      }
      SDL_FreeSurface(transparent);
      return true;
  }
  </code></pre>
  
  <p>And finaly, this is the code to render the big surface to the screen, first by creating a texture:</p>
  
  <pre><code>void ForeGround::Render(SDL_Renderer* pRenderer)
  {
      if(mTexture) SDL_DestroyTexture(mTexture);
      mTexture = SDL_CreateTextureFromSurface(pRenderer, mSurface);
  
      if(mTexture == nullptr) {
          Log("Unable to create foreground texture: %s", SDL_GetError());
      } else if (SDL_RenderCopy(pRenderer, mTexture, nullptr, nullptr)) {
          Log("Unable to render foreground: %s", SDL_GetError());
      }
  }
  </code></pre>