transparent sprite

Just starting out? Need help? Post your questions and find answers here.
RobertRioja
User
User
Posts: 71
Joined: Thu May 02, 2019 3:57 am
Location: USA
Contact:

transparent sprite

Post 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).
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: transparent sprite

Post 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)
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: transparentes Sprite

Post 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)
地球上の平和
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: transparent sprite

Post 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)
RobertRioja
User
User
Posts: 71
Joined: Thu May 02, 2019 3:57 am
Location: USA
Contact:

Re: transparent sprite

Post 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.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: transparent sprite

Post 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()
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: transparent sprite

Post 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.
RobertRioja
User
User
Posts: 71
Joined: Thu May 02, 2019 3:57 am
Location: USA
Contact:

Re: transparent sprite

Post 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!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: transparent sprite

Post by netmaestro »

I had it before the StartDrawing command which was wrong. It has to go after the StopDrawing line.
Uh...
BERESHEIT
Post Reply