transparent sprite
-
- User
- Posts: 71
- Joined: Thu May 02, 2019 3:57 am
- Location: USA
- Contact:
transparent sprite
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
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
But only the third works fine.
Win10, Pb x64 5.71 LTS
Re: transparentes Sprite
Hi,
or mean you this !
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
Fig has been quicker ! (Saki too !!)
A "home made" transparent sprite :
(from smartphone, not tested : the colors should be very ugly !)
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)
-
- User
- Posts: 71
- Joined: Thu May 02, 2019 3:57 am
- Location: USA
- Contact:
Re: transparent sprite
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:
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.
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
Re: transparent sprite
You cannot use other thing that these 4 options in the pre-draw :
1.
2.
3.
4.
XOR blend op is enabled only on the fly by handling SpriteBlendingMode()
1.
Code: Select all
DrawingMode(#PB_2DDrawing_AllChannels)
Code: Select all
DrawingMode(#PB_2DDrawing_Alphablend)
Code: Select all
DrawingMode(#PB_2DDrawing_Alphaclip)
Code: Select all
DrawingMode(#PB_2DDrawing_Alphachannel)
XOR blend op is enabled only on the fly by handling SpriteBlendingMode()
Re: transparent sprite
Use this after you create the CircleSprite: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.
Code: Select all
TransparentSpriteColor(CircleSprite, RGB(0, 0, 0))
-
- User
- Posts: 71
- Joined: Thu May 02, 2019 3:57 am
- Location: USA
- Contact:
Re: transparent sprite
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!
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!
- netmaestro
- PureBasic Bullfrog
- Posts: 8425
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: transparent sprite
Uh...I had it before the StartDrawing command which was wrong. It has to go after the StopDrawing line.
BERESHEIT