Page 1 of 1

Flip sprite horizontally

Posted: Wed Feb 04, 2026 4:40 pm
by PHP
Hello, is there no native way to flip a sprite horizontally? At most with Transform?

Thanks!

Re: Flip sprite horizontally

Posted: Wed Feb 04, 2026 10:13 pm
by STARGÅTE
PHP wrote: Wed Feb 04, 2026 4:40 pm Hello, is there no native way to flip a sprite horizontally? At most with Transform?
Yes. The backface culling was deactivated for sprites.
So you can use ZoomSprite() with negative height or width to flip vertically or horizontally.
However, you have to keep in mind that the sprite anchor is than on the other side, not on the left top corner.

Code: Select all

Enumeration
	#Window
	#Sprite
EndEnumeration

InitSprite()

OpenWindow(#Window, 0, 0, 300, 300, "Sprite", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Window), 0, 0, WindowWidth(#Window), WindowHeight(#Window), 0, 0, 0)
SpriteQuality(#PB_Sprite_BilinearFiltering)

CreateSprite(#Sprite, 80, 80)
StartDrawing(SpriteOutput(#Sprite))
	Box(0, 0, 80, 80, $303030)
	Box(16, 16, 8, 48, $FFFFFF)
	Box(24, 16, 24, 8, $FFFFFF)
	Box(24, 32, 16, 8, $FFFFFF)
StopDrawing()

Define Vertex.d

Repeat
	
	Repeat
		
		Select WindowEvent()
			Case #PB_Event_CloseWindow
				End
			Case #Null
				Break
		EndSelect
		
	ForEver
	
	ClearScreen(0)
	
	ZoomSprite(#Sprite, 80, 80)
	DisplaySprite(#Sprite, 20, 20)
	
	ZoomSprite(#Sprite, 80, -80)
	DisplaySprite(#Sprite, 20, 200)
	
	ZoomSprite(#Sprite, -80, 80)
	DisplaySprite(#Sprite, 200, 20)
	
	FlipBuffers()
	
ForEver

Re: Flip sprite horizontally

Posted: Thu Feb 05, 2026 5:28 pm
by PHP
Crazy! Why isn't that mentioned in the help section?! ;)

Re: Flip sprite horizontally

Posted: Thu Feb 05, 2026 5:50 pm
by PHP
Question: The mirroring works, but apparently doesn't work in FullHD without the two black pixel borders?

When I set it to -1920, the sprite disappears completely...

Code: Select all

Enumeration
	#Window
	#Sprite
EndEnumeration

InitSprite()

OpenWindow(#Window, 0, 0, 1920, 1080, "Sprite", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Window), 0, 0, WindowWidth(#Window), WindowHeight(#Window), 0, 0, 0)


CreateSprite(#Sprite, 1920,1080)
StartDrawing(SpriteOutput(#Sprite))
	Box(0, 0, 1920, 1080, RGB(255,0,0))
	StopDrawing()

Repeat
	
	Repeat
		
		Select WindowEvent()
			Case #PB_Event_CloseWindow
				End
			Case #Null
				Break
		EndSelect
		
	ForEver
	
	ClearScreen(0)
	
	ZoomSprite(#Sprite, -1918, 1080) ; flip horizontally
	DisplaySprite(#Sprite, 1919, 0) ; works but left/right black border
	

	FlipBuffers()
	
ForEver

Re: Flip sprite horizontally

Posted: Thu Feb 05, 2026 6:53 pm
by STARGÅTE
PHP wrote: Thu Feb 05, 2026 5:50 pm Question: The mirroring works, but apparently doesn't work in FullHD without the two black pixel borders?

When I set it to -1920, the sprite disappears completely...
Yes. This is due to the optimization in DisplaySprite() which do not display the sprite if its position is outside the screen, and DisplaySprite() do not know, that it was zoomed negatively.

Instead, you can use TransformSprite() and define all 4 vertices (x,y) and then display it at 0,0:

Code: Select all

Enumeration
	#Window
	#Sprite
EndEnumeration

InitSprite()

OpenWindow(#Window, 0, 0, 1920, 1080, "Sprite", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Window), 0, 0, WindowWidth(#Window), WindowHeight(#Window), 0, 0, 0)


CreateSprite(#Sprite, 1920,1080)
StartDrawing(SpriteOutput(#Sprite))
	Box(0, 0, 960, 1080, RGB(255,0,0))
	Box(960, 0, 960, 1080, RGB(192,0,0))
StopDrawing()

Repeat
	
	Repeat
		
		Select WindowEvent()
			Case #PB_Event_CloseWindow
				End
			Case #Null
				Break
		EndSelect
		
	ForEver
	
	ClearScreen(0)
	
	;TransformSprite(#Sprite, 0,0, 1920,0, 1920,1080, 0,1080) ; normal
	TransformSprite(#Sprite, 1920,0, 0,0, 0,1080, 1920,1080) ; hor. fliped
	DisplaySprite(#Sprite, 0, 0)
	

	FlipBuffers()
	
ForEver