#PB_Sprite_AlphaBlending and TransparentSpriteColor()

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

#PB_Sprite_AlphaBlending and TransparentSpriteColor()

Post by Joubarbe »

Hi,

I think I've spotted a bug, or at least an error somewhere, either in the doc or the general behavior of a transparent sprite.

Code: Select all

EnableExplicit

#screen_width = 1920
#screen_height = 1080

Define sprite_mouse.i, sprite_fps.i, sprite_square.i
Define frame.i, frame$, frame_time.i, event.i

InitKeyboard() : InitMouse() : InitSprite()

OpenWindow(0, 0, 0, #screen_width, #screen_height, "TITLE", #PB_Window_BorderLess | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #screen_width, #screen_height)

sprite_mouse = CreateSprite(#PB_Any, 7, 7)
StartDrawing(SpriteOutput(sprite_mouse))
Box(0, 0, OutputWidth(), OutputHeight(), #Black)
  Box(0, 0, OutputWidth() - 2, OutputHeight() - 2, #White)
StopDrawing()

sprite_fps = CreateSprite(#PB_Any, 40, 20)

MouseLocate(DesktopMouseX(), DesktopMouseY())

sprite_square = CreateSprite(#PB_Any, 200, 200, #PB_Sprite_AlphaBlending)   ; #PB_Sprite_AlphaBlending does not seem mandatory unlike what the doc says.
;sprite_square = CreateSprite(#PB_Any, 200, 200)
StartDrawing(SpriteOutput(sprite_square))
Box(0, 0, 100, 100, #White)
StopDrawing()
TransparentSpriteColor(#PB_Default, #Black)       ; 1/ Default should be black, but it's not. 2/ Assigning a new value to #PB_Default has no effect.
;TransparentSpriteColor(sprite_square, #Black)    ; TODO Comment out to see the change. This actually is the only way to make #Black transparent on this sprite (other than drawing on an intermediary transparent image, or changing the blending method).

Repeat
  Repeat
    event.i = WindowEvent()
    If event = #PB_Event_CloseWindow : Break 2 : EndIf
  Until event = 0
  
  ExamineKeyboard() : ExamineMouse()
  
  FlipBuffers()
  ClearScreen(RGB(50, 65, 70))
  
  DisplayTransparentSprite(sprite_square, ScreenWidth() / 2, ScreenHeight() / 2)
  
   ; FPS
  frame + 1
  If ElapsedMilliseconds() > frame_time
    frame_time = ElapsedMilliseconds() + 1000
    frame$ = Str(frame)
    frame = 0
    StartDrawing(SpriteOutput(sprite_fps))
      DrawText(0, 0, "fps:" + frame$)
    StopDrawing()
  EndIf
  DisplaySprite(sprite_fps, 0, 0)
  
  DisplaySprite(sprite_mouse, MouseX(), MouseY())
  
  Delay(1)
Until KeyboardReleased(#PB_Key_Escape)
My concern is from line 24 to line 30.
  • sprite_square is created with an alpha channel. But even with TransparentSpriteColor(#PB_Default, #Black), the sprite does not show any transparency.
  • Next thing to do is to uncomment line 30: TransparentSpriteColor(sprite_square, #Black). Now it works, #Black is transparent.
  • The weirdest part now. I've been coding with PB for a while now (on a very irregular basis I reckon), and I've always thought that when you want an alpha channel, you need #PB_Sprite_AlphaBlending. Even the documentation says that. So now: uncomment line 25 or create sprite_square without #PB_Sprite_AlphaBlending. Result: there is an alpha channel, and the default transparency color is #Black, as it should.
  • Without #PB_Sprite_AlphaBlending, setting a default transparent color with TransparentSpriteColor(#PB_Default) still doesn't work. TransparentSpriteColor(sprite_square, #White), however, makes #White transparent, as we could expect, even though #PB_Sprite_AlphaBlending is not specified.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: #PB_Sprite_AlphaBlending and TransparentSpriteColor()

Post by #NULL »

I also made these observations. TransparentSpriteColor() is older than the alphachannel support. I guess they were never meant to be used together.
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: #PB_Sprite_AlphaBlending and TransparentSpriteColor()

Post by Joubarbe »

What about #PB_Sprite_AlphaBlending then? This thing seems useless.

EDIT: ignore that, it actually has an effect. Transparent color is different from variable alpha channel it seems. (no variation without #PB_Sprite_AlphaBlending) Problem is that there's no default transparent color with #PB_Sprite_AlphaBlending... Dilemma dilemma!

EDIT2: well no, I don't get it. Now when changing Box(0, 0, 100, 100, #White) to Box(0, 0, 100, 100, RGBA(255, 255, 255, 10)), there's still no transparency, it's full white (#PB_Sprite_AlphaBlending activated).
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: #PB_Sprite_AlphaBlending and TransparentSpriteColor()

Post by #NULL »

Joubarbe wrote:EDIT2: well no, I don't get it. Now when changing Box(0, 0, 100, 100, #White) to Box(0, 0, 100, 100, RGBA(255, 255, 255, 10)), there's still no transparency, it's full white (#PB_Sprite_AlphaBlending activated).
Maybe you forgot to use a drawing mode with alpha channel?

Code: Select all

DrawingMode(#PB_2DDrawing_AllChannels)
Box(0, 0, 100, 100, RGBA(255,255,255,10))
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: #PB_Sprite_AlphaBlending and TransparentSpriteColor()

Post by Joubarbe »

So...

When you use #PB_2DDrawing_AllChannels, it works only without using "TransparentSpriteColor(sprite_square, #Black)". If you put a TransparentSpriteColor(), no matter what DrawingMode you use, it doesn't seem to have an effect.

Which leads me to thinking that on most drawing, "FillArea(0, 0, -1, RGBA(0 ,0 ,0, 0))" must be used if I want to have a transparent background color on my sprite. And that I should forget using TransparentSpriteColor().
Post Reply