When to call SpriteWidth() and SpriteHeight() ?

Advanced game related topics
Mijikai
Posts: 12
Joined: Sun Sep 17, 2017 9:59 am

When to call SpriteWidth() and SpriteHeight() ?

Post by Mijikai »

When and where can i request the size of an sprite?

What i tried:

Code: Select all

EnableExplicit

Global s.i

Procedure.i Load()
  FlipBuffers()  
EndProcedure

Procedure.i Update()
  ClearScreen($333344)
  DisplayTransparentSprite(s,0,0)
  FlipBuffers()
EndProcedure

Procedure.i Render()
  If OpenWindow(0,0,0,256,256,#Null$,#PB_Window_BorderLess)
    If OpenWindowedScreen(WindowID(0),0,0,WindowWidth(0),WindowHeight(0))
      SetFrameRate(60)
      BindEvent(#PB_Event_RenderFrame,@Update())
      BindEvent(#PB_Event_Loading,@Load())
      s = LoadSprite(#PB_Any,"dummy.png",#PB_Sprite_AlphaBlending)
      Debug SpriteWidth(s);<- nope!
      Debug SpriteHeight(s)
      ProcedureReturn #True
    EndIf
  EndIf
  ProcedureReturn #False
EndProcedure

Render()
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: When to call SpriteWidth() and SpriteHeight() ?

Post by Peter »

Mijikai wrote: Sun Feb 26, 2023 3:24 pmWhen and where can i request the size of an sprite?
in the Load() - Procedure.

See also the Sprite-Examples in the SpiderBasic-Documentation.
Mijikai
Posts: 12
Joined: Sun Sep 17, 2017 9:59 am

Re: When to call SpriteWidth() and SpriteHeight() ?

Post by Mijikai »

Thanks for the reply, the Sprite example in the Doc does not use Load() to load anything.

Its just some load counter:

Code: Select all

Procedure Loading(Type, Filename$)
  Static NbLoadedElements
  
  NbLoadedElements+1
  If NbLoadedElements = 1 ; Finished the loading of all images and sounds, we can start the applications
    FlipBuffers() ; start the rendering
  EndIf
EndProcedure
Is that the place where i can directly access the SpriteWidth/Height() ?
If so how would i get the SpriteId - do i need a Global var/struct ?
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: When to call SpriteWidth() and SpriteHeight() ?

Post by Peter »

Unlike PureBasic, after a LoadSprite() the object is not immediately available. For this reason you specify with BindEvent(#PB_Event_Loading, ...) the procedure that will be executed when the object is available. Only when this procedure is called, the object is available and you can for example get the dimension of the loaded sprite:

Code: Select all

EnableExplicit

Global mySprite.s = "https://cdn.pixabay.com/photo/2021/07/13/11/34/cat-6463284_960_720.jpg"

Procedure Loaded(Type, Filename$, ObjectId)
  
  Debug "Load: Success"
  Debug "Type: " + Type + " (-> #PB_Loading_Sprite)"
  Debug "Filename$: " + Filename$
  Debug "ObjectId: " + ObjectId
  
  If Filename$ = mySprite
    
    Debug "SpriteWidth: " + SpriteWidth(ObjectId)
    Debug "SpriteHeight: " + SpriteHeight(ObjectId)
    
  EndIf
  
EndProcedure

Procedure LoadingError(Type, Filename$)
  Debug Filename$ + ": loading error"
EndProcedure

BindEvent(#PB_Event_Loading, @Loaded())
BindEvent(#PB_Event_LoadingError, @LoadingError())

LoadSprite(#PB_Any, mySprite)
AZJIO
Posts: 73
Joined: Wed Dec 14, 2022 1:13 pm

Re: When to call SpriteWidth() and SpriteHeight() ?

Post by AZJIO »

I figured out how to control the sprite

Code: Select all

EnableExplicit

Global s.i
Global xx, yy
Global mySprite.s = "Sprite.png"

Procedure TimerEvents()
	If EventTimer() = 1
		xx + 1
		yy + Log(xx)
		; Debug xx
		; Debug yy
		If xx = 70
			RemoveWindowTimer(0, 1)
		EndIf
	EndIf
EndProcedure

Procedure.i Loaded(Type, Filename$, ObjectId)
	; Debug "Load: Success"
	; Debug "Type: " + Type + " (-> #PB_Loading_Sprite)"
	; Debug "Filename$: " + Filename$
	; Debug "ObjectId: " + ObjectId
	FlipBuffers() 
	
	If Filename$ = "Sprite.png"
		; Debug "SpriteWidth: " + SpriteWidth(ObjectId)
		; Debug "SpriteHeight: " + SpriteHeight(ObjectId)
		; RotateSprite(s, 90.0, #PB_Absolute)
		AddWindowTimer(0, 1, 50)
		BindEvent(#PB_Event_Timer, @TimerEvents())
	EndIf
EndProcedure


Procedure LoadingError(Type, Filename$)
	Debug Filename$ + ": loading error"
EndProcedure


Procedure.i Update()
	ClearScreen($3f3f3f)
	; DisplayTransparentSprite(s,0,0)
	DisplaySprite(s, xx, yy)
	FlipBuffers()
EndProcedure


Procedure.i Render()
	If OpenWindow(0,0,0,256,256,#Null$,#PB_Window_BorderLess)
		If OpenWindowedScreen(WindowID(0),0,0,WindowWidth(0),WindowHeight(0))
			SetFrameRate(60)
			BindEvent(#PB_Event_RenderFrame,@Update())
			BindEvent(#PB_Event_Loading,@Loaded())
			BindEvent(#PB_Event_LoadingError, @LoadingError())
			s = LoadSprite(#PB_Any,mySprite,#PB_Sprite_AlphaBlending)
			ProcedureReturn #True
		EndIf
	EndIf
	ProcedureReturn #False
EndProcedure

Render()
Post Reply