Page 1 of 1

Can i create a transparent Sprite with CreateSprite()?

Posted: Thu Dec 05, 2024 8:02 pm
by Mijikai
Is there a way to create a transparent sprite with CreateSprite()?
I thought it would be nice to have an ingame overlay.

Edit: added #PB_Sprite_AlphaBlending

Black box should be transparent:

Code: Select all

EnableExplicit

Procedure Render()
  ClearScreen(#Red)
  DisplayTransparentSprite(0,0,0)
  FlipBuffers()
EndProcedure

Procedure Main()
  OpenScreen(800,600,32,#Null$)
  SetFrameRate(60)
  CreateSprite(0,128,128,#PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(0))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0,0,SpriteWidth(0),SpriteHeight(0),RGBA(255,255,255,255))
  Box(2,2,SpriteWidth(0) - 4,SpriteHeight(0) - 4,RGBA(0,0,0,0))
  StopDrawing()
  BindEvent(#PB_Event_RenderFrame,@Render())
  PostEvent(#PB_Event_RenderFrame)
EndProcedure

Main()
Is the only way LoadSprite() with a transparent *.png?

Re: Can i create a transparent Sprite with CreateSprite()?

Posted: Thu Dec 05, 2024 8:56 pm
by munfraid
Use #PB_Sprite_AlphaBlending during creation.

Code: Select all

EnableExplicit

Procedure Render()
  ClearScreen(#Red)
  DisplayTransparentSprite(0,0,0)
  FlipBuffers()
EndProcedure

Procedure Main()
  OpenScreen(800,600,32,#Null$)
  SetFrameRate(60)
  CreateSprite(0,128,128, #PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(0))
  DrawingMode(#PB_2DDrawing_Outlined)
  Box(0,0,SpriteWidth(0),SpriteHeight(0),RGBA(255,255,255,255))
  Box(1,1,SpriteWidth(0)-1,SpriteHeight(0)-1,RGBA(255,255,255,255))
  StopDrawing()
  BindEvent(#PB_Event_RenderFrame,@Render())
  PostEvent(#PB_Event_RenderFrame)
EndProcedure

Main()



Re: Can i create a transparent Sprite with CreateSprite()?

Posted: Thu Dec 05, 2024 9:29 pm
by Mijikai
I forgot to add #PB_Sprite_AlphaBlending to my example, thanks for letting me know.

This is not really a solution imho., more like a hack -> using #PB_2DDrawing_Outlined to make it transparent.
It seems there is no way to just draw/set the alpha channel.

Edit:
So i tried the workaround but its even worse than i thought.
#PB_2DDrawing_Outlined can only be used once upon creation of the sprite to get a transparent one.
This means that a overlay sprite hacked together like this needs to be recreated all the time if the overlay is not static - which is not nice.
Further more drawing onto the overlay every frame will lead to crash (Empty Start/StopDrawing block is fine).