-1

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

huzzm
  • 489
  • 9
  • 24
  • 1
    How are you resizing the window? – TPS Apr 07 '15 at 08:03
  • @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 Apr 08 '15 at 13:44
  • 1
    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. – TPS Apr 08 '15 at 17:29
  • @Zammalad That is the weird thing. Yes, i call it every frame and yes, the alpha value is stored within the class. And I checked that alpha value which is still set to the value I wanted it to be, but somehow the program does not want to apply it anymore. – huzzm Apr 09 '15 at 19:11
  • 2
    which version of SDL are you using? – TPS Apr 09 '15 at 19:17
  • @Zammalad I use the 2.0 version. – huzzm Apr 09 '15 at 19:45
  • 1
    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. – TPS Apr 13 '15 at 21:13

2 Answers2

0

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 :)

kdyz
  • 1,447
  • 13
  • 27
  • I tried changing the value directly, but that has no effect. After resizing the window the alpha value still seems to be reseted. So it is irrelevant whether I change the variable in a public method of the class or directly. It really seems to be a problem that is caused by the SDL lib and not by me... – huzzm Apr 20 '15 at 20:42
  • I did this on my project before I typed the answer, it actually worked for me 0_0. – kdyz Apr 20 '15 at 23:56
  • Try removing this -> Texture::setAlphaValue(int alpha) and this -> SDL_RenderCopyEx(renderer, texture, &sourceRectangle, &destinationRectangle, 0, 0, SDL_Flip); Then do a quick debug using the code I typed. – kdyz Apr 20 '15 at 23:58
  • I did that, but the bug is still there, it does not solve the problem for me :/ – huzzm Apr 22 '15 at 15:36
  • Hmmm, this is just a guess okay? After you checked the alpha value, it wasn't 255 at all but the image seemed opaque? as if it was 255 right? SDL won't lie and tell you that the image's alpha isn't 255, what I think is that everytime you resize the screen, the program draws Another* image with the same rect as the one below it but it uses the original alpha value(255), which is why you could only see the one with the original alpha value, it's just covering the one that had an altered alpha. – kdyz Apr 23 '15 at 05:38
  • I'm sorry, I read your post again and noticed that the alpha change was already included in the draw function. I don't have a concrete answer for you, how about you make a statement that checks if the size of the window != the previous size then it *redraws* the previous image so that the alpha will be checked into the correct number again. – kdyz Apr 23 '15 at 05:43
  • Well, I draw the texture every frame. And I call SDL_SetTextureBlendMode() and SDL_SetTextureAlphaMod() every frame as well. Even my Texture:setAlphaValue() method is called every frame. – huzzm Apr 23 '15 at 06:08
  • 1
    Could you please edit your post and show us your primary loop area? this could also help others notice your error. Since honestly, I can't see an error based on what you described, I even tested it. – kdyz Apr 24 '15 at 03:53
  • I uploaded my alpha-blending test project to dropbox, here is the link: https://www.dropbox.com/s/zaipm8751n71cq7/Alpha.rar – huzzm Apr 25 '15 at 18:28
  • I don't get how it works for you but not for me... By the way the project I uploaded to dropbox is now simplified extremely, maybe testing that out will help you understand what I am doing^^ – huzzm Apr 26 '15 at 19:07
0

This has been a reported bug in the SDL library. It is fixed for some time now: https://bugzilla.libsdl.org/show_bug.cgi?id=2202, https://github.com/libsdl-org/SDL/issues/1085

huzzm
  • 489
  • 9
  • 24