Multiple BlitSurface operations result black Textures

So, I’m doing the following (example code):

Code:

SDL_Texture* background;
SDL_Surface *bigSurface, *scaleSurface, *loadingSurface;

loadingSurface = IMG_Load(“bg.png”);
SDL_Rect rect;
rect.w = loadingSurface->w;
rect.h = loadingSurface->h;

bigSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, window->w, window->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
scaleSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, rect->w /2.f, rect->h /2.f, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);

SDL_BlitSurface(loadingSurface, NULL, scaleSurface, NULL);
SDL_BlitSurface(scaleSurface, NULL, bigSurface, &rect);

background = SDL_CreateTextureFromSurface(mainRenderer, bigSurface);

SDL_FreeSurface(scaleSurface);
SDL_FreeSurface(loadingSurface);
SDL_FreeSurface(bigSurface);

After the first BlitSurface, the image is okay (if I convert to Texture at that point, it shows up correctly), however after the second BlitSurface operation all I got is black color in place of the image.
What could cause this behaviour, and how may I fix it?------------------------
Runic Girls - a hexa match3 game with girls created using SDL 2.0:
http://www.facebook.com/RunicGirls

Gameplay trailer:

It looks like your rect.x and rect.y are uninitialized. Try setting them
to 0. I would also be careful about blitting to transparent (brand new
RGBA) surfaces, as the blend operation preserves the destination surface
alpha value. Use SDL_SetSurfaceBlendMode() to disable alpha blending if
that is what you want.

Jonny DOn Wed, May 1, 2013 at 8:59 AM, LBandy wrote:

**
So, I’m doing the following (example code):

Code:

SDL_Texture* background;
SDL_Surface *bigSurface, *scaleSurface, *loadingSurface;

loadingSurface = IMG_Load(“bg.png”);
SDL_Rect rect;
rect.w = loadingSurface->w;
rect.h = loadingSurface->h;

bigSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, window->w, window->h, 32,
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
scaleSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, rect->w /2.f, rect->h
/2.f, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);

SDL_BlitSurface(loadingSurface, NULL, scaleSurface, NULL);
SDL_BlitSurface(scaleSurface, NULL, bigSurface, &rect);

background = SDL_CreateTextureFromSurface(mainRenderer, bigSurface);

SDL_FreeSurface(scaleSurface);
SDL_FreeSurface(loadingSurface);
SDL_FreeSurface(bigSurface);

After the first BlitSurface, the image is okay (if I convert to Texture at
that point, it shows up correctly), however after the second BlitSurface
operation all I got is black color in place of the image.
What could cause this behaviour, and how may I fix it?


Runic Girls - a hexa match3 game with girls created using SDL 2.0:
http://www.facebook.com/RunicGirls

Gameplay trailer:
http://www.youtube.com/watch?v=yMGdfNHGhIU&hd=1


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Yeah, the code was only written for demonstration purposes, that’s why it missed the rect initialization.
Disabling Blend mode solved the problem, thank you Jonny!------------------------
Runic Girls - a hexa match3 game with girls created using SDL 2.0:
http://www.facebook.com/RunicGirls

Gameplay trailer:

With the above approach I’m facing the problem of blitting transparent png-s together. If using SDL_SetSurfaceBlendMode(scaleSurface, SDL_BLENDMODE_NONE) I lose the transparency, and got black background instead, but using SDL_BLENDMODE_BLEND results in a fully transparent image, and there will be nothing to see.
Can someone point me in the right direction?------------------------
Runic Girls - a hexa match3 game with girls created using SDL 2.0:
http://www.facebook.com/RunicGirls

Gameplay trailer:

DO SDL_SetSurfaceBlendMode NONE only on the source surface.On Wed, May 1, 2013 at 5:05 PM, LBandy wrote:

**
With the above approach I’m facing the problem of blitting transparent
png-s together. If using SDL_SetSurfaceBlendMode(scaleSurface,
SDL_BLENDMODE_NONE) I lose the transparency, and got black background
instead, but using SDL_BLENDMODE_BLEND results in a fully transparent
image, and there will be nothing to see.
Can someone point me in the right direction?


Runic Girls - a hexa match3 game with girls created using SDL 2.0:
http://www.facebook.com/RunicGirls

Gameplay trailer:
http://www.youtube.com/watch?v=yMGdfNHGhIU&hd=1


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

LBandy wrote:

With the above approach I’m facing the problem of blitting transparent png-s together. If using SDL_SetSurfaceBlendMode(scaleSurface, SDL_BLENDMODE_NONE) I lose the transparency, and got black background instead, but using SDL_BLENDMODE_BLEND results in a fully transparent image, and there will be nothing to see.
Can someone point me in the right direction?

I have the exact same issue where I need to blit RGBA -> RGBA. Now, the operation that SDL2 does with blend (from reading SDL/src/video/sdl_blit*) is that it will blend RGB values but not A values.

I have patched SDL’s slow blitting function (which is used with certain calls to SDL_UpperBlitScaled, not limited to afaik) with the following:

Code:
diff -r 09ee216e6a25 src/video/SDL_blit_slow.c
— a/src/video/SDL_blit_slow.c Wed May 01 22:14:29 2013 -0700
+++ b/src/video/SDL_blit_slow.c Thu May 02 20:24:18 2013 -0500
@@ -127,6 +127,7 @@
dstR = srcR + ((255 - srcA) * dstR) / 255;
dstG = srcG + ((255 - srcA) * dstG) / 255;
dstB = srcB + ((255 - srcA) * dstB) / 255;

  •            dstA = srcA + ((255 - srcA) * dstA) / 255;
               break;
           case SDL_COPY_ADD:
               dstR = srcR + dstR;
    

@@ -138,6 +139,9 @@
dstB = srcB + dstB;
if (dstB > 255)
dstB = 255;

  •            dstA = srcA + dstA;
    
  •            if (dstA > 255)
    
  •                dstA = 255;
               break;
           case SDL_COPY_MOD:
               dstR = (srcR * dstR) / 255;
    

This is following the definition found here: http://en.wikipedia.org/wiki/Alpha_compositing

Applying the above patch and rebuilding SDL lets RGBA -> RGBA blending with SDL_BLENDMODE_BLEND to do what you are looking for. Not sure, but this seems like a much better idea than what SDL2 currently does. It could probably be improved more.

if an opaque pixel is drawn over a translucent pixel, the result should be as if the translucent pixel had been drawn over the opaque pixel (that is to say that if you drew an opaque blue pixel over a 50% translucent red pixel, the result should be an opaque purplish pixel).

Or at least this is what I have come to expect from all my time using alpha in 2D rendering. I believe this is enabled by SDL_BLENDMODE_ADD rather than SDL_BLENDMODE_BLEND (not sure what “blend” does).------------------------
Nate Fries