Flip sprite horizontally

Advanced game related topics
PHP
User
User
Posts: 79
Joined: Sat Sep 10, 2005 5:38 pm

Flip sprite horizontally

Post by PHP »

Hello, is there no native way to flip a sprite horizontally? At most with Transform?

Thanks!
User avatar
STARGÅTE
Addict
Addict
Posts: 2314
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Flip sprite horizontally

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
PHP
User
User
Posts: 79
Joined: Sat Sep 10, 2005 5:38 pm

Re: Flip sprite horizontally

Post by PHP »

Crazy! Why isn't that mentioned in the help section?! ;)
PHP
User
User
Posts: 79
Joined: Sat Sep 10, 2005 5:38 pm

Re: Flip sprite horizontally

Post 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
User avatar
STARGÅTE
Addict
Addict
Posts: 2314
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Flip sprite horizontally

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply