#PB_Sprite_AlphaBlending and TransparentSpriteColor()
Posted: Sun Jul 07, 2019 7:50 am
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.
My concern is from line 24 to line 30.
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)
- 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.