Page 1 of 1

transparent sprite

Posted: Sat May 16, 2020 4:25 am
by RobertRioja
Is there any way to make a sprite with a transparent background? I have tried everything in the documentation with no success. I have read other posts on this forum regarding this problem but I have not found a solution. I am using PureBasic 5.72 running Windows 7 (32 bit).

Re: transparent sprite

Posted: Sat May 16, 2020 6:45 pm
by Fig
Image
Image

Code: Select all

EnableExplicit
Enumeration
  #arrowPng
  #arrowBmp
EndEnumeration

#ResX=1024:#ResY=768
Define.i Event
If InitSprite() = 0 Or OpenWindow(0, 0, 0, #ResX, #ResY, "A screen in a window...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)=0 Or OpenWindowedScreen(WindowID(0), 0, 0, #ResX, #ResY)=0 Or InitKeyboard()=0 Or InitMouse()=0:MessageRequester("Error", "Can't open screen & sprite environment!", 0):End:EndIf
UsePNGImageDecoder() ;use a 32bits png
LoadSprite(#arrowPng,"arrow10.png")
LoadSprite(#arrowBMP,"096z.bmp")
;Main Loop
Repeat
  Repeat
    Event = WindowEvent()
  Until event=0
  ExamineKeyboard()
  ExamineMouse()
  FlipBuffers()  
  ClearScreen(RGB(0,0,128))
  DisplaySprite(#arrowPng,0,10)
  DisplayTransparentSprite(#arrowPng,50,10)
  DisplayTransparentSprite(#arrowPng,100,10,100)
  DisplaySprite(#arrowBmp,0,60)
  DisplayTransparentSprite(#arrowBmp,50,60)
  DisplayTransparentSprite(#arrowBmp,100,60,100)
Until KeyboardPushed(#PB_Key_Escape)

Re: transparentes Sprite

Posted: Sat May 16, 2020 7:30 pm
by Saki
Hi,

or mean you this !

Code: Select all

UsePNGImageEncoder()

imageID=LoadImage(#PB_Any, "C:\Users\Tanaka\Desktop\PureBasicLogo.bmp")

newImageID=CreateImage(#PB_Any, ImageWidth(imageID), ImageHeight(imageID), 32, #PB_Image_Transparent)

Global replace.l                                            
Procedure FilterCallback(x, y, source_color, destination_color)
  If source_color & $FFFFFF <> replace
    ProcedureReturn source_color
  EndIf
EndProcedure

StartDrawing(ImageOutput(ImageID))
replace=Point(0, 0)
StopDrawing()

StartDrawing(ImageOutput(newImageID))
DrawingMode(#PB_2DDrawing_CustomFilter)
CustomFilterCallback(@FilterCallback())
DrawImage(ImageID(imageID), 0, 0)
StopDrawing()

SaveImage(newImageID, "C:\Users\Tanaka\Desktop\PureBasicLogo.png", #PB_ImagePlugin_PNG)

Re: transparent sprite

Posted: Sat May 16, 2020 7:33 pm
by Olliv
Fig has been quicker ! (Saki too !!)

A "home made" transparent sprite :
(from smartphone, not tested : the colors should be very ugly !)

Code: Select all

;***********************************************************************************************************
;- init
ExamineDesktops()
InitSprite()
OpenScreen(DesktopWidth(0), DesktopHeight(0), 32, "")
InitMouse()


;- the home made sprite
CreateSprite(1, 64, 64, #PB_Sprite_Alphablending)
If StartDrawing(SpriteOutput(1) )
DrawingMode(#PB_2DDrawing_AllChannels)
Circle(32,32,30,RGBA(128,0,255,255)
Circle(32,32,25,RGBA(0,0,0,0) )
Circle(32,32,10,RGBA(0,255,0,128))
EndIf
StopDrawing()



;- test main loop
Repeat
 Delay(16)
 ExamineMouse()
 If MouseButton(#PB_MouseButton_Left)
  ClearScreen(0)
 EndIf
 DisplayTransparentSprite(1,MouseX(),MouseY() )
 FlipBuffers()
Until MouseButton(#PB_MouseButton_Right)

Re: transparent sprite

Posted: Sat May 16, 2020 11:30 pm
by RobertRioja
Thank you all for the replies. However, I need to know how to create a sprite that will have a transparent background. I can use ".png" files that I found in the Examples folder. But I want to create a sprite that has a transparent background and be able to display it. Here is my code:

Code: Select all

; Initialize
InitMouse()
InitSprite()

; Needed to disply png files.
UsePNGImageDecoder()

; Open the main window.
WindowMain = OpenWindow(#PB_Any, 0, 0, 800, 600, "Sprite Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

; Open a screen.
OpenWindowedScreen(WindowID(WindowMain), 0, 40, 750, 550)

; Get the mouse pointer sprite.
MouseSprite = LoadSprite(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/world.png")

MouseWidth = SpriteWidth(MouseSprite) / 2
MouseHeight = SpriteHeight(MouseSprite) / 2

; Make the circle sprite.
CircleSprite = CreateSprite(#PB_Any, 200, 200, #PB_Sprite_AlphaBlending)

; Draw to the sprite.
StartDrawing(SpriteOutput(CircleSprite))
  DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_XOr)
  Circle(100, 100, 90, RGBA(255, 0, 0, 255))
StopDrawing()

; Display my sprite.
DisplayTransparentSprite(CircleSprite, 250, 250, 0)

; Show the mouse to the right of the circle.
MouseLocate(CenterX + BasicWidth, CenterY)

; Clear mouse release flag.
Released = #False
    
Repeat
  Repeat
    ; Get a window event, if there is one.
    Event = WindowEvent()

    If Event = #PB_Event_CloseWindow
      End
    EndIf

  ; Loop till there are no more window events.
  Until Event = 0

  ; Do this if the mouse was not released.
  If Released = #False
    ; Flip for DoubleBuffering.
    FlipBuffers()

    ; Clear the screen.
    ClearScreen(RGB(255, 255, 255))

    ; Get the mouse coordinates.
    ExamineMouse()
    x = MouseX()
    y = MouseY()

    ; Display mouse.
    DisplaySprite(MouseSprite, x - MouseWidth, y - MouseWidth)

    ; Display circle.
    DisplayTransparentSprite(CircleSprite, 250, 250)

    ; If the mouse is close to the top, we want to leave the screen and go to the window.
    If Y < 5
      ReleaseMouse(#True)
      Released = #True
    EndIf

  ; Else do this if the mouse was not released.
  Else
    If WindowMouseY(WindowMain) > 45
      Released = #False
      ReleaseMouse(Released)
      ExamineMouse()
    EndIf

  EndIf

ForEver
But it generates a sprite with a black background which is not transparent. It should only display the red circle, and it should not hide the cursor when it is moved over it.

Re: transparent sprite

Posted: Sun May 17, 2020 5:55 am
by Olliv
You cannot use other thing that these 4 options in the pre-draw :

1.

Code: Select all

DrawingMode(#PB_2DDrawing_AllChannels)
2.

Code: Select all

DrawingMode(#PB_2DDrawing_Alphablend)
3.

Code: Select all

DrawingMode(#PB_2DDrawing_Alphaclip)
4.

Code: Select all

DrawingMode(#PB_2DDrawing_Alphachannel)


XOR blend op is enabled only on the fly by handling SpriteBlendingMode()

Re: transparent sprite

Posted: Sun May 17, 2020 7:42 am
by Demivec
RobertRioja wrote:Thank you all for the replies. However, I need to know how to create a sprite that will have a transparent background. I can use ".png" files that I found in the Examples folder. But I want to create a sprite that has a transparent background and be able to display it.

. . . Snip code. .

But it generates a sprite with a black background which is not transparent. It should only display the red circle, and it should not hide the cursor when it is moved over it.
Use this after you create the CircleSprite:

Code: Select all

TransparentSpriteColor(CircleSprite, RGB(0, 0, 0))
The mouse cursor should be displayed after the circle if you want it to display over the top of the circle. Even though the center of the CircleSprite's is now transparent the circle itself is still displayed in front of the mouse cursor because of the display order.

Re: transparent sprite

Posted: Sun May 17, 2020 6:39 pm
by RobertRioja
Thank you all for your replies.

Demivec gave me the best solution. I tried to use TransparentSpriteColor but I had it before the StartDrawing command which was wrong. It has to go after the StopDrawing line.

Problem solved!

Re: transparent sprite

Posted: Sun May 17, 2020 8:12 pm
by netmaestro
I had it before the StartDrawing command which was wrong. It has to go after the StopDrawing line.
Uh...