0
votes

I wanted to implement alpha blending within my Texture class. It works almost completely. I use the following functions for manipulating the alpha value:

SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
SDL_SetTextureAlphaMod(texture, alpha);

The only problem I have is that the textures that have been manipulated seem to reset to the normal alpha value of 255 when I resize or maximize the window. I checked the alpha value and recognized that it is still the value I manipulated it to be before. So the value is not 255. Why is the renderer rendering it as if the alpha value was 255 then?

Information about how and when I use these functions:

Within the main game loop I change the alpha value of the texture with a public method of my Texture class:

Texture::setAlphaValue(int alpha)

There the private alpha variable of the Texture class is changed.

Within the Draw method of my Texture class the texture is drawn and I call

 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
 SDL_SetTextureAlphaMod(texture, alpha);

before

 SDL_RenderCopyEx(renderer, texture, &sourceRectangle, &destinationRectangle, 0, 0, SDL_Flip);

Information about how I resize the window:

I basically just set the window mode to a resizable window in my SDL initialization. Then handling it like any normal window is possible:

SDL_CreateWindow(window_Title, x_Position, y_Position, window_Width, window_Height, SDL_WINDOW_RESIZABLE);  

My primary loop area:

This is the main game loop:

 void Game::Render()
 {
     // set color and draw window       

     SDL_SetRenderDrawColor(renderer, windowColor.R(), windowColor.G(), windowColor.B(), 0);

     SDL_RenderClear(renderer); 

     texture.setAlphaValue(100);
     texture.Draw(SDL_FLIP_NONE);

     // present/draw renderer

     SDL_RenderPresent(renderer);   
 }

Test my project:

I also uploaded my alpha-blending test project to dropbox. In this project I simplified everything, there isn't even a texture class anymore. So the code is really simple, but the bug is still there. Here is the link to the Visual Studio project: http://www.dropbox.com/s/zaipm8751n71cq7/Alpha.rar

2
How are you resizing the window? - Zammalad
@Zammalad I set the window mode to SDL_WINDOW_RESIZABLE and then it is just a normal window like any program, that can be resized by moving the mouse to the border of it. - huzzm
so you call SDL_SetTextureBlendMode and SDL_SetTextureAlphaMod every frame ie in the call to draw the texture. Does your texture class store the value alpha or is that a parameter you are passing to the Draw function. - Zammalad
which version of SDL are you using? - Zammalad
Seems very strange. I'm busy with a lot of work at the moment but at the weekend I will try putting a basic app together to mirror what you are doing to see if I get the same effect. It sounds like you are doing everything correct though. I would also recommend posting on the SDL forums to ask if anyone has seen this before too. - Zammalad

2 Answers

0
votes

SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);

You should directly change the alpha in this area.

example: alpha = 100;

SDL_SetTextureAlphaMod(texture, alpha); //remember that alpha is an int

SDL_RenderCopy(renderer, texture, NULL, &rect);

P.S. If you're going for a fade-out/fade-in effect, resizing will temporarily pausa alpha changes (in-case you used SDL_GetTicks() and made a float to slowly reduce/increase alpha as time goes by. This is because windows pauses the rendering inside the program but once you stop resizing, it resumes.

Another P.S. Since you're resizing the window make sure to assign the w and h values not as numbers but as products or dynamic numbers(Multiplication is faster than division but you can also use division). Assigning static numbers would cause the window to resize but the textures inside won't change size.

Happy Coding :)

0
votes

Okey, it seems to just be a bug. I am not the only one having this issue: Someone already reported this as a bug to the bugtracker. Here is the link: https://bugzilla.libsdl.org/show_bug.cgi?id=2202.

Hopefully the SDL team will fix it!